From 2f415618b39c00d77fc320ff181f4dc960b63b03 Mon Sep 17 00:00:00 2001 From: Shakker Date: Sat, 9 May 2026 01:16:10 +0100 Subject: [PATCH] test: clear openai ws fallback timeout --- src/agents/openai-ws-stream.test.ts | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) 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" }], });