|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { buildOpenAIRealtimeTranscriptionProvider } from "./realtime-transcription-provider.js"; |
| 3 | + |
| 4 | +const { FakeWebSocket } = vi.hoisted(() => { |
| 5 | + type Listener = (...args: unknown[]) => void; |
| 6 | + |
| 7 | + class MockWebSocket { |
| 8 | + static readonly OPEN = 1; |
| 9 | + static readonly CLOSED = 3; |
| 10 | + static instances: MockWebSocket[] = []; |
| 11 | + |
| 12 | + readonly listeners = new Map<string, Listener[]>(); |
| 13 | + readyState = 0; |
| 14 | + closed = false; |
| 15 | + |
| 16 | + constructor() { |
| 17 | + MockWebSocket.instances.push(this); |
| 18 | + } |
| 19 | + |
| 20 | + on(event: string, listener: Listener): this { |
| 21 | + const listeners = this.listeners.get(event) ?? []; |
| 22 | + listeners.push(listener); |
| 23 | + this.listeners.set(event, listeners); |
| 24 | + return this; |
| 25 | + } |
| 26 | + |
| 27 | + emit(event: string, ...args: unknown[]): void { |
| 28 | + for (const listener of this.listeners.get(event) ?? []) { |
| 29 | + listener(...args); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + send(): void {} |
| 34 | + |
| 35 | + close(code?: number, reason?: string): void { |
| 36 | + this.closed = true; |
| 37 | + this.readyState = MockWebSocket.CLOSED; |
| 38 | + this.emit("close", code ?? 1000, Buffer.from(reason ?? "")); |
| 39 | + } |
| 40 | + |
| 41 | + terminate(): void { |
| 42 | + this.closed = true; |
| 43 | + this.readyState = MockWebSocket.CLOSED; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return { FakeWebSocket: MockWebSocket }; |
| 48 | +}); |
| 49 | + |
| 50 | +vi.mock("ws", () => ({ |
| 51 | + default: FakeWebSocket, |
| 52 | +})); |
| 53 | + |
| 54 | +type FakeWebSocketInstance = InstanceType<typeof FakeWebSocket>; |
| 55 | + |
| 56 | +async function waitForFakeSocket(index = 0): Promise<FakeWebSocketInstance> { |
| 57 | + let socket: FakeWebSocketInstance | undefined; |
| 58 | + await vi.waitFor(() => { |
| 59 | + socket = FakeWebSocket.instances[index]; |
| 60 | + if (!socket) { |
| 61 | + throw new Error("expected session to create a websocket"); |
| 62 | + } |
| 63 | + }); |
| 64 | + if (!socket) { |
| 65 | + throw new Error("expected session to create a websocket"); |
| 66 | + } |
| 67 | + return socket; |
| 68 | +} |
| 69 | + |
| 70 | +function emitJson(socket: FakeWebSocketInstance, event: Record<string, unknown>): void { |
| 71 | + socket.emit("message", Buffer.from(JSON.stringify(event))); |
| 72 | +} |
| 73 | + |
| 74 | +async function connectFakeSession( |
| 75 | + session: { connect(): Promise<void> }, |
| 76 | + socketIndex = 0, |
| 77 | +): Promise<FakeWebSocketInstance> { |
| 78 | + const connecting = session.connect(); |
| 79 | + const socket = await waitForFakeSocket(socketIndex); |
| 80 | + socket.readyState = FakeWebSocket.OPEN; |
| 81 | + socket.emit("open"); |
| 82 | + emitJson(socket, { type: "session.updated" }); |
| 83 | + await connecting; |
| 84 | + return socket; |
| 85 | +} |
| 86 | + |
| 87 | +describe("OpenAI realtime transcription terminal history bounds", () => { |
| 88 | + beforeEach(() => { |
| 89 | + FakeWebSocket.instances = []; |
| 90 | + vi.stubEnv("OPENAI_API_KEY", ""); |
| 91 | + }); |
| 92 | + |
| 93 | + afterEach(() => { |
| 94 | + vi.unstubAllEnvs(); |
| 95 | + }); |
| 96 | + |
| 97 | + it("does not re-admit a terminal item after the settled frontier fills", async () => { |
| 98 | + const onError = vi.fn(); |
| 99 | + const onPartial = vi.fn(); |
| 100 | + const transcripts: string[] = []; |
| 101 | + const provider = buildOpenAIRealtimeTranscriptionProvider(); |
| 102 | + const session = provider.createSession({ |
| 103 | + providerConfig: { apiKey: "sk-test" }, // pragma: allowlist secret |
| 104 | + onError, |
| 105 | + onPartial, |
| 106 | + onTranscript: (transcript) => transcripts.push(transcript), |
| 107 | + }); |
| 108 | + const socket = await connectFakeSession(session); |
| 109 | + |
| 110 | + emitJson(socket, { |
| 111 | + type: "conversation.item.input_audio_transcription.completed", |
| 112 | + item_id: "oldest", |
| 113 | + transcript: "first", |
| 114 | + }); |
| 115 | + for (let index = 0; index < 4095; index += 1) { |
| 116 | + emitJson(socket, { |
| 117 | + type: "conversation.item.input_audio_transcription.completed", |
| 118 | + item_id: `settled-${index}`, |
| 119 | + transcript: "", |
| 120 | + }); |
| 121 | + } |
| 122 | + emitJson(socket, { |
| 123 | + type: "conversation.item.input_audio_transcription.completed", |
| 124 | + item_id: "oldest", |
| 125 | + transcript: "duplicate", |
| 126 | + }); |
| 127 | + emitJson(socket, { |
| 128 | + type: "conversation.item.input_audio_transcription.delta", |
| 129 | + item_id: "oldest", |
| 130 | + delta: "late partial", |
| 131 | + }); |
| 132 | + |
| 133 | + expect(transcripts).toEqual(["first"]); |
| 134 | + expect(onPartial).not.toHaveBeenCalled(); |
| 135 | + expect(onError).not.toHaveBeenCalled(); |
| 136 | + expect(session.isConnected()).toBe(true); |
| 137 | + session.close(); |
| 138 | + }); |
| 139 | + |
| 140 | + it("fails visibly instead of evicting terminal item history", async () => { |
| 141 | + const onError = vi.fn(); |
| 142 | + const onTranscript = vi.fn(); |
| 143 | + const provider = buildOpenAIRealtimeTranscriptionProvider(); |
| 144 | + const session = provider.createSession({ |
| 145 | + providerConfig: { apiKey: "sk-test" }, // pragma: allowlist secret |
| 146 | + onError, |
| 147 | + onTranscript, |
| 148 | + }); |
| 149 | + const socket = await connectFakeSession(session); |
| 150 | + |
| 151 | + for (let index = 0; index < 4096; index += 1) { |
| 152 | + emitJson(socket, { |
| 153 | + type: "conversation.item.input_audio_transcription.completed", |
| 154 | + item_id: `settled-${index}`, |
| 155 | + transcript: "", |
| 156 | + }); |
| 157 | + } |
| 158 | + emitJson(socket, { |
| 159 | + type: "input_audio_buffer.committed", |
| 160 | + item_id: "overflow-item", |
| 161 | + previous_item_id: null, |
| 162 | + }); |
| 163 | + emitJson(socket, { |
| 164 | + type: "conversation.item.input_audio_transcription.failed", |
| 165 | + item_id: "overflow-item", |
| 166 | + error: { message: "provider failure" }, |
| 167 | + }); |
| 168 | + |
| 169 | + expect(onError).toHaveBeenCalledExactlyOnceWith( |
| 170 | + expect.objectContaining({ |
| 171 | + message: "OpenAI realtime transcription exceeded the terminal item history limit", |
| 172 | + }), |
| 173 | + ); |
| 174 | + expect(onTranscript).not.toHaveBeenCalled(); |
| 175 | + expect(session.isConnected()).toBe(false); |
| 176 | + |
| 177 | + const reconnecting = session.connect(); |
| 178 | + const replacementSocket = await waitForFakeSocket(1); |
| 179 | + replacementSocket.readyState = FakeWebSocket.OPEN; |
| 180 | + replacementSocket.emit("open"); |
| 181 | + emitJson(replacementSocket, { type: "session.updated" }); |
| 182 | + await reconnecting; |
| 183 | + emitJson(replacementSocket, { |
| 184 | + type: "conversation.item.input_audio_transcription.completed", |
| 185 | + item_id: "replacement-item", |
| 186 | + transcript: "replacement transcript", |
| 187 | + }); |
| 188 | + |
| 189 | + expect(onTranscript).toHaveBeenCalledExactlyOnceWith("replacement transcript"); |
| 190 | + expect(onError).toHaveBeenCalledTimes(1); |
| 191 | + session.close(); |
| 192 | + }); |
| 193 | + |
| 194 | + it("fails visibly when terminal item identities exceed 256 KiB", async () => { |
| 195 | + const onError = vi.fn(); |
| 196 | + const onTranscript = vi.fn(); |
| 197 | + const provider = buildOpenAIRealtimeTranscriptionProvider(); |
| 198 | + const session = provider.createSession({ |
| 199 | + providerConfig: { apiKey: "sk-test" }, // pragma: allowlist secret |
| 200 | + onError, |
| 201 | + onTranscript, |
| 202 | + }); |
| 203 | + const socket = await connectFakeSession(session); |
| 204 | + |
| 205 | + for (let index = 0; index < 256; index += 1) { |
| 206 | + emitJson(socket, { |
| 207 | + type: "conversation.item.input_audio_transcription.completed", |
| 208 | + item_id: `${index.toString().padStart(4, "0")}${"i".repeat(1020)}`, |
| 209 | + transcript: "", |
| 210 | + }); |
| 211 | + } |
| 212 | + emitJson(socket, { |
| 213 | + type: "conversation.item.input_audio_transcription.completed", |
| 214 | + item_id: "overflow-item", |
| 215 | + transcript: "must not emit", |
| 216 | + }); |
| 217 | + |
| 218 | + expect(onError).toHaveBeenCalledExactlyOnceWith( |
| 219 | + expect.objectContaining({ |
| 220 | + message: "OpenAI realtime transcription exceeded the terminal item history limit", |
| 221 | + }), |
| 222 | + ); |
| 223 | + expect(onTranscript).not.toHaveBeenCalled(); |
| 224 | + expect(session.isConnected()).toBe(false); |
| 225 | + session.close(); |
| 226 | + }); |
| 227 | +}); |
0 commit comments