@@ -15,6 +15,7 @@ import {
1515 createLaunchConfirmationPort ,
1616 LAUNCH_CONFIRMATION_FOREIGN_APP_REASON ,
1717 type LaunchConfirmationPort ,
18+ type LaunchConfirmationAttempt ,
1819 URL_OWNER_LOOKUP_TIMEOUT_MS ,
1920} from './launch-confirmation.ts' ;
2021import { alertNotFound , CONFIRMATION } from './launch-confirmation.fixtures.ts' ;
@@ -60,6 +61,7 @@ function port(
6061 const resolveUrlOwner = vi . fn ( legs . resolveUrlOwner ?? ( async ( ) => 'com.example.app' ) ) ;
6162 const value : LaunchConfirmationPort = {
6263 appBundleId : 'com.example.app' ,
64+ awaitAlert : vi . fn ( readAlert ) ,
6365 readAlert : vi . fn ( readAlert ) ,
6466 acceptAlert,
6567 resolveUrlOwner,
@@ -80,7 +82,7 @@ test.each([
8082 } ) ) ;
8183
8284 await expect ( answerLaunchConfirmation ( device ) ) . resolves . toEqual ( { outcome : 'accepted' } ) ;
83- expect ( device . readAlert ) . toHaveBeenCalledOnce ( ) ;
85+ expect ( device . awaitAlert ) . toHaveBeenCalledOnce ( ) ;
8486 expect ( acceptAlert ) . toHaveBeenCalledOnce ( ) ;
8587 } ,
8688) ;
@@ -106,7 +108,7 @@ test('no alert costs one read and answers nothing', async () => {
106108 const { port : device , acceptAlert, resolveUrlOwner } = port ( async ( ) => undefined ) ;
107109
108110 await expect ( answerLaunchConfirmation ( device ) ) . resolves . toEqual ( { outcome : 'absent' } ) ;
109- expect ( device . readAlert ) . toHaveBeenCalledOnce ( ) ;
111+ expect ( device . awaitAlert ) . toHaveBeenCalledOnce ( ) ;
110112 expect ( resolveUrlOwner ) . not . toHaveBeenCalled ( ) ;
111113 expect ( acceptAlert ) . not . toHaveBeenCalled ( ) ;
112114} ) ;
@@ -305,7 +307,11 @@ test.each([
305307
306308function confirmationRunner ( ) {
307309 const acceptAlert = vi . fn ( async ( ) => ( { } ) ) ;
308- const interactor = { awaitAlert : async ( ) => CONFIRMATION , acceptAlert } as unknown as Interactor ;
310+ const interactor = {
311+ awaitAlert : async ( ) => CONFIRMATION ,
312+ readAlert : async ( ) => CONFIRMATION ,
313+ acceptAlert,
314+ } as unknown as Interactor ;
309315 return { interactor, acceptAlert } ;
310316}
311317
@@ -373,7 +379,7 @@ test('the launch confirmation waits for a system prompt that is still appearing'
373379 interactor ,
374380 new AbortController ( ) . signal ,
375381 ) ;
376- const pending = device . readAlert ( ) ;
382+ const pending = device . awaitAlert ( ) ;
377383 await vi . advanceTimersByTimeAsync ( 500 ) ;
378384 await expect ( pending ) . resolves . toEqual ( CONFIRMATION ) ;
379385 expect ( runner ) . toHaveBeenCalledTimes ( 2 ) ;
@@ -393,6 +399,7 @@ test('an expired confirmation wait retains typed absence instead of an unreadabl
393399 { url : 'example://automation' , appBundleId : 'com.example.app' } ,
394400 {
395401 awaitAlert : ( options ) => awaitAppleAlert ( simulator , { } , options ) ,
402+ readAlert : ( options ) => readAppleAlert ( simulator , { } , options ) ,
396403 acceptAlert : vi . fn ( ) ,
397404 } ,
398405 new AbortController ( ) . signal ,
@@ -406,10 +413,84 @@ test('an expired confirmation wait retains typed absence instead of an unreadabl
406413 }
407414} ) ;
408415
416+ test ( 'a failed accept checks presence once without waiting for the dismissed prompt' , async ( ) => {
417+ vi . useFakeTimers ( ) ;
418+ const runner = vi . mocked ( runAppleRunnerCommand ) ;
419+ runner . mockResolvedValueOnce ( CONFIRMATION ) . mockRejectedValue ( alertNotFound ( ) ) ;
420+ vi . mocked ( resolveIosSimulatorDeepLinkBundleId ) . mockResolvedValueOnce ( 'com.example.app' ) ;
421+ try {
422+ const readAlert = vi . fn < NonNullable < Interactor [ 'readAlert' ] > > ( ( options ) =>
423+ readAppleAlert ( simulator , { } , options ) ,
424+ ) ;
425+ const awaitAlert = vi . fn < NonNullable < Interactor [ 'awaitAlert' ] > > ( ( options ) =>
426+ awaitAppleAlert ( simulator , { } , options ) ,
427+ ) ;
428+ const interactor = {
429+ readAlert,
430+ awaitAlert,
431+ acceptAlert : vi . fn ( async ( ) => {
432+ throw spawnTimeout ( ) ;
433+ } ) ,
434+ } satisfies Pick < Interactor , 'readAlert' | 'awaitAlert' | 'acceptAlert' > ;
435+ const device = createLaunchConfirmationPort (
436+ simulator ,
437+ { url : 'example://automation' , appBundleId : 'com.example.app' } ,
438+ interactor ,
439+ new AbortController ( ) . signal ,
440+ ) ;
441+ let settled : LaunchConfirmationAttempt | undefined ;
442+ const pending = answerLaunchConfirmation ( device ) . then ( ( value ) => {
443+ settled = value ;
444+ } ) ;
445+ await vi . advanceTimersByTimeAsync ( 0 ) ;
446+ expect ( settled ) . toEqual ( { outcome : 'unreadable' , step : 'alert-accept' } ) ;
447+ expect ( awaitAlert ) . toHaveBeenCalledOnce ( ) ;
448+ expect ( readAlert ) . toHaveBeenCalledOnce ( ) ;
449+ expect ( runner ) . toHaveBeenCalledTimes ( 2 ) ;
450+ await pending ;
451+ } finally {
452+ await vi . advanceTimersByTimeAsync ( 11_000 ) ;
453+ runner . mockReset ( ) ;
454+ vi . useRealTimers ( ) ;
455+ }
456+ } ) ;
457+
458+ test ( 'a no-prompt presentation probe completes within a few seconds' , async ( ) => {
459+ vi . useFakeTimers ( ) ;
460+ const runner = vi . mocked ( runAppleRunnerCommand ) ;
461+ runner . mockRejectedValue ( alertNotFound ( ) ) ;
462+ try {
463+ const interactor = {
464+ readAlert : ( options ) => readAppleAlert ( simulator , { } , options ) ,
465+ awaitAlert : ( options ) => awaitAppleAlert ( simulator , { } , options ) ,
466+ acceptAlert : vi . fn ( ) ,
467+ } satisfies Pick < Interactor , 'readAlert' | 'awaitAlert' | 'acceptAlert' > ;
468+ const device = createLaunchConfirmationPort (
469+ simulator ,
470+ { url : 'example://automation' , appBundleId : 'com.example.app' } ,
471+ interactor ,
472+ new AbortController ( ) . signal ,
473+ ) ;
474+ let settled : LaunchConfirmationAttempt | undefined ;
475+ const pending = answerLaunchConfirmation ( device ) . then ( ( value ) => {
476+ settled = value ;
477+ } ) ;
478+ await vi . advanceTimersByTimeAsync ( 3_500 ) ;
479+ expect ( settled ) . toEqual ( { outcome : 'absent' } ) ;
480+ expect ( runner . mock . calls [ 0 ] ?. [ 1 ] ) . toMatchObject ( { timeoutMs : 3_000 } ) ;
481+ await pending ;
482+ } finally {
483+ await vi . advanceTimersByTimeAsync ( 11_000 ) ;
484+ runner . mockReset ( ) ;
485+ vi . useRealTimers ( ) ;
486+ }
487+ } ) ;
488+
409489test ( 'the port reads and accepts the alert for the session app and asks the scheme owner' , async ( ) => {
410490 const awaitAlert = vi . fn ( async ( ) => CONFIRMATION ) ;
411491 const acceptAlert = vi . fn ( async ( ) => ( { } ) ) ;
412- const interactor = { awaitAlert, acceptAlert } ;
492+ const readAlert = vi . fn ( async ( ) => CONFIRMATION ) ;
493+ const interactor = { awaitAlert, readAlert, acceptAlert } ;
413494
414495 const signal = new AbortController ( ) . signal ;
415496 vi . mocked ( resolveIosSimulatorDeepLinkBundleId ) . mockResolvedValueOnce ( 'com.example.app' ) ;
@@ -420,11 +501,13 @@ test('the port reads and accepts the alert for the session app and asks the sche
420501 interactor ,
421502 signal ,
422503 ) ;
504+ await device . awaitAlert ( ) ;
423505 await device . readAlert ( ) ;
424506 await device . acceptAlert ( ) ;
425507
426508 const target = { appBundleId : 'com.example.app' , surface : 'app' } ;
427- expect ( awaitAlert ) . toHaveBeenCalledWith ( target ) ;
509+ expect ( awaitAlert ) . toHaveBeenCalledWith ( { ...target , timeoutMs : 3_000 } ) ;
510+ expect ( readAlert ) . toHaveBeenCalledWith ( target ) ;
428511 expect ( acceptAlert ) . toHaveBeenCalledWith ( target ) ;
429512 await expect ( device . resolveUrlOwner ( ) ) . resolves . toBe ( 'com.example.app' ) ;
430513 expect ( resolveIosSimulatorDeepLinkBundleId ) . toHaveBeenCalledWith (
@@ -442,7 +525,7 @@ test('the port reads a typed absence as no alert and keeps any other failure', a
442525 . fn < ( ) => Promise < Record < string , unknown > > > ( )
443526 . mockRejectedValueOnce ( alertNotFound ( ) )
444527 . mockRejectedValueOnce ( runnerDown ) ;
445- const interactor = { awaitAlert, acceptAlert : vi . fn ( ) } ;
528+ const interactor = { awaitAlert, readAlert : vi . fn ( ) , acceptAlert : vi . fn ( ) } ;
446529
447530 const device = createLaunchConfirmationPort (
448531 simulator ,
@@ -451,8 +534,8 @@ test('the port reads a typed absence as no alert and keeps any other failure', a
451534 new AbortController ( ) . signal ,
452535 ) ;
453536
454- await expect ( device . readAlert ( ) ) . resolves . toBeUndefined ( ) ;
455- await expect ( device . readAlert ( ) ) . rejects . toBe ( runnerDown ) ;
537+ await expect ( device . awaitAlert ( ) ) . resolves . toBeUndefined ( ) ;
538+ await expect ( device . awaitAlert ( ) ) . rejects . toBe ( runnerDown ) ;
456539} ) ;
457540
458541test ( 'a runner that cannot be resolved reports an unreadable attempt' , async ( ) => {
@@ -493,6 +576,6 @@ test('a matching launch title with other buttons is left unanswered on the first
493576 outcome : 'unanswered' ,
494577 reason : 'alert-unrecognized' ,
495578 } ) ;
496- expect ( device . readAlert ) . toHaveBeenCalledOnce ( ) ;
579+ expect ( device . awaitAlert ) . toHaveBeenCalledOnce ( ) ;
497580 expect ( acceptAlert ) . not . toHaveBeenCalled ( ) ;
498581} ) ;
0 commit comments