@@ -9,19 +9,18 @@ import {
99 makeSession ,
1010 noopInvoke ,
1111} from './session-test-harness.ts' ;
12+ import { createUnavailablePlatformRuntimeFacts } from '@agent-device/contracts/platform-runtime-unavailable' ;
13+ import { applicationLifecycleOperationFacts } from '@agent-device/contracts/application-lifecycle-runtime' ;
14+ import { localRuntimeOwner } from '@agent-device/contracts/platform-runtime' ;
15+ import { handleSessionStateCommands } from '../session-state.ts' ;
1216import type { SessionState } from '../../session-state.ts' ;
1317import {
1418 handleSessionCommands ,
1519 mockBindDeviceRuntime ,
1620 mockEnsureReadyHeadlessRuntime ,
1721 mockEnsureReadyRuntime ,
1822 mockInspectDeviceRuntimeFacts ,
19- mockShutdownTargetRuntime ,
2023} from './session-command-harness.ts' ;
21- import { createUnavailablePlatformRuntimeFacts } from '@agent-device/contracts/platform-runtime-unavailable' ;
22- import { applicationLifecycleOperationFacts } from '@agent-device/contracts/application-lifecycle-runtime' ;
23- import { localRuntimeOwner } from '@agent-device/contracts/platform-runtime' ;
24- import { handleSessionStateCommands } from '../session-state.ts' ;
2524import { mkdtempForTestSync } from '../../../__tests__/test-utils/tmp-dir.ts' ;
2625import { testCreateCommandSurface } from '../../__tests__/command-surface-fixture.ts' ;
2726
@@ -446,231 +445,6 @@ test('boot keeps --target validation before facts inspection', async () => {
446445 }
447446} ) ;
448447
449- test ( 'shutdown turns off selected iOS simulator' , async ( ) => {
450- const sessionStore = makeSessionStore ( ) ;
451- const selectedDevice : SessionState [ 'device' ] = {
452- platform : 'apple' ,
453- id : 'sim-2' ,
454- name : 'iPhone 17 Pro' ,
455- kind : 'simulator' ,
456- target : 'mobile' ,
457- booted : true ,
458- } ;
459- mockResolveTargetDevice . mockResolvedValue ( selectedDevice ) ;
460-
461- const response = await handleSessionCommands ( {
462- createCommandSurface : testCreateCommandSurface ,
463- platformServices : daemonPlatformServicesFixture ( ) ,
464- req : {
465- token : 't' ,
466- session : 'default' ,
467- command : 'shutdown' ,
468- positionals : [ ] ,
469- flags : { platform : 'ios' , device : 'iPhone 17 Pro' } ,
470- } ,
471- sessionName : 'default' ,
472- logPath : path . join ( mkdtempForTestSync ( 'daemon' ) , 'daemon.log' ) ,
473- sessionStore,
474- invoke : noopInvoke ,
475- } ) ;
476-
477- expect ( response ) . toBeTruthy ( ) ;
478- expect ( response ?. ok ) . toBe ( true ) ;
479- expect ( mockEnsureDeviceReady ) . not . toHaveBeenCalled ( ) ;
480- expect ( mockInspectDeviceRuntimeFacts ) . toHaveBeenCalledOnce ( ) ;
481- expect ( mockBindDeviceRuntime ) . toHaveBeenCalledOnce ( ) ;
482- expect ( mockShutdownTargetRuntime ) . toHaveBeenCalledOnce ( ) ;
483- if ( response && response . ok ) {
484- expect ( response . data ?. platform ) . toBe ( 'ios' ) ;
485- expect ( response . data ?. id ) . toBe ( 'sim-2' ) ;
486- expect ( response . data ?. shutdown ) . toEqual ( {
487- success : true ,
488- exitCode : 0 ,
489- stdout : '' ,
490- stderr : '' ,
491- } ) ;
492- }
493- } ) ;
494-
495- test ( 'shutdown rejects active session device and points to close --shutdown' , async ( ) => {
496- const sessionStore = makeSessionStore ( ) ;
497- const sessionName = 'default' ;
498- const selectedDevice : SessionState [ 'device' ] = {
499- platform : 'apple' ,
500- id : 'sim-2' ,
501- name : 'iPhone 17 Pro' ,
502- kind : 'simulator' ,
503- target : 'mobile' ,
504- booted : true ,
505- } ;
506- sessionStore . publish ( sessionName , makeSession ( sessionName , selectedDevice ) ) ;
507- mockResolveTargetDevice . mockResolvedValue ( selectedDevice ) ;
508-
509- const response = await handleSessionCommands ( {
510- createCommandSurface : testCreateCommandSurface ,
511- platformServices : daemonPlatformServicesFixture ( ) ,
512- req : {
513- token : 't' ,
514- session : sessionName ,
515- command : 'shutdown' ,
516- positionals : [ ] ,
517- flags : { platform : 'ios' , device : 'iPhone 17 Pro' } ,
518- } ,
519- sessionName,
520- logPath : path . join ( mkdtempForTestSync ( 'daemon' ) , 'daemon.log' ) ,
521- sessionStore,
522- invoke : noopInvoke ,
523- } ) ;
524-
525- expect ( response ) . toBeTruthy ( ) ;
526- expect ( response ?. ok ) . toBe ( false ) ;
527- expect ( mockInspectDeviceRuntimeFacts ) . toHaveBeenCalledOnce ( ) ;
528- expect ( mockBindDeviceRuntime ) . not . toHaveBeenCalled ( ) ;
529- expect ( mockShutdownTargetRuntime ) . not . toHaveBeenCalled ( ) ;
530- if ( response && ! response . ok ) {
531- expect ( response . error . code ) . toBe ( 'DEVICE_IN_USE' ) ;
532- expect ( response . error . message ) . toMatch ( / c l o s e - - s h u t d o w n / i) ;
533- expect ( response . error . details ?. hint ) . toBe (
534- 'Run agent-device close --shutdown --session default' ,
535- ) ;
536- }
537- } ) ;
538-
539- test ( 'shutdown turns off selected Android emulator' , async ( ) => {
540- const sessionStore = makeSessionStore ( ) ;
541- mockResolveTargetDevice . mockResolvedValue ( {
542- platform : 'android' ,
543- id : 'emulator-5554' ,
544- name : 'Pixel_9_Pro_XL' ,
545- kind : 'emulator' ,
546- target : 'mobile' ,
547- booted : false ,
548- } ) ;
549-
550- const response = await handleSessionCommands ( {
551- createCommandSurface : testCreateCommandSurface ,
552- platformServices : daemonPlatformServicesFixture ( ) ,
553- req : {
554- token : 't' ,
555- session : 'default' ,
556- command : 'shutdown' ,
557- positionals : [ ] ,
558- flags : { platform : 'android' , device : 'Pixel_9_Pro_XL' } ,
559- } ,
560- sessionName : 'default' ,
561- logPath : path . join ( mkdtempForTestSync ( 'daemon' ) , 'daemon.log' ) ,
562- sessionStore,
563- invoke : noopInvoke ,
564- } ) ;
565-
566- expect ( response ) . toBeTruthy ( ) ;
567- expect ( response ?. ok ) . toBe ( true ) ;
568- expect ( mockEnsureDeviceReady ) . not . toHaveBeenCalled ( ) ;
569- expect ( mockResolveTargetDevice ) . toHaveBeenCalledWith (
570- { platform : 'android' , device : 'Pixel_9_Pro_XL' } ,
571- { androidAvdSelection : 'include-stopped' } ,
572- ) ;
573- expect ( mockInspectDeviceRuntimeFacts ) . toHaveBeenCalledOnce ( ) ;
574- expect ( mockBindDeviceRuntime ) . toHaveBeenCalledOnce ( ) ;
575- expect ( mockShutdownTargetRuntime ) . toHaveBeenCalledOnce ( ) ;
576- if ( response && response . ok ) {
577- expect ( response . data ?. platform ) . toBe ( 'android' ) ;
578- expect ( response . data ?. id ) . toBe ( 'emulator-5554' ) ;
579- expect ( response . data ?. shutdown ) . toEqual ( {
580- success : true ,
581- exitCode : 0 ,
582- stdout : '' ,
583- stderr : '' ,
584- } ) ;
585- }
586- } ) ;
587-
588- test ( 'shutdown rejects unsupported physical devices' , async ( ) => {
589- const sessionStore = makeSessionStore ( ) ;
590- mockResolveTargetDevice . mockResolvedValue ( {
591- platform : 'apple' ,
592- id : 'device-1' ,
593- name : 'iPhone' ,
594- kind : 'device' ,
595- target : 'mobile' ,
596- booted : true ,
597- } ) ;
598-
599- const response = await handleSessionCommands ( {
600- createCommandSurface : testCreateCommandSurface ,
601- platformServices : daemonPlatformServicesFixture ( ) ,
602- req : {
603- token : 't' ,
604- session : 'default' ,
605- command : 'shutdown' ,
606- positionals : [ ] ,
607- flags : { platform : 'ios' , udid : 'device-1' } ,
608- } ,
609- sessionName : 'default' ,
610- logPath : path . join ( mkdtempForTestSync ( 'daemon' ) , 'daemon.log' ) ,
611- sessionStore,
612- invoke : noopInvoke ,
613- } ) ;
614-
615- expect ( response ) . toBeTruthy ( ) ;
616- expect ( response ?. ok ) . toBe ( false ) ;
617- expect ( mockInspectDeviceRuntimeFacts ) . toHaveBeenCalledOnce ( ) ;
618- expect ( mockBindDeviceRuntime ) . not . toHaveBeenCalled ( ) ;
619- expect ( mockShutdownTargetRuntime ) . not . toHaveBeenCalled ( ) ;
620- if ( response && ! response . ok ) {
621- expect ( response . error . code ) . toBe ( 'UNSUPPORTED_OPERATION' ) ;
622- expect ( response . error . message ) . toMatch ( / A p p l e s i m u l a t o r s a n d A n d r o i d e m u l a t o r s / i) ;
623- }
624- } ) ;
625-
626- test ( 'shutdown returns an error response when selected target shutdown fails' , async ( ) => {
627- const sessionStore = makeSessionStore ( ) ;
628- const selectedDevice : SessionState [ 'device' ] = {
629- platform : 'apple' ,
630- id : 'sim-2' ,
631- name : 'iPhone 17 Pro' ,
632- kind : 'simulator' ,
633- target : 'mobile' ,
634- booted : true ,
635- } ;
636- mockResolveTargetDevice . mockResolvedValue ( selectedDevice ) ;
637- mockShutdownTargetRuntime . mockResolvedValue ( {
638- success : false ,
639- exitCode : 149 ,
640- stdout : '' ,
641- stderr : 'simctl shutdown failed' ,
642- } ) ;
643-
644- const response = await handleSessionCommands ( {
645- createCommandSurface : testCreateCommandSurface ,
646- platformServices : daemonPlatformServicesFixture ( ) ,
647- req : {
648- token : 't' ,
649- session : 'default' ,
650- command : 'shutdown' ,
651- positionals : [ ] ,
652- flags : { platform : 'ios' , device : 'iPhone 17 Pro' } ,
653- } ,
654- sessionName : 'default' ,
655- logPath : path . join ( mkdtempForTestSync ( 'daemon' ) , 'daemon.log' ) ,
656- sessionStore,
657- invoke : noopInvoke ,
658- } ) ;
659-
660- expect ( response ) . toBeTruthy ( ) ;
661- expect ( response ?. ok ) . toBe ( false ) ;
662- if ( response && ! response . ok ) {
663- expect ( response . error . code ) . toBe ( 'COMMAND_FAILED' ) ;
664- expect ( response . error . message ) . toBe ( 'simctl shutdown failed' ) ;
665- expect ( response . error . details ?. shutdown ) . toEqual ( {
666- success : false ,
667- exitCode : 149 ,
668- stdout : '' ,
669- stderr : 'simctl shutdown failed' ,
670- } ) ;
671- }
672- } ) ;
673-
674448test ( 'boot rejects --headless outside Android directly' , async ( ) => {
675449 const device = {
676450 platform : 'apple' as const ,
0 commit comments