|
| 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 } from '../runner-client.ts'; |
| 47 | +import { resetRunnerRecycleLedgerForTests } from '../runner-recycle-ledger.ts'; |
| 48 | + |
| 49 | +function makeRunnerArtifact( |
| 50 | + overrides: Partial<RunnerXctestrunArtifact> = {}, |
| 51 | +): RunnerXctestrunArtifact { |
| 52 | + return { |
| 53 | + xctestrunPath: '/tmp/runner.xctestrun', |
| 54 | + derived: '/tmp/derived', |
| 55 | + cache: 'exact', |
| 56 | + artifact: 'valid', |
| 57 | + buildMs: 0, |
| 58 | + xctestrunPathSource: 'manifest', |
| 59 | + ...overrides, |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +const requestCancellation = createTestRequestCancellation(); |
| 64 | +const { isRequestCanceled } = requestCancellation; |
| 65 | + |
| 66 | +beforeEach(() => { |
| 67 | + vi.resetAllMocks(); |
| 68 | + resetRunnerRecycleLedgerForTests(); |
| 69 | + mockGetRunnerSessionSnapshot.mockReturnValue(null); |
| 70 | + mockMarkRunnerXctestrunArtifactBadForRun.mockResolvedValue(undefined); |
| 71 | + requestCancellation.reset(); |
| 72 | + appleRunnerTestHost.update({ |
| 73 | + emitDiagnostic: mockEmitDiagnostic, |
| 74 | + isRequestCanceled, |
| 75 | + getRequestSignal: () => undefined, |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +// What a prepare deadline does to a restored artifact. The wipe that rebuilds a suspect |
| 80 | +// artifact comes from the rules that indict the artifact itself; a runner that never |
| 81 | +// answers inside its budget indicts the boot, not the derived data it was launched from, |
| 82 | +// so the artifact stays and the session goes. |
| 83 | + |
| 84 | +test('a restored artifact whose runner never answers past its deadline is kept while the session is dropped', async () => { |
| 85 | + const restoredSession = makeRunnerSession({ |
| 86 | + port: 8100, |
| 87 | + xctestrunPath: '/tmp/restored.xctestrun', |
| 88 | + xctestrunArtifact: makeRunnerArtifact({ xctestrunPath: '/tmp/restored.xctestrun' }), |
| 89 | + }); |
| 90 | + |
| 91 | + mockEnsureRunnerSession.mockResolvedValue(restoredSession); |
| 92 | + mockExecuteRunnerCommandWithSession.mockRejectedValue( |
| 93 | + new AppError('COMMAND_FAILED', 'xcrun simctl spawn did not answer', { |
| 94 | + cmd: 'xcrun', |
| 95 | + timeoutMs: 45_000, |
| 96 | + }), |
| 97 | + ); |
| 98 | + |
| 99 | + await assert.rejects( |
| 100 | + () => prepareIosRunner(IOS_SIMULATOR, { healthTimeoutMs: 90_000 }), |
| 101 | + (error: unknown) => { |
| 102 | + assert.ok(error instanceof AppError); |
| 103 | + assert.equal(error.message, 'xcrun simctl spawn did not answer'); |
| 104 | + return true; |
| 105 | + }, |
| 106 | + ); |
| 107 | + |
| 108 | + assert.equal(mockMarkRunnerXctestrunArtifactBadForRun.mock.calls.length, 0); |
| 109 | + assert.equal(mockEnsureRunnerSession.mock.calls.length, 2); |
| 110 | + assert.deepEqual(mockInvalidateRunnerSession.mock.calls.at(-1), [ |
| 111 | + restoredSession, |
| 112 | + 'prepare_runner_health_failed', |
| 113 | + ]); |
| 114 | +}); |
0 commit comments