From 401469dbe2039bfe9db6bef87951f9c899a3162a Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sat, 21 Nov 2026 00:26:37 +0000 Subject: [PATCH 1/2] Grep fallback tests separate deadlines from response handling Signed-off-by: zlareb1 --- .../grep-shim/tests/response-fixture.mjs | 38 +++++++++++++++++++ integrations/grep-shim/tests/shim.test.mjs | 37 +++++++++++++++++- 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 integrations/grep-shim/tests/response-fixture.mjs diff --git a/integrations/grep-shim/tests/response-fixture.mjs b/integrations/grep-shim/tests/response-fixture.mjs new file mode 100644 index 0000000000..10e07a61cb --- /dev/null +++ b/integrations/grep-shim/tests/response-fixture.mjs @@ -0,0 +1,38 @@ +// Response-path tests replace transport so the real deadline cannot hide a parser branch. +import assert from "node:assert/strict"; +import fs from "node:fs"; + +const mode = process.env.LOOM_TEST_RESPONSE_MODE; +assert.ok(mode === "401" || mode === "garbage"); +const record = process.env.LOOM_TEST_RESPONSE_RECORD; +const seen = { requests: [], requestValidated: false, jsonCalls: 0, jsonRejected: false }; +const save = () => fs.writeFileSync(record, JSON.stringify(seen)); +const response = new Response(mode === "401" ? "{}" : "{not json", { + status: mode === "401" ? 401 : 200, + headers: { "content-type": "application/json" }, +}); +const parse = response.json.bind(response); +response.json = async () => { + seen.jsonCalls++; + save(); + try { + return await parse(); + } catch (error) { + seen.jsonRejected = true; + save(); + throw error; + } +}; + +globalThis.fetch = async (url, options) => { + seen.requests.push(url); + save(); + assert.equal(url, "http://response.invalid/v1/code.enclosing"); + assert.equal(options.method, "POST"); + assert.equal(options.headers.authorization, "Bearer t"); + assert.ok(JSON.parse(options.body).locations.length > 0); + options.signal.throwIfAborted(); + seen.requestValidated = true; + save(); + return response; +}; diff --git a/integrations/grep-shim/tests/shim.test.mjs b/integrations/grep-shim/tests/shim.test.mjs index 5c4c2a9f74..8dd843e062 100644 --- a/integrations/grep-shim/tests/shim.test.mjs +++ b/integrations/grep-shim/tests/shim.test.mjs @@ -230,19 +230,52 @@ test("grep: a 401, unparseable JSON or a server past the timeout all give native const dir = checkout(); const want = await runRaw(NATIVE.grep, ["-rn", "foo", "src"], { cwd: dir }); for (const mode of [401, "garbage", "hang"]) { - await withServer(mode, async (url, seen) => { + await withServer(mode, async (url) => { const got = await shim("grep", ["-rn", "foo", "src"], { cwd: dir, env: { LOOM_API_KEY: "t", LOOM_BASE_URL: url, LOOM_GREP_SHIM_TIMEOUT_MS: "300" }, }); assert.equal(got.code, 0, String(mode)); assert.equal(got.out.equals(want.out), true, String(mode)); - assert.equal(seen.length > 0, true, String(mode)); + // The deadline can expire before HTTP dispatch; response handling is checked below. assert.ok(got.ms < 3000, `${mode} took ${got.ms} ms`); }); } }); +test("grep: a received 401 or malformed JSON takes the response fallback path", { + skip: !NATIVE.grep && "no native grep on PATH", +}, async () => { + const dir = checkout(); + try { + const want = await runRaw(NATIVE.grep, ["-rn", "foo", "src"], { cwd: dir }); + for (const mode of ["401", "garbage"]) { + const record = path.join(dir, `response-${mode}.json`); + const got = await shim("grep", ["-rn", "foo", "src"], { + cwd: dir, + env: { + LOOM_API_KEY: "t", + LOOM_BASE_URL: "http://response.invalid", + LOOM_GREP_SHIM_TIMEOUT_MS: "300", + NODE_OPTIONS: `--import=${new URL("./response-fixture.mjs", import.meta.url).href}`, + LOOM_TEST_RESPONSE_MODE: mode, + LOOM_TEST_RESPONSE_RECORD: record, + }, + }); + assert.equal(got.code, 0, mode); + assert.equal(got.out.equals(want.out), true, mode); + const seen = JSON.parse(fs.readFileSync(record, "utf8")); + assert.equal(seen.requests.length > 0, true, mode); + assert.equal(seen.requestValidated, true, mode); + assert.equal(seen.jsonCalls, mode === "garbage" ? 1 : 0, mode); + assert.equal(seen.jsonRejected, mode === "garbage", mode); + assert.ok(got.ms < 3000, `${mode} took ${got.ms} ms`); + } + } finally { + fs.rmSync(dir, { recursive: true, force: true }); + } +}); + test("grep: past the buffer cap the output streams unannotated and whole", { skip: !NATIVE.grep && "no native grep on PATH", }, async () => { From 1971fae3e4b71c29dd1d696a718980750e706383 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sat, 21 Nov 2026 09:56:17 +0000 Subject: [PATCH 2/2] Prove grep fallback aborts an admitted stalled fetch Signed-off-by: zlareb1 --- .../grep-shim/tests/response-fixture.mjs | 36 +++++++++++++++++-- integrations/grep-shim/tests/shim.test.mjs | 8 +++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/integrations/grep-shim/tests/response-fixture.mjs b/integrations/grep-shim/tests/response-fixture.mjs index 10e07a61cb..7558881297 100644 --- a/integrations/grep-shim/tests/response-fixture.mjs +++ b/integrations/grep-shim/tests/response-fixture.mjs @@ -1,11 +1,20 @@ -// Response-path tests replace transport so the real deadline cannot hide a parser branch. +// Control response handling and stalled fetches without HTTP startup timing. import assert from "node:assert/strict"; import fs from "node:fs"; const mode = process.env.LOOM_TEST_RESPONSE_MODE; -assert.ok(mode === "401" || mode === "garbage"); +assert.ok(mode === "401" || mode === "garbage" || mode === "hang"); const record = process.env.LOOM_TEST_RESPONSE_RECORD; -const seen = { requests: [], requestValidated: false, jsonCalls: 0, jsonRejected: false }; +const seen = { + requests: [], + requestValidated: false, + jsonCalls: 0, + jsonRejected: false, + pendingEntered: false, + signalAborted: false, + abortName: "", + watchdogExpired: false, +}; const save = () => fs.writeFileSync(record, JSON.stringify(seen)); const response = new Response(mode === "401" ? "{}" : "{not json", { status: mode === "401" ? 401 : 200, @@ -34,5 +43,26 @@ globalThis.fetch = async (url, options) => { options.signal.throwIfAborted(); seen.requestValidated = true; save(); + if (mode === "hang") { + seen.pendingEntered = true; + save(); + return new Promise((_, reject) => { + const onAbort = () => { + clearTimeout(watchdog); + seen.signalAborted = options.signal.aborted; + seen.abortName = options.signal.reason?.name || ""; + save(); + reject(options.signal.reason); + }; + // This test watchdog keeps Node alive; only the shim's signal proves its deadline. + const watchdog = setTimeout(() => { + options.signal.removeEventListener("abort", onAbort); + seen.watchdogExpired = true; + save(); + reject(new Error("stalled fetch did not receive the shim's abort")); + }, 3000); + options.signal.addEventListener("abort", onAbort, { once: true }); + }); + } return response; }; diff --git a/integrations/grep-shim/tests/shim.test.mjs b/integrations/grep-shim/tests/shim.test.mjs index 8dd843e062..41fe36dc83 100644 --- a/integrations/grep-shim/tests/shim.test.mjs +++ b/integrations/grep-shim/tests/shim.test.mjs @@ -243,13 +243,13 @@ test("grep: a 401, unparseable JSON or a server past the timeout all give native } }); -test("grep: a received 401 or malformed JSON takes the response fallback path", { +test("grep: received failures and an admitted stalled fetch give native output", { skip: !NATIVE.grep && "no native grep on PATH", }, async () => { const dir = checkout(); try { const want = await runRaw(NATIVE.grep, ["-rn", "foo", "src"], { cwd: dir }); - for (const mode of ["401", "garbage"]) { + for (const mode of ["401", "garbage", "hang"]) { const record = path.join(dir, `response-${mode}.json`); const got = await shim("grep", ["-rn", "foo", "src"], { cwd: dir, @@ -269,6 +269,10 @@ test("grep: a received 401 or malformed JSON takes the response fallback path", assert.equal(seen.requestValidated, true, mode); assert.equal(seen.jsonCalls, mode === "garbage" ? 1 : 0, mode); assert.equal(seen.jsonRejected, mode === "garbage", mode); + assert.equal(seen.pendingEntered, mode === "hang", mode); + assert.equal(seen.signalAborted, mode === "hang", mode); + assert.equal(seen.abortName, mode === "hang" ? "TimeoutError" : "", mode); + assert.equal(seen.watchdogExpired, false, mode); assert.ok(got.ms < 3000, `${mode} took ${got.ms} ms`); } } finally {