|
1 | | -import { beforeEach, test, vi } from 'vitest'; |
| 1 | +import { afterEach, beforeEach, test, vi } from 'vitest'; |
2 | 2 | import assert from 'node:assert/strict'; |
3 | 3 | 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'; |
5 | 5 | import { AppError } from '@agent-device/kernel/errors'; |
6 | 6 | import { runCmd } from '@agent-device/host-kit/command'; |
7 | 7 | import { IOS_TEST_SIMULATOR } from './apple-core-stub-helpers.ts'; |
@@ -121,3 +121,210 @@ test('openIosSimulatorApp times out instead of hanging indefinitely', async () = |
121 | 121 | }, |
122 | 122 | ); |
123 | 123 | }); |
| 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 | +}); |
0 commit comments