perf(test): replace gateway chat polling loops with waitFor

This commit is contained in:
Peter Steinberger
2026-02-18 17:28:25 +00:00
parent e8e47ff00e
commit f9e67f3f4c

View File

@@ -16,17 +16,6 @@ import {
installGatewayTestHooks({ scope: "suite" }); installGatewayTestHooks({ scope: "suite" });
async function waitFor(condition: () => boolean, timeoutMs = 1_500) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (condition()) {
return;
}
await new Promise((resolve) => setTimeout(resolve, 5));
}
throw new Error("timeout waiting for condition");
}
const sendReq = ( const sendReq = (
ws: { send: (payload: string) => void }, ws: { send: (payload: string) => void },
id: string, id: string,
@@ -300,7 +289,12 @@ describe("gateway server chat", () => {
const sendRes = await sendResP; const sendRes = await sendResP;
expect(sendRes.ok).toBe(true); expect(sendRes.ok).toBe(true);
await waitFor(() => spy.mock.calls.length > 0, 2_000); await vi.waitFor(
() => {
expect(spy.mock.calls.length).toBeGreaterThan(0);
},
{ timeout: 2_000, interval: 10 },
);
const inFlight = await rpcReq<{ status?: string }>(ws, "chat.send", { const inFlight = await rpcReq<{ status?: string }>(ws, "chat.send", {
sessionKey: "main", sessionKey: "main",
@@ -316,7 +310,12 @@ describe("gateway server chat", () => {
}); });
expect(abortRes.ok).toBe(true); expect(abortRes.ok).toBe(true);
expect(abortRes.payload?.aborted).toBe(true); expect(abortRes.payload?.aborted).toBe(true);
await waitFor(() => aborted, 2_000); await vi.waitFor(
() => {
expect(aborted).toBe(true);
},
{ timeout: 2_000, interval: 10 },
);
spy.mockReset(); spy.mockReset();
spy.mockResolvedValueOnce(undefined); spy.mockResolvedValueOnce(undefined);
@@ -328,20 +327,18 @@ describe("gateway server chat", () => {
}); });
expect(completeRes.ok).toBe(true); expect(completeRes.ok).toBe(true);
let completed = false; await vi.waitFor(
for (let i = 0; i < 20; i += 1) { async () => {
const again = await rpcReq<{ status?: string }>(ws, "chat.send", { const again = await rpcReq<{ status?: string }>(ws, "chat.send", {
sessionKey: "main", sessionKey: "main",
message: "hello", message: "hello",
idempotencyKey: "idem-complete-1", idempotencyKey: "idem-complete-1",
}); });
if (again.ok && again.payload?.status === "ok") { expect(again.ok).toBe(true);
completed = true; expect(again.payload?.status).toBe("ok");
break; },
} { timeout: 2_000, interval: 10 },
await new Promise((resolve) => setTimeout(resolve, 10)); );
}
expect(completed).toBe(true);
}); });
}); });
}); });