From bb9804cf7090c11297845178171df0f8b30dfa7e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 07:38:23 +0000 Subject: [PATCH] Cover stale websocket message isolation after renderer reconnect Co-authored-by: Theo Browne --- apps/renderer/src/wsNativeApi.test.ts | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/apps/renderer/src/wsNativeApi.test.ts b/apps/renderer/src/wsNativeApi.test.ts index 0587e53235..1a023c4308 100644 --- a/apps/renderer/src/wsNativeApi.test.ts +++ b/apps/renderer/src/wsNativeApi.test.ts @@ -973,6 +973,61 @@ describe("wsNativeApi", () => { await expect(secondRequest).resolves.toEqual([]); }); + it("ignores stale message events from prior sockets after reconnect", async () => { + setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4477"); + 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.closeWith({ + code: WS_CLOSE_CODES.unauthorized, + reason: WS_CLOSE_REASONS.unauthorized, + }); + + 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; + }; + + firstSocket.emitMessage( + JSON.stringify({ + type: "response", + id: secondEnvelope.id, + ok: true, + result: ["stale-result"], + }), + ); + + 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 {