@@ -6,6 +6,30 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
66import * as fs from 'fs' ;
77import * as path from 'path' ;
88import * as os from 'os' ;
9+ import { EventEmitter } from 'events' ;
10+
11+ // Mock child_process.spawn to avoid invoking real claude CLI in tests
12+ vi . mock ( 'child_process' , ( ) => ( {
13+ spawn : vi . fn ( ( ) => {
14+ const proc = new EventEmitter ( ) as any ;
15+ proc . stdout = new EventEmitter ( ) ;
16+ proc . stderr = new EventEmitter ( ) ;
17+ proc . stdin = { write : vi . fn ( ) , end : vi . fn ( ) } ;
18+ setTimeout ( ( ) => {
19+ // Emit stream-json format that subagent-client parses
20+ const event = JSON . stringify ( {
21+ type : 'assistant' ,
22+ message : {
23+ content : [ { type : 'text' , text : '{"result": "mock CLI response"}' } ] ,
24+ } ,
25+ session_id : 'test-session' ,
26+ } ) ;
27+ proc . stdout . emit ( 'data' , Buffer . from ( event + '\n' ) ) ;
28+ proc . emit ( 'close' , 0 ) ;
29+ } , 50 ) ;
30+ return proc ;
31+ } ) ,
32+ } ) ) ;
933
1034// Create hoisted mock references so they're available inside vi.mock factories
1135const {
@@ -324,9 +348,10 @@ describe('ClaudeCodeSubagentClient', () => {
324348 type : 'code' ,
325349 task : 'Generate simple function' ,
326350 context : { } ,
351+ timeout : 1000 , // Short timeout — we only care about routing, not CLI result
327352 } ;
328353
329- // This will attempt CLI execution which may fail in test env,
354+ // This will attempt CLI execution which may fail/timeout in test env,
330355 // but the important thing is it didn't try createProvider
331356 await nonMockClient . executeSubagent ( request ) . catch ( ( ) => { } ) ;
332357
@@ -372,9 +397,10 @@ describe('ClaudeCodeSubagentClient', () => {
372397 type : 'code' ,
373398 task : 'Generate code' ,
374399 context : { } ,
400+ timeout : 1000 , // Short timeout — we only care about routing, not CLI result
375401 } ;
376402
377- // Will try CLI path; may fail in test env but that's OK
403+ // Will try CLI path; may fail/timeout in test env but that's OK
378404 const response = await nonMockClient . executeSubagent ( request ) ;
379405
380406 // createProvider should not be called for anthropic
0 commit comments