11import { beforeEach , test , vi } from 'vitest' ;
22import assert from 'node:assert/strict' ;
3- import { promises as fs , existsSync , readFileSync } from 'node:fs' ;
3+ import { promises as fs , readFileSync } from 'node:fs' ;
44import path from 'node:path' ;
55import type { RunnerScreenCaptureMetadata } from '@agent-device/contracts/screen-capture-contract' ;
66import { bindTestCommandHost } from '@agent-device/test-kit' ;
@@ -65,6 +65,7 @@ import { retryWithPolicy } from '@agent-device/host-kit/retry';
6565import { AppError } from '@agent-device/kernel/errors' ;
6666
6767import { IOS_TEST_SIMULATOR , MACOS_TEST_DEVICE } from './apple-core-stub-helpers.ts' ;
68+ import { statusBarDiagnosticEvents } from './screenshot-diagnostic-log.ts' ;
6869
6970const mockRunCmd = vi . mocked ( runCmd ) ;
7071const mockRetryWithPolicy = vi . mocked ( retryWithPolicy ) ;
@@ -109,15 +110,6 @@ async function waitForFileText(filePath: string, attempts = 20): Promise<string>
109110 throw lastError ;
110111}
111112
112- function statusBarItemLogLines ( logPath : string ) {
113- if ( ! existsSync ( logPath ) ) return [ ] ;
114- return readFileSync ( logPath , 'utf8' )
115- . trim ( )
116- . split ( '\n' )
117- . filter ( ( line ) => line . includes ( '"ios_screenshot_status_bar_' ) )
118- . map ( ( line ) => JSON . parse ( line ) as { level : string ; phase : string ; data ?: unknown } ) ;
119- }
120-
121113test ( 'shouldRetryIosSimulatorScreenshot detects simulator screen-surface timeout' , ( ) => {
122114 const error = new AppError ( 'COMMAND_FAILED' , 'Detected file type from extension: PNG' , {
123115 stderr : 'Timeout waiting for screen surfaces' ,
@@ -371,27 +363,16 @@ test('captureSimulatorScreenshotWithFallback ignores status bar restore failures
371363 assert . equal ( mockRunAppleRunnerCommand . mock . calls . length , 0 ) ;
372364} ) ;
373365
374- // The screenshot flow owns the prepare and restore steps around a capture, so it owns reporting
375- // that they failed. Both must go through the status bar module's emitter: a second emitter here
376- // would drift on the phase vocabulary or drop the normalized tool failure.
377- async function statusBarDiagnosticEvents ( run : ( ) => Promise < void > ) {
378- const tmpDir = await mkdtempForTest ( 'agent-device-ios-screenshot-status-bar-diag-' ) ;
379- const logPath = path . join ( tmpDir , 'diag.ndjson' ) ;
380- try {
381- await withDiagnosticsScope ( { command : 'screenshot' , debug : true , logPath } , run ) ;
382- return statusBarItemLogLines ( logPath ) ;
383- } finally {
384- await fs . rm ( tmpDir , { recursive : true , force : true } ) ;
385- }
386- }
387-
388- function statusBarItemSummary ( event : { level : string ; phase : string ; data ?: unknown } ) {
389- const data = event . data as { deviceId ?: string ; exitCode ?: number } ;
366+ function statusBarItemSummary ( event : {
367+ level : string ;
368+ phase : string ;
369+ data ?: Record < string , unknown > ;
370+ } ) {
390371 return {
391372 level : event . level ,
392373 phase : event . phase ,
393- deviceId : data . deviceId ,
394- exitCode : data . exitCode ,
374+ deviceId : event . data ? .deviceId ,
375+ exitCode : event . data ? .exitCode ,
395376 } ;
396377}
397378
@@ -408,7 +389,7 @@ test('captureSimulatorScreenshotWithFallback logs a status bar preparation failu
408389 env : process . env ,
409390 appBundleId : 'com.example.app' ,
410391 normalizeStatusBar : true ,
411- deps : { normalizeDensity : async ( ) => { } } ,
392+ deps : { normalizeDensity : async ( ) => { } , resolveCaptureTarget : async ( ) => ( { } ) } ,
412393 } ) ;
413394 } ) ;
414395
@@ -435,7 +416,7 @@ test('captureSimulatorScreenshotWithFallback logs a status bar restoration failu
435416 env : process . env ,
436417 appBundleId : 'com.example.app' ,
437418 normalizeStatusBar : true ,
438- deps : { normalizeDensity : async ( ) => { } } ,
419+ deps : { normalizeDensity : async ( ) => { } , resolveCaptureTarget : async ( ) => ( { } ) } ,
439420 } ) ;
440421 } ) ;
441422
0 commit comments