@@ -57,14 +57,18 @@ import { handleRecordTraceCommands } from '../record-trace.ts';
5757import { deriveRecordingTelemetryPath } from '../../recording-telemetry.ts' ;
5858import { SessionStore } from '../../session-store.ts' ;
5959import type { SessionState } from '../../types.ts' ;
60- import { runAppleRunnerCommand } from '../../../platforms/apple/core/runner/runner-client.ts' ;
60+ import {
61+ IOS_RUNNER_CONTAINER_BUNDLE_IDS ,
62+ runAppleRunnerCommand ,
63+ } from '../../../platforms/apple/core/runner/runner-client.ts' ;
6164import {
6265 getRecordingOverlaySupportWarning ,
6366 resizeRecording ,
6467 trimRecordingStart ,
6568 overlayRecordingTouches ,
6669} from '../../../recording/overlay.ts' ;
6770import { resolveTargetDevice } from '../../../core/dispatch.ts' ;
71+ import { ensureDeviceReady } from '../../device-ready.ts' ;
6872import { runCmd , runCmdBackground } from '../../../utils/exec.ts' ;
6973import { isPlayableVideo , waitForStableFile } from '../../../utils/video.ts' ;
7074import { withWebProvider , type WebProvider } from '../../../platforms/web/provider.ts' ;
@@ -83,6 +87,7 @@ const mockRunCmd = vi.mocked(runCmd);
8387const mockRunCmdBackground = vi . mocked ( runCmdBackground ) ;
8488const mockRunAppleRunnerCommand = vi . mocked ( runAppleRunnerCommand ) ;
8589const mockResolveTargetDevice = vi . mocked ( resolveTargetDevice ) ;
90+ const mockEnsureDeviceReady = vi . mocked ( ensureDeviceReady ) ;
8691const mockResizeRecording = vi . mocked ( resizeRecording ) ;
8792const mockTrimRecordingStart = vi . mocked ( trimRecordingStart ) ;
8893const mockOverlayRecordingTouches = vi . mocked ( overlayRecordingTouches ) ;
@@ -129,6 +134,13 @@ function makeIosSimulatorSession(name: string): SessionState {
129134 } ) ;
130135}
131136
137+ function makeOpenedIosSimulatorSession ( name : string ) : SessionState {
138+ const session = makeIosSimulatorSession ( name ) ;
139+ session . appBundleId = 'com.apple.Preferences' ;
140+ session . appName = 'Settings' ;
141+ return session ;
142+ }
143+
132144function makeWebSession ( name : string ) : SessionState {
133145 return makeSession ( name , {
134146 platform : 'web' ,
@@ -203,6 +215,7 @@ async function runRecordCommand(params: {
203215 screenshotMaxSize ?: number ;
204216 hideTouches ?: boolean ;
205217 } ;
218+ sessionExplicit ?: boolean ;
206219 clientArtifactPaths ?: Record < string , string > ;
207220} ) {
208221 return handleRecordTraceCommands ( {
@@ -213,12 +226,13 @@ async function runRecordCommand(params: {
213226 positionals : params . positionals ,
214227 flags : params . flags ?? { } ,
215228 meta :
216- params . cwd || params . clientArtifactPaths
229+ params . cwd || params . clientArtifactPaths || params . sessionExplicit
217230 ? {
218231 ...( params . cwd ? { cwd : params . cwd } : { } ) ,
219232 ...( params . clientArtifactPaths
220233 ? { clientArtifactPaths : params . clientArtifactPaths }
221234 : { } ) ,
235+ ...( params . sessionExplicit ? { sessionExplicit : true } : { } ) ,
222236 }
223237 : undefined ,
224238 } ,
@@ -879,16 +893,7 @@ test('record stop trims iOS device recordings from target app readiness before o
879893test ( 'record stop resizes iOS simulator recording when max-size is explicit' , async ( ) => {
880894 const sessionStore = makeSessionStore ( ) ;
881895 const sessionName = 'ios-sim-quality' ;
882- sessionStore . set (
883- sessionName ,
884- makeSession ( sessionName , {
885- platform : 'apple' ,
886- id : 'sim-1' ,
887- name : 'Simulator' ,
888- kind : 'simulator' ,
889- booted : true ,
890- } ) ,
891- ) ;
896+ sessionStore . set ( sessionName , makeOpenedIosSimulatorSession ( sessionName ) ) ;
892897
893898 mockRunCmdBackground . mockImplementation ( ( ) => ( {
894899 child : { kill : ( ) => { } } as any ,
@@ -920,16 +925,7 @@ test('record stop resizes iOS simulator recording when max-size is explicit', as
920925test ( 'record stop forwards the requested quality to the resize step' , async ( ) => {
921926 const sessionStore = makeSessionStore ( ) ;
922927 const sessionName = 'ios-sim-export-quality' ;
923- sessionStore . set (
924- sessionName ,
925- makeSession ( sessionName , {
926- platform : 'apple' ,
927- id : 'sim-1' ,
928- name : 'Simulator' ,
929- kind : 'simulator' ,
930- booted : true ,
931- } ) ,
932- ) ;
928+ sessionStore . set ( sessionName , makeOpenedIosSimulatorSession ( sessionName ) ) ;
933929
934930 mockRunCmdBackground . mockImplementation ( ( ) => ( {
935931 child : { kill : ( ) => { } } as any ,
@@ -1128,19 +1124,109 @@ test('record stop measures too-short Android failures from stop request time', a
11281124 expect ( ( response as any ) . error ?. message ) . toMatch ( / f a i l e d t o s t o p / i) ;
11291125} ) ;
11301126
1127+ test ( 'record start on iOS simulator requires active app session context' , async ( ) => {
1128+ const sessionStore = makeSessionStore ( ) ;
1129+ const sessionName = 'ios-sim-no-app' ;
1130+ sessionStore . set ( sessionName , makeIosSimulatorSession ( sessionName ) ) ;
1131+
1132+ mockRunCmdBackground . mockImplementation ( ( ) => {
1133+ throw new Error ( 'simctl recordVideo should not start without active app context' ) ;
1134+ } ) ;
1135+
1136+ const response = await runRecordCommand ( {
1137+ sessionStore,
1138+ sessionName,
1139+ positionals : [ 'start' , './sim-no-app.mp4' ] ,
1140+ flags : { hideTouches : true } ,
1141+ } ) ;
1142+
1143+ expect ( response ?. ok ) . toBe ( false ) ;
1144+ expect ( ( response as any ) . error ?. code ) . toBe ( 'INVALID_ARGS' ) ;
1145+ expect ( ( response as any ) . error ?. message ?? '' ) . toMatch ( / r e q u i r e s a n a c t i v e a p p s e s s i o n / i) ;
1146+ expect ( sessionStore . get ( sessionName ) ?. recording ) . toBeUndefined ( ) ;
1147+ } ) ;
1148+
1149+ test ( 'record start with explicit missing session does not fall back to record-only capture' , async ( ) => {
1150+ const sessionStore = makeSessionStore ( ) ;
1151+ const sessionName = 'ios-sim-explicit-missing' ;
1152+ mockRunCmdBackground . mockImplementation ( ( ) => {
1153+ throw new Error ( 'simctl recordVideo should not start for an explicit missing session' ) ;
1154+ } ) ;
1155+
1156+ const response = await runRecordCommand ( {
1157+ sessionStore,
1158+ sessionName,
1159+ positionals : [ 'start' , './sim-explicit-missing.mp4' ] ,
1160+ flags : { hideTouches : true } ,
1161+ sessionExplicit : true ,
1162+ } ) ;
1163+
1164+ expect ( response ?. ok ) . toBe ( false ) ;
1165+ expect ( ( response as any ) . error ?. code ) . toBe ( 'INVALID_ARGS' ) ;
1166+ expect ( ( response as any ) . error ?. message ?? '' ) . toMatch ( / e x p l i c i t s e s s i o n / i) ;
1167+ expect ( mockResolveTargetDevice ) . not . toHaveBeenCalled ( ) ;
1168+ expect ( mockEnsureDeviceReady ) . not . toHaveBeenCalled ( ) ;
1169+ expect ( sessionStore . get ( sessionName ) ) . toBeUndefined ( ) ;
1170+ } ) ;
1171+
1172+ test ( 'record start on iOS simulator rejects Agent Device Runner as active app context' , async ( ) => {
1173+ const sessionStore = makeSessionStore ( ) ;
1174+ const sessionName = 'ios-sim-runner-app' ;
1175+ const session = makeIosSimulatorSession ( sessionName ) ;
1176+ session . appBundleId = IOS_RUNNER_CONTAINER_BUNDLE_IDS [ 0 ] ;
1177+ sessionStore . set ( sessionName , session ) ;
1178+
1179+ mockRunCmdBackground . mockImplementation ( ( ) => {
1180+ throw new Error ( 'simctl recordVideo should not start for the runner app' ) ;
1181+ } ) ;
1182+
1183+ const response = await runRecordCommand ( {
1184+ sessionStore,
1185+ sessionName,
1186+ positionals : [ 'start' , './sim-runner-app.mp4' ] ,
1187+ flags : { hideTouches : true } ,
1188+ } ) ;
1189+
1190+ expect ( response ?. ok ) . toBe ( false ) ;
1191+ expect ( ( response as any ) . error ?. code ) . toBe ( 'INVALID_ARGS' ) ;
1192+ expect ( ( response as any ) . error ?. message ?? '' ) . toMatch ( / A g e n t D e v i c e R u n n e r / ) ;
1193+ expect ( sessionStore . get ( sessionName ) ?. recording ) . toBeUndefined ( ) ;
1194+ } ) ;
1195+
1196+ test ( 'record start on iOS simulator still supports record-only capture without a session' , async ( ) => {
1197+ const sessionStore = makeSessionStore ( ) ;
1198+ const sessionName = 'ios-sim-record-only' ;
1199+ mockResolveTargetDevice . mockResolvedValueOnce ( {
1200+ platform : 'apple' ,
1201+ id : 'ios-sim-1' ,
1202+ name : 'iPhone 16' ,
1203+ kind : 'simulator' ,
1204+ booted : true ,
1205+ } ) ;
1206+ mockRunCmdBackground . mockImplementation ( ( ) => ( {
1207+ child : { kill : ( ) => { } , pid : 5152 } as any ,
1208+ wait : Promise . resolve ( { stdout : '' , stderr : '' , exitCode : 0 } ) ,
1209+ } ) ) ;
1210+
1211+ const response = await runRecordCommand ( {
1212+ sessionStore,
1213+ sessionName,
1214+ positionals : [ 'start' , './sim-record-only.mp4' ] ,
1215+ flags : { hideTouches : true } ,
1216+ } ) ;
1217+
1218+ expect ( response ?. ok ) . toBe ( true ) ;
1219+ expect ( ( response as any ) . data ?. recordingBackend ) . toBe ( 'simctl recordVideo' ) ;
1220+ expect ( ( response as any ) . data ?. recordOnlySession ) . toBe ( true ) ;
1221+ expect ( ( response as any ) . data ?. activeSessionApp ) . toBeUndefined ( ) ;
1222+ expect ( sessionStore . get ( sessionName ) ?. recordOnlySession ) . toBe ( true ) ;
1223+ expect ( sessionStore . get ( sessionName ) ?. recording ?. platform ) . toBe ( 'ios' ) ;
1224+ } ) ;
1225+
11311226test ( 'record start stores iOS simulator recorder pid for scoped cleanup' , async ( ) => {
11321227 const sessionStore = makeSessionStore ( ) ;
11331228 const sessionName = 'ios-sim-recorder-pid' ;
1134- sessionStore . set (
1135- sessionName ,
1136- makeSession ( sessionName , {
1137- platform : 'apple' ,
1138- id : 'sim-1' ,
1139- name : 'Simulator' ,
1140- kind : 'simulator' ,
1141- booted : true ,
1142- } ) ,
1143- ) ;
1229+ sessionStore . set ( sessionName , makeOpenedIosSimulatorSession ( sessionName ) ) ;
11441230 mockRunCmdBackground . mockImplementation ( ( ) => ( {
11451231 child : { kill : ( ) => { } , pid : 5151 } as any ,
11461232 wait : Promise . resolve ( { stdout : '' , stderr : '' , exitCode : 0 } ) ,
@@ -1154,6 +1240,12 @@ test('record start stores iOS simulator recorder pid for scoped cleanup', async
11541240 } ) ;
11551241
11561242 expect ( response ?. ok ) . toBe ( true ) ;
1243+ expect ( ( response as any ) . data ?. recordingBackend ) . toBe ( 'simctl recordVideo' ) ;
1244+ expect ( ( response as any ) . data ?. recordOnlySession ) . toBe ( false ) ;
1245+ expect ( ( response as any ) . data ?. activeSessionApp ) . toEqual ( {
1246+ bundleId : 'com.apple.Preferences' ,
1247+ name : 'Settings' ,
1248+ } ) ;
11571249 const recording = sessionStore . get ( sessionName ) ?. recording ;
11581250 expect ( recording ?. platform ) . toBe ( 'ios' ) ;
11591251 if ( recording ?. platform === 'ios' ) {
@@ -1283,16 +1375,7 @@ test('record stop falls back to path matching for stale iOS simulator recordVide
12831375test ( 'record stop keeps iOS simulator video when overlay export fails' , async ( ) => {
12841376 const sessionStore = makeSessionStore ( ) ;
12851377 const sessionName = 'ios-sim-overlay-warning' ;
1286- sessionStore . set (
1287- sessionName ,
1288- makeSession ( sessionName , {
1289- platform : 'apple' ,
1290- id : 'sim-1' ,
1291- name : 'Simulator' ,
1292- kind : 'simulator' ,
1293- booted : true ,
1294- } ) ,
1295- ) ;
1378+ sessionStore . set ( sessionName , makeOpenedIosSimulatorSession ( sessionName ) ) ;
12961379
12971380 mockRunCmdBackground . mockImplementation ( ( ) => ( {
12981381 child : { kill : ( ) => { } } as any ,
@@ -1329,16 +1412,7 @@ test('record stop keeps iOS simulator video when overlay export fails', async ()
13291412test ( 'record stop skips touch overlay export when no gestures were recorded' , async ( ) => {
13301413 const sessionStore = makeSessionStore ( ) ;
13311414 const sessionName = 'ios-sim-no-gestures' ;
1332- sessionStore . set (
1333- sessionName ,
1334- makeSession ( sessionName , {
1335- platform : 'apple' ,
1336- id : 'sim-1' ,
1337- name : 'Simulator' ,
1338- kind : 'simulator' ,
1339- booted : true ,
1340- } ) ,
1341- ) ;
1415+ sessionStore . set ( sessionName , makeOpenedIosSimulatorSession ( sessionName ) ) ;
13421416
13431417 mockRunCmdBackground . mockImplementation ( ( ) => ( {
13441418 child : { kill : ( ) => { } } as any ,
@@ -1365,16 +1439,7 @@ test('record stop skips touch overlay export when no gestures were recorded', as
13651439test ( 'record stop keeps iOS simulator video when resize export fails' , async ( ) => {
13661440 const sessionStore = makeSessionStore ( ) ;
13671441 const sessionName = 'ios-sim-resize-fail' ;
1368- sessionStore . set (
1369- sessionName ,
1370- makeSession ( sessionName , {
1371- platform : 'apple' ,
1372- id : 'sim-1' ,
1373- name : 'Simulator' ,
1374- kind : 'simulator' ,
1375- booted : true ,
1376- } ) ,
1377- ) ;
1442+ sessionStore . set ( sessionName , makeOpenedIosSimulatorSession ( sessionName ) ) ;
13781443
13791444 mockRunCmdBackground . mockImplementation ( ( ) => ( {
13801445 child : { kill : ( ) => { } } as any ,
0 commit comments