Skip to content

Commit 5108ef2

Browse files
committed
fix(ios): bound launch confirmation presentation probes
1 parent 7c40b78 commit 5108ef2

5 files changed

Lines changed: 124 additions & 25 deletions

File tree

‎packages/platform-apple/src/launch-confirmation.test.ts‎

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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';
2021
import { 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

306308
function 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+
409489
test('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

458541
test('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
});

‎packages/platform-apple/src/launch-confirmation.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { resolveIosSimulatorDeepLinkBundleId } from './core/app-resolution.ts';
88

99
export const LAUNCH_CONFIRMATION_FOREIGN_APP_REASON = 'launch_confirmation_foreign_app';
1010

11+
/** Short presentation window for the optional launch-confirmation probe. */
12+
const LAUNCH_CONFIRMATION_PRESENTATION_TIMEOUT_MS = 3_000;
13+
1114
/**
1215
* How one launch-confirmation answer attempt ended. `accepted` verifies dismissal; an unreadable
1316
* `alert-accept` has an unknown outcome. `alert-still-present` proves the recognized prompt remains
@@ -49,7 +52,9 @@ export type LaunchConfirmationTarget = Readonly<{ url: string; appBundleId: stri
4952
/** The device reads and the answer one launch confirmation needs, bound to the session app. */
5053
export type LaunchConfirmationPort = Readonly<{
5154
appBundleId: string;
52-
/** The alert after its bounded presentation wait, or `undefined` when none appears. */
55+
/** Initial presentation wait, or `undefined` when no alert appears within its window. */
56+
awaitAlert(): Promise<Record<string, unknown> | undefined>;
57+
/** One presence read after an attempted accept. */
5358
readAlert(): Promise<Record<string, unknown> | undefined>;
5459
acceptAlert(): Promise<unknown>;
5560
/** The installed app that owns the launch URL's scheme, when exactly one does. */
@@ -66,7 +71,7 @@ export type LaunchConfirmationPort = Readonly<{
6671
export async function answerLaunchConfirmation(
6772
port: LaunchConfirmationPort,
6873
): Promise<LaunchConfirmationAttempt> {
69-
const read = await readAlert(port);
74+
const read = await readAlert(() => port.awaitAlert());
7075
if ('outcome' in read) return read;
7176
const alert = read.alert;
7277
if (!alert) return { outcome: 'absent' };
@@ -106,7 +111,7 @@ async function verifyFailedAcceptance(
106111
original: Record<string, unknown>,
107112
error: unknown,
108113
): Promise<LaunchConfirmationAttempt> {
109-
const read = await readAlert(port);
114+
const read = await readAlert(() => port.readAlert());
110115
if ('outcome' in read) return unreadable('alert-accept')(error);
111116
if (
112117
read.alert &&
@@ -120,9 +125,9 @@ async function verifyFailedAcceptance(
120125
}
121126

122127
async function readAlert(
123-
port: LaunchConfirmationPort,
128+
read: LaunchConfirmationPort['readAlert'],
124129
): Promise<Readonly<{ alert: Record<string, unknown> | undefined }> | LaunchConfirmationAttempt> {
125-
return await port.readAlert().then((alert) => ({ alert }), unreadable('alert-read'));
130+
return await read().then((alert) => ({ alert }), unreadable('alert-read'));
126131
}
127132

128133
async function resolveUrlOwner(
@@ -188,18 +193,23 @@ export async function answerSimulatorLaunchConfirmation(
188193
export function createLaunchConfirmationPort(
189194
device: DeviceInfo,
190195
{ url, appBundleId }: LaunchConfirmationTarget,
191-
interactor: Pick<Interactor, 'awaitAlert' | 'acceptAlert'>,
196+
interactor: Pick<Interactor, 'awaitAlert' | 'readAlert' | 'acceptAlert'>,
192197
signal: AbortSignal,
193198
): LaunchConfirmationPort {
194199
const awaitAlert = interactor.awaitAlert?.bind(interactor);
200+
const readAlert = interactor.readAlert?.bind(interactor);
195201
const acceptAlert = interactor.acceptAlert?.bind(interactor);
196-
if (!awaitAlert || !acceptAlert) {
197-
throw invalidRuntimeContract('Apple interactor has no alert wait or accept leg');
202+
if (!awaitAlert || !readAlert || !acceptAlert) {
203+
throw invalidRuntimeContract('Apple interactor has no alert wait, read or accept leg');
198204
}
199205
const target = { appBundleId, surface: 'app' } as const;
200206
return Object.freeze({
201207
appBundleId,
202-
readAlert: async () => await alertIfPresent(awaitAlert(target)),
208+
awaitAlert: async () =>
209+
await alertIfPresent(
210+
awaitAlert({ ...target, timeoutMs: LAUNCH_CONFIRMATION_PRESENTATION_TIMEOUT_MS }),
211+
),
212+
readAlert: async () => await alertIfPresent(readAlert(target)),
203213
acceptAlert: async () => await acceptAlert(target),
204214
resolveUrlOwner: async () =>
205215
await resolveIosSimulatorDeepLinkBundleId(device, url, {

‎packages/platform-apple/src/lifecycle.fixtures.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ export function launchUrlSimulator(
148148
open: vi.fn(async (_app: string, options?: { url?: string }) => {
149149
events.push(options?.url ? `open ${options.url}` : 'open');
150150
}),
151+
readAlert: vi.fn(async () => {
152+
events.push('alert get');
153+
return await readAlert();
154+
}),
151155
awaitAlert: vi.fn(async () => {
152156
events.push('alert wait');
153157
return await readAlert();

‎packages/platform-apple/src/lifecycle.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ test('an accept that dies with the runner session hands the URL over again and r
780780
'observe unobservable',
781781
'alert wait',
782782
'alert accept',
783-
'alert wait',
783+
'alert get',
784784
'observe unobservable',
785785
'open',
786786
'alert wait',
@@ -813,7 +813,8 @@ test.each([
813813
details: { reason: 'launch_confirmation_unanswered', appBundleId: 'com.example.app' },
814814
});
815815
expect(interactor.acceptAlert).toHaveBeenCalledOnce();
816-
expect(interactor.awaitAlert).toHaveBeenCalledTimes(2);
816+
expect(interactor.awaitAlert).toHaveBeenCalledOnce();
817+
expect(interactor.readAlert).toHaveBeenCalledOnce();
817818
expect(events.filter((event) => event === 'open' || event === `open ${LAUNCH_URL}`)).toEqual([
818819
`open ${LAUNCH_URL}`,
819820
]);
@@ -860,7 +861,8 @@ test.each([
860861
expect(outcome.launchConfirmation).toBeUndefined();
861862
expect(outcome.timing.postOpenObservation).toBe('unobservable');
862863
expect(interactor.acceptAlert).toHaveBeenCalledOnce();
863-
expect(interactor.awaitAlert).toHaveBeenCalledTimes(2);
864+
expect(interactor.awaitAlert).toHaveBeenCalledOnce();
865+
expect(interactor.readAlert).toHaveBeenCalledOnce();
864866
expect(events.filter((event) => event === 'open' || event === `open ${LAUNCH_URL}`)).toEqual([
865867
`open ${LAUNCH_URL}`,
866868
]);

‎packages/platform-apple/src/open-policy.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export function releaseSpeculativeRunner(
111111
* A launch the bridge cannot read as up may be a launch URL SpringBoard holds behind a
112112
* confirmation, so any verdict short of `observable` — including a target discovery that failed on
113113
* its own deadline — reads the sheet. Only a verdict that proves the app up skips the read, and a
114-
* sheet that is not there costs one bounded alert wait.
114+
* sheet that is not there costs one 3-second presentation window.
115115
*
116116
* An accept is the only answer that changes the device, so only its outcome is re-observed: the URL
117117
* is in flight and this open is the only one that knows whether it landed. When the app is then
@@ -121,7 +121,7 @@ export function releaseSpeculativeRunner(
121121
* session whose every later command then fails `app is not running`. A recognized prompt still
122122
* visible after a failed accept also fails the open, even if the app is running. A missing or
123123
* unrecognized prompt, or an unresolved URL owner, leaves the launch unchanged. An unresolved owner
124-
* spends both the bounded alert wait and the bounded URL-owner lookup.
124+
* spends both the 3-second presentation window and the bounded URL-owner lookup.
125125
*/
126126
export async function settleAppleOpen(
127127
host: Pick<PlatformRuntimeHost, 'clock'>,
@@ -177,7 +177,7 @@ export async function settleAppleOpen(
177177
/**
178178
* Reads and answers the confirmation the launch may be held behind. Only an accept changes the
179179
* device: a read that finds no prompt, or a prompt this open must not answer, leaves the launch
180-
* exactly as the first verdict described it, at the cost of one bounded alert wait. Once an accept has
180+
* exactly as the first verdict described it, at the cost of one 3-second presentation window. Once an accept has
181181
* been attempted the launch URL is in flight and only this open knows whether it landed — an accept
182182
* that reported failure died with the runner session that raised it, and one that reported success
183183
* can still leave the device having dropped a held URL. Either way, an app then proven to have no

0 commit comments

Comments
 (0)