Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: honor explicit enclosing test budgets in the client timeout guard (#9736)

[._.]/ Adam Eivy96218c92102b
diff --git a/client/src/test/timeouts.test.js b/client/src/test/timeouts.test.js
index 360dbae561..b8d249cbf6 100644
--- a/client/src/test/timeouts.test.js
+++ b/client/src/test/timeouts.test.js
@@ -36,6 +36,113 @@ const WAIT_FOR_OPTIONS = new Set([
*/
const RAISES_OWN_BUDGET = /vi\.setConfig\(\s*\{[^{}]*\btestTimeout\b/;
+/**
+ * `source` with comments and the CONTENTS of string/template literals blanked
+ * to spaces (offsets preserved), so bracket matching and call discovery never
+ * trip on prose or on a `it(\'x\', ...)` inside a comment.
+ */
+function maskNonCode(source) {
+ const out = source.split('');
+ const blank = (from, to) => { for (let k = from; k < to; k += 1) if (out[k] !== '\n') out[k] = ' '; };
+ let i = 0;
+ while (i < source.length) {
+ const c = source[i];
+ const next = source[i + 1];
+ if (c === '/' && next === '/') {
+ const end = source.indexOf('\n', i);
+ const stop = end === -1 ? source.length : end;
+ blank(i, stop);
+ i = stop;
+ } else if (c === '/' && next === '*') {
+ const end = source.indexOf('*/', i + 2);
+ const stop = end === -1 ? source.length : end + 2;
+ blank(i, stop);
+ i = stop;
+ } else if (c === '"' || c === "'" || c === '`') {
+ let j = i + 1;
+ while (j < source.length && source[j] !== c) j += source[j] === '\\' ? 2 : 1;
+ blank(i + 1, j);
+ i = j + 1;
+ } else i += 1;
+ }
+ return out.join('');
+}
+
+/** Index of the bracket closing the one at `open` in masked source, or -1. */
+function matchClose(masked, open) {
+ let depth = 0;
+ for (let i = open; i < masked.length; i += 1) {
+ if ('([{'.includes(masked[i])) depth += 1;
+ else if (')]}'.includes(masked[i])) {
+ depth -= 1;
+ if (depth === 0) return i;
+ }
+ }
+ return -1;
+}
+
+/** Top-level comma-separated argument spans between `open` and `close` parens. */
+function splitArgs(masked, open, close) {
+ const args = [];
+ let depth = 0;
+ let start = open + 1;
+ for (let i = open + 1; i < close; i += 1) {
+ if ('([{'.includes(masked[i])) depth += 1;
+ else if (')]}'.includes(masked[i])) depth -= 1;
+ else if (masked[i] === ',' && depth === 0) {
+ args.push([start, i]);
+ start = i + 1;
+ }
+ }
+ if (masked.slice(start, close).trim()) args.push([start, close]);
+ return args;
+}
+
+const TEST_CALL = /\b(?:it|test)((?:\.\w+)*)\s*(?=[(`])/g;
+const NUMBER_ARG = /^\s*(\d[\d_]*)\s*$/;
+const OPTION_ARG = /^\s*\{[^{}]*?\btimeout:\s*(\d[\d_]*)/;
+
+/**
+ * Every `it`/`test` call (including `.each` forms) with its source span, its
+ * effective explicit timeout (third numeric argument or `{ timeout }` second
+ * argument; null when it inherits the suite budget), and the span of a
+ * `{ timeout }` options argument, which declares the budget rather than
+ * consuming it.
+ */
+function testCalls(source, masked) {
+ const calls = [];
+ for (const m of masked.matchAll(TEST_CALL)) {
+ let open = m.index + m[0].length;
+ if (/\.each\b/.test(m[1])) {
+ if (masked[open] === '`') {
+ open = masked.indexOf('`', open + 1) + 1;
+ } else {
+ const tableClose = matchClose(masked, open);
+ if (tableClose === -1) continue;
+ open = tableClose + 1;
+ }
+ while (/\s/.test(masked[open] ?? '')) open += 1;
+ }
+ if (masked[open] !== '(') continue;
+ const close = matchClose(masked, open);
+ if (close === -1) continue;
+ const args = splitArgs(masked, open, close);
+ let timeout = null;
+ let optionSpan = null;
+ if (args.length >= 3) {
+ const last = NUMBER_ARG.exec(masked.slice(...args[args.length - 1]));
+ const opt = OPTION_ARG.exec(source.slice(...args[1]));
+ if (last) timeout = Number(last[1].replace(/_/g, ''));
+ else if (opt) {
+ timeout = Number(opt[1].replace(/_/g, ''));
+ optionSpan = args[1];
+ }
+ }
+ calls.push({ start: open, end: close, timeout, optionSpan });
+ }
+ return calls;
+}
+
/**
* Inline async bounds in `source` that the enclosing budget would cut short —
* the test dies first, so the extended wait never happens and the failure names
@@ -49,11 +156,20 @@ const RAISES_OWN_BUDGET = /vi\.setConfig\(\s*\{[^{}]*\btestTimeout\b/;
*/
export function inertTimeoutBounds(source, budgetMs = TEST_TIMEOUT_MS) {
if (RAISES_OWN_BUDGET.test(source)) return [];
+ const calls = testCalls(source, maskNonCode(source));
return [...source.matchAll(FLAT_OBJECT)]
- .map(([, body]) => body)
- .filter((body) => [...body.matchAll(OBJECT_KEYS)].every(([, key]) => WAIT_FOR_OPTIONS.has(key)))
- .map((body) => TIMEOUT_KEY.exec(body)?.[1])
- .filter((digits) => digits && Number(digits.replace(/_/g, '')) >= budgetMs);
+ .filter((m) => [...m[1].matchAll(OBJECT_KEYS)].every(([, key]) => WAIT_FOR_OPTIONS.has(key)))
+ .filter((m) => !calls.some((c) => c.optionSpan && m.index >= c.optionSpan[0] && m.index < c.optionSpan[1]))
+ .flatMap((m) => {
+ const digits = TIMEOUT_KEY.exec(m[1])?.[1];
+ if (!digits) return [];
+ // Innermost enclosing test call decides the budget; outside one, the suite default.
+ const enclosing = calls
+ .filter((c) => m.index > c.start && m.index < c.end)
+ .sort((a, b) => b.start - a.start)[0];
+ const budget = enclosing?.timeout ?? budgetMs;
+ return Number(digits.replace(/_/g, '')) >= budget ? [digits] : [];
+ });
}
describe('client suite time budgets', () => {
@@ -97,6 +213,20 @@ describe('client suite time budgets', () => {
// call in prose is not.
expect(inertTimeoutBounds('vi.setConfig({ testTimeout: 60000 });\nwaitFor(fn, { timeout: 30000 });')).toEqual([]);
expect(inertTimeoutBounds('// vi.setConfig( is discussed here\nwaitFor(fn, { timeout: 30000 });')).toEqual(['30000']);
+ // An explicit budget on the enclosing test is the effective one...
+ expect(inertTimeoutBounds("it('slow', async () => {\n await waitFor(fn, { timeout: 15000 });\n}, 30000);")).toEqual([]);
+ expect(inertTimeoutBounds("it('slow', { timeout: 30000 }, async () => {\n await waitFor(fn, { timeout: 15000 });\n});")).toEqual([]);
+ // ...but only for that test: a sibling with the default budget still fails.
+ expect(inertTimeoutBounds("it('slow', async () => {\n await waitFor(a, { timeout: 15000 });\n}, 30000);\nit('fast', async () => {\n await waitFor(b, { timeout: 15000 });\n});")).toEqual(['15000']);
+ // A bound at or above the explicit budget is still inert.
+ expect(inertTimeoutBounds("it('slow', async () => {\n await waitFor(fn, { timeout: 30000 });\n}, 30000);")).toEqual(['30000']);
+ // each forms, array-table and tagged-template.
+ expect(inertTimeoutBounds("it.each([[1], [2]])('n %s', async (n) => {\n await waitFor(fn, { timeout: 15000 });\n}, 30000);")).toEqual([]);
+ expect(inertTimeoutBounds("test.each`\n a\n ${1}\n`('n', async () => {\n await waitFor(fn, { timeout: 15000 });\n}, 30000);")).toEqual([]);
+ expect(inertTimeoutBounds("it.each([[1]])('n', async () => {\n await waitFor(fn, { timeout: 15000 });\n});")).toEqual(['15000']);
+ // A test call or timeout that only appears in a comment or string grants nothing.
+ expect(inertTimeoutBounds("// it('x', () => {}, 30000)\nit('y', async () => {\n await waitFor(fn, { timeout: 15000 });\n});")).toEqual(['15000']);
+ expect(inertTimeoutBounds("it('y', async () => {\n // , 30000)\n await waitFor(fn, { timeout: 15000 });\n});")).toEqual(['15000']);
// Fixture data that merely carries a `timeout` field is not an async bound.
expect(inertTimeoutBounds("const p = { id: 'p1', timeout: 300000 };")).toEqual([]);
});