|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import test from 'node:test'; |
| 3 | + |
| 4 | +import type { CliJsonResult } from './cli-json.ts'; |
| 5 | +import { |
| 6 | + answerDeepLinkConfirmation, |
| 7 | + type DeepLinkConfirmationDevice, |
| 8 | +} from './ios-simulator-e2e/live-deep-link-confirmation.ts'; |
| 9 | + |
| 10 | +function result(status: number, json?: unknown): CliJsonResult { |
| 11 | + return { json, status, stderr: '', stdout: '' }; |
| 12 | +} |
| 13 | + |
| 14 | +const LANDED = result(0, { success: true }); |
| 15 | +const LAUNCH_PENDING = result(1, { |
| 16 | + error: { |
| 17 | + code: 'COMMAND_FAILED', |
| 18 | + details: { reason: 'wait_capture_stalled', runnerErrorCode: 'APP_NOT_RUNNING' }, |
| 19 | + }, |
| 20 | +}); |
| 21 | +const WRONG_ROUTE = result(1, { |
| 22 | + error: { code: 'COMMAND_FAILED', details: { reason: 'wait_target_absent' } }, |
| 23 | +}); |
| 24 | +const READABLE_TIMEOUT = result(1, { |
| 25 | + error: { |
| 26 | + code: 'COMMAND_FAILED', |
| 27 | + details: { reason: 'wait_deadline_exceeded', readableCaptures: 5, captureTruncated: true }, |
| 28 | + }, |
| 29 | +}); |
| 30 | +const OPEN_PROMPT = result(0, { |
| 31 | + data: { message: 'Open in “Agent Device Tester”?', items: ['Cancel', 'Open'] }, |
| 32 | +}); |
| 33 | +const NO_ALERT = result(1, { error: { code: 'COMMAND_FAILED' } }); |
| 34 | + |
| 35 | +/** A simulator whose destination waits and alert probes answer in the order given. */ |
| 36 | +function simulator(destinationWaits: CliJsonResult[], alerts: CliJsonResult[] = [OPEN_PROMPT]) { |
| 37 | + const log: string[] = []; |
| 38 | + const device: DeepLinkConfirmationDevice = { |
| 39 | + waitForDestination: async (step) => { |
| 40 | + log.push(step); |
| 41 | + const next = destinationWaits.shift(); |
| 42 | + assert.ok(next, `unexpected destination wait: ${step}`); |
| 43 | + return next; |
| 44 | + }, |
| 45 | + inspectAlert: async () => { |
| 46 | + log.push('alert get'); |
| 47 | + const next = alerts.shift(); |
| 48 | + assert.ok(next, 'unexpected alert probe'); |
| 49 | + return next; |
| 50 | + }, |
| 51 | + acceptAlert: async () => { |
| 52 | + log.push('alert accept'); |
| 53 | + }, |
| 54 | + }; |
| 55 | + return { device, log }; |
| 56 | +} |
| 57 | + |
| 58 | +const waits = (log: string[]) => log.filter((step) => step.startsWith('wait for')).length; |
| 59 | + |
| 60 | +test('a destination that arrives never probes for the confirmation', async () => { |
| 61 | + const { device, log } = simulator([LANDED]); |
| 62 | + |
| 63 | + await answerDeepLinkConfirmation(device); |
| 64 | + |
| 65 | + assert.deepEqual(log, ['wait for the deep-link destination (1/5)']); |
| 66 | +}); |
| 67 | + |
| 68 | +test('a readable destination timeout still answers a real Open confirmation', async () => { |
| 69 | + const { device, log } = simulator([READABLE_TIMEOUT, LANDED]); |
| 70 | + |
| 71 | + await answerDeepLinkConfirmation(device); |
| 72 | + |
| 73 | + assert.deepEqual(log, [ |
| 74 | + 'wait for the deep-link destination (1/5)', |
| 75 | + 'alert get', |
| 76 | + 'alert accept', |
| 77 | + 'wait for the deep-link destination (2/5)', |
| 78 | + ]); |
| 79 | +}); |
| 80 | + |
| 81 | +test('a readable no-match that answers Open waits for the released launch', async () => { |
| 82 | + const { device, log } = simulator([WRONG_ROUTE, LAUNCH_PENDING, LANDED]); |
| 83 | + |
| 84 | + await answerDeepLinkConfirmation(device); |
| 85 | + |
| 86 | + assert.deepEqual(log, [ |
| 87 | + 'wait for the deep-link destination (1/5)', |
| 88 | + 'alert get', |
| 89 | + 'alert accept', |
| 90 | + 'wait for the deep-link destination (2/5)', |
| 91 | + 'wait for the deep-link destination (3/5)', |
| 92 | + ]); |
| 93 | +}); |
| 94 | + |
| 95 | +test('a truncated capture retries for four bounded waits without accepting a missing alert', async () => { |
| 96 | + const { device, log } = simulator( |
| 97 | + [READABLE_TIMEOUT, READABLE_TIMEOUT, READABLE_TIMEOUT, READABLE_TIMEOUT, LANDED], |
| 98 | + [NO_ALERT, NO_ALERT, NO_ALERT, NO_ALERT], |
| 99 | + ); |
| 100 | + |
| 101 | + await answerDeepLinkConfirmation(device); |
| 102 | + |
| 103 | + assert.equal(waits(log), 5); |
| 104 | + assert.equal(log.filter((step) => step === 'alert get').length, 4); |
| 105 | + assert.equal(log.includes('alert accept'), false); |
| 106 | +}); |
| 107 | + |
| 108 | +test('the launch an accepted confirmation releases is waited for until it lands', async () => { |
| 109 | + // CI run 35991523779: the app reached the foreground 20.7 s after `alert accept` tapped Open. |
| 110 | + const { device, log } = simulator([LAUNCH_PENDING, LAUNCH_PENDING, LAUNCH_PENDING, LANDED]); |
| 111 | + |
| 112 | + await answerDeepLinkConfirmation(device); |
| 113 | + |
| 114 | + assert.deepEqual(log, [ |
| 115 | + 'wait for the deep-link destination (1/5)', |
| 116 | + 'alert get', |
| 117 | + 'alert accept', |
| 118 | + 'wait for the deep-link destination (2/5)', |
| 119 | + 'wait for the deep-link destination (3/5)', |
| 120 | + 'wait for the deep-link destination (4/5)', |
| 121 | + ]); |
| 122 | +}); |
| 123 | + |
| 124 | +test('a readable miss probes once and leaves a wrong route to the caller', async () => { |
| 125 | + const { device, log } = simulator([WRONG_ROUTE], [NO_ALERT]); |
| 126 | + |
| 127 | + await answerDeepLinkConfirmation(device); |
| 128 | + |
| 129 | + assert.deepEqual(log, ['wait for the deep-link destination (1/5)', 'alert get']); |
| 130 | +}); |
| 131 | + |
| 132 | +test('after the accept, a readable no-match still gets a bounded launch wait', async () => { |
| 133 | + const { device, log } = simulator([LAUNCH_PENDING, WRONG_ROUTE, LANDED]); |
| 134 | + |
| 135 | + await answerDeepLinkConfirmation(device); |
| 136 | + |
| 137 | + assert.equal(waits(log), 3); |
| 138 | +}); |
| 139 | + |
| 140 | +test('a confirmation that appears late is still answered once', async () => { |
| 141 | + const { device, log } = simulator( |
| 142 | + [LAUNCH_PENDING, LAUNCH_PENDING, LAUNCH_PENDING, LANDED], |
| 143 | + [NO_ALERT, OPEN_PROMPT], |
| 144 | + ); |
| 145 | + |
| 146 | + await answerDeepLinkConfirmation(device); |
| 147 | + |
| 148 | + assert.deepEqual( |
| 149 | + log.filter((step) => step.startsWith('alert')), |
| 150 | + ['alert get', 'alert get', 'alert accept'], |
| 151 | + ); |
| 152 | +}); |
| 153 | + |
| 154 | +test('the wait budget is bounded when the app never starts', async () => { |
| 155 | + const { device, log } = simulator(Array.from({ length: 5 }, () => LAUNCH_PENDING)); |
| 156 | + |
| 157 | + await answerDeepLinkConfirmation(device); |
| 158 | + |
| 159 | + assert.equal(waits(log), 5); |
| 160 | +}); |
| 161 | + |
| 162 | +test('a prompt that is not the deep-link confirmation is never accepted', async () => { |
| 163 | + const { device, log } = simulator( |
| 164 | + [LAUNCH_PENDING], |
| 165 | + [result(0, { data: { message: 'Allow notifications?', items: ['Allow'] } })], |
| 166 | + ); |
| 167 | + |
| 168 | + await assert.rejects(answerDeepLinkConfirmation(device)); |
| 169 | + assert.equal(log.includes('alert accept'), false); |
| 170 | +}); |
0 commit comments