diff --git a/apps/renderer/src/wsNativeApi.test.ts b/apps/renderer/src/wsNativeApi.test.ts index 1a023c4308..b83e690a60 100644 --- a/apps/renderer/src/wsNativeApi.test.ts +++ b/apps/renderer/src/wsNativeApi.test.ts @@ -1985,6 +1985,92 @@ describe("wsNativeApi", () => { expect(received).toHaveLength(1); }); + it("ignores provider events from stale sockets after reconnect", async () => { + setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4478"); + const { getOrCreateWsNativeApi } = await import("./wsNativeApi"); + const api = getOrCreateWsNativeApi(); + + const received: unknown[] = []; + const unsubscribe = api.providers.onEvent((event) => { + received.push(event); + }); + + const firstRequest = api.todos.list(); + const firstSocket = MockWebSocket.instances[0]; + await waitForCondition(() => (firstSocket?.sentMessages.length ?? 0) > 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([]); + + firstSocket?.emitMessage( + JSON.stringify({ + type: "event", + channel: "provider:event", + payload: { + id: "stale-provider-event", + kind: "notification", + provider: "codex", + sessionId: "sess-1", + createdAt: "2026-02-01T00:00:00.000Z", + method: "turn/started", + }, + }), + ); + await new Promise((resolve) => { + setTimeout(resolve, 25); + }); + expect(received).toHaveLength(0); + + secondSocket?.emitMessage( + JSON.stringify({ + type: "event", + channel: "provider:event", + payload: { + id: "active-provider-event", + kind: "notification", + provider: "codex", + sessionId: "sess-1", + createdAt: "2026-02-01T00:00:01.000Z", + method: "turn/started", + }, + }), + ); + await waitForCondition(() => received.length === 1); + + unsubscribe(); + }); + it("dispatches agent output and exit events to subscribers", async () => { setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4410"); const { getOrCreateWsNativeApi } = await import("./wsNativeApi");