@@ -11,7 +11,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
1111import { VoiceCallConfigSchema } from "../config.js" ;
1212import type { VoiceCallProvider } from "../providers/base.js" ;
1313import { setVoiceCallStateRuntime } from "../runtime-state.js" ;
14- import type { AnswerCallInput , HangupCallInput , NormalizedEvent } from "../types.js" ;
14+ import type { AnswerCallInput , CallRecord , HangupCallInput , NormalizedEvent } from "../types.js" ;
1515import type { CallManagerContext } from "./context.js" ;
1616import { processEvent } from "./events.js" ;
1717import { speakInitialMessage } from "./outbound.js" ;
@@ -48,15 +48,19 @@ vi.mock("openclaw/plugin-sdk/runtime-env", async (importOriginal) => {
4848
4949const contexts : CallManagerContext [ ] = [ ] ;
5050
51- function installStateRuntime ( ) : void {
51+ function installStateRuntime ( shouldFail ?: ( ) => boolean ) : void {
5252 setVoiceCallStateRuntime ( {
5353 state : {
5454 resolveStateDir : ( ) => "" ,
5555 openKeyedStore : ( ( ) => {
5656 throw new Error ( "openKeyedStore is not used by voice-call event tests" ) ;
5757 } ) as never ,
58- openSyncKeyedStore : ( options : OpenKeyedStoreOptions ) =>
59- createPluginStateSyncKeyedStoreForTests ( "voice-call" , options ) ,
58+ openSyncKeyedStore : ( options : OpenKeyedStoreOptions ) => {
59+ if ( shouldFail ?.( ) ) {
60+ throw new Error ( "synthetic SQLite persistence failure" ) ;
61+ }
62+ return createPluginStateSyncKeyedStoreForTests ( "voice-call" , options ) ;
63+ } ,
6064 openChannelIngressQueue : ( ( ) => {
6165 throw new Error ( "openChannelIngressQueue is not used by voice-call event tests" ) ;
6266 } ) as never ,
@@ -181,6 +185,97 @@ function requireFirstActiveCall(ctx: CallManagerContext) {
181185}
182186
183187describe ( "processEvent (functional)" , ( ) => {
188+ it . each ( [ "speech" , "answered" , "terminal" ] as const ) (
189+ "publishes %s side effects only after SQLite persistence succeeds" ,
190+ ( kind ) => {
191+ let failPersistence = true ;
192+ installStateRuntime ( ( ) => failPersistence ) ;
193+ const onCallAnswered = vi . fn ( ) ;
194+ const ctx = createContext ( { onCallAnswered } ) ;
195+ const call : CallRecord = {
196+ callId : `call-durable-${ kind } ` ,
197+ providerCallId : "provider-before" ,
198+ provider : "plivo" ,
199+ direction : "outbound" ,
200+ state : kind === "answered" ? "ringing" : "active" ,
201+ from : "+15550000000" ,
202+ to : "+15550000001" ,
203+ startedAt : Date . now ( ) ,
204+ transcript : [ ] ,
205+ processedEventIds : [ ] ,
206+ } ;
207+ ctx . activeCalls . set ( call . callId , call ) ;
208+ ctx . providerCallIdMap . set ( "provider-before" , call . callId ) ;
209+ const resolve = vi . fn ( ) ;
210+ const reject = vi . fn ( ) ;
211+ if ( kind !== "answered" ) {
212+ ctx . transcriptWaiters . set ( call . callId , {
213+ resolve,
214+ reject,
215+ timeout : setTimeout ( ( ) => { } , 60_000 ) ,
216+ } ) ;
217+ }
218+ if ( kind === "terminal" ) {
219+ ctx . maxDurationTimers . set (
220+ call . callId ,
221+ setTimeout ( ( ) => { } , 60_000 ) ,
222+ ) ;
223+ }
224+ const base = { id : `event-${ kind } ` , callId : call . callId , timestamp : Date . now ( ) } ;
225+ const event : NormalizedEvent =
226+ kind === "speech"
227+ ? { ...base , type : "call.speech" , transcript : "durable" , isFinal : true }
228+ : kind === "answered"
229+ ? { ...base , type : "call.answered" , providerCallId : "provider-after" }
230+ : { ...base , type : "call.ended" , reason : "hangup-user" } ;
231+
232+ expect ( ( ) => processEvent ( ctx , event ) ) . toThrow ( "synthetic SQLite persistence failure" ) ;
233+ expect ( ctx . processedEventIds . has ( event . id ) ) . toBe ( false ) ;
234+ expect ( call . processedEventIds ) . toEqual ( [ ] ) ;
235+ expect ( call . transcript ) . toEqual ( [ ] ) ;
236+ expect ( call . providerCallId ) . toBe ( "provider-before" ) ;
237+ expect ( ctx . providerCallIdMap . has ( "provider-after" ) ) . toBe ( false ) ;
238+ expect ( resolve ) . not . toHaveBeenCalled ( ) ;
239+ expect ( reject ) . not . toHaveBeenCalled ( ) ;
240+ expect ( ctx . maxDurationTimers . has ( call . callId ) ) . toBe ( kind === "terminal" ) ;
241+
242+ failPersistence = false ;
243+ processEvent ( ctx , event ) ;
244+ expect ( ctx . processedEventIds . has ( event . id ) ) . toBe ( true ) ;
245+ expect ( resolve ) . toHaveBeenCalledTimes ( kind === "speech" ? 1 : 0 ) ;
246+ expect ( reject ) . toHaveBeenCalledTimes ( kind === "terminal" ? 1 : 0 ) ;
247+ expect ( onCallAnswered ) . toHaveBeenCalledTimes ( kind === "answered" ? 1 : 0 ) ;
248+ expect ( ctx . activeCalls . has ( call . callId ) ) . toBe ( kind !== "terminal" ) ;
249+ } ,
250+ ) ;
251+
252+ it . each ( [ "created" , "rejected" ] as const ) (
253+ "does not publish %s inbound calls before SQLite persistence succeeds" ,
254+ ( kind ) => {
255+ let failPersistence = true ;
256+ installStateRuntime ( ( ) => failPersistence ) ;
257+ const { ctx, hangupCalls } = createRejectingInboundContext ( ) ;
258+ ctx . config . inboundPolicy = kind === "created" ? "open" : "disabled" ;
259+ const event = createInboundInitiatedEvent ( {
260+ id : `event-durable-${ kind } ` ,
261+ providerCallId : `provider-durable-${ kind } ` ,
262+ from : "+15550000002" ,
263+ } ) ;
264+
265+ expect ( ( ) => processEvent ( ctx , event ) ) . toThrow ( "synthetic SQLite persistence failure" ) ;
266+ expect ( ctx . activeCalls . size ) . toBe ( 0 ) ;
267+ expect ( ctx . providerCallIdMap . size ) . toBe ( 0 ) ;
268+ expect ( ctx . rejectedProviderCallIds . size ) . toBe ( 0 ) ;
269+ expect ( ctx . processedEventIds . size ) . toBe ( 0 ) ;
270+ expect ( hangupCalls ) . toHaveLength ( 0 ) ;
271+
272+ failPersistence = false ;
273+ expect ( processEvent ( ctx , event ) ) . toEqual ( { kind : "processed" } ) ;
274+ expect ( ctx . activeCalls . size ) . toBe ( kind === "created" ? 1 : 0 ) ;
275+ expect ( hangupCalls ) . toHaveLength ( kind === "rejected" ? 1 : 0 ) ;
276+ } ,
277+ ) ;
278+
184279 it ( "calls provider hangup when rejecting inbound call" , ( ) => {
185280 const { ctx, hangupCalls } = createRejectingInboundContext ( ) ;
186281 const event = createInboundInitiatedEvent ( {
@@ -332,7 +427,7 @@ describe("processEvent (functional)", () => {
332427 timestamp : now + 1 ,
333428 } ;
334429
335- processEvent ( ctx , event ) ;
430+ expect ( processEvent ( ctx , event ) ) . toEqual ( { kind : "ignored" , replayable : true } ) ;
336431
337432 expect ( ctx . processedEventIds . size ) . toBe ( 0 ) ;
338433
@@ -887,7 +982,7 @@ describe("processEvent (functional)", () => {
887982 retryable : true ,
888983 } ;
889984
890- processEvent ( ctx , event ) ;
985+ expect ( processEvent ( ctx , event ) ) . toEqual ( { kind : "processed" , replayable : true } ) ;
891986 processEvent ( ctx , event ) ;
892987
893988 const call = ctx . activeCalls . get ( "call-retryable-error" ) ;
0 commit comments