11import assert from 'node:assert/strict' ;
2- import { test } from 'vitest' ;
2+ import { afterEach , test , vi } from 'vitest' ;
33import type { DeviceLease } from '@agent-device/contracts/device' ;
44import {
55 AppError ,
@@ -15,6 +15,10 @@ import { buildCloudWebDriverBaseCapabilities } from './runtime.ts';
1515
1616const ARN = 'arn:aws:devicefarm:us-west-2:1:session/pending' ;
1717
18+ afterEach ( ( ) => {
19+ vi . restoreAllMocks ( ) ;
20+ } ) ;
21+
1822// Once `create-remote-access-session` answers, the ARN is a billed session that
1923// nothing else will stop. Every way the startup wait can end short of RUNNING
2024// must stop it before the failure surfaces (#1774 ownership rule, one phase
@@ -75,6 +79,40 @@ test('the allocation deadline caps the startup wait below its own default', asyn
7579 assert . deepEqual ( client . stopped , [ ARN ] ) ;
7680} ) ;
7781
82+ // Live iOS real devices needed ~128s to reach RUNNING while the daemon's 300s
83+ // allocation budget still had room, and the standalone 120s default cut them
84+ // off. When the daemon supplies a deadline it is THE bound; the default only
85+ // applies without one. Time is a virtual clock advanced 10s per poll, so this is
86+ // deterministic and fails on the old `min(default, deadline)` logic (which
87+ // throws at 120s, before the 150s RUNNING).
88+ test ( 'the allocation deadline lets startup run past the standalone 120s default' , async ( ) => {
89+ const startedAt = 1_700_000_000_000 ;
90+ let virtualNow = startedAt ;
91+ vi . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => virtualNow ) ;
92+ const client = fakeClient ( { status : 'PENDING' } , ( ) => {
93+ virtualNow += 10_000 ;
94+ if ( virtualNow - startedAt >= 150_000 ) {
95+ client . session . status = 'RUNNING' ;
96+ client . session . endpoints = { appium : 'https://appium.example/wd/hub' } ;
97+ }
98+ } ) ;
99+ const prepare = createAwsDeviceFarmPrepareSession ( {
100+ ...baseOptions ( client ) ,
101+ // The standalone default; a daemon-supplied deadline must override it.
102+ startupTimeoutMs : 120_000 ,
103+ pollIntervalMs : 1 ,
104+ } ) ;
105+
106+ const prepared = await prepare ( {
107+ lease : makeLease ( ) ,
108+ req : { deadline : startedAt + 300_000 } ,
109+ base : baseSession ( ) ,
110+ } ) ;
111+ assert . equal ( prepared . providerSessionId , ARN ) ;
112+ assert . ok ( virtualNow - startedAt >= 150_000 , 'RUNNING must have been observed after 120s' ) ;
113+ assert . deepEqual ( client . stopped , [ ] ) ;
114+ } ) ;
115+
78116test ( 'a session that reaches RUNNING is handed on and not stopped' , async ( ) => {
79117 const client = fakeClient ( {
80118 status : 'RUNNING' ,
@@ -91,10 +129,11 @@ test('a session that reaches RUNNING is handed on and not stopped', async () =>
91129function fakeClient (
92130 session : Partial < AwsDeviceFarmRemoteAccessSession > ,
93131 onPoll ?: ( ) => void ,
94- ) : AwsDeviceFarmClient & { stopped : string [ ] } {
132+ ) : AwsDeviceFarmClient & { stopped : string [ ] ; session : Partial < AwsDeviceFarmRemoteAccessSession > } {
95133 const stopped : string [ ] = [ ] ;
96134 return {
97135 stopped,
136+ session,
98137 createRemoteAccessSession : async ( ) => ( { arn : ARN , status : 'PENDING' } ) ,
99138 getRemoteAccessSession : async ( arn ) => {
100139 onPoll ?.( ) ;
0 commit comments