Cover websocket reconnect behavior after socket close

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-10 00:11:40 +00:00
1 parent 4f48f21186
commit b869dc61b4
1 file changed
+43
+43
View File
@@ -345,6 +345,49 @@ describe("wsNativeApi", () => {
await expect(request).rejects.toThrow("websocket disconnected");
});
it("reconnects on subsequent requests after websocket close", async () => {
setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4428");
const { getOrCreateWsNativeApi } = await import("./wsNativeApi");
const api = getOrCreateWsNativeApi();
const firstRequest = api.todos.list();
const firstSocket = await waitForSocket();
await waitForCondition(() => firstSocket.sentMessages.length > 0);
const firstEnvelope = JSON.parse(firstSocket.sentMessages[0] ?? "{}") as {
id: string;
};
firstSocket.emitMessage(
JSON.stringify({
type: "response",
id: firstEnvelope.id,
ok: true,
result: [],
}),
);
await expect(firstRequest).resolves.toEqual([]);
firstSocket.close();
await waitForCondition(() => MockWebSocket.instances.length >= 1);
const secondRequest = api.todos.list();
await waitForCondition(() => MockWebSocket.instances.length >= 2);
const secondSocket = MockWebSocket.instances[1];
await waitForCondition(() => (secondSocket?.sentMessages.length ?? 0) > 0);
const secondEnvelope = JSON.parse(secondSocket?.sentMessages[0] ?? "{}") as {
id: string;
};
secondSocket?.emitMessage(
JSON.stringify({
type: "response",
id: secondEnvelope.id,
ok: true,
result: [],
}),
);
await expect(secondRequest).resolves.toEqual([]);
});
it("rejects requests when runtime does not respond before timeout", async () => {
vi.useFakeTimers();
try {