Skip to content

Commit 6b39b1e

Browse files
authored
test(apple): cover simulator boot retry and the macOS helper source walk (#3459)
* 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. * test(apple): assert the boot memo is clean before each boot case The unit lane clears process memos between cases, so a case cannot inherit a Booted simulator and pass without reaching the retry policy. Assert that per case instead of trusting the lane-wide setup: poisoning the memo makes every boot case fail, so none of them can pass by skipping `simctl list`.
1 parent 1d4b16d commit 6b39b1e

3 files changed

Lines changed: 446 additions & 22 deletions

File tree

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

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

‎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)