Skip to content
Merged
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
68 changes: 68 additions & 0 deletions integrations/grep-shim/tests/response-fixture.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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" || mode === "hang");
const record = process.env.LOOM_TEST_RESPONSE_RECORD;
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,
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();
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;
};
41 changes: 39 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,56 @@ 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: 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", "hang"]) {
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.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 {
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