1- import { beforeEach , test , vi } from 'vitest' ;
1+ import { beforeEach , expect , test , vi } from 'vitest' ;
22import assert from 'node:assert/strict' ;
33import { promises as fs } from 'node:fs' ;
44import path from 'node:path' ;
55import { mkdtempForTest } from '../../__tests__/tmp-dir.ts' ;
6-
76vi . mock ( '@agent-device/host-kit/command' , async ( importOriginal ) => {
87 const actual = await importOriginal < typeof import ( '@agent-device/host-kit/command' ) > ( ) ;
98 return { ...actual , runCmd : vi . fn ( actual . runCmd ) } ;
@@ -12,6 +11,15 @@ vi.mock('../simulator.ts', async (importOriginal) => {
1211 const actual = await importOriginal < typeof import ( '../simulator.ts' ) > ( ) ;
1312 return { ...actual , ensureBootedSimulator : vi . fn ( actual . ensureBootedSimulator ) } ;
1413} ) ;
14+ vi . mock ( '@agent-device/host-kit/diagnostics' , async ( importOriginal ) => {
15+ const actual = await importOriginal < typeof import ( '@agent-device/host-kit/diagnostics' ) > ( ) ;
16+ return { ...actual , emitDiagnostic : vi . fn ( ) } ;
17+ } ) ;
18+ // The probe build a capture launch triggers belongs to launch-env.test.ts and the provider
19+ // scenario; here the cache answers with a fixed path so no test in this file reaches clang.
20+ vi . mock ( '../../network/probe-cache.ts' , ( ) => ( {
21+ ensureNetworkCaptureProbe : vi . fn ( ) ,
22+ } ) ) ;
1523
1624const execActual = await vi . importActual < typeof import ( '@agent-device/host-kit/command' ) > (
1725 '@agent-device/host-kit/command' ,
@@ -20,9 +28,12 @@ const simulatorActual = await vi.importActual<typeof import('../simulator.ts')>(
2028
2129import { openIosApp } from '../app-launch.ts' ;
2230import { ensureBootedSimulator } from '../simulator.ts' ;
23- import { IOS_SIMULATOR_OPENURL_TIMEOUT_MS } from '../config.ts' ;
31+ import { ensureNetworkCaptureProbe } from '../../network/probe-cache.ts' ;
32+ import { IOS_SIMULATOR_CONSOLE_CAPTURE_MS , IOS_SIMULATOR_OPENURL_TIMEOUT_MS } from '../config.ts' ;
2433import type { DeviceInfo } from '@agent-device/kernel/device' ;
25- import { runCmd } from '@agent-device/host-kit/command' ;
34+ import { AppError } from '@agent-device/kernel/errors' ;
35+ import { runCmd , withCommandHost } from '@agent-device/host-kit/command' ;
36+ import { emitDiagnostic } from '@agent-device/host-kit/diagnostics' ;
2637import { assertRejectsAppError } from '../../__tests__/app-error.ts' ;
2738import { withFakeAppleTool } from '../../__tests__/fake-apple-tool.ts' ;
2839import {
@@ -33,6 +44,17 @@ import {
3344
3445const mockRunCmd = vi . mocked ( runCmd ) ;
3546const mockEnsureBootedSimulator = vi . mocked ( ensureBootedSimulator ) ;
47+ const mockEnsureNetworkCaptureProbe = vi . mocked ( ensureNetworkCaptureProbe ) ;
48+ const mockEmitDiagnostic = vi . mocked ( emitDiagnostic ) ;
49+
50+ /** The one diagnostic this file's timeout branches emit, by phase. */
51+ function findDiagnostic ( phase : string ) : { level : string ; data : Record < string , unknown > } {
52+ const event = mockEmitDiagnostic . mock . calls
53+ . map ( ( [ event ] ) => event )
54+ . find ( ( event ) => event . phase === phase ) ;
55+ assert . ok ( event , `expected a diagnostic for phase ${ phase } ` ) ;
56+ return event as unknown as { level : string ; data : Record < string , unknown > } ;
57+ }
3658
3759beforeEach ( ( ) => {
3860 vi . resetAllMocks ( ) ;
@@ -126,12 +148,15 @@ test('openIosApp captures iOS simulator launch console output when requested', a
126148 }
127149 return '' ;
128150 } ,
129- async ( { calls } ) => {
151+ async ( { calls, execOptions } ) => {
130152 await openIosApp ( IOS_TEST_SIMULATOR , 'MyApp' , {
131153 appBundleId : 'com.example.app' ,
132154 launchConsole : launchConsolePath ,
133155 } ) ;
134156 assert . deepEqual ( calls , [ [ 'simctl' , 'launch' , '--console-pty' , 'sim-1' , 'com.example.app' ] ] ) ;
157+ // The closest negative to the capture launch: with no capture requested the console
158+ // launch spawns through the bare seam and no environment reaches it.
159+ assert . equal ( execOptions [ 0 ] ?. env , undefined ) ;
135160 assert . equal (
136161 await fs . readFile ( launchConsolePath , 'utf8' ) ,
137162 'console stdout\nconsole stderr\n' ,
@@ -140,6 +165,188 @@ test('openIosApp captures iOS simulator launch console output when requested', a
140165 ) ;
141166} ) ;
142167
168+ test ( 'openIosApp with a console and a network capture spawns the merged host environment' , async ( ) => {
169+ const tmpDir = await mkdtempForTest ( 'agent-device-ios-console-capture-test-' ) ;
170+ const launchConsolePath = path . join ( tmpDir , 'console.log' ) ;
171+ mockEnsureBootedSimulator . mockResolvedValue ( ) ;
172+ mockEnsureNetworkCaptureProbe . mockResolvedValue ( { path : '/cache/ADNetworkCapture.dylib' } ) ;
173+
174+ await withCommandHost (
175+ { host : { env : { PATH : '/usr/bin' } , cwd : tmpDir } , requestEnv : undefined } ,
176+ async ( ) =>
177+ await withFakeAppleTool (
178+ ( ) => ( { stdout : 'captured stdout' , stderr : '' } ) ,
179+ async ( { calls, execOptions } ) => {
180+ await openIosApp ( IOS_TEST_SIMULATOR , 'MyApp' , {
181+ appBundleId : 'com.example.app' ,
182+ launchConsole : launchConsolePath ,
183+ networkCapturePath : '/sessions/one/network.jsonl' ,
184+ } ) ;
185+ assert . deepEqual ( calls , [
186+ [
187+ 'simctl' ,
188+ 'launch' ,
189+ '--console-pty' ,
190+ '--terminate-running-process' ,
191+ 'sim-1' ,
192+ 'com.example.app' ,
193+ ] ,
194+ ] ) ;
195+ // A capture launch crosses the host-env spawn boundary even for the console variant:
196+ // the host environment carries along and the two SIMCTL_CHILD overrides land on top.
197+ assert . equal ( execOptions [ 0 ] ?. timeoutMs , IOS_SIMULATOR_CONSOLE_CAPTURE_MS ) ;
198+ assert . equal ( execOptions [ 0 ] ?. env ?. PATH , '/usr/bin' ) ;
199+ assert . equal (
200+ execOptions [ 0 ] ?. env ?. SIMCTL_CHILD_DYLD_INSERT_LIBRARIES ,
201+ '/cache/ADNetworkCapture.dylib' ,
202+ ) ;
203+ assert . equal (
204+ execOptions [ 0 ] ?. env ?. SIMCTL_CHILD_AGENT_DEVICE_NETWORK_CAPTURE_PATH ,
205+ '/sessions/one/network.jsonl' ,
206+ ) ;
207+ assert . equal ( await fs . readFile ( launchConsolePath , 'utf8' ) , 'captured stdout' ) ;
208+ } ,
209+ ) ,
210+ ) ;
211+ } ) ;
212+
213+ test ( 'openIosApp with a network capture and no console spawns the plain launch with that environment' , async ( ) => {
214+ mockEnsureBootedSimulator . mockResolvedValue ( ) ;
215+ mockEnsureNetworkCaptureProbe . mockResolvedValue ( { path : '/cache/ADNetworkCapture.dylib' } ) ;
216+
217+ await withCommandHost (
218+ {
219+ host : { env : { PATH : '/usr/bin' } , cwd : process . cwd ( ) } ,
220+ requestEnv : undefined ,
221+ } ,
222+ async ( ) =>
223+ await withFakeAppleTool (
224+ ( ) => '' ,
225+ async ( { calls, execOptions } ) => {
226+ await openIosApp ( IOS_TEST_SIMULATOR , 'MyApp' , {
227+ appBundleId : 'com.example.app' ,
228+ networkCapturePath : '/sessions/one/network.jsonl' ,
229+ } ) ;
230+ // Capture forces a fresh process but does not ask for the console pipe.
231+ assert . deepEqual ( calls , [
232+ [ 'simctl' , 'launch' , '--terminate-running-process' , 'sim-1' , 'com.example.app' ] ,
233+ ] ) ;
234+ assert . equal (
235+ execOptions [ 0 ] ?. env ?. SIMCTL_CHILD_DYLD_INSERT_LIBRARIES ,
236+ '/cache/ADNetworkCapture.dylib' ,
237+ ) ;
238+ assert . equal ( execOptions [ 0 ] ?. env ?. PATH , '/usr/bin' ) ;
239+ } ,
240+ ) ,
241+ ) ;
242+ } ) ;
243+
244+ test ( 'openIosApp keeps the open alive when the console capture bound fires and logs what was printed' , async ( ) => {
245+ const tmpDir = await mkdtempForTest ( 'agent-device-ios-console-timeout-test-' ) ;
246+ const launchConsolePath = path . join ( tmpDir , 'console.log' ) ;
247+ mockEnsureBootedSimulator . mockResolvedValue ( ) ;
248+
249+ await withFakeAppleTool (
250+ ( args ) => {
251+ if ( args [ 0 ] === 'simctl' && args [ 1 ] === 'launch' ) {
252+ throw new AppError ( 'COMMAND_FAILED' , 'xcrun timed out after 25000ms' , {
253+ stdout : 'partial output' ,
254+ stderr : 'truncated stderr' ,
255+ exitCode : 124 ,
256+ timeoutMs : IOS_SIMULATOR_CONSOLE_CAPTURE_MS ,
257+ } ) ;
258+ }
259+ return '' ;
260+ } ,
261+ async ( ) => {
262+ // The bound on the console pipe is a capture budget, not a launch failure: the open
263+ // resolves, the bytes killed with the child land in the log, and the diagnostic tells.
264+ await openIosApp ( IOS_TEST_SIMULATOR , 'MyApp' , {
265+ appBundleId : 'com.example.app' ,
266+ launchConsole : launchConsolePath ,
267+ } ) ;
268+ assert . equal (
269+ await fs . readFile ( launchConsolePath , 'utf8' ) ,
270+ 'partial output\ntruncated stderr' ,
271+ ) ;
272+ const diagnostic = findDiagnostic ( 'ios_simulator_launch_console_capture_timeout' ) ;
273+ expect ( diagnostic ) . toMatchObject ( {
274+ level : 'warn' ,
275+ data : {
276+ timeoutMs : IOS_SIMULATOR_CONSOLE_CAPTURE_MS ,
277+ logPath : launchConsolePath ,
278+ stdoutBytes : 14 ,
279+ stderrBytes : 16 ,
280+ } ,
281+ } ) ;
282+ } ,
283+ ) ;
284+ } ) ;
285+
286+ test ( 'openIosApp logs an empty console when the capture-bound error carries no output at all' , async ( ) => {
287+ const tmpDir = await mkdtempForTest ( 'agent-device-ios-console-timeout-empty-test-' ) ;
288+ const launchConsolePath = path . join ( tmpDir , 'console.log' ) ;
289+ mockEnsureBootedSimulator . mockResolvedValue ( ) ;
290+ mockEnsureNetworkCaptureProbe . mockResolvedValue ( { path : '/cache/ADNetworkCapture.dylib' } ) ;
291+
292+ await withCommandHost (
293+ { host : { env : { PATH : '/usr/bin' } , cwd : tmpDir } , requestEnv : undefined } ,
294+ async ( ) =>
295+ await withFakeAppleTool (
296+ ( args ) => {
297+ if ( args [ 0 ] === 'simctl' && args [ 1 ] === 'launch' ) {
298+ throw new AppError ( 'COMMAND_FAILED' , 'xcrun timed out after 25000ms' , {
299+ timeoutMs : IOS_SIMULATOR_CONSOLE_CAPTURE_MS ,
300+ } ) ;
301+ }
302+ return '' ;
303+ } ,
304+ async ( { execOptions } ) => {
305+ await openIosApp ( IOS_TEST_SIMULATOR , 'MyApp' , {
306+ appBundleId : 'com.example.app' ,
307+ launchConsole : launchConsolePath ,
308+ networkCapturePath : '/sessions/one/network.jsonl' ,
309+ } ) ;
310+ // The timeout branch reads missing stdout/stderr as empty and still writes the log,
311+ // on the capture spawn variant.
312+ assert . equal (
313+ execOptions [ 0 ] ?. env ?. SIMCTL_CHILD_DYLD_INSERT_LIBRARIES ,
314+ '/cache/ADNetworkCapture.dylib' ,
315+ ) ;
316+ assert . equal ( await fs . readFile ( launchConsolePath , 'utf8' ) , '' ) ;
317+ const diagnostic = findDiagnostic ( 'ios_simulator_launch_console_capture_timeout' ) ;
318+ expect ( diagnostic ) . toMatchObject ( {
319+ data : { stdoutBytes : 0 , stderrBytes : 0 } ,
320+ } ) ;
321+ } ,
322+ ) ,
323+ ) ;
324+ } ) ;
325+
326+ test ( 'openIosApp rethrows a console launch failure that is not the capture bound' , async ( ) => {
327+ const tmpDir = await mkdtempForTest ( 'agent-device-ios-console-error-test-' ) ;
328+ const launchConsolePath = path . join ( tmpDir , 'console.log' ) ;
329+ mockEnsureBootedSimulator . mockResolvedValue ( ) ;
330+
331+ await assert . rejects (
332+ withFakeAppleTool (
333+ ( args ) => {
334+ if ( args [ 0 ] === 'simctl' && args [ 1 ] === 'launch' ) {
335+ throw new Error ( 'console pipe exploded' ) ;
336+ }
337+ return '' ;
338+ } ,
339+ async ( ) => {
340+ await openIosApp ( IOS_TEST_SIMULATOR , 'MyApp' , {
341+ appBundleId : 'com.example.app' ,
342+ launchConsole : launchConsolePath ,
343+ } ) ;
344+ } ,
345+ ) ,
346+ / c o n s o l e p i p e e x p l o d e d / ,
347+ ) ;
348+ } ) ;
349+
143350test ( 'openIosApp emits a clean simctl launch when launchArgs is an empty array' , async ( ) => {
144351 mockEnsureBootedSimulator . mockResolvedValue ( ) ;
145352
0 commit comments