|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { beforeEach, test, vi } from 'vitest'; |
| 5 | +import { AppError, normalizeError } from '@agent-device/kernel/errors'; |
| 6 | +import { resetAllProcessMemosForTests } from '@agent-device/kernel/ttl-memo'; |
| 7 | +import type { ExecBackgroundResult } from '@agent-device/host-kit/command'; |
| 8 | +import { buildRunnerEarlyExitError, waitForRunner } from '../runner-startup-transport.ts'; |
| 9 | +import { readRunnerLogTail } from '../runner-io.ts'; |
| 10 | +import { RunnerCommandAccounting, type RunnerSession } from '../runner-session-types.ts'; |
| 11 | +import { |
| 12 | + isRetryableRunnerError, |
| 13 | + shouldRebuildCachedRunnerArtifact, |
| 14 | + shouldRestartRunnerAfterReadinessPreflight, |
| 15 | + shouldRestartRunnerBeforeCommandSend, |
| 16 | + shouldRetryRunnerConnectError, |
| 17 | +} from '../runner-error-classification.ts'; |
| 18 | +import { RUNNER_ENDPOINT_FIXTURE, iosSimulator } from './runner-transport.fixtures.ts'; |
| 19 | +import { buildFixtureById } from './runner-startup-failure-fixtures.ts'; |
| 20 | +import { mkdtempForTestSync } from './tmp-dir.ts'; |
| 21 | + |
| 22 | +beforeEach(resetAllProcessMemosForTests); |
| 23 | + |
| 24 | +/** |
| 25 | + * An Xcode older than the macOS it sits on cannot load its own `DTServiceHub`: AMFI refuses its |
| 26 | + * version-restriction entitlement (#3449). Every `xcodebuild test-without-building` then writes the |
| 27 | + * bless refusal into `runner.log` while the runner never serves a command, and the caller only ever |
| 28 | + * saw `Runner did not accept connection` on a runner the request had just restarted. The cause is |
| 29 | + * readable nowhere but that log, so both connect-stage failures have to read it, name it, and refuse |
| 30 | + * the restart and resend they would otherwise order. These cases drive the real producers. |
| 31 | + */ |
| 32 | + |
| 33 | +const BLESS_LOG = buildFixtureById('simulator-service-hub-bless-refused').output; |
| 34 | + |
| 35 | +function sessionWithLaunchLog(log: string, exitCode: number | null): RunnerSession { |
| 36 | + const runnerLogPath = path.join(mkdtempForTestSync('runner-service-hub-'), 'runner.log'); |
| 37 | + fs.writeFileSync(runnerLogPath, log); |
| 38 | + return { |
| 39 | + sessionId: 'service-hub-session', |
| 40 | + device: iosSimulator, |
| 41 | + deviceId: iosSimulator.id, |
| 42 | + endpoint: RUNNER_ENDPOINT_FIXTURE, |
| 43 | + xctestrunPath: '/tmp/runner.xctestrun', |
| 44 | + jsonPath: '/tmp/runner.json', |
| 45 | + runnerLogPath, |
| 46 | + readLogTail: (maxBytes) => |
| 47 | + readRunnerLogTail({ logPath: runnerLogPath, startOffset: 0 }, maxBytes), |
| 48 | + testPromise: |
| 49 | + exitCode === null |
| 50 | + ? new Promise<never>(() => {}) |
| 51 | + : Promise.resolve({ exitCode, stdout: '', stderr: '' }), |
| 52 | + child: { pid: 4242, exitCode } as ExecBackgroundResult['child'], |
| 53 | + state: 'starting', |
| 54 | + commandCharges: new RunnerCommandAccounting(), |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +/** The device states a simulator startup carries; the bless cause must outrank them (#2683). */ |
| 59 | +const SIMULATOR_WITH_IMAGE_DOWN = { |
| 60 | + developerMode: 'enabled' as const, |
| 61 | + developerDiskImage: 'unavailable' as const, |
| 62 | + developerDiskImageHint: 'Unlock the iPhone so it can mount the developer disk image.', |
| 63 | +}; |
| 64 | + |
| 65 | +test('a launch that cannot bless the simulator service names the Xcode cause and its fix', async () => { |
| 66 | + const session = { |
| 67 | + ...sessionWithLaunchLog(BLESS_LOG, 65), |
| 68 | + startupDeviceStates: SIMULATOR_WITH_IMAGE_DOWN, |
| 69 | + }; |
| 70 | + |
| 71 | + const error = await buildRunnerEarlyExitError({ session, port: 8100 }); |
| 72 | + const envelope = normalizeError(error, { diagnosticId: 'diag-service-hub' }); |
| 73 | + |
| 74 | + assert.equal(envelope.code, 'COMMAND_FAILED'); |
| 75 | + assert.equal( |
| 76 | + envelope.message, |
| 77 | + 'Runner did not accept connection (xcodebuild exited early): this Xcode cannot start its simulator service on this macOS', |
| 78 | + ); |
| 79 | + assert.equal(envelope.details?.reason, 'simulator_service_hub_unavailable'); |
| 80 | + // The fix is the daemon host's Xcode selection, so the hint has to name both levers for it. |
| 81 | + assert.match(String(envelope.hint), /xcode-select -s/); |
| 82 | + assert.match(String(envelope.hint), /DEVELOPER_DIR/); |
| 83 | + // The bless line is in the runner's log, and that is the file the caller is sent to. |
| 84 | + assert.equal(envelope.logPath, session.runnerLogPath); |
| 85 | + assert.equal(envelope.diagnosticId, 'diag-service-hub'); |
| 86 | + // A cause this log proved outranks a device state read before the launch; the state still travels. |
| 87 | + assert.equal(envelope.details?.developerDiskImage, 'unavailable'); |
| 88 | +}); |
| 89 | + |
| 90 | +test('a still-alive xcodebuild whose log carries the bless refusal fails the connect stage by name and stays terminal', async () => { |
| 91 | + vi.stubGlobal( |
| 92 | + 'fetch', |
| 93 | + vi.fn(async () => { |
| 94 | + throw Object.assign(new Error('connect ECONNREFUSED 127.0.0.1:8100'), { |
| 95 | + code: 'ECONNREFUSED', |
| 96 | + }); |
| 97 | + }), |
| 98 | + ); |
| 99 | + const session = sessionWithLaunchLog(BLESS_LOG, null); |
| 100 | + |
| 101 | + const error = await waitForRunner( |
| 102 | + iosSimulator, |
| 103 | + RUNNER_ENDPOINT_FIXTURE, |
| 104 | + { command: 'tap', x: 200, y: 300 }, |
| 105 | + undefined, |
| 106 | + 300, |
| 107 | + session, |
| 108 | + ).catch((error: unknown) => error as AppError); |
| 109 | + |
| 110 | + assert.ok(error instanceof AppError); |
| 111 | + assert.equal( |
| 112 | + error.message, |
| 113 | + 'Runner did not accept connection: this Xcode cannot start its simulator service on this macOS', |
| 114 | + ); |
| 115 | + assert.equal(error.details?.reason, 'simulator_service_hub_unavailable'); |
| 116 | + assert.match(String(error.details?.hint), /xcode-select -s/); |
| 117 | + assert.equal(error.details?.logPath, session.runnerLogPath); |
| 118 | + // Nothing was written before the loop gave up, so the disclosure the refusal earns still holds. |
| 119 | + assert.equal(error.details?.dispatched, 'no'); |
| 120 | + // Before the cause had a name, this same failure ordered a session restart, a resend, and a clean |
| 121 | + // rebuild of the very binary this macOS refuses to load (#3449). None of that may survive. |
| 122 | + assert.equal(shouldRetryRunnerConnectError(error), false); |
| 123 | + assert.equal(isRetryableRunnerError(error), false); |
| 124 | + assert.equal(shouldRestartRunnerBeforeCommandSend(error, { command: 'tap' }), false); |
| 125 | + assert.equal(shouldRebuildCachedRunnerArtifact(error), false); |
| 126 | +}); |
| 127 | + |
| 128 | +test('a still-alive xcodebuild whose log names no service-hub cause keeps the ordinary connect failure', async () => { |
| 129 | + vi.stubGlobal( |
| 130 | + 'fetch', |
| 131 | + vi.fn(async () => { |
| 132 | + throw Object.assign(new Error('connect ECONNREFUSED 127.0.0.1:8100'), { |
| 133 | + code: 'ECONNREFUSED', |
| 134 | + }); |
| 135 | + }), |
| 136 | + ); |
| 137 | + |
| 138 | + await assert.rejects( |
| 139 | + () => |
| 140 | + waitForRunner( |
| 141 | + iosSimulator, |
| 142 | + RUNNER_ENDPOINT_FIXTURE, |
| 143 | + { command: 'snapshot' }, |
| 144 | + undefined, |
| 145 | + 300, |
| 146 | + sessionWithLaunchLog( |
| 147 | + '2026-10-15 10:23:41.112 xcodebuild[9:1] [MT] iOSSimulator: waiting for the simulator to boot\n', |
| 148 | + null, |
| 149 | + ), |
| 150 | + ), |
| 151 | + (error: unknown) => { |
| 152 | + const appError = error as AppError; |
| 153 | + assert.equal(appError.message, 'Runner did not accept connection'); |
| 154 | + assert.equal(appError.details?.reason, 'IOS_RUNNER_CONNECT_TIMEOUT'); |
| 155 | + return true; |
| 156 | + }, |
| 157 | + ); |
| 158 | +}); |
| 159 | + |
| 160 | +test('bless wording without the typed reason cannot stop the connect retry or the resend', () => { |
| 161 | + // The recovery rows key on the reason the launch paths publish, so foreign text carrying Xcode's |
| 162 | + // own sentence — in a message or a captured log — cannot by itself mark a failure terminal. |
| 163 | + const untyped = new AppError( |
| 164 | + 'COMMAND_FAILED', |
| 165 | + 'Runner did not accept connection: DTServiceHubClient failed to bless service hub for simulator iPhone (UDID)', |
| 166 | + { stderr: BLESS_LOG }, |
| 167 | + ); |
| 168 | + |
| 169 | + // The connect loop keeps waiting on it, exactly as it does on any unclassified connect failure. |
| 170 | + assert.equal(shouldRetryRunnerConnectError(untyped), true); |
| 171 | + assert.equal(shouldRestartRunnerAfterReadinessPreflight(untyped), false); |
| 172 | +}); |
0 commit comments