|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { StandardSessionStreamManager } from "./manager.js"; |
| 3 | +import type { ApiClient } from "../apiClient/index.js"; |
| 4 | +import type { SSEStreamPart } from "../apiClient/runStream.js"; |
| 5 | + |
| 6 | +// Single-shot mock that mimics S2's long-poll: delivers `records` once via |
| 7 | +// `onPart` on the first subscribe call, then keeps the returned async |
| 8 | +// iterable OPEN until the abort signal fires. Real S2 keeps the SSE |
| 9 | +// connection alive on a long-poll; the manager's `runTail` finally / |
| 10 | +// reconnect path only fires when the connection actually closes. Returning |
| 11 | +// an empty stream synchronously triggers a tight reconnect loop, so the |
| 12 | +// mock parks indefinitely instead. |
| 13 | +function singleShotApiClient( |
| 14 | + records: Array<{ id: string; chunk: unknown; timestamp: number }> |
| 15 | +): ApiClient { |
| 16 | + let delivered = false; |
| 17 | + return { |
| 18 | + async subscribeToSessionStream<T>( |
| 19 | + _sessionIdOrExternalId: string, |
| 20 | + _io: "out" | "in", |
| 21 | + options?: { onPart?: (part: SSEStreamPart<T>) => void; signal?: AbortSignal } |
| 22 | + ) { |
| 23 | + if (!delivered) { |
| 24 | + delivered = true; |
| 25 | + for (const record of records) { |
| 26 | + options?.onPart?.(record as SSEStreamPart<T>); |
| 27 | + } |
| 28 | + } |
| 29 | + const signal = options?.signal; |
| 30 | + return (async function* () { |
| 31 | + if (signal?.aborted) return; |
| 32 | + await new Promise<void>((resolve) => { |
| 33 | + if (!signal) { |
| 34 | + // No signal — block the stream forever; tests must |
| 35 | + // explicitly call `disconnectStream` / `disconnect` to |
| 36 | + // unblock. |
| 37 | + return; |
| 38 | + } |
| 39 | + signal.addEventListener("abort", () => resolve(), { once: true }); |
| 40 | + }); |
| 41 | + })() as unknown as Awaited<ReturnType<ApiClient["subscribeToSessionStream"]>>; |
| 42 | + }, |
| 43 | + } as unknown as ApiClient; |
| 44 | +} |
| 45 | + |
| 46 | +describe("StandardSessionStreamManager — minTimestamp filter", () => { |
| 47 | + const sessionId = "session-1"; |
| 48 | + const io = "in" as const; |
| 49 | + |
| 50 | + it("dispatches records when no filter is set", async () => { |
| 51 | + const records = [ |
| 52 | + { id: "0", chunk: { kind: "message", payload: { id: "u1" } }, timestamp: 1000 }, |
| 53 | + { id: "1", chunk: { kind: "message", payload: { id: "u2" } }, timestamp: 2000 }, |
| 54 | + ]; |
| 55 | + const manager = new StandardSessionStreamManager(singleShotApiClient(records), "http://localhost"); |
| 56 | + |
| 57 | + const first = await manager.once(sessionId, io); |
| 58 | + expect(first).toEqual({ ok: true, output: { kind: "message", payload: { id: "u1" } } }); |
| 59 | + |
| 60 | + const second = await manager.once(sessionId, io); |
| 61 | + expect(second).toEqual({ ok: true, output: { kind: "message", payload: { id: "u2" } } }); |
| 62 | + |
| 63 | + manager.disconnectStream(sessionId, io); // stop reconnect loop |
| 64 | + manager.disconnect(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("drops records whose timestamp is <= minTimestamp", async () => { |
| 68 | + const records = [ |
| 69 | + { id: "0", chunk: { kind: "message", payload: { id: "u1" } }, timestamp: 1000 }, |
| 70 | + { id: "1", chunk: { kind: "message", payload: { id: "u2" } }, timestamp: 2000 }, |
| 71 | + { id: "2", chunk: { kind: "message", payload: { id: "u3" } }, timestamp: 3000 }, |
| 72 | + ]; |
| 73 | + const manager = new StandardSessionStreamManager(singleShotApiClient(records), "http://localhost"); |
| 74 | + |
| 75 | + // Cutoff at 2000 (inclusive: `<=` is dropped). Only u3 should pass. |
| 76 | + manager.setMinTimestamp(sessionId, io, 2000); |
| 77 | + |
| 78 | + const passed = await manager.once(sessionId, io, { timeoutMs: 200 }); |
| 79 | + expect(passed).toEqual({ ok: true, output: { kind: "message", payload: { id: "u3" } } }); |
| 80 | + |
| 81 | + manager.disconnectStream(sessionId, io); |
| 82 | + manager.disconnect(); |
| 83 | + }); |
| 84 | + |
| 85 | + it("clears the filter when set to undefined", async () => { |
| 86 | + const records = [ |
| 87 | + { id: "0", chunk: { kind: "message", payload: { id: "u1" } }, timestamp: 1000 }, |
| 88 | + ]; |
| 89 | + const manager = new StandardSessionStreamManager(singleShotApiClient(records), "http://localhost"); |
| 90 | + |
| 91 | + manager.setMinTimestamp(sessionId, io, 5000); |
| 92 | + manager.setMinTimestamp(sessionId, io, undefined); |
| 93 | + |
| 94 | + const passed = await manager.once(sessionId, io, { timeoutMs: 200 }); |
| 95 | + expect(passed).toEqual({ ok: true, output: { kind: "message", payload: { id: "u1" } } }); |
| 96 | + |
| 97 | + manager.disconnectStream(sessionId, io); |
| 98 | + manager.disconnect(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("filter is per-(sessionId, io) and doesn't bleed across streams", async () => { |
| 102 | + const inApi = singleShotApiClient([ |
| 103 | + { id: "0", chunk: { kind: "in-record" }, timestamp: 1000 }, |
| 104 | + ]); |
| 105 | + const manager = new StandardSessionStreamManager(inApi, "http://localhost"); |
| 106 | + |
| 107 | + manager.setMinTimestamp(sessionId, "in", 5000); |
| 108 | + |
| 109 | + // The "out" stream uses the same singleShotApiClient instance — its |
| 110 | + // single-shot delivers the same fixture, but the filter doesn't apply |
| 111 | + // to "out" so the record passes. |
| 112 | + const outResult = await manager.once(sessionId, "out", { timeoutMs: 200 }); |
| 113 | + expect(outResult).toEqual({ ok: true, output: { kind: "in-record" } }); |
| 114 | + |
| 115 | + // The "in" stream is filtered (minTimestamp=5000, record ts=1000): the |
| 116 | + // once() call should idle-timeout instead of resolving with the record. |
| 117 | + // But the singleShot instance has already delivered to the "out" tail, |
| 118 | + // so the "in" tail will get nothing on first connect anyway. Use a |
| 119 | + // separate manager+api to keep the assertion crisp. |
| 120 | + const inApi2 = singleShotApiClient([ |
| 121 | + { id: "0", chunk: { kind: "in-record-2" }, timestamp: 1000 }, |
| 122 | + ]); |
| 123 | + const manager2 = new StandardSessionStreamManager(inApi2, "http://localhost"); |
| 124 | + manager2.setMinTimestamp(sessionId, "in", 5000); |
| 125 | + |
| 126 | + const inResult = await manager2.once(sessionId, "in", { timeoutMs: 100 }); |
| 127 | + expect(inResult.ok).toBe(false); // filter-dropped → idle timeout |
| 128 | + |
| 129 | + manager.disconnectStream(sessionId, "in"); |
| 130 | + manager.disconnectStream(sessionId, "out"); |
| 131 | + manager.disconnect(); |
| 132 | + manager2.disconnectStream(sessionId, "in"); |
| 133 | + manager2.disconnect(); |
| 134 | + }); |
| 135 | + |
| 136 | + it("reset() clears all per-stream timestamp filters", async () => { |
| 137 | + const records = [ |
| 138 | + { id: "0", chunk: { kind: "message", payload: { id: "u1" } }, timestamp: 1000 }, |
| 139 | + ]; |
| 140 | + const manager = new StandardSessionStreamManager(singleShotApiClient(records), "http://localhost"); |
| 141 | + |
| 142 | + manager.setMinTimestamp(sessionId, io, 5000); |
| 143 | + manager.reset(); |
| 144 | + |
| 145 | + const passed = await manager.once(sessionId, io, { timeoutMs: 200 }); |
| 146 | + expect(passed).toEqual({ ok: true, output: { kind: "message", payload: { id: "u1" } } }); |
| 147 | + |
| 148 | + manager.disconnectStream(sessionId, io); |
| 149 | + manager.disconnect(); |
| 150 | + }); |
| 151 | +}); |
0 commit comments