|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { beforeEach, test, vi } from 'vitest'; |
| 3 | +import { AppError } from '@agent-device/kernel/errors'; |
| 4 | +import { appleRunnerTestHost } from '../test-host.ts'; |
| 5 | +import type { RunnerXctestrunArtifact } from '../runner-xctestrun.ts'; |
| 6 | +import { IOS_SIMULATOR } from './device-fixtures.ts'; |
| 7 | +import { createTestRequestCancellation, makeRunnerSession } from './runner-session-fixtures.ts'; |
| 8 | + |
| 9 | +const { |
| 10 | + mockEnsureRunnerSession, |
| 11 | + mockExecuteRunnerCommandWithSession, |
| 12 | + mockEmitDiagnostic, |
| 13 | + mockGetRunnerSessionSnapshot, |
| 14 | + mockInvalidateRunnerSession, |
| 15 | + mockMarkRunnerXctestrunArtifactBadForRun, |
| 16 | +} = vi.hoisted(() => ({ |
| 17 | + mockEnsureRunnerSession: vi.fn(), |
| 18 | + mockExecuteRunnerCommandWithSession: vi.fn(), |
| 19 | + mockEmitDiagnostic: vi.fn(), |
| 20 | + mockGetRunnerSessionSnapshot: vi.fn(), |
| 21 | + mockInvalidateRunnerSession: vi.fn(), |
| 22 | + mockMarkRunnerXctestrunArtifactBadForRun: 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 | + getRunnerSessionSnapshot: mockGetRunnerSessionSnapshot, |
| 33 | + invalidateRunnerSession: mockInvalidateRunnerSession, |
| 34 | + }; |
| 35 | +}); |
| 36 | + |
| 37 | +vi.mock('../runner-xctestrun.ts', async () => { |
| 38 | + const actual = |
| 39 | + await vi.importActual<typeof import('../runner-xctestrun.ts')>('../runner-xctestrun.ts'); |
| 40 | + return { |
| 41 | + ...actual, |
| 42 | + markRunnerXctestrunArtifactBadForRun: mockMarkRunnerXctestrunArtifactBadForRun, |
| 43 | + }; |
| 44 | +}); |
| 45 | + |
| 46 | +import { prepareIosRunner, runAppleRunnerCommand } from '../runner-client.ts'; |
| 47 | +import { resetRunnerRecycleLedgerForTests } from '../runner-recycle-ledger.ts'; |
| 48 | + |
| 49 | +// What `executeRunnerCommand` decides when a readiness preflight refuses the runner |
| 50 | +// before the command was ever written: the table's own verdict axes say whether the |
| 51 | +// session restarts, whether the restored artifact is suspect, and whether the caller |
| 52 | +// simply hears the refusal. |
| 53 | + |
| 54 | +const requestCancellation = createTestRequestCancellation(); |
| 55 | +const { isRequestCanceled } = requestCancellation; |
| 56 | + |
| 57 | +beforeEach(() => { |
| 58 | + vi.resetAllMocks(); |
| 59 | + resetRunnerRecycleLedgerForTests(); |
| 60 | + mockGetRunnerSessionSnapshot.mockReturnValue(null); |
| 61 | + mockMarkRunnerXctestrunArtifactBadForRun.mockResolvedValue(undefined); |
| 62 | + requestCancellation.reset(); |
| 63 | + appleRunnerTestHost.update({ |
| 64 | + emitDiagnostic: mockEmitDiagnostic, |
| 65 | + isRequestCanceled, |
| 66 | + getRequestSignal: () => undefined, |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +function makeRunnerArtifact( |
| 71 | + overrides: Partial<RunnerXctestrunArtifact> = {}, |
| 72 | +): RunnerXctestrunArtifact { |
| 73 | + return { |
| 74 | + xctestrunPath: '/tmp/runner.xctestrun', |
| 75 | + derived: '/tmp/derived', |
| 76 | + cache: 'exact', |
| 77 | + artifact: 'valid', |
| 78 | + buildMs: 0, |
| 79 | + xctestrunPathSource: 'manifest', |
| 80 | + ...overrides, |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +test('mutating commands restart stale sessions when readiness preflight fails before command send', async () => { |
| 85 | + const staleSession = makeRunnerSession({ port: 8100, ready: true }); |
| 86 | + const freshSession = makeRunnerSession({ port: 8101, ready: false }); |
| 87 | + |
| 88 | + mockEnsureRunnerSession.mockResolvedValueOnce(staleSession).mockResolvedValueOnce(freshSession); |
| 89 | + mockExecuteRunnerCommandWithSession |
| 90 | + .mockRejectedValueOnce( |
| 91 | + new AppError('COMMAND_FAILED', 'fetch failed', { |
| 92 | + runnerReadinessPreflightFailed: true, |
| 93 | + }), |
| 94 | + ) |
| 95 | + .mockResolvedValueOnce({ message: 'tapped' }); |
| 96 | + |
| 97 | + const result = await runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }); |
| 98 | + |
| 99 | + assert.deepEqual(result, { message: 'tapped' }); |
| 100 | + assert.equal(mockEnsureRunnerSession.mock.calls.length, 2); |
| 101 | + assert.deepEqual(mockInvalidateRunnerSession.mock.calls[0], [ |
| 102 | + staleSession, |
| 103 | + 'runner_readiness_preflight_failed_before_command_send', |
| 104 | + ]); |
| 105 | + assert.equal(mockExecuteRunnerCommandWithSession.mock.calls.length, 2); |
| 106 | + assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], freshSession); |
| 107 | +}); |
| 108 | + |
| 109 | +test('mutating commands restart stale sessions when readiness preflight outlives its deadline', async () => { |
| 110 | + const staleSession = makeRunnerSession({ port: 8100, ready: true }); |
| 111 | + const freshSession = makeRunnerSession({ port: 8101, ready: false }); |
| 112 | + |
| 113 | + mockEnsureRunnerSession.mockResolvedValueOnce(staleSession).mockResolvedValueOnce(freshSession); |
| 114 | + mockExecuteRunnerCommandWithSession |
| 115 | + .mockRejectedValueOnce( |
| 116 | + new AppError('COMMAND_FAILED', 'xcrun timed out after 45000ms', { |
| 117 | + cmd: 'xcrun', |
| 118 | + timeoutMs: 45_000, |
| 119 | + runnerReadinessPreflightFailed: true, |
| 120 | + }), |
| 121 | + ) |
| 122 | + .mockResolvedValueOnce({ message: 'tapped' }); |
| 123 | + |
| 124 | + const result = await runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }); |
| 125 | + |
| 126 | + assert.deepEqual(result, { message: 'tapped' }); |
| 127 | + assert.equal(mockEnsureRunnerSession.mock.calls.length, 2); |
| 128 | + assert.deepEqual(mockInvalidateRunnerSession.mock.calls[0], [ |
| 129 | + staleSession, |
| 130 | + 'runner_readiness_preflight_failed_before_command_send', |
| 131 | + ]); |
| 132 | + assert.equal(mockExecuteRunnerCommandWithSession.mock.calls.length, 2); |
| 133 | + assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], freshSession); |
| 134 | +}); |
| 135 | + |
| 136 | +test('a readiness preflight refusal that is neither transport-shaped nor deadline-shaped surfaces', async () => { |
| 137 | + const staleSession = makeRunnerSession({ port: 8100, ready: true }); |
| 138 | + |
| 139 | + mockEnsureRunnerSession.mockResolvedValueOnce(staleSession); |
| 140 | + mockExecuteRunnerCommandWithSession.mockRejectedValueOnce( |
| 141 | + new AppError('COMMAND_FAILED', 'Runner readiness refused', { |
| 142 | + runnerReadinessPreflightFailed: true, |
| 143 | + }), |
| 144 | + ); |
| 145 | + |
| 146 | + await assert.rejects( |
| 147 | + () => runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }), |
| 148 | + (error: unknown) => { |
| 149 | + assert.ok(error instanceof AppError); |
| 150 | + assert.equal(error.message, 'Runner readiness refused'); |
| 151 | + return true; |
| 152 | + }, |
| 153 | + ); |
| 154 | + assert.equal(mockEnsureRunnerSession.mock.calls.length, 1); |
| 155 | +}); |
| 156 | + |
| 157 | +test('a boot that exited early does not wipe a restored runner artifact', async () => { |
| 158 | + const restoredSession = makeRunnerSession({ |
| 159 | + port: 8100, |
| 160 | + xctestrunPath: '/tmp/restored.xctestrun', |
| 161 | + xctestrunArtifact: makeRunnerArtifact({ xctestrunPath: '/tmp/restored.xctestrun' }), |
| 162 | + }); |
| 163 | + |
| 164 | + mockEnsureRunnerSession.mockResolvedValueOnce(restoredSession); |
| 165 | + mockExecuteRunnerCommandWithSession.mockRejectedValueOnce( |
| 166 | + new AppError('COMMAND_FAILED', 'Runner did not accept connection (xcodebuild exited early)'), |
| 167 | + ); |
| 168 | + |
| 169 | + await assert.rejects( |
| 170 | + () => prepareIosRunner(IOS_SIMULATOR, { healthTimeoutMs: 90_000 }), |
| 171 | + (error: unknown) => { |
| 172 | + assert.ok(error instanceof AppError); |
| 173 | + assert.equal(error.message, 'Runner did not accept connection (xcodebuild exited early)'); |
| 174 | + return true; |
| 175 | + }, |
| 176 | + ); |
| 177 | + assert.equal(mockMarkRunnerXctestrunArtifactBadForRun.mock.calls.length, 0); |
| 178 | + assert.equal(mockEnsureRunnerSession.mock.calls.length, 1); |
| 179 | +}); |
0 commit comments