@openclaw/voice-call 2026.5.2 → 2026.5.3-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/dist/api.js +2 -0
  2. package/dist/call-status-CXldV5o8.js +32 -0
  3. package/dist/cli-metadata.js +12 -0
  4. package/dist/config-7w04YpHh.js +548 -0
  5. package/dist/config-compat-B0me39_4.js +129 -0
  6. package/dist/guarded-json-api-Btx5EE4w.js +591 -0
  7. package/dist/http-headers-BrnxBasF.js +10 -0
  8. package/dist/index.js +1284 -0
  9. package/dist/mock-CeKvfVEd.js +135 -0
  10. package/dist/plivo-B-a7KFoT.js +393 -0
  11. package/dist/realtime-handler-B63CIDP2.js +325 -0
  12. package/dist/realtime-transcription.runtime-B2h70y2W.js +2 -0
  13. package/dist/realtime-voice.runtime-Bkh4nvLn.js +2 -0
  14. package/dist/response-generator-BrcmwDZU.js +182 -0
  15. package/dist/response-model-CyF5K80p.js +12 -0
  16. package/dist/runtime-api.js +6 -0
  17. package/dist/runtime-entry-88ytYAQa.js +3119 -0
  18. package/dist/runtime-entry.js +2 -0
  19. package/dist/setup-api.js +37 -0
  20. package/dist/telnyx-jjBE8boz.js +260 -0
  21. package/dist/twilio-1OqbcXLL.js +676 -0
  22. package/dist/voice-mapping-BYDGdWGx.js +40 -0
  23. package/package.json +14 -6
  24. package/api.ts +0 -16
  25. package/cli-metadata.ts +0 -10
  26. package/config-api.ts +0 -12
  27. package/index.test.ts +0 -943
  28. package/index.ts +0 -794
  29. package/runtime-api.ts +0 -20
  30. package/runtime-entry.ts +0 -1
  31. package/setup-api.ts +0 -47
  32. package/src/allowlist.test.ts +0 -18
  33. package/src/allowlist.ts +0 -19
  34. package/src/cli.ts +0 -845
  35. package/src/config-compat.test.ts +0 -120
  36. package/src/config-compat.ts +0 -227
  37. package/src/config.test.ts +0 -479
  38. package/src/config.ts +0 -808
  39. package/src/core-bridge.ts +0 -14
  40. package/src/deep-merge.test.ts +0 -40
  41. package/src/deep-merge.ts +0 -23
  42. package/src/gateway-continue-operation.ts +0 -200
  43. package/src/http-headers.test.ts +0 -16
  44. package/src/http-headers.ts +0 -15
  45. package/src/manager/context.ts +0 -42
  46. package/src/manager/events.test.ts +0 -581
  47. package/src/manager/events.ts +0 -288
  48. package/src/manager/lifecycle.ts +0 -53
  49. package/src/manager/lookup.test.ts +0 -52
  50. package/src/manager/lookup.ts +0 -35
  51. package/src/manager/outbound.test.ts +0 -528
  52. package/src/manager/outbound.ts +0 -486
  53. package/src/manager/state.ts +0 -48
  54. package/src/manager/store.ts +0 -106
  55. package/src/manager/timers.test.ts +0 -129
  56. package/src/manager/timers.ts +0 -113
  57. package/src/manager/twiml.test.ts +0 -13
  58. package/src/manager/twiml.ts +0 -17
  59. package/src/manager.closed-loop.test.ts +0 -236
  60. package/src/manager.inbound-allowlist.test.ts +0 -188
  61. package/src/manager.notify.test.ts +0 -377
  62. package/src/manager.restore.test.ts +0 -183
  63. package/src/manager.test-harness.ts +0 -127
  64. package/src/manager.ts +0 -392
  65. package/src/media-stream.test.ts +0 -768
  66. package/src/media-stream.ts +0 -708
  67. package/src/providers/base.ts +0 -97
  68. package/src/providers/mock.test.ts +0 -78
  69. package/src/providers/mock.ts +0 -185
  70. package/src/providers/plivo.test.ts +0 -93
  71. package/src/providers/plivo.ts +0 -601
  72. package/src/providers/shared/call-status.test.ts +0 -24
  73. package/src/providers/shared/call-status.ts +0 -24
  74. package/src/providers/shared/guarded-json-api.test.ts +0 -106
  75. package/src/providers/shared/guarded-json-api.ts +0 -42
  76. package/src/providers/telnyx.test.ts +0 -340
  77. package/src/providers/telnyx.ts +0 -394
  78. package/src/providers/twilio/api.test.ts +0 -145
  79. package/src/providers/twilio/api.ts +0 -93
  80. package/src/providers/twilio/twiml-policy.test.ts +0 -84
  81. package/src/providers/twilio/twiml-policy.ts +0 -87
  82. package/src/providers/twilio/webhook.ts +0 -34
  83. package/src/providers/twilio.test.ts +0 -591
  84. package/src/providers/twilio.ts +0 -861
  85. package/src/providers/twilio.types.ts +0 -17
  86. package/src/realtime-defaults.ts +0 -3
  87. package/src/realtime-fast-context.test.ts +0 -88
  88. package/src/realtime-fast-context.ts +0 -165
  89. package/src/realtime-transcription.runtime.ts +0 -4
  90. package/src/realtime-voice.runtime.ts +0 -5
  91. package/src/response-generator.test.ts +0 -321
  92. package/src/response-generator.ts +0 -318
  93. package/src/response-model.test.ts +0 -71
  94. package/src/response-model.ts +0 -23
  95. package/src/runtime.test.ts +0 -536
  96. package/src/runtime.ts +0 -510
  97. package/src/telephony-audio.test.ts +0 -61
  98. package/src/telephony-audio.ts +0 -12
  99. package/src/telephony-tts.test.ts +0 -196
  100. package/src/telephony-tts.ts +0 -235
  101. package/src/test-fixtures.ts +0 -73
  102. package/src/tts-provider-voice.test.ts +0 -34
  103. package/src/tts-provider-voice.ts +0 -21
  104. package/src/tunnel.test.ts +0 -166
  105. package/src/tunnel.ts +0 -314
  106. package/src/types.ts +0 -291
  107. package/src/utils.test.ts +0 -17
  108. package/src/utils.ts +0 -14
  109. package/src/voice-mapping.test.ts +0 -34
  110. package/src/voice-mapping.ts +0 -68
  111. package/src/webhook/realtime-handler.test.ts +0 -598
  112. package/src/webhook/realtime-handler.ts +0 -485
  113. package/src/webhook/stale-call-reaper.test.ts +0 -88
  114. package/src/webhook/stale-call-reaper.ts +0 -38
  115. package/src/webhook/tailscale.test.ts +0 -214
  116. package/src/webhook/tailscale.ts +0 -129
  117. package/src/webhook-exposure.test.ts +0 -33
  118. package/src/webhook-exposure.ts +0 -84
  119. package/src/webhook-security.test.ts +0 -770
  120. package/src/webhook-security.ts +0 -994
  121. package/src/webhook.hangup-once.lifecycle.test.ts +0 -135
  122. package/src/webhook.test.ts +0 -1470
  123. package/src/webhook.ts +0 -908
  124. package/src/webhook.types.ts +0 -5
  125. package/src/websocket-test-support.ts +0 -72
  126. package/tsconfig.json +0 -16
