diff --git a/apps/renderer/src/wsNativeApi.test.ts b/apps/renderer/src/wsNativeApi.test.ts index ae2bd8666a..ddb11fd22f 100644 --- a/apps/renderer/src/wsNativeApi.test.ts +++ b/apps/renderer/src/wsNativeApi.test.ts @@ -579,6 +579,96 @@ describe("wsNativeApi", () => { await expect(secondRequest).resolves.toEqual([]); }); + it("reconnects on subsequent requests after unauthorized websocket close", async () => { + setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4465"); + 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; + }; + secondSocket?.emitMessage( + JSON.stringify({ + type: "response", + id: secondEnvelope.id, + ok: true, + result: [], + }), + ); + + await expect(secondRequest).resolves.toEqual([]); + }); + + it("reconnects on subsequent requests after replacement websocket close", async () => { + setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4466"); + 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.replacedByNewClient, + reason: WS_CLOSE_REASONS.replacedByNewClient, + }); + + 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 {