fix: recover live voice polling after stalled requests

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-09 16:13:16 -05:00
1 parent 845e622b06
commit 38057142bb
2 files changed
+19 -1

No files matched your search

+18
View File
@@ -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);
});
+1 -1
View File
@@ -13,6 +13,6 @@ export const voiceSessionsApi = {
return api.post<VoiceSession>(`${base(companyId)}/${encodeURIComponent(sessionId)}/end`, {});
},
notification(companyId: string, sessionId: string) {
return api.get<VoiceSessionNotification | null>(`${base(companyId)}/${encodeURIComponent(sessionId)}/notification`, { cache: "no-store" });
return api.get<VoiceSessionNotification | null>(`${base(companyId)}/${encodeURIComponent(sessionId)}/notification`, { cache: "no-store", signal: AbortSignal.timeout(10_000) });
},
};