1- import { afterEach , describe , expect , it , vi } from 'vitest'
1+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
22import { join } from 'node:path'
3+ import { EventEmitter } from 'node:events'
4+ import { PassThrough } from 'node:stream'
5+
6+ function shellProbe ( output = '' , code = 0 ) {
7+ const child = Object . assign ( new EventEmitter ( ) , { stdout : new PassThrough ( ) } )
8+ queueMicrotask ( ( ) => {
9+ child . stdout . end ( output )
10+ child . emit ( 'close' , code )
11+ } )
12+ return child
13+ }
14+
15+ const spawn = vi . fn ( ( _shell : string , _args : string [ ] , _options : { detached ?: boolean ; timeout ?: number } ) => shellProbe ( ) )
16+ const spawnSync = vi . fn ( ( _shell : string , _args : string [ ] , _options : { detached ?: boolean ; timeout ?: number } ) => ( { stdout : '' , status : 0 } ) )
17+
18+ beforeEach ( ( ) => {
19+ spawn . mockReset ( )
20+ spawn . mockImplementation ( ( ) => shellProbe ( ) )
21+ spawnSync . mockClear ( )
22+ // Discovery tests must not execute the owner's login/interactive shell profiles.
23+ vi . doMock ( 'child_process' , async importOriginal => ( {
24+ ...await importOriginal < typeof import ( 'child_process' ) > ( ) ,
25+ spawn,
26+ spawnSync,
27+ } ) )
28+ } )
329
430afterEach ( ( ) => {
31+ vi . doUnmock ( 'child_process' )
32+ vi . doUnmock ( './agents/model-detect' )
533 vi . doUnmock ( 'fs' )
634 vi . doUnmock ( 'os' )
735 vi . resetModules ( )
@@ -32,9 +60,43 @@ describe('headless agent registry', () => {
3260 available : true ,
3361 path : expectedPath ,
3462 } )
63+ expect ( spawnSync ) . not . toHaveBeenCalled ( )
64+ if ( process . platform !== 'win32' ) {
65+ expect ( spawn ) . toHaveBeenCalled ( )
66+ for ( const [ , , options ] of spawn . mock . calls ) {
67+ expect ( options ) . toMatchObject ( { detached : true , timeout : 3_000 } )
68+ }
69+ }
70+ } )
71+
72+ it . skipIf ( process . platform === 'win32' ) ( 'retains interactive-shell discovery without caller terminal access' , async ( ) => {
73+ const expectedPath = join ( '/shell' , 'codex' )
74+ vi . doMock ( 'fs' , async importOriginal => {
75+ const actual = await importOriginal < typeof import ( 'fs' ) > ( )
76+ return {
77+ ...actual ,
78+ promises : { ...actual . promises , access : vi . fn ( async ( candidate : string ) => {
79+ if ( candidate !== expectedPath ) throw new Error ( 'missing' )
80+ } ) } ,
81+ }
82+ } )
83+ spawn . mockImplementation ( ( _shell , args ) => shellProbe (
84+ args [ 0 ] === '-ic' && args [ 1 ] === 'command -v codex' ? `${ expectedPath } \n` : '' ,
85+ ) )
86+
87+ const { headlessAgentRegistry } = await import ( './headless-agent-resolver' )
88+ const registry = await headlessAgentRegistry ( )
89+ expect ( registry . find ( agent => agent . id === 'codex' ) ) . toMatchObject ( { available : true , path : expectedPath } )
90+ expect ( spawn ) . toHaveBeenCalledWith ( expect . any ( String ) , [ '-ic' , 'command -v codex' ] , expect . objectContaining ( {
91+ detached : true , stdio : [ 'ignore' , 'pipe' , 'ignore' ] , timeout : 3_000 ,
92+ } ) )
3593 } )
3694
3795 it ( 'lists models from the same provider CLIs as desktop' , async ( ) => {
96+ vi . doMock ( 'fs' , async importOriginal => ( {
97+ ...await importOriginal < typeof import ( 'fs' ) > ( ) ,
98+ existsSync : ( ) => false ,
99+ } ) )
38100 vi . doMock ( './agents/model-detect' , ( ) => ( {
39101 listModels : vi . fn ( async ( provider : string ) => (
40102 provider === 'claude'
@@ -47,5 +109,11 @@ describe('headless agent registry', () => {
47109 { id : 'claude-sonnet-4-6' , label : 'Sonnet' , provider : 'anthropic' } ,
48110 ] )
49111 expect ( await listHeadlessAgentModels ( 'codex' ) ) . toEqual ( [ ] )
112+ if ( process . platform !== 'win32' ) {
113+ expect ( spawnSync ) . toHaveBeenCalled ( )
114+ for ( const [ , , options ] of spawnSync . mock . calls ) {
115+ expect ( options ) . toMatchObject ( { detached : true , timeout : 3_000 } )
116+ }
117+ }
50118 } )
51119} )
0 commit comments