|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import test from 'node:test'; |
| 3 | + |
| 4 | +import type { CliJsonResult } from './cli-json.ts'; |
| 5 | +import { retryCleanupStep } from './ios-simulator-e2e/live-harness.ts'; |
| 6 | + |
| 7 | +// Deterministic regression for the retry/guard policy behind #1548: a full-tier iOS |
| 8 | +// e2e run's cleanup must tolerate a dead session and the known appless mic-permission |
| 9 | +// reset without swallowing an unrelated failure. `retryCleanupStep` runs the exact |
| 10 | +// per-step policy `cleanupSession` uses, driven here by a scripted `runAttempt` instead |
| 11 | +// of the real CLI subprocess, so these cases run in milliseconds with no simulator. |
| 12 | + |
| 13 | +const MIC_STEP = 'reset microphone permission'; |
| 14 | +const OTHER_STEP = 'restore portrait orientation'; |
| 15 | +const MIC_APPLESS_MESSAGE = 'permission setting requires an active app in session'; |
| 16 | + |
| 17 | +function invalidArgsResult(message: string): CliJsonResult { |
| 18 | + return { json: { error: { code: 'INVALID_ARGS', message } }, status: 1, stderr: '', stdout: '' }; |
| 19 | +} |
| 20 | + |
| 21 | +function sessionNotFoundResult(): CliJsonResult { |
| 22 | + return { |
| 23 | + json: { error: { code: 'SESSION_NOT_FOUND', message: 'No active session' } }, |
| 24 | + status: 1, |
| 25 | + stderr: '', |
| 26 | + stdout: '', |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +// retryCleanupStep sleeps 500ms between attempts. Drive fake timers instead of waiting |
| 31 | +// real time: repeatedly flush the microtask queue and advance the mocked clock until |
| 32 | +// the retry promise settles. |
| 33 | +async function drainRetry( |
| 34 | + t: { mock: { timers: { tick: (ms: number) => void } } }, |
| 35 | + promise: Promise<unknown>, |
| 36 | +): Promise<unknown> { |
| 37 | + let settled = false; |
| 38 | + promise.finally(() => { |
| 39 | + settled = true; |
| 40 | + }); |
| 41 | + while (!settled) { |
| 42 | + await new Promise((resolve) => setImmediate(resolve)); |
| 43 | + t.mock.timers.tick(500); |
| 44 | + } |
| 45 | + return promise; |
| 46 | +} |
| 47 | + |
| 48 | +test('a dead session (SESSION_NOT_FOUND) skips cleanup without error, on any step', async () => { |
| 49 | + let attempts = 0; |
| 50 | + const failure = await retryCleanupStep(MIC_STEP, async () => { |
| 51 | + attempts += 1; |
| 52 | + return sessionNotFoundResult(); |
| 53 | + }); |
| 54 | + assert.equal(failure, undefined); |
| 55 | + assert.equal(attempts, 1, 'should not retry once the session is confirmed gone'); |
| 56 | +}); |
| 57 | + |
| 58 | +test('the known mic-permission appless response stops retrying immediately', async () => { |
| 59 | + let attempts = 0; |
| 60 | + const failure = await retryCleanupStep(MIC_STEP, async () => { |
| 61 | + attempts += 1; |
| 62 | + return invalidArgsResult(MIC_APPLESS_MESSAGE); |
| 63 | + }); |
| 64 | + assert.equal(failure, undefined); |
| 65 | + assert.equal(attempts, 1, 'should not retry the known appless response'); |
| 66 | +}); |
| 67 | + |
| 68 | +test('a different INVALID_ARGS still fails after exhausting retries', async (t) => { |
| 69 | + // Same message, wrong step: proves the guard is scoped to the mic-permission reset |
| 70 | + // and does not tolerate the identical string on another step. |
| 71 | + t.mock.timers.enable({ apis: ['setTimeout'] }); |
| 72 | + let attempts = 0; |
| 73 | + const failure = await drainRetry( |
| 74 | + t, |
| 75 | + retryCleanupStep(OTHER_STEP, async (attempt) => { |
| 76 | + attempts += 1; |
| 77 | + if (attempt < 3) return invalidArgsResult(MIC_APPLESS_MESSAGE); |
| 78 | + // Mirrors runStep(..., { allowFailure: false }) on the final attempt: it throws |
| 79 | + // instead of returning a failed result. |
| 80 | + throw new Error(`cleanup: ${OTHER_STEP} (attempt 3) failed`); |
| 81 | + }), |
| 82 | + ); |
| 83 | + assert.ok(failure instanceof Error, `expected a propagated failure, got ${String(failure)}`); |
| 84 | + assert.equal(attempts, 3, 'should exhaust all three attempts'); |
| 85 | +}); |
| 86 | + |
| 87 | +test('a different INVALID_ARGS message on the mic-permission step still fails after retries', async (t) => { |
| 88 | + // Same step, a message sharing the "requires an active app in session" suffix with |
| 89 | + // the location-setting call site (app-settings.ts): proves the match is the exact |
| 90 | + // known string, not any INVALID_ARGS message that happens to overlap it. |
| 91 | + t.mock.timers.enable({ apis: ['setTimeout'] }); |
| 92 | + let attempts = 0; |
| 93 | + const failure = await drainRetry( |
| 94 | + t, |
| 95 | + retryCleanupStep(MIC_STEP, async (attempt) => { |
| 96 | + attempts += 1; |
| 97 | + if (attempt < 3) |
| 98 | + return invalidArgsResult('location setting requires an active app in session'); |
| 99 | + throw new Error(`cleanup: ${MIC_STEP} (attempt 3) failed`); |
| 100 | + }), |
| 101 | + ); |
| 102 | + assert.ok(failure instanceof Error, `expected a propagated failure, got ${String(failure)}`); |
| 103 | + assert.equal(attempts, 3, 'should exhaust all three attempts'); |
| 104 | +}); |
0 commit comments