diff --git a/ui/src/api/voiceSessions.test.ts b/ui/src/api/voiceSessions.test.ts new file mode 100644 index 00000000000..dd879339547 --- /dev/null +++ b/ui/src/api/voiceSessions.test.ts @@ -0,0 +1,18 @@ +import { afterEach, expect, it, vi } from "vitest"; +import { voiceSessionsApi } from "./voiceSessions"; + +afterEach(() => { vi.restoreAllMocks(); vi.unstubAllGlobals(); }); +it("aborts a stuck notification fetch so the next poll can retrieve the saved answer", async () => { + const deadline = new AbortController(); + const timeout = vi.spyOn(AbortSignal, "timeout").mockReturnValue(deadline.signal); + const fetch = vi.fn().mockImplementationOnce((_url: string, init: RequestInit) => new Promise((_resolve, reject) => { + init.signal!.addEventListener("abort", () => reject(new DOMException("Aborted", "AbortError")), { once: true }); + })).mockResolvedValueOnce(new Response(JSON.stringify({sessionId: "session", generation: 1, publicationId: "saved-answer", attempt: 0}), {headers: {"content-type": "application/json"}})); + vi.stubGlobal("fetch", fetch); + const stalled = voiceSessionsApi.notification("company", "session"); + const rejected = expect(stalled).rejects.toMatchObject({name: "AbortError"}); + expect(timeout).toHaveBeenCalledWith(10_000); deadline.abort(); await rejected; + timeout.mockReturnValue(new AbortController().signal); + await expect(voiceSessionsApi.notification("company", "session")).resolves.toMatchObject({publicationId: "saved-answer"}); + expect(fetch).toHaveBeenCalledTimes(2); +}); diff --git a/ui/src/api/voiceSessions.ts b/ui/src/api/voiceSessions.ts index fda39017ac9..ce153e802b3 100644 --- a/ui/src/api/voiceSessions.ts +++ b/ui/src/api/voiceSessions.ts @@ -13,6 +13,6 @@ export const voiceSessionsApi = { return api.post(`${base(companyId)}/${encodeURIComponent(sessionId)}/end`, {}); }, notification(companyId: string, sessionId: string) { - return api.get(`${base(companyId)}/${encodeURIComponent(sessionId)}/notification`, { cache: "no-store" }); + return api.get(`${base(companyId)}/${encodeURIComponent(sessionId)}/notification`, { cache: "no-store", signal: AbortSignal.timeout(10_000) }); }, };