test(realtime): cover the SSE idle timeout and its comment reset (04-06)
The IDLE_TIMEOUT path had no test, so flipping it to retryable:false (M7) passed. Two ManualClock tests now fail on that mutation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
2138896472
commit
905189ef70
@@ -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<void> {
|
||||
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<void> {
|
||||
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<Uint8Array>({
|
||||
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({
|
||||
|
||||
Reference in New Issue
Block a user