@@ -61,6 +61,7 @@ import { dispatchCommand } from '../../../core/dispatch.ts';
6161import { cleanupAppleXctracePerfCapture } from '../../../platforms/apple/core/perf-xctrace.ts' ;
6262import { cleanupAndroidNativePerfSession } from '../../../platforms/android/perf.ts' ;
6363import { stopAndroidSnapshotHelperSessionForDevice } from '../../../platforms/android/snapshot-helper.ts' ;
64+ import { stopIosRunnerSession } from '../../../platforms/apple/core/runner/runner-client.ts' ;
6465import { WEB_DESKTOP_DEVICE } from '../../../__tests__/test-utils/index.ts' ;
6566
6667const mockShutdownSimulator = vi . mocked ( shutdownSimulator ) ;
@@ -71,6 +72,7 @@ const mockCleanupAndroidNativePerfSession = vi.mocked(cleanupAndroidNativePerfSe
7172const mockStopAndroidSnapshotHelperSessionForDevice = vi . mocked (
7273 stopAndroidSnapshotHelperSessionForDevice ,
7374) ;
75+ const mockStopIosRunnerSession = vi . mocked ( stopIosRunnerSession ) ;
7476
7577const noopInvoke = async ( _req : DaemonRequest ) : Promise < DaemonResponse > => ( { ok : true , data : { } } ) ;
7678
@@ -730,3 +732,215 @@ test('close --shutdown returns success and failure payload when shutdownSimulato
730732 expect ( shutdown ?. error ?. message ) . toBe ( 'simctl shutdown failed' ) ;
731733 }
732734} ) ;
735+
736+ test ( 'daemon session teardown attempts every resource after an earlier cleanup rejects' , async ( ) => {
737+ const sessionName = 'android-teardown-isolation-session' ;
738+ const activeCapture = {
739+ type : 'trace' ,
740+ kind : 'perfetto' ,
741+ packageName : 'com.example.app' ,
742+ appPid : '1234' ,
743+ profilerPid : '5678' ,
744+ remotePath : '/data/misc/perfetto-traces/app.perfetto-trace' ,
745+ outPath : '/tmp/app.perfetto-trace' ,
746+ startedAt : Date . now ( ) ,
747+ state : 'running' ,
748+ } ;
749+ const session = {
750+ ...makeSession ( sessionName , {
751+ platform : 'android' ,
752+ id : 'emulator-5554' ,
753+ name : 'Pixel' ,
754+ kind : 'emulator' ,
755+ booted : true ,
756+ } ) ,
757+ appBundleId : 'com.example.app' ,
758+ nativePerf : {
759+ android : activeCapture ,
760+ } ,
761+ } as unknown as SessionState ;
762+
763+ // The native-perf cleanup runs before the snapshot-helper cleanup.
764+ mockCleanupAndroidNativePerfSession . mockRejectedValueOnce (
765+ new AppError ( 'COMMAND_FAILED' , 'perfetto stop failed' ) ,
766+ ) ;
767+
768+ await expect ( teardownSessionResources ( session , sessionName ) ) . rejects . toMatchObject ( {
769+ code : 'COMMAND_FAILED' ,
770+ details : expect . objectContaining ( {
771+ reason : 'session_cleanup_incomplete' ,
772+ failedSteps : [ 'android_native_perf' ] ,
773+ } ) ,
774+ } ) ;
775+
776+ // The later resource still runs despite the earlier rejection.
777+ expect ( mockStopAndroidSnapshotHelperSessionForDevice ) . toHaveBeenCalledWith ( session . device ) ;
778+ } ) ;
779+
780+ test ( 'close still runs later cleanup and deletes the session after an earlier cleanup rejects' , async ( ) => {
781+ const sessionStore = makeSessionStore ( ) ;
782+ const sessionName = 'android-close-isolation-session' ;
783+ const activeCapture = {
784+ type : 'trace' ,
785+ kind : 'perfetto' ,
786+ packageName : 'com.example.app' ,
787+ appPid : '1234' ,
788+ profilerPid : '5678' ,
789+ remotePath : '/data/misc/perfetto-traces/app.perfetto-trace' ,
790+ outPath : '/tmp/app.perfetto-trace' ,
791+ startedAt : Date . now ( ) ,
792+ state : 'running' ,
793+ } ;
794+ const session = {
795+ ...makeSession ( sessionName , {
796+ platform : 'android' ,
797+ id : 'emulator-5554' ,
798+ name : 'Pixel' ,
799+ kind : 'emulator' ,
800+ booted : true ,
801+ } ) ,
802+ appBundleId : 'com.example.app' ,
803+ nativePerf : {
804+ android : activeCapture ,
805+ } ,
806+ } as unknown as SessionState ;
807+ sessionStore . set ( sessionName , session ) ;
808+
809+ mockCleanupAndroidNativePerfSession . mockRejectedValueOnce (
810+ new AppError ( 'COMMAND_FAILED' , 'perfetto stop failed' ) ,
811+ ) ;
812+
813+ await expect (
814+ handleSessionCommands ( {
815+ req : {
816+ token : 't' ,
817+ session : sessionName ,
818+ command : 'close' ,
819+ positionals : [ ] ,
820+ flags : { } ,
821+ } ,
822+ sessionName,
823+ logPath : path . join ( os . tmpdir ( ) , 'daemon.log' ) ,
824+ sessionStore,
825+ invoke : noopInvoke ,
826+ } ) ,
827+ ) . rejects . toMatchObject ( {
828+ details : expect . objectContaining ( {
829+ reason : 'session_cleanup_incomplete' ,
830+ failedSteps : [ 'android_native_perf' ] ,
831+ } ) ,
832+ } ) ;
833+
834+ // A later cleanup step still ran, and the session was still deleted.
835+ expect ( mockStopAndroidSnapshotHelperSessionForDevice ) . toHaveBeenCalledWith ( session . device ) ;
836+ expect ( sessionStore . get ( sessionName ) ) . toBeUndefined ( ) ;
837+ } ) ;
838+
839+ test ( 'targeted close preserves the platform-close AppError and still runs later cleanup' , async ( ) => {
840+ const sessionStore = makeSessionStore ( ) ;
841+ const sessionName = 'targeted-close-error-session' ;
842+ const session = {
843+ ...makeSession ( sessionName , {
844+ platform : 'apple' ,
845+ id : 'sim-udid-close-error' ,
846+ name : 'iPhone 15' ,
847+ kind : 'simulator' ,
848+ booted : true ,
849+ } ) ,
850+ // Recording defeats runner retention so the apple_runner cleanup runs after
851+ // the failed platform close, proving subsequent cleanup is still attempted.
852+ recording : { outPath : '/tmp/recording.mp4' } ,
853+ } as unknown as SessionState ;
854+ sessionStore . set ( sessionName , session ) ;
855+
856+ const platformCloseError = new AppError ( 'DEVICE_UNAVAILABLE' , 'platform close failed' , {
857+ reason : 'device_disconnected' ,
858+ hint : 'Reconnect the device and retry close.' ,
859+ } ) ;
860+ mockDispatchCommand . mockRejectedValueOnce ( platformCloseError ) ;
861+
862+ await expect (
863+ handleSessionCommands ( {
864+ req : {
865+ token : 't' ,
866+ session : sessionName ,
867+ command : 'close' ,
868+ positionals : [ 'com.example.app' ] ,
869+ flags : { } ,
870+ } ,
871+ sessionName,
872+ logPath : path . join ( os . tmpdir ( ) , 'daemon.log' ) ,
873+ sessionStore,
874+ invoke : noopInvoke ,
875+ } ) ,
876+ ) . rejects . toBe ( platformCloseError ) ;
877+
878+ // The original AppError code/details/hint are preserved, not collapsed into a
879+ // generic cleanup aggregate.
880+ expect ( platformCloseError . code ) . toBe ( 'DEVICE_UNAVAILABLE' ) ;
881+ expect ( platformCloseError . details ) . toMatchObject ( {
882+ reason : 'device_disconnected' ,
883+ hint : 'Reconnect the device and retry close.' ,
884+ } ) ;
885+ // A failed close is not recorded as `Closed`.
886+ expect ( session . actions . some ( ( action ) => action . command === 'close' ) ) . toBe ( false ) ;
887+ // Subsequent independent cleanup still ran, and the session was still deleted.
888+ expect ( mockStopIosRunnerSession ) . toHaveBeenCalledWith ( session . device . id ) ;
889+ expect ( sessionStore . get ( sessionName ) ) . toBeUndefined ( ) ;
890+ } ) ;
891+
892+ test ( 'targeted close skips platform dispatch and preserves the error when the required pre-close runner stop fails' , async ( ) => {
893+ const sessionStore = makeSessionStore ( ) ;
894+ const sessionName = 'targeted-close-preclose-failure-session' ;
895+ // A physical Apple target (non-simulator) must stop its runner before the
896+ // platform close is dispatched — the runner owns the device connection.
897+ const session = {
898+ ...makeSession ( sessionName , {
899+ platform : 'apple' ,
900+ id : 'physical-device-close' ,
901+ name : 'My iPhone' ,
902+ kind : 'device' ,
903+ booted : true ,
904+ } ) ,
905+ appBundleId : 'com.example.app' ,
906+ } as unknown as SessionState ;
907+ sessionStore . set ( sessionName , session ) ;
908+
909+ const preCloseError = new AppError ( 'RUNNER_UNAVAILABLE' , 'runner stop failed' , {
910+ reason : 'runner_stop_failed' ,
911+ hint : 'Retry once the runner is reachable.' ,
912+ } ) ;
913+ mockStopIosRunnerSession . mockRejectedValue ( preCloseError ) ;
914+
915+ await expect (
916+ handleSessionCommands ( {
917+ req : {
918+ token : 't' ,
919+ session : sessionName ,
920+ command : 'close' ,
921+ positionals : [ 'com.example.app' ] ,
922+ flags : { } ,
923+ } ,
924+ sessionName,
925+ logPath : path . join ( os . tmpdir ( ) , 'daemon.log' ) ,
926+ sessionStore,
927+ invoke : noopInvoke ,
928+ } ) ,
929+ ) . rejects . toBe ( preCloseError ) ;
930+
931+ // The platform close must not be dispatched after a required pre-close stop fails.
932+ expect ( mockDispatchCommand ) . not . toHaveBeenCalled ( ) ;
933+ // The original failure code/details/hint are preserved, not collapsed into a
934+ // generic cleanup aggregate.
935+ expect ( preCloseError . code ) . toBe ( 'RUNNER_UNAVAILABLE' ) ;
936+ expect ( preCloseError . details ) . toMatchObject ( {
937+ reason : 'runner_stop_failed' ,
938+ hint : 'Retry once the runner is reachable.' ,
939+ } ) ;
940+ // A skipped close is not recorded as `Closed`.
941+ expect ( session . actions . some ( ( action ) => action . command === 'close' ) ) . toBe ( false ) ;
942+ // Later independent cleanup still ran (runner stop re-attempted), and the
943+ // session was still deleted.
944+ expect ( mockStopIosRunnerSession . mock . calls . length ) . toBeGreaterThan ( 1 ) ;
945+ expect ( sessionStore . get ( sessionName ) ) . toBeUndefined ( ) ;
946+ } ) ;
0 commit comments