diff --git a/src/agents/openai-ws-stream.test.ts b/src/agents/openai-ws-stream.test.ts index 185ce90fce9..c22e8cdd260 100644 --- a/src/agents/openai-ws-stream.test.ts +++ b/src/agents/openai-ws-stream.test.ts @@ -39,6 +39,22 @@ function countMatching(items: readonly T[], predicate: (item: T) => boolean) return count; } +async function withTimeout(promise: Promise, timeoutMs: number, message: string): Promise { + let timer: ReturnType | undefined; + try { + return await Promise.race([ + promise, + new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error(message)), timeoutMs); + }), + ]); + } finally { + if (timer) { + clearTimeout(timer); + } + } +} + // ───────────────────────────────────────────────────────────────────────────── // Mock OpenAIWebSocketManager // ───────────────────────────────────────────────────────────────────────────── @@ -2722,16 +2738,15 @@ describe("createOpenAIWebSocketStreamFn", () => { ); await expect( - Promise.race([ + withTimeout( ( stream as unknown as { result: () => Promise<{ content?: Array<{ text?: string }> }>; } ).result(), - new Promise((_, reject) => - setTimeout(() => reject(new Error("SSE fallback result timed out")), 100), - ), - ]), + 100, + "SSE fallback result timed out", + ), ).resolves.toMatchObject({ content: [{ text: "http fallback response" }], });