@@ -9,6 +9,9 @@ import type { Interactor, SnapshotOptions } from '@agent-device/contracts/intera
99import type { DeviceInfo } from '@agent-device/kernel/device' ;
1010import { AppError , createRequestCanceledError } from '@agent-device/kernel/errors' ;
1111import { bindAndroidApplicationLifecycle } from './lifecycle.ts' ;
12+ import { closeAndroidApp } from './app-lifecycle.ts' ;
13+ import { withAndroidAdbProvider } from './adb-executor.ts' ;
14+ import { bindAndroidAdbHostStub } from './adb-host.fixtures.ts' ;
1215
1316const device : DeviceInfo = {
1417 platform : 'android' ,
@@ -20,6 +23,7 @@ const device: DeviceInfo = {
2023} ;
2124
2225afterEach ( ( ) => {
26+ vi . useRealTimers ( ) ;
2327 vi . restoreAllMocks ( ) ;
2428} ) ;
2529
@@ -38,6 +42,7 @@ function createLifecycle(
3842 input : Readonly < { stateDir : string ; shutdownTarget : boolean ; signal : AbortSignal } > ,
3943 ) => Promise < void > ;
4044 shutdownTarget ?: ( ) => Promise < unknown > ;
45+ close ?: Interactor [ 'close' ] ;
4146 } > = { } ,
4247) : LifecycleFixture {
4348 const calls : string [ ] = [ ] ;
@@ -48,7 +53,7 @@ function createLifecycle(
4853 calls . push ( `open:${ app } ` ) ;
4954 } ,
5055 openDevice : async ( ) => { } ,
51- close : async ( ) => { } ,
56+ close : params . close ?? ( async ( ) => { } ) ,
5257 setSetting : async ( ) => { } ,
5358 snapshot : async ( options : SnapshotOptions ) => {
5459 calls . push ( `observe:${ options . appBundleId } ` ) ;
@@ -113,6 +118,43 @@ function openInput(overrides: Partial<OpenApplicationInput> = {}): OpenApplicati
113118 } ;
114119}
115120
121+ test ( 'relaunch refuses to open or observe after the Android process close fence expires' , async ( ) => {
122+ vi . useFakeTimers ( ) ;
123+ bindAndroidAdbHostStub ( { } ) ;
124+ const { calls, lifecycle } = createLifecycle ( {
125+ close : async ( app ) => await closeAndroidApp ( device , app ) ,
126+ } ) ;
127+ await withAndroidAdbProvider (
128+ {
129+ exec : async ( args ) => ( {
130+ stdout :
131+ args [ 1 ] === 'pidof'
132+ ? '12345\n'
133+ : args [ 1 ] === 'dumpsys'
134+ ? 'mCurrentFocus=Window{43 u0 com.android.launcher/.Launcher}\n'
135+ : '' ,
136+ stderr : '' ,
137+ exitCode : 0 ,
138+ } ) ,
139+ } ,
140+ { serial : device . id } ,
141+ async ( ) => {
142+ const rejection = expect (
143+ lifecycle . openApplication ( openInput ( { relaunch : true } ) ) ,
144+ ) . rejects . toMatchObject ( {
145+ code : 'COMMAND_FAILED' ,
146+ details : {
147+ reason : 'android-app-close-timeout' ,
148+ phase : 'process' ,
149+ packageName : 'com.example.app' ,
150+ } ,
151+ } ) ;
152+ await Promise . all ( [ rejection , vi . runAllTimersAsync ( ) ] ) ;
153+ } ,
154+ ) ;
155+ expect ( calls ) . toEqual ( [ ] ) ;
156+ } ) ;
157+
116158test ( 'preserves a runtime launch URL duration after the admitted Android follow-up open' , async ( ) => {
117159 const { calls, lifecycle } = createLifecycle ( ) ;
118160 vi . spyOn ( Date , 'now' )
0 commit comments