test(voice-call): share active call context helper

This commit is contained in:
Vincent Koc
2026-04-12 08:41:30 +01:00
parent f4697f7b0d
commit 52623e033f

View File

@@ -50,6 +50,21 @@ vi.mock("./twiml.js", () => ({
import { endCall, initiateCall, speak } from "./outbound.js";
function createActiveCallContext(params: { hangupCall?: ReturnType<typeof vi.fn> } = {}) {
const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
const hangupCall = params.hangupCall ?? vi.fn(async () => {});
const ctx = {
activeCalls: new Map([["call-1", call]]),
providerCallIdMap: new Map([["provider-1", "call-1"]]),
provider: { hangupCall },
storePath: "/tmp/voice-call.json",
transcriptWaiters: new Map(),
maxDurationTimers: new Map(),
};
return { call, ctx, hangupCall };
}
describe("voice-call outbound helpers", () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -212,16 +227,7 @@ describe("voice-call outbound helpers", () => {
});
it("ends connected calls, clears timers, and rejects pending transcripts", async () => {
const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
const hangupCall = vi.fn(async () => {});
const ctx = {
activeCalls: new Map([["call-1", call]]),
providerCallIdMap: new Map([["provider-1", "call-1"]]),
provider: { hangupCall },
storePath: "/tmp/voice-call.json",
transcriptWaiters: new Map(),
maxDurationTimers: new Map(),
};
const { call, ctx, hangupCall } = createActiveCallContext();
await expect(endCall(ctx as never, "call-1")).resolves.toEqual({ success: true });
expect(hangupCall).toHaveBeenCalledWith({
@@ -250,16 +256,7 @@ describe("voice-call outbound helpers", () => {
});
it("preserves timeout reasons when ending timed out calls", async () => {
const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
const hangupCall = vi.fn(async () => {});
const ctx = {
activeCalls: new Map([["call-1", call]]),
providerCallIdMap: new Map([["provider-1", "call-1"]]),
provider: { hangupCall },
storePath: "/tmp/voice-call.json",
transcriptWaiters: new Map(),
maxDurationTimers: new Map(),
};
const { call, ctx, hangupCall } = createActiveCallContext();
await expect(endCall(ctx as never, "call-1", { reason: "timeout" })).resolves.toEqual({
success: true,