|
| 1 | +import { beforeEach, test, vi } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { IOS_SIMULATOR } from './device-fixtures.ts'; |
| 4 | +import { createTestRequestCancellation, makeRunnerSession } from './runner-session-fixtures.ts'; |
| 5 | +import { AppError, isRequestCanceledError } from '@agent-device/kernel/errors'; |
| 6 | +import { appleRunnerTestHost } from '../test-host.ts'; |
| 7 | + |
| 8 | +// The read-only resend policy around a `RUNNER_BUSY` refusal. The runner refuses fast while |
| 9 | +// watchdog-abandoned XCTest work drains, so the daemon's resend has to outlast that drain, keep |
| 10 | +// the old three-attempt budget for transport failures, and stay cancellable across the seconds it |
| 11 | +// may now wait. |
| 12 | + |
| 13 | +const { |
| 14 | + mockEnsureRunnerSession, |
| 15 | + mockExecuteRunnerCommandWithSession, |
| 16 | + mockReadRunnerSessionLiveness, |
| 17 | + mockInvalidateRunnerSession, |
| 18 | +} = vi.hoisted(() => ({ |
| 19 | + mockEnsureRunnerSession: vi.fn(), |
| 20 | + mockExecuteRunnerCommandWithSession: vi.fn(), |
| 21 | + mockReadRunnerSessionLiveness: vi.fn(), |
| 22 | + mockInvalidateRunnerSession: vi.fn(), |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock('../runner-session.ts', async () => { |
| 26 | + const actual = |
| 27 | + await vi.importActual<typeof import('../runner-session.ts')>('../runner-session.ts'); |
| 28 | + return { |
| 29 | + ...actual, |
| 30 | + ensureRunnerSession: mockEnsureRunnerSession, |
| 31 | + executeRunnerCommandWithSession: mockExecuteRunnerCommandWithSession, |
| 32 | + readRunnerSessionLiveness: mockReadRunnerSessionLiveness, |
| 33 | + invalidateRunnerSession: mockInvalidateRunnerSession, |
| 34 | + }; |
| 35 | +}); |
| 36 | + |
| 37 | +import { runAppleRunnerCommand } from '../runner-client.ts'; |
| 38 | +import { buildRunnerResponseError } from '../runner-contract.ts'; |
| 39 | +import { resetRunnerRecycleLedgerForTests } from '../runner-recycle-ledger.ts'; |
| 40 | + |
| 41 | +const requestCancellation = createTestRequestCancellation(); |
| 42 | + |
| 43 | +beforeEach(() => { |
| 44 | + vi.resetAllMocks(); |
| 45 | + vi.useRealTimers(); |
| 46 | + resetRunnerRecycleLedgerForTests(); |
| 47 | + requestCancellation.reset(); |
| 48 | + appleRunnerTestHost.update({ |
| 49 | + emitDiagnostic: vi.fn(), |
| 50 | + isRequestCanceled: requestCancellation.isRequestCanceled, |
| 51 | + getRequestSignal: () => undefined, |
| 52 | + }); |
| 53 | + const session = makeRunnerSession({ state: 'ready' }); |
| 54 | + mockEnsureRunnerSession.mockResolvedValue(session); |
| 55 | + // A live session, or `executeRunnerCommand` reads the resend as a recycle boot and charges the |
| 56 | + // per-request recycle budget instead of resending. |
| 57 | + mockReadRunnerSessionLiveness.mockReturnValue({ |
| 58 | + sessionId: session.sessionId, |
| 59 | + liveness: 'ready', |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +/** The runner's live refusal, exactly as the transport decodes it. */ |
| 64 | +function busyRefusal(): AppError { |
| 65 | + return buildRunnerResponseError({ |
| 66 | + ok: false, |
| 67 | + error: { code: 'RUNNER_BUSY', message: 'The iOS runner is still finishing a previous command' }, |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +function sentCommands(): string[] { |
| 72 | + return mockExecuteRunnerCommandWithSession.mock.calls.map((call) => call[2].command); |
| 73 | +} |
| 74 | + |
| 75 | +test('read-only commands wait out RUNNER_BUSY past the transport resend backoff', async () => { |
| 76 | + // Three busy answers spaced 200/400/800ms apart, then the runner answers. The default three |
| 77 | + // attempts stopped after the second delay, before the abandoned work had drained. |
| 78 | + vi.useFakeTimers(); |
| 79 | + mockExecuteRunnerCommandWithSession |
| 80 | + .mockRejectedValueOnce(busyRefusal()) |
| 81 | + .mockRejectedValueOnce(busyRefusal()) |
| 82 | + .mockRejectedValueOnce(busyRefusal()) |
| 83 | + .mockResolvedValueOnce({ nodes: [], truncated: false }); |
| 84 | + |
| 85 | + const pending = runAppleRunnerCommand(IOS_SIMULATOR, { command: 'snapshot' }); |
| 86 | + await vi.advanceTimersByTimeAsync(6_000); |
| 87 | + const result = await pending; |
| 88 | + |
| 89 | + assert.deepEqual(result, { nodes: [], truncated: false }); |
| 90 | + assert.equal(mockInvalidateRunnerSession.mock.calls.length, 0); |
| 91 | + // A structured refusal already says the command did not run, so no status probe sits between |
| 92 | + // the resends. |
| 93 | + assert.deepEqual(sentCommands(), ['snapshot', 'snapshot', 'snapshot', 'snapshot']); |
| 94 | +}); |
| 95 | + |
| 96 | +test('a RUNNER_BUSY resend still ends when the refusals outlast the window', async () => { |
| 97 | + vi.useFakeTimers(); |
| 98 | + mockExecuteRunnerCommandWithSession.mockImplementation(async () => { |
| 99 | + throw busyRefusal(); |
| 100 | + }); |
| 101 | + |
| 102 | + const pending = runAppleRunnerCommand(IOS_SIMULATOR, { command: 'snapshot' }); |
| 103 | + const settled = pending.then( |
| 104 | + () => undefined, |
| 105 | + (error: unknown) => error, |
| 106 | + ); |
| 107 | + await vi.advanceTimersByTimeAsync(20_000); |
| 108 | + const error = await settled; |
| 109 | + |
| 110 | + assert.ok(error instanceof AppError); |
| 111 | + assert.equal(error.details?.runnerErrorCode, 'RUNNER_BUSY'); |
| 112 | + assert.equal(error.details?.recovery, undefined, 'the raw refusal reaches the caller unwrapped'); |
| 113 | + assert.deepEqual(sentCommands(), Array<string>(8).fill('snapshot')); |
| 114 | +}); |
| 115 | + |
| 116 | +test('read-only transport failures keep the three-attempt resend budget', async () => { |
| 117 | + mockExecuteRunnerCommandWithSession |
| 118 | + .mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'fetch failed')) |
| 119 | + .mockResolvedValueOnce({ lifecycleState: 'started' }) |
| 120 | + .mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'fetch failed')) |
| 121 | + .mockResolvedValueOnce({ lifecycleState: 'started' }) |
| 122 | + .mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'fetch failed')) |
| 123 | + .mockResolvedValueOnce({ lifecycleState: 'started' }) |
| 124 | + .mockResolvedValueOnce({ nodes: [], truncated: false }); |
| 125 | + |
| 126 | + await assert.rejects( |
| 127 | + () => runAppleRunnerCommand(IOS_SIMULATOR, { command: 'snapshot' }), |
| 128 | + (error: unknown) => { |
| 129 | + assert.ok(error instanceof AppError); |
| 130 | + assert.equal(error.message, 'fetch failed'); |
| 131 | + return true; |
| 132 | + }, |
| 133 | + ); |
| 134 | + |
| 135 | + assert.equal(sentCommands().filter((command) => command === 'snapshot').length, 3); |
| 136 | +}); |
| 137 | + |
| 138 | +test('a cancelled request wakes the RUNNER_BUSY delay instead of sleeping it out', async () => { |
| 139 | + vi.useFakeTimers(); |
| 140 | + const controller = new AbortController(); |
| 141 | + mockExecuteRunnerCommandWithSession |
| 142 | + .mockRejectedValueOnce(busyRefusal()) |
| 143 | + .mockResolvedValue({ nodes: [], truncated: false }); |
| 144 | + |
| 145 | + const pending = runAppleRunnerCommand( |
| 146 | + IOS_SIMULATOR, |
| 147 | + { command: 'snapshot' }, |
| 148 | + { signal: controller.signal }, |
| 149 | + ); |
| 150 | + const settled = pending.then( |
| 151 | + () => undefined, |
| 152 | + (error: unknown) => error, |
| 153 | + ); |
| 154 | + // Inside the first 200ms delay; the next attempt would otherwise succeed at the timer. |
| 155 | + await vi.advanceTimersByTimeAsync(50); |
| 156 | + controller.abort(); |
| 157 | + await vi.advanceTimersByTimeAsync(0); |
| 158 | + const error = await settled; |
| 159 | + |
| 160 | + assert.ok(isRequestCanceledError(error), `expected a canceled request, got ${String(error)}`); |
| 161 | + assert.deepEqual(sentCommands(), ['snapshot']); |
| 162 | +}); |
0 commit comments