test(gateway): stabilize chat abort
This commit is contained in:
@@ -1970,7 +1970,7 @@ describe("gateway server", () => {
|
|||||||
await server.close();
|
await server.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
test("chat.abort cancels an in-flight chat.send", 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,66 +1989,82 @@ describe("gateway server", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const { server, ws } = await startServerWithClient();
|
const { server, ws } = await startServerWithClient();
|
||||||
await connectOk(ws);
|
try {
|
||||||
|
await connectOk(ws);
|
||||||
|
|
||||||
const spy = vi.mocked(agentCommand);
|
const spy = vi.mocked(agentCommand);
|
||||||
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) => {
|
||||||
if (!signal) return resolve();
|
if (!signal) return resolve();
|
||||||
if (signal.aborted) return resolve();
|
if (signal.aborted) return resolve();
|
||||||
signal.addEventListener("abort", () => resolve(), { once: true });
|
signal.addEventListener("abort", () => resolve(), { once: true });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
const abortedEventP = onceMessage(
|
const sendResP = onceMessage(
|
||||||
ws,
|
ws,
|
||||||
(o) => o.type === "event" && o.event === "chat" && o.payload?.state === "aborted",
|
(o) => o.type === "res" && o.id === "send-abort-1",
|
||||||
);
|
8000,
|
||||||
|
);
|
||||||
|
const abortResP = onceMessage(
|
||||||
|
ws,
|
||||||
|
(o) => o.type === "res" && o.id === "abort-1",
|
||||||
|
8000,
|
||||||
|
);
|
||||||
|
const abortedEventP = onceMessage(
|
||||||
|
ws,
|
||||||
|
(o) => o.type === "event" && o.event === "chat" && o.payload?.state === "aborted",
|
||||||
|
8000,
|
||||||
|
);
|
||||||
|
|
||||||
ws.send(
|
ws.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
type: "req",
|
type: "req",
|
||||||
id: "send-abort-1",
|
id: "send-abort-1",
|
||||||
method: "chat.send",
|
method: "chat.send",
|
||||||
params: {
|
params: {
|
||||||
sessionKey: "main",
|
sessionKey: "main",
|
||||||
message: "hello",
|
message: "hello",
|
||||||
idempotencyKey: "idem-abort-1",
|
idempotencyKey: "idem-abort-1",
|
||||||
timeoutMs: 30_000,
|
timeoutMs: 30_000,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
await new Promise((r) => setTimeout(r, 10));
|
await new Promise<void>((resolve, reject) => {
|
||||||
|
const deadline = Date.now() + 1000;
|
||||||
|
const tick = () => {
|
||||||
|
if (spy.mock.calls.length > 0) return resolve();
|
||||||
|
if (Date.now() > deadline)
|
||||||
|
return reject(new Error("timeout waiting for agentCommand"));
|
||||||
|
setTimeout(tick, 5);
|
||||||
|
};
|
||||||
|
tick();
|
||||||
|
});
|
||||||
|
|
||||||
ws.send(
|
ws.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
type: "req",
|
type: "req",
|
||||||
id: "abort-1",
|
id: "abort-1",
|
||||||
method: "chat.abort",
|
method: "chat.abort",
|
||||||
params: { sessionKey: "main", runId: "idem-abort-1" },
|
params: { sessionKey: "main", runId: "idem-abort-1" },
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
const abortRes = await onceMessage(
|
const abortRes = await abortResP;
|
||||||
ws,
|
expect(abortRes.ok).toBe(true);
|
||||||
(o) => o.type === "res" && o.id === "abort-1",
|
|
||||||
);
|
|
||||||
expect(abortRes.ok).toBe(true);
|
|
||||||
|
|
||||||
const sendRes = await onceMessage(
|
const sendRes = await sendResP;
|
||||||
ws,
|
expect(sendRes.ok).toBe(true);
|
||||||
(o) => o.type === "res" && o.id === "send-abort-1",
|
|
||||||
);
|
|
||||||
expect(sendRes.ok).toBe(true);
|
|
||||||
|
|
||||||
const evt = await abortedEventP;
|
const evt = await abortedEventP;
|
||||||
expect(evt.payload?.runId).toBe("idem-abort-1");
|
expect(evt.payload?.runId).toBe("idem-abort-1");
|
||||||
expect(evt.payload?.sessionKey).toBe("main");
|
expect(evt.payload?.sessionKey).toBe("main");
|
||||||
|
} finally {
|
||||||
ws.close();
|
ws.close();
|
||||||
await server.close();
|
await server.close();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test("bridge RPC chat.history returns session messages", async () => {
|
test("bridge RPC chat.history returns session messages", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user