@@ -4,7 +4,10 @@ import type { Interactor } from '@agent-device/contracts/interactor-types';
44import type { DeviceInfo } from '@agent-device/kernel/device' ;
55import { emitDiagnostic } from '@agent-device/host-kit/diagnostics' ;
66import { AppError } from '@agent-device/kernel/errors' ;
7+ import type { ExecOptions , ExecResult } from '@agent-device/host-kit/command' ;
78import { resolveIosSimulatorDeepLinkBundleId } from './core/app-resolution.ts' ;
9+ import { IOS_APP_LAUNCH_TIMEOUT_MS } from './core/config.ts' ;
10+ import { type AppleToolProvider , withAppleToolProvider } from './core/tool-provider.ts' ;
811import {
912 answerLaunchConfirmation ,
1013 answerSimulatorLaunchConfirmation ,
@@ -18,9 +21,13 @@ vi.mock('@agent-device/host-kit/diagnostics', async (importOriginal) => {
1821 return { ...actual , emitDiagnostic : vi . fn ( ) } ;
1922} ) ;
2023
21- vi . mock ( './core/app-resolution.ts' , ( ) => ( {
22- resolveIosSimulatorDeepLinkBundleId : vi . fn ( async ( ) => 'com.example.app' ) ,
23- } ) ) ;
24+ vi . mock ( './core/app-resolution.ts' , async ( importOriginal ) => {
25+ const actual = await importOriginal < typeof import ( './core/app-resolution.ts' ) > ( ) ;
26+ return {
27+ ...actual ,
28+ resolveIosSimulatorDeepLinkBundleId : vi . fn ( actual . resolveIosSimulatorDeepLinkBundleId ) ,
29+ } ;
30+ } ) ;
2431
2532const simulator : DeviceInfo = {
2633 platform : 'apple' ,
@@ -186,40 +193,93 @@ test.each([
186193 await expect ( answerLaunchConfirmation ( device ) ) . resolves . toBeUndefined ( ) ;
187194} ) ;
188195
189- test ( 'the answer returns only after its accept settled, however long the accept takes' , async ( ) => {
196+ /**
197+ * A CoreSimulator that never answers: each spawn settles only when its exec timeout fires or its
198+ * signal aborts, the way `exec.ts` ends a wedged child.
199+ */
200+ function wedgedCoreSimulator ( ) : AppleToolProvider & { spawns : ( ) => number } {
201+ let spawns = 0 ;
202+ const hang = async ( options ?: ExecOptions ) : Promise < ExecResult > =>
203+ await new Promise ( ( _resolve , reject ) => {
204+ spawns += 1 ;
205+ if ( options ?. timeoutMs ) {
206+ setTimeout ( ( ) => reject ( spawnTimeout ( ) ) , options . timeoutMs ) ;
207+ }
208+ options ?. signal ?. addEventListener ( 'abort' , ( ) => reject ( options . signal ?. reason ) , {
209+ once : true ,
210+ } ) ;
211+ } ) ;
212+ return {
213+ whichCommand : async ( ) => true ,
214+ runCommand : async ( _cmd , _args , options ) => await hang ( options ) ,
215+ simctl : { run : async ( _args , options ) => await hang ( options ) } ,
216+ devicectl : { run : async ( _args , options ) => await hang ( options ) } ,
217+ spawns : ( ) => spawns ,
218+ } ;
219+ }
220+
221+ function confirmationRunner ( ) {
222+ const acceptAlert = vi . fn ( async ( ) => ( { } ) ) ;
223+ const interactor = { readAlert : async ( ) => CONFIRMATION , acceptAlert } as unknown as Interactor ;
224+ return { interactor, acceptAlert } ;
225+ }
226+
227+ test ( 'a hung URL owner lookup leaves the open unanswered within the launch budget' , async ( ) => {
190228 vi . useFakeTimers ( ) ;
191229 try {
192- const acceptMs = 70_000 ;
193- const { port : device , acceptAlert } = port ( async ( ) => CONFIRMATION , {
194- acceptAlert : async ( ) =>
195- await new Promise ( ( resolve ) => {
196- setTimeout ( ( ) => resolve ( { } ) , acceptMs ) ;
197- } ) ,
198- } ) ;
230+ const { interactor, acceptAlert } = confirmationRunner ( ) ;
199231 let answered : unknown = 'pending' ;
200- const answer = answerLaunchConfirmation ( device ) . then ( ( value ) => ( answered = value ) ) ;
201-
202- await vi . advanceTimersByTimeAsync ( acceptMs - 1 ) ;
203- expect ( acceptAlert ) . toHaveBeenCalledOnce ( ) ;
204- expect ( answered ) . toBe ( 'pending' ) ;
232+ const answer = withAppleToolProvider ( wedgedCoreSimulator ( ) , async ( ) =>
233+ answerSimulatorLaunchConfirmation (
234+ simulator ,
235+ { url : 'example://automation' , appBundleId : 'com.example.app' } ,
236+ Promise . resolve ( interactor ) ,
237+ new AbortController ( ) . signal ,
238+ ) . then ( ( value ) => ( answered = value ) ) ,
239+ ) ;
205240
206- await vi . advanceTimersByTimeAsync ( 1 ) ;
241+ await vi . advanceTimersByTimeAsync ( IOS_APP_LAUNCH_TIMEOUT_MS - 1 ) ;
242+ expect ( answered ) . toBeUndefined ( ) ;
207243 await answer ;
208- expect ( answered ) . toBe ( 'accepted' ) ;
244+ expect ( acceptAlert ) . not . toHaveBeenCalled ( ) ;
209245 } finally {
210246 vi . useRealTimers ( ) ;
211247 }
212248} ) ;
213249
250+ test ( 'the open aborting ends the URL owner lookup' , async ( ) => {
251+ const { interactor, acceptAlert } = confirmationRunner ( ) ;
252+ const open = new AbortController ( ) ;
253+ const coreSimulator = wedgedCoreSimulator ( ) ;
254+ const answer = withAppleToolProvider ( coreSimulator , async ( ) =>
255+ answerSimulatorLaunchConfirmation (
256+ simulator ,
257+ { url : 'example://automation' , appBundleId : 'com.example.app' } ,
258+ Promise . resolve ( interactor ) ,
259+ open . signal ,
260+ ) ,
261+ ) ;
262+
263+ await vi . waitFor ( ( ) => expect ( coreSimulator . spawns ( ) ) . toBe ( 1 ) ) ;
264+ open . abort ( new AppError ( 'COMMAND_FAILED' , 'request canceled' ) ) ;
265+
266+ await expect ( answer ) . resolves . toBeUndefined ( ) ;
267+ expect ( acceptAlert ) . not . toHaveBeenCalled ( ) ;
268+ } ) ;
269+
214270test ( 'the port reads and accepts the alert for the session app and asks the scheme owner' , async ( ) => {
215271 const readAlert = vi . fn ( async ( ) => CONFIRMATION ) ;
216272 const acceptAlert = vi . fn ( async ( ) => ( { } ) ) ;
217273 const interactor = { readAlert, acceptAlert } as unknown as Interactor ;
218274
275+ const signal = new AbortController ( ) . signal ;
276+ vi . mocked ( resolveIosSimulatorDeepLinkBundleId ) . mockResolvedValueOnce ( 'com.example.app' ) ;
277+
219278 const device = createLaunchConfirmationPort (
220279 simulator ,
221280 { url : 'example://automation' , appBundleId : 'com.example.app' } ,
222281 interactor ,
282+ signal ,
223283 ) ;
224284 await device . readAlert ( ) ;
225285 await device . acceptAlert ( ) ;
@@ -231,6 +291,7 @@ test('the port reads and accepts the alert for the session app and asks the sche
231291 expect ( resolveIosSimulatorDeepLinkBundleId ) . toHaveBeenCalledWith (
232292 simulator ,
233293 'example://automation' ,
294+ { timeoutMs : expect . any ( Number ) , signal } ,
234295 ) ;
235296} ) ;
236297
@@ -248,6 +309,7 @@ test('the port reads a typed absence as no alert and keeps any other failure', a
248309 simulator ,
249310 { url : 'example://automation' , appBundleId : 'com.example.app' } ,
250311 interactor ,
312+ new AbortController ( ) . signal ,
251313 ) ;
252314
253315 await expect ( device . readAlert ( ) ) . resolves . toBeUndefined ( ) ;
@@ -260,6 +322,7 @@ test('a runner that cannot be resolved leaves the open unanswered', async () =>
260322 simulator ,
261323 { url : 'example://automation' , appBundleId : 'com.example.app' } ,
262324 Promise . reject ( new AppError ( 'COMMAND_FAILED' , 'xcrun timed out' , { timeoutMs : 10_000 } ) ) ,
325+ new AbortController ( ) . signal ,
263326 ) ,
264327 ) . resolves . toBeUndefined ( ) ;
265328} ) ;
0 commit comments