Skip to content

Commit 942f3d2

Browse files
committed
test(apple): cover the simulator boot retry policy and helper source walk
`ensureBootedSimulator` rebuilt the same `classifyBootFailure` input in its retry policy, its deadline classifier, and its final rethrow, so all three sat at 0% coverage. One local `classifyAttempt` closure reads the two captures live and replaces the copies; the retry, refusal, deadline and exhaustion paths are then covered through the boot entry point against the existing `runCmd` fake. The macOS helper's source-tree walk decides which files make the build fresh, and nothing exercised it. A new test drives the helper-build entry point against a scratch package tree with a `.build` directory, a nested source directory, `Package.swift`, a non-Swift file and a symlink, and asserts the build/install/manifest round trip it drives. Closes #3451.
1 parent 18d22c7 commit 942f3d2

3 files changed

Lines changed: 431 additions & 21 deletions

File tree

‎packages/platform-apple/src/core/__tests__/simulator.test.ts‎

Lines changed: 209 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { beforeEach, test, vi } from 'vitest';
1+
import { afterEach, beforeEach, test, vi } from 'vitest';
22
import assert from 'node:assert/strict';
33
import { ensureBootedSimulator, openIosSimulatorApp } from '../simulator.ts';
4-
import { IOS_SIMULATOR_FOCUS_TIMEOUT_MS } from '../config.ts';
4+
import { IOS_BOOT_TIMEOUT_MS, IOS_SIMULATOR_FOCUS_TIMEOUT_MS } from '../config.ts';
55
import { AppError } from '@agent-device/kernel/errors';
66
import { runCmd } from '@agent-device/host-kit/command';
77
import { IOS_TEST_SIMULATOR } from './apple-core-stub-helpers.ts';
@@ -121,3 +121,210 @@ test('openIosSimulatorApp times out instead of hanging indefinitely', async () =
121121
},
122122
);
123123
});
124+
125+
function failedSimctlBoot(stderr: string): MockRunCmdResult {
126+
return { exitCode: 1, stdout: '', stderr };
127+
}
128+
129+
/**
130+
* Drives the retry delays and the boot deadline from the fake clock: every timer the boot path
131+
* schedules runs to completion, so a test that expects three attempts never sleeps for real. The
132+
* outcome is captured the moment it settles because the boot path can reject while the fake clock
133+
* is still turning, and an unobserved rejection would surface as an unhandled error.
134+
*/
135+
async function settleBoot(promise: Promise<void>): Promise<void> {
136+
const settled = promise.then(
137+
() => ({ ok: true as const, error: undefined }),
138+
(error: unknown) => ({ ok: false as const, error }),
139+
);
140+
await vi.runAllTimersAsync();
141+
const outcome = await settled;
142+
if (!outcome.ok) throw outcome.error;
143+
}
144+
145+
beforeEach(() => {
146+
vi.useFakeTimers({ now: 1_000 });
147+
});
148+
149+
afterEach(() => {
150+
vi.useRealTimers();
151+
});
152+
153+
test('ensureBootedSimulator retries a transient simctl boot failure and boots on the next attempt', async () => {
154+
let bootCalls = 0;
155+
mockRunCmdResponses({
156+
// The initial read says Shutdown; the attempt that lands the boot re-reads it as Booted.
157+
'xcrun simctl list devices -j': simulatorStateSequence('Shutdown', 'Booted'),
158+
'xcrun simctl boot sim-1': () => {
159+
bootCalls += 1;
160+
return bootCalls === 1 ? failedSimctlBoot('Unable to boot sim-1 right now') : OK_RESULT;
161+
},
162+
'xcrun simctl bootstatus sim-1 -b': OK_RESULT,
163+
'open -a Simulator': OK_RESULT,
164+
});
165+
166+
await settleBoot(ensureBootedSimulator(IOS_TEST_SIMULATOR));
167+
168+
// A boot failure that names no timeout or resource pressure is worth another attempt, and the
169+
// second attempt is what proves the retry policy let it through rather than the harness.
170+
assert.equal(bootCalls, 2);
171+
assert.equal(
172+
mockRunCmd.mock.calls.some(
173+
([cmd, args]) => cmd === 'open' && args.join(' ') === '-a Simulator',
174+
),
175+
true,
176+
);
177+
});
178+
179+
test('ensureBootedSimulator gives up without retrying a boot that reports a timeout', async () => {
180+
mockRunCmdResponses({
181+
'xcrun simctl list devices -j': simulatorListDevicesResult('Shutdown'),
182+
'xcrun simctl boot sim-1': () => failedSimctlBoot('timed out waiting for the device to boot'),
183+
});
184+
185+
await assert.rejects(
186+
() => settleBoot(ensureBootedSimulator(IOS_TEST_SIMULATOR)),
187+
(error: unknown) => {
188+
assert.equal(error instanceof AppError, true);
189+
const bootError = error as AppError;
190+
assert.equal(bootError.message, 'iOS simulator failed to boot');
191+
assert.equal(bootError.details?.reason, 'IOS_BOOT_TIMEOUT');
192+
assert.match(String(bootError.details?.hint), /simctl bootstatus logs/);
193+
// The failure happened before any bootstatus ran, so the classifier had to fall back to
194+
// the boot output: the typed reason proves it read the capture that exists.
195+
assert.deepEqual(bootError.details?.boot, {
196+
exitCode: 1,
197+
stdout: '',
198+
stderr: 'timed out waiting for the device to boot',
199+
});
200+
assert.equal(bootError.details?.bootstatus, undefined);
201+
return true;
202+
},
203+
);
204+
205+
assert.equal(
206+
mockRunCmd.mock.calls.filter(([, args]) => args.includes('boot') && args.includes('sim-1'))
207+
.length,
208+
1,
209+
);
210+
});
211+
212+
test('ensureBootedSimulator gives up without retrying a boot killed by resource starvation', async () => {
213+
mockRunCmdResponses({
214+
'xcrun simctl list devices -j': simulatorListDevicesResult('Shutdown'),
215+
'xcrun simctl boot sim-1': () => failedSimctlBoot('Killed: 9'),
216+
});
217+
218+
await assert.rejects(
219+
() => settleBoot(ensureBootedSimulator(IOS_TEST_SIMULATOR)),
220+
(error: unknown) => {
221+
const bootError = error as AppError;
222+
assert.equal(bootError.details?.reason, 'CI_RESOURCE_STARVATION_SUSPECTED');
223+
assert.match(String(bootError.details?.hint), /resource constrained/);
224+
return true;
225+
},
226+
);
227+
228+
assert.equal(
229+
mockRunCmd.mock.calls.filter(([, args]) => args[1] === 'boot' && args[2] === 'sim-1').length,
230+
1,
231+
);
232+
});
233+
234+
test('ensureBootedSimulator stops retrying when only the bootstatus output reports a timeout', async () => {
235+
mockRunCmdResponses({
236+
'xcrun simctl list devices -j': simulatorListDevicesResult('Shutdown'),
237+
'xcrun simctl boot sim-1': OK_RESULT,
238+
// `bootstatus -b` exits happily but its own progress output says the wait timed out, and the
239+
// state re-read that follows is what fails. Its error carries no command output, so only the
240+
// captured bootstatus can turn this into a boot timeout.
241+
'xcrun simctl bootstatus sim-1 -b': {
242+
exitCode: 0,
243+
stdout: 'Waiting for the device to boot\nTimed out waiting for the device to boot\n',
244+
stderr: '',
245+
},
246+
});
247+
248+
await assert.rejects(
249+
() => settleBoot(ensureBootedSimulator(IOS_TEST_SIMULATOR)),
250+
(error: unknown) => {
251+
const bootError = error as AppError;
252+
assert.equal(bootError.details?.reason, 'IOS_BOOT_TIMEOUT');
253+
assert.deepEqual(bootError.details?.bootstatus, {
254+
exitCode: 0,
255+
stdout: 'Waiting for the device to boot\nTimed out waiting for the device to boot\n',
256+
stderr: '',
257+
});
258+
return true;
259+
},
260+
);
261+
262+
assert.equal(mockRunCmd.mock.calls.filter(([, args]) => args[1] === 'boot').length, 1);
263+
});
264+
265+
test('ensureBootedSimulator reports a simulator that never reaches Booted after every attempt', async () => {
266+
mockRunCmdResponses({
267+
// Every attempt re-reads the state and finds the simulator still starting.
268+
'xcrun simctl list devices -j': simulatorStateSequence('Shutdown'),
269+
'xcrun simctl boot sim-1': OK_RESULT,
270+
'xcrun simctl bootstatus sim-1 -b': OK_RESULT,
271+
});
272+
273+
await assert.rejects(
274+
() => settleBoot(ensureBootedSimulator(IOS_TEST_SIMULATOR)),
275+
(error: unknown) => {
276+
const bootError = error as AppError;
277+
assert.equal(bootError.details?.reason, 'BOOT_COMMAND_FAILED');
278+
assert.equal(bootError.details?.deviceId, 'sim-1');
279+
assert.equal(bootError.details?.timeoutMs, IOS_BOOT_TIMEOUT_MS);
280+
return true;
281+
},
282+
);
283+
284+
assert.equal(mockRunCmd.mock.calls.filter(([, args]) => args[1] === 'boot').length, 3);
285+
assert.equal(
286+
mockRunCmd.mock.calls.some(
287+
([cmd, args]) => cmd === 'open' && args.join(' ') === '-a Simulator',
288+
),
289+
false,
290+
);
291+
});
292+
293+
test('ensureBootedSimulator stops at the boot deadline instead of starting another attempt', async () => {
294+
let booted = false;
295+
mockRunCmd.mockImplementation(async (cmd, args) => {
296+
const key = formatMockRunCmdCall(cmd, args);
297+
if (key === 'xcrun simctl list devices -j') {
298+
// Once the attempt that fails has re-read the state, the boot budget is spent: the policy
299+
// must clamp its wait to zero and stop, not spawn another `simctl boot`.
300+
const listReads = mockRunCmd.mock.calls.filter(([, callArgs]) =>
301+
callArgs.includes('list'),
302+
).length;
303+
if (listReads === 2) vi.advanceTimersByTime(IOS_BOOT_TIMEOUT_MS);
304+
return simulatorListDevicesResult('Shutdown');
305+
}
306+
if (key === 'xcrun simctl boot sim-1') {
307+
assert.ok(!booted, 'the deadline must stop the second attempt before it runs simctl boot');
308+
booted = true;
309+
return OK_RESULT;
310+
}
311+
if (key === 'xcrun simctl bootstatus sim-1 -b') return OK_RESULT;
312+
throw new Error(`Unexpected command: ${key}`);
313+
});
314+
315+
await assert.rejects(
316+
() => settleBoot(ensureBootedSimulator(IOS_TEST_SIMULATOR)),
317+
(error: unknown) => {
318+
const bootError = error as AppError;
319+
assert.equal(bootError.message, 'iOS simulator failed to boot');
320+
assert.equal(
321+
(bootError.details?.elapsedMs as number) >= IOS_BOOT_TIMEOUT_MS,
322+
true,
323+
`expected the budget to be spent, saw ${String(bootError.details?.elapsedMs)}ms`,
324+
);
325+
return true;
326+
},
327+
);
328+
329+
assert.equal(booted, true);
330+
});

