diff --git a/src/auto-reply/reply/dispatch-acp-delivery.test.ts b/src/auto-reply/reply/dispatch-acp-delivery.test.ts index 796e5526047..d10f0a2ed21 100644 --- a/src/auto-reply/reply/dispatch-acp-delivery.test.ts +++ b/src/auto-reply/reply/dispatch-acp-delivery.test.ts @@ -87,6 +87,26 @@ function createCoordinator(onReplyStart?: (...args: unknown[]) => Promise) }); } +async function raceWithTimeoutResult( + promise: Promise, + timeoutMs: number, + timeoutResult: T, +): Promise { + let timer: ReturnType | undefined; + try { + return await Promise.race([ + promise, + new Promise((resolve) => { + timer = setTimeout(() => resolve(timeoutResult), timeoutMs); + }), + ]); + } finally { + if (timer) { + clearTimeout(timer); + } + } +} + function createVisibleChatAcpCoordinator(cfg: OpenClawConfig) { return createAcpDispatchDeliveryCoordinator({ cfg, @@ -356,12 +376,11 @@ describe("createAcpDispatchDeliveryCoordinator", () => { ); const coordinator = createCoordinator(onReplyStart); - const delivered = await Promise.race([ + const delivered = await raceWithTimeoutResult( coordinator.deliver("final", { text: "hello" }).then(() => "delivered"), - new Promise((resolve) => { - setTimeout(() => resolve("timed-out"), 50); - }), - ]); + 50, + "timed-out", + ); expect(delivered).toBe("delivered"); expect(onReplyStart).toHaveBeenCalledTimes(1);