@@ -8,6 +8,7 @@ import { afterEach, test, vi } from 'vitest';
88import {
99 skipWhenLoopbackUnavailable ,
1010 closeLoopbackServer ,
11+ listenOnLoopback ,
1112} from '../../../__tests__/test-utils/loopback.ts' ;
1213import { mkdtempForTestSync } from '../../../__tests__/test-utils/tmp-dir.ts' ;
1314import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution' ;
@@ -16,6 +17,7 @@ const mocks = vi.hoisted(() => ({
1617 ensureDaemon : vi . fn ( ) ,
1718 writeCommandOutput : vi . fn ( async ( ..._args : unknown [ ] ) => undefined ) ,
1819 createdServers : [ ] as http . Server [ ] ,
20+ listenArgs : [ ] as { port : number ; host : string } [ ] ,
1921} ) ) ;
2022
2123vi . mock ( '../../../daemon-client/daemon-client-lifecycle.ts' , async ( importOriginal ) => ( {
@@ -28,6 +30,12 @@ vi.mock('@agent-device/proxy', async (importOriginal) => {
2830 ...actual ,
2931 createDaemonProxyServer : ( options : Parameters < typeof actual . createDaemonProxyServer > [ 0 ] ) => {
3032 const server = actual . createDaemonProxyServer ( options ) ;
33+ // Record what the command asks the real server to bind; the bind itself stays real.
34+ const listen = server . listen . bind ( server ) ;
35+ server . listen = ( ( port ?: number , host ?: string , callback ?: ( ) => void ) => {
36+ mocks . listenArgs . push ( { port : port ?? 0 , host : String ( host ) } ) ;
37+ return listen ( port , host , callback ) ;
38+ } ) as typeof server . listen ;
3139 mocks . createdServers . push ( server ) ;
3240 return server ;
3341 } ,
@@ -39,6 +47,7 @@ import { proxyCommand } from '../proxy.ts';
3947
4048afterEach ( async ( ) => {
4149 vi . clearAllMocks ( ) ;
50+ mocks . listenArgs . splice ( 0 , mocks . listenArgs . length ) ;
4251 const servers = mocks . createdServers . splice ( 0 , mocks . createdServers . length ) ;
4352 for ( const server of servers ) {
4453 await closeLoopbackServer ( server ) ;
@@ -114,6 +123,7 @@ test('proxy binds a loopback listener and reports the supplied client token', as
114123 } ) ;
115124 const { data, render } = await waitForStartupOutput ( ) ;
116125
126+ assert . deepEqual ( mocks . listenArgs , [ { port : 0 , host : '127.0.0.1' } ] ) ;
117127 assert . equal ( data . upstreamBaseUrl , 'http://127.0.0.1:54321' ) ;
118128 assert . equal ( data . token , 'proxy-token' ) ;
119129 assert . equal ( data . stateDir , resolveDaemonPaths ( stateDir ) . baseDir ) ;
@@ -135,10 +145,18 @@ test('proxy honors proxy host and port flags and mints a token when none is supp
135145 info : { httpPort : 54322 , token : 'daemon-secret' , pid : 4242 } ,
136146 startedByClient : false ,
137147 } ) ;
148+ // Reserve a real port through an ephemeral bind so the flag value is distinguishable
149+ // from the `0` default the command would otherwise pass to listen.
150+ const sentinel = http . createServer ( ) ;
151+ const reservedPort = await listenOnLoopback ( sentinel ) ;
152+ await closeLoopbackServer ( sentinel ) ;
138153
139- runProxy ( { proxyHost : ' localhost ' , proxyPort : 0 } ) ;
154+ runProxy ( { proxyHost : ' localhost ' , proxyPort : reservedPort } ) ;
140155 const { data } = await waitForStartupOutput ( ) ;
141156
142- assert . match ( String ( data . proxyBaseUrl ) , / ^ h t t p : \/ \/ ( l o c a l h o s t | \[ : : 1 \] | 1 2 7 \. 0 \. 0 \. 1 ) : \d + $ / ) ;
157+ assert . deepEqual ( mocks . listenArgs , [ { port : reservedPort , host : 'localhost' } ] ) ;
158+ // The reported URL carries the bound address, which names the reserved port either way.
159+ assert . match ( String ( data . proxyBaseUrl ) , / ^ h t t p : \/ \/ ( l o c a l h o s t | 1 2 7 \. 0 \. 0 \. 1 | \[ : : 1 \] ) : \d + $ / ) ;
160+ assert . ok ( String ( data . proxyBaseUrl ) . endsWith ( `:${ reservedPort } ` ) ) ;
143161 assert . match ( String ( data . token ) , / ^ [ 0 - 9 a - f ] { 64 } $ / ) ;
144162} ) ;
0 commit comments