|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { afterEach, beforeEach, test, vi } from 'vitest'; |
| 5 | +import { AppError } from '@agent-device/kernel/errors'; |
| 6 | +import { resetAllProcessMemosForTests } from '@agent-device/kernel/ttl-memo'; |
| 7 | +import { appleRunnerTestHost } from '../test-host.ts'; |
| 8 | +import type { ExecOptions, ExecResult, ExecStreamOptions } from '../host.ts'; |
| 9 | +import { ensureXctestrunArtifact } from '../runner-xctestrun.ts'; |
| 10 | +import { appleToolchainProbeResult } from './apple-toolchain-fixtures.ts'; |
| 11 | +import { MACOS_DEVICE } from './device-fixtures.ts'; |
| 12 | +import { mkdtempForTestSync } from './tmp-dir.ts'; |
| 13 | + |
| 14 | +/** |
| 15 | + * The build phase spends one budget, not two (#2422). |
| 16 | + * |
| 17 | + * `ensureXctestrunArtifact` runs the cache decision's blocking toolchain probes |
| 18 | + * before it starts `xcodebuild`. Those probes used to be handed |
| 19 | + * `buildTimeoutMs` and the build was then handed the same number again, so a |
| 20 | + * cold-start probe stall added its 30 to 45 seconds on top of the phase budget |
| 21 | + * instead of coming out of it. These cases pin the shared deadline: the clock |
| 22 | + * moves only when a probe actually blocks for the timeout it was handed, so a |
| 23 | + * case that claims the budget was spent had to spend it. |
| 24 | + */ |
| 25 | + |
| 26 | +const clock = { nowMs: 0 }; |
| 27 | +const runCmdSync = vi.fn(); |
| 28 | +const runCmdStreaming = vi.fn(); |
| 29 | +let projectRoot: string; |
| 30 | + |
| 31 | +beforeEach(() => { |
| 32 | + resetAllProcessMemosForTests(); |
| 33 | + clock.nowMs = 0; |
| 34 | + projectRoot = mkdtempForTestSync('agent-device-runner-phase-root-'); |
| 35 | + // `buildXctestrunArtifact` refuses to start a build without the runner project. |
| 36 | + fs.mkdirSync( |
| 37 | + path.join(projectRoot, 'apple', 'runner', 'AgentDeviceRunner', 'AgentDeviceRunner.xcodeproj'), |
| 38 | + { recursive: true }, |
| 39 | + ); |
| 40 | + process.env.AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH = mkdtempForTestSync( |
| 41 | + 'agent-device-runner-phase-derived-', |
| 42 | + ); |
| 43 | + runCmdSync.mockReset().mockImplementation(appleToolchainProbeResult); |
| 44 | + runCmdStreaming |
| 45 | + .mockReset() |
| 46 | + .mockImplementation(async (): Promise<ExecResult> => ({ exitCode: 0, stdout: '', stderr: '' })); |
| 47 | + appleRunnerTestHost.update({ |
| 48 | + runCmdSync, |
| 49 | + runCmdStreaming, |
| 50 | + findProjectRoot: () => projectRoot, |
| 51 | + readVersion: () => '0.0.0-test', |
| 52 | + deadlineFromTimeoutMs: (timeoutMs: number) => { |
| 53 | + const startedAtMs = clock.nowMs; |
| 54 | + const expiresAtMs = startedAtMs + Math.max(0, timeoutMs); |
| 55 | + return { |
| 56 | + remainingMs: () => Math.max(0, expiresAtMs - clock.nowMs), |
| 57 | + elapsedMs: () => Math.max(0, clock.nowMs - startedAtMs), |
| 58 | + isExpired: () => expiresAtMs - clock.nowMs <= 0, |
| 59 | + }; |
| 60 | + }, |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +afterEach(() => { |
| 65 | + delete process.env.AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH; |
| 66 | +}); |
| 67 | + |
| 68 | +test('a warm toolchain leaves the build the whole phase budget', async () => { |
| 69 | + await assert.rejects( |
| 70 | + ensureXctestrunArtifact(MACOS_DEVICE, { buildTimeoutMs: 120_000 }), |
| 71 | + missingXctestrun, |
| 72 | + ); |
| 73 | + |
| 74 | + assert.equal(clock.nowMs, 0); |
| 75 | + assert.equal(buildTimeoutMsGiven(), 120_000); |
| 76 | +}); |
| 77 | + |
| 78 | +test('a cold-start probe stall comes out of the build budget instead of being added to it', async () => { |
| 79 | + let xcodebuildProbes = 0; |
| 80 | + runCmdSync.mockImplementation((command: string, args: string[], options: ExecOptions) => { |
| 81 | + // The syspolicyd stall: the first `xcodebuild` exec blocks for the whole |
| 82 | + // timeout it was handed, the immediate next one answers at once. |
| 83 | + xcodebuildProbes += command === 'xcodebuild' ? 1 : 0; |
| 84 | + if (xcodebuildProbes === 1 && command === 'xcodebuild') { |
| 85 | + throw blockForWholeTimeout(command, options); |
| 86 | + } |
| 87 | + return appleToolchainProbeResult(command, args); |
| 88 | + }); |
| 89 | + |
| 90 | + await assert.rejects( |
| 91 | + ensureXctestrunArtifact(MACOS_DEVICE, { buildTimeoutMs: 120_000 }), |
| 92 | + missingXctestrun, |
| 93 | + ); |
| 94 | + |
| 95 | + // 30 s of stall, absorbed by the retry -- and charged to the phase, so the |
| 96 | + // build is given 90 s rather than a second full 120 s. |
| 97 | + assert.equal(clock.nowMs, 30_000); |
| 98 | + assert.equal(buildTimeoutMsGiven(), 90_000); |
| 99 | +}); |
| 100 | + |
| 101 | +test('a probe that spends the whole phase fails before xcodebuild is spawned', async () => { |
| 102 | + runCmdSync.mockImplementation((command: string, args: string[], options: ExecOptions) => { |
| 103 | + // The last of the three probes answers, but only after blocking for |
| 104 | + // everything the phase had left. |
| 105 | + if (args.includes('--show-sdk-build-version')) clock.nowMs += options.timeoutMs ?? 0; |
| 106 | + return appleToolchainProbeResult(command, args); |
| 107 | + }); |
| 108 | + |
| 109 | + await assert.rejects( |
| 110 | + ensureXctestrunArtifact(MACOS_DEVICE, { buildTimeoutMs: 30_000 }), |
| 111 | + (error: unknown) => |
| 112 | + error instanceof AppError && |
| 113 | + error.details?.reason === 'runner_phase_budget_exhausted' && |
| 114 | + error.details?.phase === 'runner_xctestrun_build', |
| 115 | + ); |
| 116 | + |
| 117 | + assert.equal(clock.nowMs, 30_000); |
| 118 | + assert.equal(runCmdStreaming.mock.calls.length, 0); |
| 119 | +}); |
| 120 | + |
| 121 | +/** The fake build writes no `.xctestrun`; the budget it was handed is what these cases read. */ |
| 122 | +function missingXctestrun(error: unknown): boolean { |
| 123 | + return error instanceof AppError && error.message === 'Failed to locate .xctestrun after build'; |
| 124 | +} |
| 125 | + |
| 126 | +function buildTimeoutMsGiven(): number | undefined { |
| 127 | + assert.equal(runCmdStreaming.mock.calls.length, 1); |
| 128 | + const [command, , options] = runCmdStreaming.mock.calls[0] as [ |
| 129 | + string, |
| 130 | + string[], |
| 131 | + ExecStreamOptions, |
| 132 | + ]; |
| 133 | + assert.equal(command, 'xcodebuild'); |
| 134 | + return options.timeoutMs; |
| 135 | +} |
| 136 | + |
| 137 | +/** A probe that blocked for its whole timeout and was then killed, as the exec layer reports it. */ |
| 138 | +function blockForWholeTimeout(command: string, options: ExecOptions): AppError { |
| 139 | + const timeoutMs = options.timeoutMs ?? 0; |
| 140 | + clock.nowMs += timeoutMs; |
| 141 | + return new AppError('COMMAND_FAILED', `${command} timed out after ${timeoutMs}ms`, { |
| 142 | + cmd: command, |
| 143 | + timeoutMs, |
| 144 | + }); |
| 145 | +} |
0 commit comments