@@ -2045,6 +2045,77 @@ describe('runNonInteractive', () => {
20452045 expect ( mockGeminiClient . sendMessageStream ) . toHaveBeenCalledTimes ( 1 ) ;
20462046 } ) ;
20472047
2048+ it ( 'should write JSON output when AgentExecutionStopped event occurs' , async ( ) => {
2049+ vi . mocked ( mockConfig . getOutputFormat ) . mockReturnValue ( OutputFormat . JSON ) ;
2050+ vi . spyOn ( uiTelemetryService , 'getMetrics' ) . mockReturnValue (
2051+ MOCK_SESSION_METRICS ,
2052+ ) ;
2053+
2054+ const events : ServerGeminiStreamEvent [ ] = [
2055+ { type : GeminiEventType . Content , value : 'Partial content' } ,
2056+ {
2057+ type : GeminiEventType . AgentExecutionStopped ,
2058+ value : { reason : 'Stopped by hook' } ,
2059+ } ,
2060+ ] ;
2061+
2062+ mockGeminiClient . sendMessageStream . mockReturnValue (
2063+ createStreamFromEvents ( events ) ,
2064+ ) ;
2065+
2066+ await runNonInteractive ( {
2067+ config : mockConfig ,
2068+ settings : mockSettings ,
2069+ input : 'test stop' ,
2070+ prompt_id : 'prompt-id-stop-json' ,
2071+ } ) ;
2072+
2073+ expect ( processStdoutSpy ) . toHaveBeenCalledWith (
2074+ JSON . stringify (
2075+ {
2076+ session_id : 'test-session-id' ,
2077+ response : 'Partial content' ,
2078+ stats : MOCK_SESSION_METRICS ,
2079+ warnings : [ 'Agent execution stopped: Stopped by hook' ] ,
2080+ } ,
2081+ null ,
2082+ 2 ,
2083+ ) ,
2084+ ) ;
2085+ } ) ;
2086+
2087+ it ( 'should emit result event when AgentExecutionStopped event occurs in streaming JSON mode' , async ( ) => {
2088+ vi . mocked ( mockConfig . getOutputFormat ) . mockReturnValue (
2089+ OutputFormat . STREAM_JSON ,
2090+ ) ;
2091+ vi . spyOn ( uiTelemetryService , 'getMetrics' ) . mockReturnValue (
2092+ MOCK_SESSION_METRICS ,
2093+ ) ;
2094+
2095+ const events : ServerGeminiStreamEvent [ ] = [
2096+ { type : GeminiEventType . Content , value : 'Partial content' } ,
2097+ {
2098+ type : GeminiEventType . AgentExecutionStopped ,
2099+ value : { reason : 'Stopped by hook' } ,
2100+ } ,
2101+ ] ;
2102+
2103+ mockGeminiClient . sendMessageStream . mockReturnValue (
2104+ createStreamFromEvents ( events ) ,
2105+ ) ;
2106+
2107+ await runNonInteractive ( {
2108+ config : mockConfig ,
2109+ settings : mockSettings ,
2110+ input : 'test stop' ,
2111+ prompt_id : 'prompt-id-stop-stream' ,
2112+ } ) ;
2113+
2114+ const output = getWrittenOutput ( ) ;
2115+ expect ( output ) . toContain ( '"type":"result"' ) ;
2116+ expect ( output ) . toContain ( '"status":"success"' ) ;
2117+ } ) ;
2118+
20482119 it ( 'should handle AgentExecutionBlocked event' , async ( ) => {
20492120 const allEvents : ServerGeminiStreamEvent [ ] = [
20502121 {
0 commit comments