@@ -16,7 +16,10 @@ import type { SessionState } from '../../../core/session-state.ts';
1616import { daemonPlatformServicesFixture } from '../../../__tests__/support/platform-services-fixture.ts' ;
1717import { fakeCreateCommandSurface } from '../../../__tests__/support/command-surface-fake.ts' ;
1818import { platformResourceCleanupFixture } from '../../../__tests__/support/platform-resource-cleanup-fixture.ts' ;
19- import { dispatchApplicationLifecycleEffect } from '../../../__tests__/support/application-lifecycle-runtime-fixture.ts' ;
19+ import {
20+ clearRuntimeHintsFixture ,
21+ dispatchApplicationLifecycleEffect ,
22+ } from '../../../__tests__/support/application-lifecycle-runtime-fixture.ts' ;
2023import {
2124 handleSessionCommands ,
2225 mockBindDeviceRuntime ,
@@ -142,7 +145,7 @@ test.each([
142145 device,
143146 shutdownRequested : false ,
144147 hasScreenRecording : false ,
145- hasLease : false ,
148+ leaseWillEnd : false ,
146149 } ) ;
147150 expect ( finalizations ) . toEqual ( [
148151 expect . objectContaining ( { retainRunner : retainExecutionHost , shutdownTarget : false } ) ,
@@ -204,42 +207,118 @@ test('close hands the session app and surface to the platform finalization', asy
204207 expect ( sessionStore . get ( sessionName ) ) . toBeUndefined ( ) ;
205208} ) ;
206209
207- test ( 'close of a leased session tells the platform that a lease holds the device' , async ( ) => {
210+ test . each ( [ false , true ] ) (
211+ 'close reports whether a lease with retainOnClose=%s will end' ,
212+ async ( retainOnClose ) => {
213+ const sessionStore = makeSessionStore ( ) ;
214+ const sessionName = 'ios-leased-simulator-session' ;
215+ const leaseRegistry = new LeaseRegistry ( ) ;
216+ const lease = leaseRegistry . allocateLease ( {
217+ tenantId : 'tenant-a' ,
218+ runId : 'run-1' ,
219+ leaseBackend : 'ios-simulator' ,
220+ deviceKey : 'ios:sim-1' ,
221+ clientId : 'client-a' ,
222+ retainOnClose,
223+ } ) ;
224+ publishSession ( sessionStore , sessionName , IOS_SIMULATOR , {
225+ lease : {
226+ leaseId : lease . leaseId ,
227+ tenantId : lease . tenantId ,
228+ runId : lease . runId ,
229+ leaseBackend : lease . backend ,
230+ deviceKey : lease . deviceKey ,
231+ clientId : lease . clientId ,
232+ expiresAt : lease . expiresAt ,
233+ } ,
234+ } ) ;
235+
236+ const { response, finalizations, platformResourceCleanup } = await closeSession ( {
237+ sessionStore,
238+ sessionName,
239+ retainExecutionHost : retainOnClose ,
240+ leaseRegistry,
241+ } ) ;
242+
243+ expect ( response ?. ok ) . toBe ( true ) ;
244+ expect ( platformResourceCleanup . retainExecutionHostAfterClose ) . toHaveBeenCalledWith (
245+ expect . objectContaining ( { leaseWillEnd : ! retainOnClose } ) ,
246+ ) ;
247+ expect ( finalizations ) . toEqual ( [ expect . objectContaining ( { retainRunner : retainOnClose } ) ] ) ;
248+ expect ( sessionStore . get ( sessionName ) ) . toBeUndefined ( ) ;
249+ } ,
250+ ) ;
251+
252+ test ( 'a lease scope mismatch still closes the app and runner before returning a retriable release error' , async ( ) => {
208253 const sessionStore = makeSessionStore ( ) ;
209- const sessionName = 'ios-leased-simulator-session ' ;
254+ const sessionName = 'ios-close-mismatched-lease ' ;
210255 const leaseRegistry = new LeaseRegistry ( ) ;
211256 const lease = leaseRegistry . allocateLease ( {
212257 tenantId : 'tenant-a' ,
213258 runId : 'run-1' ,
214259 leaseBackend : 'ios-simulator' ,
215260 deviceKey : 'ios:sim-1' ,
216261 clientId : 'client-a' ,
262+ retainOnClose : true ,
217263 } ) ;
218264 publishSession ( sessionStore , sessionName , IOS_SIMULATOR , {
265+ appBundleId : 'com.example.app' ,
219266 lease : {
220267 leaseId : lease . leaseId ,
221268 tenantId : lease . tenantId ,
222269 runId : lease . runId ,
223270 leaseBackend : lease . backend ,
224271 deviceKey : lease . deviceKey ,
225- clientId : lease . clientId ,
272+ clientId : 'client-b' ,
226273 expiresAt : lease . expiresAt ,
227274 } ,
228275 } ) ;
276+ sessionStore . setRuntimeHints ( sessionName , { platform : 'ios' , metroHost : '127.0.0.1' } ) ;
229277
230278 const { response, finalizations, platformResourceCleanup } = await closeSession ( {
231279 sessionStore,
232280 sessionName,
233281 retainExecutionHost : false ,
282+ positionals : [ 'com.example.app' ] ,
234283 leaseRegistry,
235284 } ) ;
236285
237- expect ( response ?. ok ) . toBe ( true ) ;
286+ expect ( platformResourceCleanup . stopSnapshotHelper ) . toHaveBeenCalledWith ( IOS_SIMULATOR ) ;
287+ expect ( mockDispatch ) . toHaveBeenCalledWith (
288+ IOS_SIMULATOR ,
289+ 'close' ,
290+ [ 'com.example.app' ] ,
291+ undefined ,
292+ expect . any ( Object ) ,
293+ ) ;
294+ expect ( clearRuntimeHintsFixture ) . toHaveBeenCalledWith ( {
295+ device : IOS_SIMULATOR ,
296+ appId : 'com.example.app' ,
297+ values : expect . objectContaining ( { metroHost : '127.0.0.1' } ) ,
298+ } ) ;
238299 expect ( platformResourceCleanup . retainExecutionHostAfterClose ) . toHaveBeenCalledWith (
239- expect . objectContaining ( { hasLease : true } ) ,
300+ expect . objectContaining ( { leaseWillEnd : true } ) ,
240301 ) ;
241302 expect ( finalizations ) . toEqual ( [ expect . objectContaining ( { retainRunner : false } ) ] ) ;
242- expect ( sessionStore . get ( sessionName ) ) . toBeUndefined ( ) ;
303+ expect ( response ) . toMatchObject ( {
304+ ok : false ,
305+ error : {
306+ code : 'UNAUTHORIZED' ,
307+ details : { reason : 'LEASE_SCOPE_MISMATCH' , session : sessionName } ,
308+ retriable : true ,
309+ } ,
310+ } ) ;
311+ expect ( sessionStore . get ( sessionName ) ?. lease ?. leaseId ) . toBe ( lease . leaseId ) ;
312+ expect (
313+ leaseRegistry . getLease ( {
314+ leaseId : lease . leaseId ,
315+ tenantId : lease . tenantId ,
316+ runId : lease . runId ,
317+ leaseBackend : lease . backend ,
318+ deviceKey : lease . deviceKey ,
319+ clientId : lease . clientId ,
320+ } ) ,
321+ ) . toMatchObject ( { retainOnClose : true } ) ;
243322} ) ;
244323
245324test ( 'an app-only close terminates the app without finalizing or ending the session' , async ( ) => {
0 commit comments