|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import type { ApiClient } from "../apiClient/index.js"; |
| 3 | +import { StandardRealtimeStreamsManager } from "./manager.js"; |
| 4 | + |
| 5 | +// The cache lives on a private method to keep `pipe()` callers from having |
| 6 | +// to thread cache concerns. Tests exercise it via bracket-notation to keep |
| 7 | +// the assertions tight on cache contracts and avoid spinning up real |
| 8 | +// `StreamsWriterV1`/`StreamsWriterV2` infrastructure (HTTP requests, S2 |
| 9 | +// connections) for what is purely an in-memory dedup check. |
| 10 | +type GetCached = ( |
| 11 | + runId: string, |
| 12 | + key: string, |
| 13 | + requestOptions?: undefined |
| 14 | +) => Promise<{ version: string; headers?: Record<string, string> }>; |
| 15 | + |
| 16 | +function getCached(manager: StandardRealtimeStreamsManager, runId: string, key: string) { |
| 17 | + return (manager as unknown as { getCachedCreateStream: GetCached }).getCachedCreateStream( |
| 18 | + runId, |
| 19 | + key |
| 20 | + ); |
| 21 | +} |
| 22 | + |
| 23 | +function makeApiClient(impl: () => Promise<{ version: string; headers?: Record<string, string> }>) { |
| 24 | + const spy = vi.fn(impl); |
| 25 | + const client = { createStream: spy } as unknown as ApiClient; |
| 26 | + return { client, spy }; |
| 27 | +} |
| 28 | + |
| 29 | +describe("StandardRealtimeStreamsManager createStream cache", () => { |
| 30 | + it("dedupes repeated calls for the same (runId, key)", async () => { |
| 31 | + const { client, spy } = makeApiClient(async () => ({ version: "v1", headers: {} })); |
| 32 | + const manager = new StandardRealtimeStreamsManager(client, "http://localhost"); |
| 33 | + |
| 34 | + const p1 = getCached(manager, "run-1", "chat"); |
| 35 | + const p2 = getCached(manager, "run-1", "chat"); |
| 36 | + |
| 37 | + expect(p1).toBe(p2); |
| 38 | + expect(spy).toHaveBeenCalledTimes(1); |
| 39 | + await Promise.all([p1, p2]); |
| 40 | + expect(spy).toHaveBeenCalledTimes(1); |
| 41 | + }); |
| 42 | + |
| 43 | + it("issues a separate PUT for each distinct stream key on the same run", async () => { |
| 44 | + const { client, spy } = makeApiClient(async () => ({ version: "v1", headers: {} })); |
| 45 | + const manager = new StandardRealtimeStreamsManager(client, "http://localhost"); |
| 46 | + |
| 47 | + await Promise.all([ |
| 48 | + getCached(manager, "run-1", "chat"), |
| 49 | + getCached(manager, "run-1", "tool-output"), |
| 50 | + ]); |
| 51 | + |
| 52 | + expect(spy).toHaveBeenCalledTimes(2); |
| 53 | + expect(spy).toHaveBeenNthCalledWith(1, "run-1", "self", "chat", undefined); |
| 54 | + expect(spy).toHaveBeenNthCalledWith(2, "run-1", "self", "tool-output", undefined); |
| 55 | + }); |
| 56 | + |
| 57 | + it("issues a separate PUT for each distinct run, even with the same key", async () => { |
| 58 | + const { client, spy } = makeApiClient(async () => ({ version: "v1", headers: {} })); |
| 59 | + const manager = new StandardRealtimeStreamsManager(client, "http://localhost"); |
| 60 | + |
| 61 | + await Promise.all([ |
| 62 | + getCached(manager, "run-1", "chat"), |
| 63 | + getCached(manager, "run-2", "chat"), |
| 64 | + ]); |
| 65 | + |
| 66 | + expect(spy).toHaveBeenCalledTimes(2); |
| 67 | + }); |
| 68 | + |
| 69 | + it("evicts on failure so the next call retries instead of returning a poisoned entry", async () => { |
| 70 | + const spy = vi |
| 71 | + .fn() |
| 72 | + .mockRejectedValueOnce(new Error("boom")) |
| 73 | + .mockResolvedValueOnce({ version: "v1", headers: {} }); |
| 74 | + const client = { createStream: spy } as unknown as ApiClient; |
| 75 | + const manager = new StandardRealtimeStreamsManager(client, "http://localhost"); |
| 76 | + |
| 77 | + await expect(getCached(manager, "run-1", "chat")).rejects.toThrow("boom"); |
| 78 | + |
| 79 | + const retried = await getCached(manager, "run-1", "chat"); |
| 80 | + |
| 81 | + expect(retried).toEqual({ version: "v1", headers: {} }); |
| 82 | + expect(spy).toHaveBeenCalledTimes(2); |
| 83 | + }); |
| 84 | + |
| 85 | + it("reset() clears cached entries so the next call re-PUTs", async () => { |
| 86 | + const { client, spy } = makeApiClient(async () => ({ version: "v1", headers: {} })); |
| 87 | + const manager = new StandardRealtimeStreamsManager(client, "http://localhost"); |
| 88 | + |
| 89 | + await getCached(manager, "run-1", "chat"); |
| 90 | + expect(spy).toHaveBeenCalledTimes(1); |
| 91 | + |
| 92 | + manager.reset(); |
| 93 | + |
| 94 | + await getCached(manager, "run-1", "chat"); |
| 95 | + expect(spy).toHaveBeenCalledTimes(2); |
| 96 | + }); |
| 97 | + |
| 98 | + it("evictCreateStreamIfStale clears the matching entry so the next call re-PUTs", async () => { |
| 99 | + const { client, spy } = makeApiClient(async () => ({ version: "v1", headers: {} })); |
| 100 | + const manager = new StandardRealtimeStreamsManager(client, "http://localhost"); |
| 101 | + |
| 102 | + // Prime the cache and capture which promise was stored. |
| 103 | + const cachedPromise = getCached(manager, "run-1", "chat"); |
| 104 | + await cachedPromise; |
| 105 | + expect(spy).toHaveBeenCalledTimes(1); |
| 106 | + |
| 107 | + // Simulate the reactive invalidation path that `pipe()` runs when a |
| 108 | + // writer's `wait()` rejects. |
| 109 | + ( |
| 110 | + manager as unknown as { |
| 111 | + evictCreateStreamIfStale: ( |
| 112 | + runId: string, |
| 113 | + key: string, |
| 114 | + expected: Promise<unknown> |
| 115 | + ) => void; |
| 116 | + } |
| 117 | + ).evictCreateStreamIfStale("run-1", "chat", cachedPromise); |
| 118 | + |
| 119 | + await getCached(manager, "run-1", "chat"); |
| 120 | + expect(spy).toHaveBeenCalledTimes(2); |
| 121 | + }); |
| 122 | + |
| 123 | + it("evictCreateStreamIfStale is a no-op when the cache holds a different promise", async () => { |
| 124 | + const { client, spy } = makeApiClient(async () => ({ version: "v1", headers: {} })); |
| 125 | + const manager = new StandardRealtimeStreamsManager(client, "http://localhost"); |
| 126 | + |
| 127 | + const original = getCached(manager, "run-1", "chat"); |
| 128 | + await original; |
| 129 | + |
| 130 | + // A different promise (e.g. from a concurrent caller that already |
| 131 | + // refreshed) shouldn't trigger eviction. |
| 132 | + const stalePromise = Promise.resolve({ version: "v1", headers: {} }); |
| 133 | + ( |
| 134 | + manager as unknown as { |
| 135 | + evictCreateStreamIfStale: ( |
| 136 | + runId: string, |
| 137 | + key: string, |
| 138 | + expected: Promise<unknown> |
| 139 | + ) => void; |
| 140 | + } |
| 141 | + ).evictCreateStreamIfStale("run-1", "chat", stalePromise); |
| 142 | + |
| 143 | + // Cache should still hold the original entry; next call is a hit. |
| 144 | + await getCached(manager, "run-1", "chat"); |
| 145 | + expect(spy).toHaveBeenCalledTimes(1); |
| 146 | + }); |
| 147 | +}); |
0 commit comments