Skip to content
Closed
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
18 changes: 16 additions & 2 deletions test/local-ask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,31 @@ describe("an ask to a self-hosted server", () => {

it("ends at the timeout while the stream is still open, closing the request", async () => {
let aborted = false;
let socket: ReturnType<typeof setInterval> | undefined;
const fetchFn = (async (_u: string, init: { signal?: AbortSignal }) => {
init.signal?.addEventListener("abort", () => (aborted = true));
// An open connection is a socket, and a socket keeps the event loop
// alive. This in-memory stream is not one, and the ask's deadline is
// AbortSignal.timeout, whose timer is unref'd: with nothing else
// holding the loop it drains before the deadline fires. Node 26's test
// runner happens to hold the loop open while a test is pending; Node
// 22's (CI) does not, so this test and every one after it in the file
// was cancelled with "Promise resolution is still pending but the
// event loop has already resolved". Stand in for the socket until the
// request is closed.
socket = setInterval(() => {}, 1_000);
const body = new ReadableStream<Uint8Array>({
start(c) {
c.enqueue(new TextEncoder().encode(`data: ${JSON.stringify(delta({ reasoning_content: "x" }))}\n\n`));
init.signal?.addEventListener("abort", () => c.error(init.signal!.reason));
init.signal?.addEventListener("abort", () => {
clearInterval(socket);
c.error(init.signal!.reason);
});
},
});
return new Response(body, { headers: { "content-type": "text/event-stream" } });
}) as unknown as typeof fetch;
const r = await askModel({ baseUrl: LOCAL, model: "m", system: "s", prompt: "p", fetchFn, timeoutMs: 30 });
const r = await askModel({ baseUrl: LOCAL, model: "m", system: "s", prompt: "p", fetchFn, timeoutMs: 30 }).finally(() => clearInterval(socket));
assert.equal(aborted, true);
assert.match(!r.ok ? r.error : "", /did not answer within/);
});
Expand Down
Loading