@@ -6,6 +6,8 @@ import { Command } from "commander";
66import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime" ;
77import { afterEach , describe , expect , it , vi } from "vitest" ;
88const callGatewayFromCliMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
9+ const findCallMatchesInStoreMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
10+ const loadActiveCallsFromStoreMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
911const sleepMock = vi . hoisted ( ( ) =>
1012 vi . fn (
1113 async ( ms : number ) =>
@@ -23,6 +25,11 @@ vi.mock("../api.js", async (importOriginal) => ({
2325 ...( await importOriginal < typeof import ( "../api.js" ) > ( ) ) ,
2426 sleep : sleepMock ,
2527} ) ) ;
28+ vi . mock ( "./manager/store.js" , async ( importOriginal ) => ( {
29+ ...( await importOriginal < typeof import ( "./manager/store.js" ) > ( ) ) ,
30+ findCallMatchesInStore : findCallMatchesInStoreMock ,
31+ loadActiveCallsFromStore : loadActiveCallsFromStoreMock ,
32+ } ) ) ;
2633
2734import { registerVoiceCallCli } from "./cli.js" ;
2835
@@ -41,6 +48,8 @@ function captureStdout() {
4148describe ( "voice-call CLI status fallback" , ( ) => {
4249 afterEach ( ( ) => {
4350 callGatewayFromCliMock . mockReset ( ) ;
51+ findCallMatchesInStoreMock . mockReset ( ) ;
52+ loadActiveCallsFromStoreMock . mockReset ( ) ;
4453 sleepMock . mockReset ( ) ;
4554 sleepMock . mockImplementation (
4655 async ( ms : number ) =>
@@ -66,53 +75,72 @@ describe("voice-call CLI status fallback", () => {
6675 return program ;
6776 }
6877
69- async function runStatusWithUnavailableGateway (
70- manager : Record < string , unknown > ,
71- error = new Error ( "connect ECONNREFUSED 127.0.0.1:18789" ) ,
72- ) : Promise < unknown > {
73- callGatewayFromCliMock . mockRejectedValue ( error ) ;
74- const program = buildProgram ( manager ) ;
78+ async function runStatusWithUnavailableGateway ( params : {
79+ persisted ?: unknown ;
80+ error ?: Error ;
81+ args ?: string [ ] ;
82+ } ) : Promise < unknown > {
83+ callGatewayFromCliMock . mockRejectedValue (
84+ params . error ?? new Error ( "connect ECONNREFUSED 127.0.0.1:18789" ) ,
85+ ) ;
86+ findCallMatchesInStoreMock . mockResolvedValue ( { byCallId : params . persisted } ) ;
87+ const ensureRuntime = vi . fn ( async ( ) => {
88+ throw new Error ( "status fallback must not initialize the telephony runtime" ) ;
89+ } ) ;
90+ const program = new Command ( ) ;
91+ registerVoiceCallCli ( {
92+ program,
93+ config : { } as never ,
94+ ensureRuntime,
95+ stateRuntime : { } as never ,
96+ logger : { info ( ) { } , warn ( ) { } , error ( ) { } , debug ( ) { } } as never ,
97+ } ) ;
7598 const capturer = captureStdout ( ) ;
7699 try {
77- await program . parseAsync ( [ "voicecall" , "status" , "--call-id" , "call-1" , "--json" ] , {
78- from : "user" ,
79- } ) ;
100+ await program . parseAsync (
101+ [ "voicecall" , "status" , ...( params . args ?? [ "--call-id" , "call-1" ] ) , "--json" ] ,
102+ { from : "user" } ,
103+ ) ;
80104 } finally {
81105 capturer . restore ( ) ;
82106 }
107+ expect ( ensureRuntime ) . not . toHaveBeenCalled ( ) ;
83108 return JSON . parse ( capturer . output ( ) . trim ( ) ) ;
84109 }
85110
86111 it ( "uses the manager's persisted fallback when the gateway is unavailable" , async ( ) => {
87112 const result = await runStatusWithUnavailableGateway ( {
88- getActiveCalls : ( ) => [ ] ,
89- getCallFromMemoryOrStore : async ( ) => ( {
113+ persisted : {
90114 callId : "call-1" ,
91115 providerCallId : "CA123" ,
92116 state : "completed" ,
93117 endReason : "completed" ,
94118 endedAt : 1 ,
95- } ) ,
119+ } ,
96120 } ) ;
97121 expect ( result ) . toMatchObject ( { callId : "call-1" , state : "completed" } ) ;
98122 } ) ;
99123
100124 it ( "reports found:false when the call is neither active nor persisted" , async ( ) => {
101- const result = await runStatusWithUnavailableGateway ( {
102- getActiveCalls : ( ) => [ ] ,
103- getCallFromMemoryOrStore : async ( ) => undefined ,
104- } ) ;
125+ const result = await runStatusWithUnavailableGateway ( { } ) ;
105126 expect ( result ) . toEqual ( { found : false } ) ;
106127 } ) ;
107128
129+ it ( "lists persisted active calls without initializing the telephony runtime" , async ( ) => {
130+ loadActiveCallsFromStoreMock . mockReturnValue ( {
131+ activeCalls : new Map ( [ [ "call-1" , { callId : "call-1" , state : "ringing" } ] ] ) ,
132+ } ) ;
133+ expect ( await runStatusWithUnavailableGateway ( { args : [ ] } ) ) . toEqual ( {
134+ found : true ,
135+ calls : [ { callId : "call-1" , state : "ringing" } ] ,
136+ } ) ;
137+ } ) ;
138+
108139 it ( "falls back after an abnormal local gateway close" , async ( ) => {
109- const result = await runStatusWithUnavailableGateway (
110- {
111- getActiveCalls : ( ) => [ ] ,
112- getCallFromMemoryOrStore : async ( ) => ( { callId : "call-1" , state : "completed" } ) ,
113- } ,
114- new Error ( "gateway closed (1006 abnormal closure (no close frame)): no close reason" ) ,
115- ) ;
140+ const result = await runStatusWithUnavailableGateway ( {
141+ persisted : { callId : "call-1" , state : "completed" } ,
142+ error : new Error ( "gateway closed (1006 abnormal closure (no close frame)): no close reason" ) ,
143+ } ) ;
116144 expect ( result ) . toMatchObject ( { callId : "call-1" , state : "completed" } ) ;
117145 } ) ;
118146
0 commit comments