Carries 359 template commits, tag v0.2.0 plus one documentation commit: the generator-built feature index with its app, bounded-context and feature declarations, the src/platform kernel and scale tooling, the 2026-09-24 src review fixes, and the 2026-09-25 maturity review round with its CHANGELOG [보안]/[계약] rows. 691 of the 824 template paths are applied, 85 of them merged with product changes; 127 are not carried. Product identity is unchanged: name, version 0.1.0, routes, design and the TechLog domain. Three decisions worth naming. TechLog stays composed directly, as the last two syncs recorded; its directory carries a NOT_INSTALLED marker that says so, and every installed-* composer takes the template's projection with TechLog added on top. The reference sample is no longer installed: the template now installs a feature only through feature-module.ts and context-routes.ts, which load the screens this product deleted, so its three operations, six schemas and one topic leave the registries, recorded as registry change evidence. And AppRouter mounts the platform and TechLog routes only; the template's bounded-context route gateway waits for a generator-installed feature. Required fixes: readSubject on the BFF session owner (without it every HTTP Studio session would close as integration-failed), zod through the facade in nine TechLog files, noUnusedLocals, and public/config.json written out to match the local profile. Derived baselines were recomputed after reproducing both sides' values: CI contract 101 commands / 106 references / 141 artifacts, gate shape c9adcf85; registry approval 2dc6c3a3; the runtime config schema baseline equals the schema at8210d48. Verified here: types, lint, architecture, adapter inventory, CI contract, i18n, design system, diagnostics, browser security, dev release manifest, TechLog contract and build pass; 220 component, 155 integration, 15 contract, 418 TechLog, 43 reference-feature, 18 recipe and 46 release-coherence tests pass, and 2381 of 2383 unit tests. Reproduced at8210d48first and unchanged: check:registries (byte-identical), test:runtime-schema, verify:documentation. Brought by the template and escalated rather than suppressed: check:request-fanout on TechLog's cursor pagination, check:remediation-ledger (fails upstream too), the shared-chunk hash test, and the registry-dependent sample removal and second-feature tests. Playwright suites were not run. Full record, the five open questions and the follow-ups in docs/operations/template-sync-2026-09-29.md. (cherry picked from commit b2170cf265dea45dc9cc05d3c61e627e97f31cdb)
747 lines
20 KiB
TypeScript
747 lines
20 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
SSE_CONTINUE,
|
|
createFetchSseConnection,
|
|
} from "../../../src/adapters/realtime/sse/fetch-sse-connection.ts";
|
|
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,
|
|
} from "../../../src/contracts/realtime-streams.ts";
|
|
import {
|
|
realtimeFailure,
|
|
realtimeSuccess,
|
|
} from "../../../src/adapters/realtime/result.ts";
|
|
|
|
const encoder = new TextEncoder();
|
|
const continueEvent = () => realtimeSuccess(SSE_CONTINUE);
|
|
const RECOVERY_CHECKPOINT = Object.freeze({
|
|
recoveryMode: "CURSOR" as const,
|
|
streamEpoch: "stream-epoch.0001",
|
|
lastAppliedSequence: "1",
|
|
resumeCursor: "cursor-1",
|
|
}) as RealtimeRecoveryCheckpoint;
|
|
const RECOVERY_OUTCOME = realtimeTransportRecoveryCommitted(
|
|
"REFERENCE_STREAM" as StreamRegistrationId,
|
|
RECOVERY_CHECKPOINT,
|
|
);
|
|
|
|
function eventStream(
|
|
chunks: readonly string[],
|
|
options: Readonly<{
|
|
status?: number;
|
|
contentType?: string;
|
|
}> = {},
|
|
): Response {
|
|
return new Response(
|
|
new ReadableStream<Uint8Array>({
|
|
start(controller) {
|
|
for (const chunk of chunks) {
|
|
controller.enqueue(encoder.encode(chunk));
|
|
}
|
|
controller.close();
|
|
},
|
|
}),
|
|
{
|
|
status: options.status ?? 200,
|
|
headers: {
|
|
"Content-Type":
|
|
options.contentType ?? "text/event-stream; charset=utf-8",
|
|
},
|
|
},
|
|
);
|
|
}
|
|
|
|
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" =
|
|
"CURSOR",
|
|
) {
|
|
return createFetchSseConnection({
|
|
endpoint: "https://app.example.test/events",
|
|
applicationOrigin: "https://app.example.test",
|
|
recoveryMode,
|
|
fetcher,
|
|
});
|
|
}
|
|
|
|
describe("fetch SSE connection", () => {
|
|
it("uses the fixed request profile and streams events sequentially", async () => {
|
|
const response = eventStream([
|
|
": heartbeat\r\n",
|
|
"retry: 2500\n",
|
|
"id: cursor-1\ndata: first\n",
|
|
"data: second\n\n",
|
|
]);
|
|
const fetcher = vi.fn(
|
|
async (_input: RequestInfo | URL, _init?: RequestInit) =>
|
|
response,
|
|
);
|
|
const received: string[] = [];
|
|
const comments = vi.fn();
|
|
const hints = vi.fn();
|
|
const runtime = connection(fetcher as typeof fetch);
|
|
|
|
await expect(
|
|
runtime.read({
|
|
resumeCursor: "cursor-0",
|
|
onEvent: async (event) => {
|
|
received.push(event.data);
|
|
return realtimeSuccess(SSE_CONTINUE);
|
|
},
|
|
onComment: comments,
|
|
onRetryHint: hints,
|
|
}),
|
|
).resolves.toEqual({
|
|
ok: true,
|
|
value: {
|
|
kind: "EOF",
|
|
incompleteEventDiscarded: false,
|
|
retryHintMs: 2_500,
|
|
},
|
|
});
|
|
|
|
expect(received).toEqual(["first\nsecond"]);
|
|
expect(comments).toHaveBeenCalledOnce();
|
|
expect(hints).toHaveBeenCalledWith(2_500);
|
|
const [target, init] = fetcher.mock.calls[0] ?? [];
|
|
expect(target).toBe("https://app.example.test/events");
|
|
expect(init).toMatchObject({
|
|
method: "GET",
|
|
credentials: "same-origin",
|
|
redirect: "error",
|
|
cache: "no-store",
|
|
referrerPolicy: "no-referrer",
|
|
});
|
|
expect(new Headers(init?.headers)).toEqual(
|
|
new Headers({
|
|
Accept: "text/event-stream",
|
|
"Last-Event-ID": "cursor-0",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("holds event consumption behind the validated open barrier gate", async () => {
|
|
const runtime = connection(
|
|
(async () =>
|
|
eventStream([
|
|
"id: cursor-1\ndata: after-barrier\n\n",
|
|
])) as typeof fetch,
|
|
);
|
|
let release:
|
|
| ((result: RealtimeResult<void>) => void)
|
|
| undefined;
|
|
const gate = new Promise<RealtimeResult<void>>((resolve) => {
|
|
release = resolve;
|
|
});
|
|
const onOpen = vi.fn(() => gate);
|
|
const onEvent = vi.fn(continueEvent);
|
|
|
|
const reading = runtime.read({
|
|
resumeCursor: null,
|
|
onOpen,
|
|
onEvent,
|
|
});
|
|
await vi.waitFor(() => expect(onOpen).toHaveBeenCalledOnce());
|
|
expect(onEvent).not.toHaveBeenCalled();
|
|
|
|
release?.(realtimeSuccess(undefined));
|
|
await expect(reading).resolves.toMatchObject({
|
|
ok: true,
|
|
value: { kind: "EOF" },
|
|
});
|
|
expect(onEvent).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("propagates an exact open-barrier failure before reading events", async () => {
|
|
const runtime = connection(
|
|
(async () =>
|
|
eventStream([
|
|
"id: cursor-1\ndata: unreachable\n\n",
|
|
])) as typeof fetch,
|
|
);
|
|
const barrierFailure = realtimeFailure(
|
|
"SCOPE_FENCED",
|
|
"RECOVER",
|
|
false,
|
|
);
|
|
const onEvent = vi.fn(continueEvent);
|
|
|
|
await expect(
|
|
runtime.read({
|
|
resumeCursor: null,
|
|
onOpen: () => barrierFailure,
|
|
onEvent,
|
|
}),
|
|
).resolves.toBe(barrierFailure);
|
|
expect(onEvent).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("treats 204 as terminal and rejects incorrect media types", async () => {
|
|
const terminal = connection(
|
|
(async () => new Response(null, { status: 204 })) as typeof fetch,
|
|
);
|
|
await expect(
|
|
terminal.read({
|
|
resumeCursor: null,
|
|
onEvent: continueEvent,
|
|
}),
|
|
).resolves.toEqual({
|
|
ok: true,
|
|
value: { kind: "NO_RECONNECT" },
|
|
});
|
|
|
|
const wrongType = connection(
|
|
(async () =>
|
|
eventStream(["data: value\n\n"], {
|
|
contentType: "application/json",
|
|
})) as typeof fetch,
|
|
);
|
|
await expect(
|
|
wrongType.read({
|
|
resumeCursor: null,
|
|
onEvent: continueEvent,
|
|
}),
|
|
).resolves.toEqual({
|
|
ok: false,
|
|
error: {
|
|
kind: "PROTOCOL_MISMATCH",
|
|
operation: "CONNECT",
|
|
retryable: false,
|
|
},
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
[401, "AUTH_REQUIRED", false],
|
|
[403, "FORBIDDEN", false],
|
|
[409, "CURSOR_EXPIRED", false],
|
|
[410, "CURSOR_EXPIRED", false],
|
|
[429, "RATE_LIMITED", true],
|
|
[502, "PROVIDER_UNAVAILABLE", true],
|
|
[503, "PROVIDER_UNAVAILABLE", true],
|
|
[504, "PROVIDER_UNAVAILABLE", true],
|
|
[500, "PROTOCOL_MISMATCH", false],
|
|
] as const)(
|
|
"maps HTTP %i to %s without exposing a response body",
|
|
async (status, kind, retryable) => {
|
|
const runtime = connection(
|
|
(async () =>
|
|
new Response("private backend text", {
|
|
status,
|
|
headers:
|
|
status === 429 || status === 503
|
|
? { "Retry-After": "10" }
|
|
: undefined,
|
|
})) as typeof fetch,
|
|
);
|
|
const result = await runtime.read({
|
|
resumeCursor: null,
|
|
onEvent: continueEvent,
|
|
});
|
|
expect(result).toEqual({
|
|
ok: false,
|
|
error: {
|
|
kind,
|
|
operation: "CONNECT",
|
|
retryable,
|
|
},
|
|
});
|
|
expect(JSON.stringify(result)).not.toContain("private backend");
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
[429, "RATE_LIMITED"],
|
|
[503, "PROVIDER_UNAVAILABLE"],
|
|
] as const)(
|
|
"does not retry HTTP %i without a bounded server hint",
|
|
async (status, kind) => {
|
|
const runtime = connection(
|
|
(async () => new Response(null, { status })) as typeof fetch,
|
|
);
|
|
await expect(
|
|
runtime.read({
|
|
resumeCursor: null,
|
|
onEvent: continueEvent,
|
|
}),
|
|
).resolves.toEqual({
|
|
ok: false,
|
|
error: {
|
|
kind,
|
|
operation: "CONNECT",
|
|
retryable: false,
|
|
},
|
|
});
|
|
},
|
|
);
|
|
|
|
it("does not clamp an excessive Retry-After into an early retry", async () => {
|
|
const runtime = connection(
|
|
(async () =>
|
|
new Response(null, {
|
|
status: 429,
|
|
headers: { "Retry-After": "120" },
|
|
})) as typeof fetch,
|
|
);
|
|
await expect(
|
|
runtime.read({
|
|
resumeCursor: null,
|
|
onEvent: continueEvent,
|
|
}),
|
|
).resolves.toEqual({
|
|
ok: false,
|
|
error: {
|
|
kind: "RATE_LIMITED",
|
|
operation: "CONNECT",
|
|
retryable: false,
|
|
},
|
|
});
|
|
});
|
|
|
|
it("enforces direct event IDs only for CURSOR recovery", async () => {
|
|
const missingCursorId = connection(
|
|
(async () =>
|
|
eventStream(["data: value\n\n"])) as typeof fetch,
|
|
);
|
|
await expect(
|
|
missingCursorId.read({
|
|
resumeCursor: null,
|
|
onEvent: continueEvent,
|
|
}),
|
|
).resolves.toEqual({
|
|
ok: false,
|
|
error: {
|
|
kind: "PROTOCOL_MISMATCH",
|
|
operation: "DECODE",
|
|
retryable: false,
|
|
},
|
|
});
|
|
|
|
const unexpectedCursorId = connection(
|
|
(async () =>
|
|
eventStream([
|
|
"id: cursor-1\ndata: value\n\n",
|
|
])) as typeof fetch,
|
|
"SNAPSHOT_ONLY",
|
|
);
|
|
await expect(
|
|
unexpectedCursorId.read({
|
|
resumeCursor: null,
|
|
onEvent: continueEvent,
|
|
}),
|
|
).resolves.toEqual({
|
|
ok: false,
|
|
error: {
|
|
kind: "PROTOCOL_MISMATCH",
|
|
operation: "DECODE",
|
|
retryable: false,
|
|
},
|
|
});
|
|
});
|
|
|
|
it("stops the old stream after authority recovery commits", async () => {
|
|
const runtime = connection(
|
|
(async () =>
|
|
eventStream([
|
|
"id: cursor-1\ndata: first\n\n",
|
|
"id: cursor-2\ndata: stale-generation\n\n",
|
|
])) as typeof fetch,
|
|
);
|
|
const onEvent = vi.fn(() =>
|
|
realtimeSuccess(RECOVERY_OUTCOME),
|
|
);
|
|
|
|
await expect(
|
|
runtime.read({
|
|
resumeCursor: null,
|
|
onEvent,
|
|
}),
|
|
).resolves.toEqual({
|
|
ok: true,
|
|
value: RECOVERY_OUTCOME,
|
|
});
|
|
expect(onEvent).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("propagates a canonical consumer failure and aborts its event generation", async () => {
|
|
const runtime = connection(
|
|
(async () =>
|
|
eventStream([
|
|
"id: cursor-1\ndata: forbidden\n\n",
|
|
])) as typeof fetch,
|
|
);
|
|
let handlerSignal: AbortSignal | undefined;
|
|
|
|
await expect(
|
|
runtime.read({
|
|
resumeCursor: null,
|
|
onEvent: (_event, signal) => {
|
|
handlerSignal = signal;
|
|
return realtimeFailure("FORBIDDEN", "RECEIVE");
|
|
},
|
|
}),
|
|
).resolves.toEqual(
|
|
realtimeFailure("FORBIDDEN", "RECEIVE"),
|
|
);
|
|
expect(handlerSignal?.aborted).toBe(true);
|
|
});
|
|
|
|
it("fences recovery immediately and waits for bounded reader cancellation", async () => {
|
|
let releaseCancellation: (() => void) | undefined;
|
|
let cancellationStarted = false;
|
|
const stream = new ReadableStream<Uint8Array>({
|
|
start(controller) {
|
|
controller.enqueue(
|
|
encoder.encode("id: cursor-1\ndata: first\n\n"),
|
|
);
|
|
},
|
|
cancel() {
|
|
cancellationStarted = true;
|
|
return new Promise<void>((resolve) => {
|
|
releaseCancellation = resolve;
|
|
});
|
|
},
|
|
});
|
|
const runtime = connection(
|
|
(async () =>
|
|
new Response(stream, {
|
|
headers: { "Content-Type": "text/event-stream" },
|
|
})) as typeof fetch,
|
|
);
|
|
const pending = runtime.read({
|
|
resumeCursor: null,
|
|
onEvent: () =>
|
|
realtimeSuccess(RECOVERY_OUTCOME),
|
|
});
|
|
let settled = false;
|
|
void pending.then(() => {
|
|
settled = true;
|
|
});
|
|
for (let turn = 0; turn < 20; turn += 1) {
|
|
await Promise.resolve();
|
|
}
|
|
|
|
expect(cancellationStarted).toBe(true);
|
|
expect(settled).toBe(false);
|
|
releaseCancellation?.();
|
|
await expect(pending).resolves.toEqual({
|
|
ok: true,
|
|
value: RECOVERY_OUTCOME,
|
|
});
|
|
});
|
|
|
|
it("discards incomplete EOF and closes an active reader idempotently", async () => {
|
|
const incomplete = connection(
|
|
(async () =>
|
|
eventStream([
|
|
"id: cursor-1\ndata: incomplete\n",
|
|
])) as typeof fetch,
|
|
);
|
|
await expect(
|
|
incomplete.read({
|
|
resumeCursor: null,
|
|
onEvent() {
|
|
throw new Error("must not run");
|
|
},
|
|
}),
|
|
).resolves.toEqual({
|
|
ok: true,
|
|
value: {
|
|
kind: "EOF",
|
|
incompleteEventDiscarded: true,
|
|
retryHintMs: null,
|
|
},
|
|
});
|
|
|
|
const pendingStream = new ReadableStream<Uint8Array>({
|
|
pull: () => new Promise<void>(() => {}),
|
|
});
|
|
const active = connection(
|
|
(async () =>
|
|
new Response(pendingStream, {
|
|
headers: { "Content-Type": "text/event-stream" },
|
|
})) as typeof fetch,
|
|
);
|
|
const pending = active.read({
|
|
resumeCursor: null,
|
|
onEvent: continueEvent,
|
|
});
|
|
await Promise.resolve();
|
|
await Promise.resolve();
|
|
active.close();
|
|
active.close();
|
|
await expect(pending).resolves.toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
kind: "ABORTED",
|
|
operation: "CONNECT",
|
|
retryable: false,
|
|
},
|
|
});
|
|
await expect(
|
|
active.read({
|
|
resumeCursor: null,
|
|
onEvent: continueEvent,
|
|
}),
|
|
).resolves.toEqual({
|
|
ok: false,
|
|
error: {
|
|
kind: "CLOSED",
|
|
operation: "CONNECT",
|
|
retryable: false,
|
|
},
|
|
});
|
|
});
|
|
|
|
it("dispatches a CR-terminated blank block completed at EOF", async () => {
|
|
const runtime = connection(
|
|
(async () =>
|
|
eventStream([
|
|
"id: cursor-1\rdata: final\r\r",
|
|
])) as typeof fetch,
|
|
);
|
|
const received = vi.fn(() =>
|
|
realtimeSuccess(SSE_CONTINUE),
|
|
);
|
|
await expect(
|
|
runtime.read({
|
|
resumeCursor: null,
|
|
onEvent: received,
|
|
}),
|
|
).resolves.toMatchObject({
|
|
ok: true,
|
|
value: {
|
|
kind: "EOF",
|
|
incompleteEventDiscarded: false,
|
|
},
|
|
});
|
|
expect(received).toHaveBeenCalledWith(
|
|
expect.objectContaining({ data: "final" }),
|
|
expect.any(AbortSignal),
|
|
);
|
|
});
|
|
|
|
it("dispatches a single chunk holding more events than the per-chunk item cap", async () => {
|
|
const chunk = Array.from(
|
|
{ length: 300 },
|
|
(_, index) =>
|
|
`id: cursor-${index}\ndata: ${"x".repeat(300)}\n\n`,
|
|
).join("");
|
|
const runtime = connection(
|
|
(async () => eventStream([chunk])) as typeof fetch,
|
|
);
|
|
const received: string[] = [];
|
|
await expect(
|
|
runtime.read({
|
|
resumeCursor: null,
|
|
onEvent: (event) => {
|
|
received.push(event.id ?? "");
|
|
return realtimeSuccess(SSE_CONTINUE);
|
|
},
|
|
}),
|
|
).resolves.toMatchObject({ ok: true, value: { kind: "EOF" } });
|
|
expect(received).toHaveLength(300);
|
|
expect(received[299]).toBe("cursor-299");
|
|
});
|
|
|
|
it("dispatches every normal event from a 1MB reader chunk", async () => {
|
|
// The browser, not the server, picks the chunk size.
|
|
const chunk = Array.from(
|
|
{ length: 2_000 },
|
|
(_, index) =>
|
|
`id: cursor-${index}\ndata: ${"é".repeat(250)}\n\n`,
|
|
).join("");
|
|
expect(encoder.encode(chunk).byteLength).toBeGreaterThan(1_000_000);
|
|
const runtime = connection(
|
|
(async () => eventStream([chunk])) as typeof fetch,
|
|
);
|
|
const received: string[] = [];
|
|
await expect(
|
|
runtime.read({
|
|
resumeCursor: null,
|
|
onEvent: (event) => {
|
|
received.push(event.id ?? "");
|
|
return realtimeSuccess(SSE_CONTINUE);
|
|
},
|
|
}),
|
|
).resolves.toMatchObject({ ok: true, value: { kind: "EOF" } });
|
|
expect(received).toHaveLength(2_000);
|
|
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({
|
|
endpoint: "https://other.example.test/events",
|
|
applicationOrigin: "https://app.example.test",
|
|
recoveryMode: "CURSOR",
|
|
}),
|
|
).toThrow(TypeError);
|
|
expect(() =>
|
|
createFetchSseConnection({
|
|
endpoint:
|
|
"https://app.example.test/events?token=not-allowed",
|
|
applicationOrigin: "https://app.example.test",
|
|
recoveryMode: "CURSOR",
|
|
}),
|
|
).toThrow(TypeError);
|
|
|
|
const fetcher = vi.fn(
|
|
async () => eventStream(["data: unreachable\n\n"]),
|
|
);
|
|
const snapshotOnly = connection(
|
|
fetcher as unknown as typeof fetch,
|
|
"SNAPSHOT_ONLY",
|
|
);
|
|
await expect(
|
|
snapshotOnly.read({
|
|
resumeCursor: "cursor-not-allowed",
|
|
onEvent: continueEvent,
|
|
}),
|
|
).resolves.toEqual({
|
|
ok: false,
|
|
error: {
|
|
kind: "PROTOCOL_MISMATCH",
|
|
operation: "CONNECT",
|
|
retryable: false,
|
|
},
|
|
});
|
|
expect(fetcher).not.toHaveBeenCalled();
|
|
});
|
|
});
|