fix(test): stabilize chat.abort

main
Peter Steinberger 2025-12-17 22:12:16 +01:00
parent 35214b6dec
commit 69daa24869
1 changed files with 96 additions and 85 deletions

View File

@ -1970,7 +1970,10 @@ describe("gateway server", () => {
await server.close(); await server.close();
}); });
test("chat.abort cancels an in-flight chat.send", { timeout: 15000 }, async () => { test(
"chat.abort cancels an in-flight chat.send",
{ timeout: 15000 },
async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdis-gw-")); const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdis-gw-"));
testSessionStorePath = path.join(dir, "sessions.json"); testSessionStorePath = path.join(dir, "sessions.json");
await fs.writeFile( await fs.writeFile(
@ -1989,10 +1992,12 @@ describe("gateway server", () => {
); );
const { server, ws } = await startServerWithClient(); const { server, ws } = await startServerWithClient();
let inFlight: Promise<unknown> | undefined;
try { try {
await connectOk(ws); await connectOk(ws);
const spy = vi.mocked(agentCommand); const spy = vi.mocked(agentCommand);
const callsBefore = spy.mock.calls.length;
spy.mockImplementationOnce(async (opts) => { spy.mockImplementationOnce(async (opts) => {
const signal = (opts as { abortSignal?: AbortSignal }).abortSignal; const signal = (opts as { abortSignal?: AbortSignal }).abortSignal;
await new Promise<void>((resolve) => { await new Promise<void>((resolve) => {
@ -2014,9 +2019,13 @@ describe("gateway server", () => {
); );
const abortedEventP = onceMessage( const abortedEventP = onceMessage(
ws, ws,
(o) => o.type === "event" && o.event === "chat" && o.payload?.state === "aborted", (o) =>
o.type === "event" &&
o.event === "chat" &&
o.payload?.state === "aborted",
8000, 8000,
); );
inFlight = Promise.allSettled([sendResP, abortResP, abortedEventP]);
ws.send( ws.send(
JSON.stringify({ JSON.stringify({
@ -2035,7 +2044,7 @@ describe("gateway server", () => {
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
const deadline = Date.now() + 1000; const deadline = Date.now() + 1000;
const tick = () => { const tick = () => {
if (spy.mock.calls.length > 0) return resolve(); if (spy.mock.calls.length > callsBefore) return resolve();
if (Date.now() > deadline) if (Date.now() > deadline)
return reject(new Error("timeout waiting for agentCommand")); return reject(new Error("timeout waiting for agentCommand"));
setTimeout(tick, 5); setTimeout(tick, 5);
@ -2063,9 +2072,11 @@ describe("gateway server", () => {
expect(evt.payload?.sessionKey).toBe("main"); expect(evt.payload?.sessionKey).toBe("main");
} finally { } finally {
ws.close(); ws.close();
await inFlight;
await server.close(); await server.close();
} }
}); },
);
test("bridge RPC chat.history returns session messages", async () => { test("bridge RPC chat.history returns session messages", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdis-gw-")); const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdis-gw-"));