@@ -1,528 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
2
-
3
- const {
4
- addTranscriptEntryMock,
5
- clearMaxDurationTimerMock,
6
- generateDtmfRedirectTwimlMock,
7
- generateNotifyTwimlMock,
8
- getCallByProviderCallIdMock,
9
- mapVoiceToPollyMock,
10
- persistCallRecordMock,
11
- rejectTranscriptWaiterMock,
12
- transitionStateMock,
13
- } = vi.hoisted(() => ({
14
- addTranscriptEntryMock: vi.fn(),
15
- clearMaxDurationTimerMock: vi.fn(),
16
- generateDtmfRedirectTwimlMock: vi.fn(),
17
- generateNotifyTwimlMock: vi.fn(),
18
- getCallByProviderCallIdMock: vi.fn(),
19
- mapVoiceToPollyMock: vi.fn(),
20
- persistCallRecordMock: vi.fn(),
21
- rejectTranscriptWaiterMock: vi.fn(),
22
- transitionStateMock: vi.fn(),
23
- }));
24
-
25
- vi.mock("./state.js", () => ({
26
- addTranscriptEntry: addTranscriptEntryMock,
27
- transitionState: transitionStateMock,
28
- }));
29
-
30
- vi.mock("./store.js", () => ({
31
- persistCallRecord: persistCallRecordMock,
32
- }));
33
-
34
- vi.mock("./timers.js", () => ({
35
- clearMaxDurationTimer: clearMaxDurationTimerMock,
36
- clearTranscriptWaiter: vi.fn(),
37
- rejectTranscriptWaiter: rejectTranscriptWaiterMock,
38
- waitForFinalTranscript: vi.fn(),
39
- }));
40
-
41
- vi.mock("./lookup.js", () => ({
42
- getCallByProviderCallId: getCallByProviderCallIdMock,
43
- }));
44
-
45
- vi.mock("../voice-mapping.js", () => ({
46
- mapVoiceToPolly: mapVoiceToPollyMock,
47
- }));
48
-
49
- vi.mock("./twiml.js", () => ({
50
- generateDtmfRedirectTwiml: generateDtmfRedirectTwimlMock,
51
- generateNotifyTwiml: generateNotifyTwimlMock,
52
- }));
53
-
54
- import { endCall, initiateCall, sendDtmf, speak } from "./outbound.js";
55
-
56
- function createActiveCallContext(params: { hangupCall?: ReturnType<typeof vi.fn> } = {}) {
57
- const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
58
- const hangupCall = params.hangupCall ?? vi.fn(async () => {});
59
- const ctx = {
60
- activeCalls: new Map([["call-1", call]]),
61
- providerCallIdMap: new Map([["provider-1", "call-1"]]),
62
- provider: { hangupCall },
63
- storePath: "/tmp/voice-call.json",
64
- transcriptWaiters: new Map(),
65
- maxDurationTimers: new Map(),
66
- };
67
-
68
- return { call, ctx, hangupCall };
69
- }
70
-
71
- describe("voice-call outbound helpers", () => {
72
- beforeEach(() => {
73
- vi.clearAllMocks();
74
- mapVoiceToPollyMock.mockReturnValue("Polly.Joanna");
75
- generateDtmfRedirectTwimlMock.mockReturnValue("<DtmfRedirect />");
76
- generateNotifyTwimlMock.mockReturnValue("<Response />");
77
- });
78
-
79
- it("guards initiateCall when provider, webhook, capacity, or fromNumber are missing", async () => {
80
- const base = {
81
- activeCalls: new Map(),
82
- providerCallIdMap: new Map(),
83
- config: {
84
- maxConcurrentCalls: 1,
85
- outbound: { defaultMode: "conversation", notifyHangupDelaySec: 0 },
86
- },
87
- storePath: "/tmp/voice-call.json",
88
- webhookUrl: "https://example.com/webhook",
89
- };
90
-
91
- await expect(
92
- initiateCall({ ...base, provider: undefined } as never, "+14155550123"),
93
- ).resolves.toEqual({
94
- callId: "",
95
- success: false,
96
- error: "Provider not initialized",
97
- });
98
-
99
- await expect(
100
- initiateCall(
101
- { ...base, provider: { name: "twilio" }, webhookUrl: undefined } as never,
102
- "+14155550123",
103
- ),
104
- ).resolves.toEqual({
105
- callId: "",
106
- success: false,
107
- error: "Webhook URL not configured",
108
- });
109
-
110
- const saturated = {
111
- ...base,
112
- activeCalls: new Map([["existing", {}]]),
113
- provider: { name: "twilio" },
114
- };
115
- await expect(initiateCall(saturated as never, "+14155550123")).resolves.toEqual({
116
- callId: "",
117
- success: false,
118
- error: "Maximum concurrent calls (1) reached",
119
- });
120
-
121
- await expect(
122
- initiateCall(
123
- {
124
- ...base,
125
- provider: { name: "twilio" },
126
- config: { ...base.config, fromNumber: "" },
127
- } as never,
128
- "+14155550123",
129
- ),
130
- ).resolves.toEqual({
131
- callId: "",
132
- success: false,
133
- error: "fromNumber not configured",
134
- });
135
- });
136
-
137
- it("initiates notify-mode calls with inline TwiML and records provider ids", async () => {
138
- const initiateProviderCall = vi.fn(async () => ({ providerCallId: "provider-1" }));
139
- const ctx = {
140
- activeCalls: new Map(),
141
- providerCallIdMap: new Map(),
142
- provider: { name: "twilio", initiateCall: initiateProviderCall },
143
- config: {
144
- maxConcurrentCalls: 3,
145
- outbound: { defaultMode: "conversation" },
146
- fromNumber: "+14155550100",
147
- tts: { provider: "openai", providers: { openai: { voice: "nova" } } },
148
- },
149
- storePath: "/tmp/voice-call.json",
150
- webhookUrl: "https://example.com/webhook",
151
- };
152
-
153
- const result = await initiateCall(ctx as never, "+14155550123", "session-1", {
154
- mode: "notify",
155
- message: "hello there",
156
- });
157
- expect(result).toEqual({
158
- callId: expect.any(String),
159
- success: true,
160
- });
161
- const callId = result.callId;
162
-
163
- expect(mapVoiceToPollyMock).toHaveBeenCalledWith("nova");
164
- expect(generateNotifyTwimlMock).toHaveBeenCalledWith("hello there", "Polly.Joanna");
165
- expect(initiateProviderCall).toHaveBeenCalledWith({
166
- callId,
167
- from: "+14155550100",
168
- to: "+14155550123",
169
- webhookUrl: "https://example.com/webhook",
170
- inlineTwiml: "<Response />",
171
- });
172
- expect(ctx.providerCallIdMap.get("provider-1")).toBe(callId);
173
- expect(ctx.activeCalls.get(callId)?.sessionKey).toBe("session-1");
174
- expect(persistCallRecordMock).toHaveBeenCalledTimes(2);
175
- });
176
-
177
- it("assigns per-call session keys to outbound calls when configured", async () => {
178
- const initiateProviderCall = vi.fn(async () => ({ providerCallId: "provider-1" }));
179
- const ctx = {
180
- activeCalls: new Map(),
181
- providerCallIdMap: new Map(),
182
- provider: { name: "twilio", initiateCall: initiateProviderCall },
183
- config: {
184
- maxConcurrentCalls: 3,
185
- outbound: { defaultMode: "conversation" },
186
- fromNumber: "+14155550100",
187
- sessionScope: "per-call",
188
- },
189
- storePath: "/tmp/voice-call.json",
190
- webhookUrl: "https://example.com/webhook",
191
- };
192
-
193
- const result = await initiateCall(ctx as never, "+14155550123");
194
-
195
- expect(result).toEqual({
196
- callId: expect.any(String),
197
- success: true,
198
- });
199
- expect(ctx.activeCalls.get(result.callId)?.sessionKey).toBe(`voice:call:${result.callId}`);
200
- });
201
-
202
- it("initiates conversation calls with pre-connect DTMF TwiML", async () => {
203
- const initiateProviderCall = vi.fn(async () => ({ providerCallId: "provider-1" }));
204
- const ctx = {
205
- activeCalls: new Map(),
206
- providerCallIdMap: new Map(),
207
- provider: { name: "twilio", initiateCall: initiateProviderCall },
208
- config: {
209
- maxConcurrentCalls: 3,
210
- outbound: { defaultMode: "conversation" },
211
- fromNumber: "+14155550100",
212
- },
213
- storePath: "/tmp/voice-call.json",
214
- webhookUrl: "https://example.com/webhook",
215
- };
216
-
217
- const result = await initiateCall(ctx as never, "+14155550123", "session-1", {
218
- mode: "conversation",
219
- message: "hello meet",
220
- dtmfSequence: "ww123456#",
221
- });
222
-
223
- expect(result).toEqual({
224
- callId: expect.any(String),
225
- success: true,
226
- });
227
- const callId = result.callId;
228
-
229
- expect(generateDtmfRedirectTwimlMock).toHaveBeenCalledWith(
230
- "ww123456#",
231
- "https://example.com/webhook",
232
- );
233
- expect(initiateProviderCall).toHaveBeenCalledWith({
234
- callId,
235
- from: "+14155550100",
236
- to: "+14155550123",
237
- webhookUrl: "https://example.com/webhook",
238
- inlineTwiml: undefined,
239
- preConnectTwiml: "<DtmfRedirect />",
240
- });
241
- expect(ctx.activeCalls.get(callId)?.metadata).toMatchObject({
242
- initialMessage: "hello meet",
243
- mode: "conversation",
244
- });
245
- });
246
-
247
- it("rejects DTMF sequences outside conversation mode", async () => {
248
- const initiateProviderCall = vi.fn(async () => ({ providerCallId: "provider-1" }));
249
- const ctx = {
250
- activeCalls: new Map(),
251
- providerCallIdMap: new Map(),
252
- provider: { name: "twilio", initiateCall: initiateProviderCall },
253
- config: {
254
- maxConcurrentCalls: 3,
255
- outbound: { defaultMode: "notify" },
256
- fromNumber: "+14155550100",
257
- },
258
- storePath: "/tmp/voice-call.json",
259
- webhookUrl: "https://example.com/webhook",
260
- };
261
-
262
- await expect(
263
- initiateCall(ctx as never, "+14155550123", "session-1", {
264
- message: "hello",
265
- dtmfSequence: "123456#",
266
- }),
267
- ).resolves.toEqual({
268
- callId: "",
269
- success: false,
270
- error: "dtmfSequence requires conversation mode",
271
- });
272
-
273
- expect(initiateProviderCall).not.toHaveBeenCalled();
274
- expect(ctx.activeCalls.size).toBe(0);
275
- });
276
-
277
- it("fails initiateCall cleanly when provider initiation throws", async () => {
278
- const ctx = {
279
- activeCalls: new Map(),
280
- providerCallIdMap: new Map(),
281
- provider: {
282
- name: "mock",
283
- initiateCall: vi.fn(async () => {
284
- throw new Error("provider down");
285
- }),
286
- },
287
- config: {
288
- maxConcurrentCalls: 3,
289
- outbound: { defaultMode: "conversation" },
290
- },
291
- storePath: "/tmp/voice-call.json",
292
- webhookUrl: "https://example.com/webhook",
293
- };
294
-
295
- await expect(initiateCall(ctx as never, "+14155550123")).resolves.toEqual({
296
- callId: expect.any(String),
297
- success: false,
298
- error: "provider down",
299
- });
300
- expect(ctx.activeCalls.size).toBe(0);
301
- });
302
-
303
- it("speaks through connected calls and rolls back to listening on provider errors", async () => {
304
- const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
305
- const playTts = vi.fn(async () => {});
306
- const ctx = {
307
- activeCalls: new Map([["call-1", call]]),
308
- providerCallIdMap: new Map(),
309
- provider: { name: "twilio", playTts },
310
- config: { tts: { provider: "openai", providers: { openai: { voice: "alloy" } } } },
311
- storePath: "/tmp/voice-call.json",
312
- };
313
-
314
- await expect(speak(ctx as never, "call-1", "hello")).resolves.toEqual({ success: true });
315
- expect(transitionStateMock).toHaveBeenCalledWith(call, "speaking");
316
- expect(playTts).toHaveBeenCalledWith({
317
- callId: "call-1",
318
- providerCallId: "provider-1",
319
- text: "hello",
320
- voice: "alloy",
321
- });
322
- expect(addTranscriptEntryMock).toHaveBeenCalledWith(call, "bot", "hello");
323
-
324
- playTts.mockImplementationOnce(async () => {
325
- throw new Error("tts failed");
326
- });
327
- await expect(speak(ctx as never, "call-1", "hello again")).resolves.toEqual({
328
- success: false,
329
- error: "tts failed",
330
- });
331
- expect(transitionStateMock).toHaveBeenLastCalledWith(call, "listening");
332
- });
333
-
334
- it("passes configured voice ids through to Telnyx speak", async () => {
335
- const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
336
- const playTts = vi.fn(async () => {});
337
- const ctx = {
338
- activeCalls: new Map([["call-1", call]]),
339
- providerCallIdMap: new Map(),
340
- provider: { name: "telnyx", playTts },
341
- config: {
342
- tts: {
343
- provider: "telnyx",
344
- providers: {
345
- telnyx: {
346
- voiceId: "Telnyx.Qwen3TTS.12345678-1234-1234-1234-123456789abc",
347
- },
348
- },
349
- },
350
- },
351
- storePath: "/tmp/voice-call.json",
352
- };
353
-
354
- await expect(speak(ctx as never, "call-1", "hello")).resolves.toEqual({ success: true });
355
-
356
- expect(playTts).toHaveBeenCalledWith({
357
- callId: "call-1",
358
- providerCallId: "provider-1",
359
- text: "hello",
360
- voice: "Telnyx.Qwen3TTS.12345678-1234-1234-1234-123456789abc",
361
- });
362
- });
363
-
364
- it("uses per-number route TTS voice for routed inbound calls", async () => {
365
- const call = {
366
- callId: "call-1",
367
- providerCallId: "provider-1",
368
- state: "active",
369
- to: "+15550002222",
370
- metadata: { numberRouteKey: "+15550002222" },
371
- };
372
- const playTts = vi.fn(async () => {});
373
- const ctx = {
374
- activeCalls: new Map([["call-1", call]]),
375
- providerCallIdMap: new Map(),
376
- provider: { name: "twilio", playTts },
377
- config: {
378
- tts: { provider: "openai", providers: { openai: { voice: "coral" } } },
379
- numbers: {
380
- "+15550002222": {
381
- tts: {
382
- providers: {
383
- openai: { voice: "alloy" },
384
- },
385
- },
386
- },
387
- },
388
- },
389
- storePath: "/tmp/voice-call.json",
390
- };
391
-
392
- await expect(speak(ctx as never, "call-1", "hello")).resolves.toEqual({ success: true });
393
-
394
- expect(playTts).toHaveBeenCalledWith({
395
- callId: "call-1",
396
- providerCallId: "provider-1",
397
- text: "hello",
398
- voice: "alloy",
399
- });
400
- });
401
-
402
- it("sends DTMF through connected provider calls", async () => {
403
- const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
404
- const sendDtmfProvider = vi.fn(async () => {});
405
- const ctx = {
406
- activeCalls: new Map([["call-1", call]]),
407
- providerCallIdMap: new Map(),
408
- provider: { name: "twilio", sendDtmf: sendDtmfProvider },
409
- config: {},
410
- storePath: "/tmp/voice-call.json",
411
- };
412
-
413
- await expect(sendDtmf(ctx as never, "call-1", "ww123#")).resolves.toEqual({
414
- success: true,
415
- });
416
- expect(sendDtmfProvider).toHaveBeenCalledWith({
417
- callId: "call-1",
418
- providerCallId: "provider-1",
419
- digits: "ww123#",
420
- });
421
- });
422
-
423
- it("rejects invalid or unsupported outbound DTMF", async () => {
424
- const call = { callId: "call-1", providerCallId: "provider-1", state: "active" };
425
- const ctx = {
426
- activeCalls: new Map([["call-1", call]]),
427
- providerCallIdMap: new Map(),
428
- provider: { name: "telnyx" },
429
- config: {},
430
- storePath: "/tmp/voice-call.json",
431
- };
432
-
433
- await expect(sendDtmf(ctx as never, "call-1", "abc")).resolves.toEqual({
434
- success: false,
435
- error: "digits may only contain digits, *, #, comma, w, p",
436
- });
437
- await expect(sendDtmf(ctx as never, "call-1", "123#")).resolves.toEqual({
438
- success: false,
439
- error: "telnyx does not support outbound DTMF",
440
- });
441
- });
442
-
443
- it("ends connected calls, clears timers, and rejects pending transcripts", async () => {
444
- const { call, ctx, hangupCall } = createActiveCallContext();
445
-
446
- await expect(endCall(ctx as never, "call-1")).resolves.toEqual({ success: true });
447
- expect(hangupCall).toHaveBeenCalledWith({
448
- callId: "call-1",
449
- providerCallId: "provider-1",
450
- reason: "hangup-bot",
451
- });
452
- expect(call).toEqual(
453
- expect.objectContaining({
454
- endReason: "hangup-bot",
455
- endedAt: expect.any(Number),
456
- }),
457
- );
458
- expect(transitionStateMock).toHaveBeenCalledWith(call, "hangup-bot");
459
- expect(clearMaxDurationTimerMock).toHaveBeenCalledWith(
460
- { maxDurationTimers: ctx.maxDurationTimers },
461
- "call-1",
462
- );
463
- expect(rejectTranscriptWaiterMock).toHaveBeenCalledWith(
464
- { transcriptWaiters: ctx.transcriptWaiters },
465
- "call-1",
466
- "Call ended: hangup-bot",
467
- );
468
- expect(ctx.activeCalls.size).toBe(0);
469
- expect(ctx.providerCallIdMap.size).toBe(0);
470
- });
471
-
472
- it("preserves timeout reasons when ending timed out calls", async () => {
473
- const { call, ctx, hangupCall } = createActiveCallContext();
474
-
475
- await expect(endCall(ctx as never, "call-1", { reason: "timeout" })).resolves.toEqual({
476
- success: true,
477
- });
478
- expect(hangupCall).toHaveBeenCalledWith({
479
- callId: "call-1",
480
- providerCallId: "provider-1",
481
- reason: "timeout",
482
- });
483
- expect(call).toEqual(
484
- expect.objectContaining({
485
- endReason: "timeout",
486
- endedAt: expect.any(Number),
487
- }),
488
- );
489
- expect(transitionStateMock).toHaveBeenCalledWith(call, "timeout");
490
- expect(rejectTranscriptWaiterMock).toHaveBeenCalledWith(
491
- { transcriptWaiters: ctx.transcriptWaiters },
492
- "call-1",
493
- "Call ended: timeout",
494
- );
495
- });
496
-
497
- it("handles missing, disconnected, and already-ended calls", async () => {
498
- await expect(
499
- speak(
500
- {
501
- activeCalls: new Map(),
502
- providerCallIdMap: new Map(),
503
- provider: { name: "twilio", playTts: vi.fn() },
504
- config: {},
505
- storePath: "/tmp/voice-call.json",
506
- } as never,
507
- "missing",
508
- "hello",
509
- ),
510
- ).resolves.toEqual({ success: false, error: "Call not found" });
511
-
512
- await expect(
513
- endCall(
514
- {
515
- activeCalls: new Map([
516
- ["call-1", { callId: "call-1", state: "completed", providerCallId: "provider-1" }],
517
- ]),
518
- providerCallIdMap: new Map(),
519
- provider: { hangupCall: vi.fn() },
520
- storePath: "/tmp/voice-call.json",
521
- transcriptWaiters: new Map(),
522
- maxDurationTimers: new Map(),
523
- } as never,
524
- "call-1",
525
- ),
526
- ).resolves.toEqual({ success: true });
527
- });
528
- });