diff --git a/tests/unit/realtime/fetch-sse-connection.test.ts b/tests/unit/realtime/fetch-sse-connection.test.ts index 4057709f..aa5862b5 100644 --- a/tests/unit/realtime/fetch-sse-connection.test.ts +++ b/tests/unit/realtime/fetch-sse-connection.test.ts @@ -8,6 +8,7 @@ import { realtimeTransportRecoveryCommitted, type RealtimeRecoveryCheckpoint, } from "../../../src/application/ports/realtime/event-authority.ts"; +import type { ClockPort } from "../../../src/application/ports/clock-port.ts"; import type { RealtimeResult } from "../../../src/application/ports/realtime/shared.ts"; import type { StreamRegistrationId, @@ -56,6 +57,66 @@ function eventStream( ); } +type Sleeper = { dueAt: number; resolve(): void }; + +class ManualClock implements ClockPort { + current = 0; + private sleepers: Sleeper[] = []; + + now(): number { + return this.current; + } + + sleep(milliseconds: number, signal?: AbortSignal): Promise { + return new Promise((resolve, reject) => { + if (signal?.aborted) { + reject(new Error("Aborted.")); + return; + } + const sleeper = { dueAt: this.current + milliseconds, resolve }; + signal?.addEventListener( + "abort", + () => { + this.sleepers = this.sleepers.filter((entry) => entry !== sleeper); + reject(new Error("Aborted.")); + }, + { once: true }, + ); + this.sleepers.push(sleeper); + }); + } + + advance(milliseconds: number): void { + this.current += milliseconds; + const ready = this.sleepers.filter( + (sleeper) => sleeper.dueAt <= this.current, + ); + this.sleepers = this.sleepers.filter( + (sleeper) => sleeper.dueAt > this.current, + ); + for (const sleeper of ready) sleeper.resolve(); + } +} + +async function settle(): Promise { + await new Promise((resolve) => setTimeout(resolve, 0)); +} + +function openStream() { + let push: ((text: string) => void) | undefined; + const cancel = vi.fn(); + const response = new Response( + new ReadableStream({ + start(controller) { + push = (text) => controller.enqueue(encoder.encode(text)); + }, + cancel, + }), + { headers: { "Content-Type": "text/event-stream" } }, + ); + return { response, cancel, push: (text: string) => push?.(text) }; +} + function connection( fetcher: typeof fetch, recoveryMode: "CURSOR" | "SESSION_REBUILD" | "SNAPSHOT_ONLY" = @@ -574,6 +635,75 @@ describe("fetch SSE connection", () => { expect(received[1_999]).toBe("cursor-1999"); }); + it("ends a silent stream with a retryable IDLE_TIMEOUT and cancels the reader", async () => { + const clock = new ManualClock(); + const stream = openStream(); + const runtime = createFetchSseConnection({ + endpoint: "https://app.example.test/events", + applicationOrigin: "https://app.example.test", + recoveryMode: "CURSOR", + fetcher: (async () => stream.response) as typeof fetch, + clock, + idleTimeoutMs: 1_000, + }); + let settled = false; + const pending = runtime.read({ + resumeCursor: null, + onEvent: continueEvent, + }); + void pending.then(() => { + settled = true; + }); + await settle(); + + clock.advance(999); + await settle(); + expect(settled).toBe(false); + + clock.advance(1); + await expect(pending).resolves.toEqual( + realtimeFailure("IDLE_TIMEOUT", "RECEIVE", true), + ); + expect(stream.cancel).toHaveBeenCalledOnce(); + }); + + it("restarts the idle deadline on every comment", async () => { + const clock = new ManualClock(); + const stream = openStream(); + const comments = vi.fn(); + const runtime = createFetchSseConnection({ + endpoint: "https://app.example.test/events", + applicationOrigin: "https://app.example.test", + recoveryMode: "CURSOR", + fetcher: (async () => stream.response) as typeof fetch, + clock, + idleTimeoutMs: 1_000, + }); + let settled = false; + const pending = runtime.read({ + resumeCursor: null, + onEvent: continueEvent, + onComment: comments, + }); + void pending.then(() => { + settled = true; + }); + await settle(); + + for (let beat = 0; beat < 3; beat += 1) { + clock.advance(900); + stream.push(": heartbeat\n"); + await settle(); + } + expect(comments).toHaveBeenCalledTimes(3); + expect(settled).toBe(false); + + clock.advance(1_000); + await expect(pending).resolves.toEqual( + realtimeFailure("IDLE_TIMEOUT", "RECEIVE", true), + ); + }); + it("rejects arbitrary endpoints and mode-incoherent cursors before fetch", async () => { expect(() => createFetchSseConnection({