‎packages/platform-apple/src/core/simulator.ts‎

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ export async function ensureBootedSimulator(
8888
}
8989
| undefined;
9090

91+
// The retry policy, the deadline classifier, and the final rethrow classify the same failure
92+
// against the same captured output. Either capture may still be absent when a classifier runs
93+
// — an early boot failure has no bootstatus — so the fallback order reads them live here.
94+
const classifyAttempt = (error: unknown) =>
95+
classifyBootFailure({
96+
error,
97+
stdout: bootStatusResult?.stdout ?? bootResult?.stdout,
98+
stderr: bootStatusResult?.stderr ?? bootResult?.stderr,
99+
context: { platform: 'ios', phase: 'boot' },
100+
});
101+
91102
try {
92103
await retryWithPolicy(
93104
async ({ deadline: attemptDeadline }) => {
@@ -140,35 +151,19 @@ export async function ensureBootedSimulator(
140151
maxDelayMs: 2000,
141152
jitter: 0.2,
142153
shouldRetry: (error) => {
143-
const reason = classifyBootFailure({
144-
error,
145-
stdout: bootStatusResult?.stdout ?? bootResult?.stdout,
146-
stderr: bootStatusResult?.stderr ?? bootResult?.stderr,
147-
context: { platform: 'ios', phase: 'boot' },
148-
});
154+
const reason = classifyAttempt(error);
149155
return reason !== 'IOS_BOOT_TIMEOUT' && reason !== 'CI_RESOURCE_STARVATION_SUSPECTED';
150156
},
151157
},
152158
{
153159
deadline,
154160
phase: 'boot',
155161
signal: options.signal,
156-
classifyReason: (error) =>
157-
classifyBootFailure({
158-
error,
159-
stdout: bootStatusResult?.stdout ?? bootResult?.stdout,
160-
stderr: bootStatusResult?.stderr ?? bootResult?.stderr,
161-
context: { platform: 'ios', phase: 'boot' },
162-
}),
162+
classifyReason: classifyAttempt,
163163
},
164164
);
165165
} catch (error) {
166-
const reason = classifyBootFailure({
167-
error,
168-
stdout: bootStatusResult?.stdout ?? bootResult?.stdout,
169-
stderr: bootStatusResult?.stderr ?? bootResult?.stderr,
170-
context: { platform: 'ios', phase: 'boot' },
171-
});
166+
const reason = classifyAttempt(error);
172167

173168
throw new AppError('COMMAND_FAILED', 'iOS simulator failed to boot', {
174169
platform: 'ios',

0 commit comments

Comments
 (0)