@@ -5198,6 +5198,8 @@ describe("runAgentTurnWithFallback", () => {
51985198 } ) ;
51995199
52005200 it ( "uses compact generic copy for raw external chat errors when verbose is off" , async ( ) => {
5201+ const agentEvents = await import ( "../../infra/agent-events.js" ) ;
5202+ const emitAgentEvent = vi . mocked ( agentEvents . emitAgentEvent ) ;
52015203 state . runEmbeddedAgentMock . mockRejectedValueOnce (
52025204 new Error ( "INVALID_ARGUMENT: some other failure" ) ,
52035205 ) ;
@@ -5210,7 +5212,7 @@ describe("runAgentTurnWithFallback", () => {
52105212 Provider : "whatsapp" ,
52115213 MessageSid : "msg" ,
52125214 } as unknown as TemplateContext ,
5213- opts : { } ,
5215+ opts : { runId : "run-provider-failure" } as GetReplyOptions ,
52145216 typingSignals : createMockTypingSignaler ( ) ,
52155217 blockReplyPipeline : null ,
52165218 blockStreamingEnabled : false ,
@@ -5230,6 +5232,21 @@ describe("runAgentTurnWithFallback", () => {
52305232 if ( result . kind === "final" ) {
52315233 expect ( result . payload . text ) . toBe ( GENERIC_RUN_FAILURE_TEXT ) ;
52325234 }
5235+ const terminalFailureEvent = emitAgentEvent . mock . calls
5236+ . map ( ( call ) => call [ 0 ] )
5237+ . find ( ( event ) => {
5238+ if ( ! event || typeof event !== "object" ) {
5239+ return false ;
5240+ }
5241+ const data = ( event as { data ?: Record < string , unknown > } ) . data ;
5242+ return (
5243+ ( event as { runId ?: unknown } ) . runId === "run-provider-failure" &&
5244+ ( event as { stream ?: unknown } ) . stream === "lifecycle" &&
5245+ data ?. phase === "error" &&
5246+ data . finalFailure === true
5247+ ) ;
5248+ } ) ;
5249+ expect ( terminalFailureEvent ) . toBeDefined ( ) ;
52335250 } ) ;
52345251
52355252 it ( "uses heartbeat failure copy for raw external errors during heartbeat runs" , async ( ) => {
0 commit comments