From a909dad266915b5ad6202d8d3c7d5a16bb635bbc Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Sat, 21 Nov 2026 00:26:37 +0000 Subject: [PATCH] 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 () => {