Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions integrations/grep-shim/tests/response-fixture.mjs
Original file line number Diff line number Diff line change
@@ -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;
};
37 changes: 35 additions & 2 deletions integrations/grep-shim/tests/shim.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Loading