fix(test): stabilize chat.abort
parent
35214b6dec
commit
69daa24869
|
|
@ -1970,102 +1970,113 @@ describe("gateway server", () => {
|
||||||
await server.close();
|
await server.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
test("chat.abort cancels an in-flight chat.send", { timeout: 15000 }, async () => {
|
test(
|
||||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdis-gw-"));
|
"chat.abort cancels an in-flight chat.send",
|
||||||
testSessionStorePath = path.join(dir, "sessions.json");
|
{ timeout: 15000 },
|
||||||
await fs.writeFile(
|
async () => {
|
||||||
testSessionStorePath,
|
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdis-gw-"));
|
||||||
JSON.stringify(
|
testSessionStorePath = path.join(dir, "sessions.json");
|
||||||
{
|
await fs.writeFile(
|
||||||
main: {
|
testSessionStorePath,
|
||||||
sessionId: "sess-main",
|
JSON.stringify(
|
||||||
updatedAt: Date.now(),
|
{
|
||||||
|
main: {
|
||||||
|
sessionId: "sess-main",
|
||||||
|
updatedAt: Date.now(),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
null,
|
||||||
null,
|
2,
|
||||||
2,
|
),
|
||||||
),
|
"utf-8",
|
||||||
"utf-8",
|
);
|
||||||
);
|
|
||||||
|
|
||||||
const { server, ws } = await startServerWithClient();
|
const { server, ws } = await startServerWithClient();
|
||||||
try {
|
let inFlight: Promise<unknown> | undefined;
|
||||||
await connectOk(ws);
|
try {
|
||||||
|
await connectOk(ws);
|
||||||
|
|
||||||
const spy = vi.mocked(agentCommand);
|
const spy = vi.mocked(agentCommand);
|
||||||
spy.mockImplementationOnce(async (opts) => {
|
const callsBefore = spy.mock.calls.length;
|
||||||
const signal = (opts as { abortSignal?: AbortSignal }).abortSignal;
|
spy.mockImplementationOnce(async (opts) => {
|
||||||
await new Promise<void>((resolve) => {
|
const signal = (opts as { abortSignal?: AbortSignal }).abortSignal;
|
||||||
if (!signal) return resolve();
|
await new Promise<void>((resolve) => {
|
||||||
if (signal.aborted) return resolve();
|
if (!signal) return resolve();
|
||||||
signal.addEventListener("abort", () => resolve(), { once: true });
|
if (signal.aborted) return resolve();
|
||||||
|
signal.addEventListener("abort", () => resolve(), { once: true });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
const sendResP = onceMessage(
|
const sendResP = onceMessage(
|
||||||
ws,
|
ws,
|
||||||
(o) => o.type === "res" && o.id === "send-abort-1",
|
(o) => o.type === "res" && o.id === "send-abort-1",
|
||||||
8000,
|
8000,
|
||||||
);
|
);
|
||||||
const abortResP = onceMessage(
|
const abortResP = onceMessage(
|
||||||
ws,
|
ws,
|
||||||
(o) => o.type === "res" && o.id === "abort-1",
|
(o) => o.type === "res" && o.id === "abort-1",
|
||||||
8000,
|
8000,
|
||||||
);
|
);
|
||||||
const abortedEventP = onceMessage(
|
const abortedEventP = onceMessage(
|
||||||
ws,
|
ws,
|
||||||
(o) => o.type === "event" && o.event === "chat" && o.payload?.state === "aborted",
|
(o) =>
|
||||||
8000,
|
o.type === "event" &&
|
||||||
);
|
o.event === "chat" &&
|
||||||
|
o.payload?.state === "aborted",
|
||||||
|
8000,
|
||||||
|
);
|
||||||
|
inFlight = Promise.allSettled([sendResP, abortResP, abortedEventP]);
|
||||||
|
|
||||||
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<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);
|
||||||
};
|
};
|
||||||
tick();
|
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 abortResP;
|
const abortRes = await abortResP;
|
||||||
expect(abortRes.ok).toBe(true);
|
expect(abortRes.ok).toBe(true);
|
||||||
|
|
||||||
const sendRes = await sendResP;
|
const sendRes = await sendResP;
|
||||||
expect(sendRes.ok).toBe(true);
|
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 {
|
} finally {
|
||||||
ws.close();
|
ws.close();
|
||||||
await server.close();
|
await inFlight;
|
||||||
}
|
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-"));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue