























@@ -128,6 +128,13 @@ const startRealtimeServer = async (
128128});
129129};
130130131+async function waitForRealtimeTest(
132+callback: () => void | Promise<void>,
133+options: { timeout?: number; interval?: number } = {},
134+) {
135+await vi.waitFor(callback, { interval: 1, ...options });
136+}
137+131138describe("RealtimeCallHandler path routing", () => {
132139it("uses the request host and stream path in TwiML", () => {
133140const handler = makeHandler();
@@ -213,7 +220,7 @@ describe("RealtimeCallHandler path routing", () => {
213220start: { streamSid: "MZ-outbound", callSid: "CA-outbound" },
214221}),
215222);
216-await vi.waitFor(() => {
223+await waitForRealtimeTest(() => {
217224expect(createBridge).toHaveBeenCalled();
218225});
219226callbacks?.onReady?.();
@@ -280,7 +287,7 @@ describe("RealtimeCallHandler path routing", () => {
280287start: { streamSid: "MZ-silent", callSid: "CA-silent" },
281288}),
282289);
283-await vi.waitFor(() => {
290+await waitForRealtimeTest(() => {
284291expect(createBridge).toHaveBeenCalled();
285292});
286293@@ -332,7 +339,7 @@ describe("RealtimeCallHandler path routing", () => {
332339start: { streamSid: "MZ-speak", callSid: "CA-speak" },
333340}),
334341);
335-await vi.waitFor(() => {
342+await waitForRealtimeTest(() => {
336343expect(createBridge).toHaveBeenCalled();
337344});
338345@@ -400,13 +407,13 @@ describe("RealtimeCallHandler path routing", () => {
400407start: { streamSid: "MZ-complete", callSid: "CA-complete" },
401408}),
402409);
403-await vi.waitFor(() => {
410+await waitForRealtimeTest(() => {
404411expect(createBridge).toHaveBeenCalled();
405412});
406413407414ws.send(JSON.stringify({ event: "stop" }));
408415409-await vi.waitFor(() => {
416+await waitForRealtimeTest(() => {
410417const events = processEvent.mock.calls.map(([event]) => event as NormalizedEvent);
411418const ended = events.find((event) => event.type === "call.ended");
412419expect(ended).toBeDefined();
@@ -477,7 +484,7 @@ describe("RealtimeCallHandler path routing", () => {
477484start: { streamSid: "MZ-talk-events", callSid: "CA-talk-events" },
478485}),
479486);
480-await vi.waitFor(() => {
487+await waitForRealtimeTest(() => {
481488expect(createBridge).toHaveBeenCalled();
482489});
483490@@ -488,7 +495,7 @@ describe("RealtimeCallHandler path routing", () => {
488495media: { payload: Buffer.from([0xff, 0xff]).toString("base64") },
489496}),
490497);
491-await vi.waitFor(() => {
498+await waitForRealtimeTest(() => {
492499expect(sendAudio).toHaveBeenCalledWith(Buffer.from([0xff, 0xff]));
493500});
494501callbacks?.onTranscript?.("user", "hello", true);
@@ -575,7 +582,7 @@ describe("RealtimeCallHandler path routing", () => {
575582start: { streamSid: "MZ-barge-in", callSid: "CA-barge-in" },
576583}),
577584);
578-await vi.waitFor(() => {
585+await waitForRealtimeTest(() => {
579586expect(createBridge).toHaveBeenCalled();
580587});
581588@@ -584,7 +591,7 @@ describe("RealtimeCallHandler path routing", () => {
584591ws.send(JSON.stringify({ event: "media", media: { payload: speechPayload } }));
585592ws.send(JSON.stringify({ event: "media", media: { payload: speechPayload } }));
586593587-await vi.waitFor(() => {
594+await waitForRealtimeTest(() => {
588595expect(sendAudio).toHaveBeenCalledTimes(2);
589596});
590597@@ -676,7 +683,7 @@ describe("RealtimeCallHandler path routing", () => {
676683start: { streamSid: "MZ-tool", callSid: "CA-tool" },
677684}),
678685);
679-await vi.waitFor(() => {
686+await waitForRealtimeTest(() => {
680687expect(createBridge).toHaveBeenCalled();
681688});
682689@@ -689,11 +696,11 @@ describe("RealtimeCallHandler path routing", () => {
689696args: { question: "Are the basement lights on?" },
690697});
691698await vi.advanceTimersByTimeAsync(350);
692-await vi.waitFor(() => {
699+await waitForRealtimeTest(() => {
693700expect(receivedPartialTranscript).toBe("Are the basement");
694701});
695702696-await vi.waitFor(() => {
703+await waitForRealtimeTest(() => {
697704const workingCall = submitToolResult.mock.calls.find(
698705([callId]) => callId === "consult-call",
699706);
@@ -708,7 +715,7 @@ describe("RealtimeCallHandler path routing", () => {
708715709716resolveConsult?.({ text: "The basement lights are on." });
710717711-await vi.waitFor(() => {
718+await waitForRealtimeTest(() => {
712719expect(submitToolResult).toHaveBeenLastCalledWith(
713720"consult-call",
714721{
@@ -726,7 +733,7 @@ describe("RealtimeCallHandler path routing", () => {
726733args: {},
727734});
728735729-await vi.waitFor(() => {
736+await waitForRealtimeTest(() => {
730737expect(submitToolResult).toHaveBeenCalledWith("custom-call", { ok: true }, undefined);
731738});
732739expect(submitToolResult).not.toHaveBeenCalledWith("custom-call", expect.anything(), {
@@ -795,15 +802,15 @@ describe("RealtimeCallHandler path routing", () => {
795802start: { streamSid: "MZ-force", callSid: "CA-force" },
796803}),
797804);
798-await vi.waitFor(() => {
805+await waitForRealtimeTest(() => {
799806expect(createBridge).toHaveBeenCalled();
800807});
801808802809vi.useFakeTimers();
803810callbacks?.onTranscript?.("user", "Create a smoke test file for me.", true);
804811await vi.advanceTimersByTimeAsync(200);
805812806-await vi.waitFor(() => {
813+await waitForRealtimeTest(() => {
807814expect(consult).toHaveBeenCalledTimes(1);
808815});
809816const [args, callId, context] = consult.mock.calls[0] ?? [];
@@ -814,7 +821,7 @@ describe("RealtimeCallHandler path routing", () => {
814821});
815822expect(callId).toBe("call-1");
816823expect(context).toEqual({});
817-await vi.waitFor(() => {
824+await waitForRealtimeTest(() => {
818825expect(sendUserMessage).toHaveBeenCalledWith(
819826expect.stringContaining("I created the smoke test file."),
820827);
@@ -875,7 +882,7 @@ describe("RealtimeCallHandler path routing", () => {
875882start: { streamSid: "MZ-direct-turns", callSid: "CA-direct-turns" },
876883}),
877884);
878-await vi.waitFor(() => {
885+await waitForRealtimeTest(() => {
879886expect(createBridge).toHaveBeenCalled();
880887});
881888@@ -955,7 +962,7 @@ describe("RealtimeCallHandler path routing", () => {
955962start: { streamSid: "MZ-settle", callSid: "CA-settle" },
956963}),
957964);
958-await vi.waitFor(() => {
965+await waitForRealtimeTest(() => {
959966expect(createBridge).toHaveBeenCalled();
960967});
961968@@ -971,7 +978,7 @@ describe("RealtimeCallHandler path routing", () => {
971978callbacks?.onTranscript?.("user", "message.", false);
972979await vi.advanceTimersByTimeAsync(350);
973980974-await vi.waitFor(
981+await waitForRealtimeTest(
975982() => {
976983expect(consult).toHaveBeenCalledTimes(1);
977984},
@@ -985,7 +992,7 @@ describe("RealtimeCallHandler path routing", () => {
985992);
986993expect(callId).toBe("call-1");
987994expect(context).toEqual({ partialUserTranscript: "Send a Discord message." });
988-await vi.waitFor(() => {
995+await waitForRealtimeTest(() => {
989996expect(submitToolResult).toHaveBeenLastCalledWith(
990997"consult-call",
991998{ text: "I sent it." },
@@ -1057,7 +1064,7 @@ describe("RealtimeCallHandler path routing", () => {
10571064start: { streamSid: "MZ-native", callSid: "CA-native" },
10581065}),
10591066);
1060-await vi.waitFor(() => {
1067+await waitForRealtimeTest(() => {
10611068expect(createBridge).toHaveBeenCalled();
10621069});
10631070@@ -1070,7 +1077,7 @@ describe("RealtimeCallHandler path routing", () => {
10701077args: { question: "Send me a Discord message." },
10711078});
107210791073-await vi.waitFor(() => {
1080+await waitForRealtimeTest(() => {
10741081expect(submitToolResult).toHaveBeenLastCalledWith(
10751082"consult-call",
10761083{ text: "Native consult result." },
@@ -1150,7 +1157,7 @@ describe("RealtimeCallHandler path routing", () => {
11501157start: { streamSid: "MZ-fast", callSid: "CA-fast" },
11511158}),
11521159);
1153-await vi.waitFor(() => {
1160+await waitForRealtimeTest(() => {
11541161expect(createBridge).toHaveBeenCalled();
11551162});
11561163@@ -1161,7 +1168,7 @@ describe("RealtimeCallHandler path routing", () => {
11611168args: { question: "What do you remember?" },
11621169});
116311701164-await vi.waitFor(() => {
1171+await waitForRealtimeTest(() => {
11651172expect(submitToolResult).toHaveBeenCalledWith(
11661173"consult-call",
11671174{ text: "Fast context." },
@@ -1220,7 +1227,7 @@ describe("RealtimeCallHandler websocket hardening", () => {
12201227start: { streamSid: "MZ-backpressure", callSid: "CA-backpressure" },
12211228}),
12221229);
1223-await vi.waitFor(() => {
1230+await waitForRealtimeTest(() => {
12241231if (!sendProviderAudio) {
12251232throw new Error("expected realtime provider audio sender");
12261233}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。