Skip to content

Commit 2c90df4

Browse files
committed
test(ios): cover every branch of the simulator console capture launch
The Coverage CRAP gate scored runIosSimulatorConsoleLaunch at 30.5 (blocks at 30): the capture-launch env arm, the capture-bound timeout branch and the rethrow arm had no test. All of them now run through the Apple tool provider seam: console+capture spawns the merged host environment with the SIMCTL_CHILD pair, console-only spawns with no environment, the 25s console bound keeps the open alive and logs the partial bytes with its diagnostic (with and without output on the error), and a non-bound failure rethrows. The fake tool provider now records the exec options so the env merge is observable at the seam. The console-capture budget moved to core/config.ts beside every other launch timeout so the test names it without duplicating 25_000.
1 parent c3769fd commit 2c90df4

4 files changed

Lines changed: 242 additions & 12 deletions

File tree

‎packages/platform-apple/src/__tests__/fake-apple-tool.ts‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,38 @@ import type { DeviceInfo } from '@agent-device/kernel/device';
22
import { AppError } from '@agent-device/kernel/errors';
33
import type { AppleToolProvider } from '../core/tool-provider.ts';
44
import { withAppleToolProvider } from '../core/tool-provider.ts';
5-
import { execFailureDetails, type ExecResult } from '@agent-device/host-kit/command';
5+
import {
6+
execFailureDetails,
7+
type ExecOptions,
8+
type ExecResult,
9+
} from '@agent-device/host-kit/command';
610
import { IOS_DEVICE } from './device-fixtures.ts';
711

812
export type FakeAppleToolResponse = string | Partial<ExecResult> | Error;
913
export type FakeAppleToolScript = (args: string[]) => FakeAppleToolResponse | undefined;
1014

1115
export async function withFakeAppleTool<T>(
1216
script: FakeAppleToolScript,
13-
run: (ctx: { calls: string[][]; device: DeviceInfo }) => Promise<T>,
17+
run: (ctx: {
18+
calls: string[][];
19+
/** One entry per call: the exec options the command boundary passed through. */
20+
execOptions: Array<{ env?: NodeJS.ProcessEnv; timeoutMs?: number }>;
21+
device: DeviceInfo;
22+
}) => Promise<T>,
1423
options: { device?: DeviceInfo; provider?: Partial<AppleToolProvider> } = {},
1524
): Promise<T> {
1625
const device: DeviceInfo = { ...(options.device ?? IOS_DEVICE) };
1726
const calls: string[][] = [];
27+
const execOptions: Array<{ env?: NodeJS.ProcessEnv; timeoutMs?: number }> = [];
1828

1929
const respond = async (
2030
flat: string[],
2131
executable: string,
2232
allowFailure: boolean | undefined,
33+
options?: ExecOptions,
2334
): Promise<ExecResult> => {
2435
calls.push([...flat]);
36+
execOptions.push({ env: options?.env, timeoutMs: options?.timeoutMs });
2537
const response = script(flat);
2638
if (response instanceof Error) throw response;
2739
const result: ExecResult =
@@ -42,16 +54,24 @@ export async function withFakeAppleTool<T>(
4254
...options.provider,
4355
whichCommand: options.provider?.whichCommand ?? (async () => true),
4456
runCommand: async (cmd, args, execOptions) =>
45-
await respond(cmd === 'xcrun' ? [...args] : [cmd, ...args], cmd, execOptions?.allowFailure),
57+
await respond(
58+
cmd === 'xcrun' ? [...args] : [cmd, ...args],
59+
cmd,
60+
execOptions?.allowFailure,
61+
execOptions,
62+
),
4663
simctl: {
4764
run: async (args, execOptions) =>
48-
await respond(['simctl', ...args], 'xcrun', execOptions?.allowFailure),
65+
await respond(['simctl', ...args], 'xcrun', execOptions?.allowFailure, execOptions),
4966
},
5067
devicectl: {
5168
run: async (args, execOptions) =>
52-
await respond(['devicectl', ...args], 'xcrun', execOptions?.allowFailure),
69+
await respond(['devicectl', ...args], 'xcrun', execOptions?.allowFailure, execOptions),
5370
},
5471
};
5572

56-
return await withAppleToolProvider(provider, async () => await run({ calls, device }));
73+
return await withAppleToolProvider(
74+
provider,
75+
async () => await run({ calls, execOptions, device }),
76+
);
5777
}

‎packages/platform-apple/src/core/__tests__/app-launch-open.test.ts‎

Lines changed: 212 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { beforeEach, test, vi } from 'vitest';
1+
import { beforeEach, expect, test, vi } from 'vitest';
22
import assert from 'node:assert/strict';
33
import { promises as fs } from 'node:fs';
44
import path from 'node:path';
55
import { mkdtempForTest } from '../../__tests__/tmp-dir.ts';
6-
76
vi.mock('@agent-device/host-kit/command', async (importOriginal) => {
87
const actual = await importOriginal<typeof import('@agent-device/host-kit/command')>();
98
return { ...actual, runCmd: vi.fn(actual.runCmd) };
@@ -12,6 +11,15 @@ vi.mock('../simulator.ts', async (importOriginal) => {
1211
const actual = await importOriginal<typeof import('../simulator.ts')>();
1312
return { ...actual, ensureBootedSimulator: vi.fn(actual.ensureBootedSimulator) };
1413
});
14+
vi.mock('@agent-device/host-kit/diagnostics', async (importOriginal) => {
15+
const actual = await importOriginal<typeof import('@agent-device/host-kit/diagnostics')>();
16+
return { ...actual, emitDiagnostic: vi.fn() };
17+
});
18+
// The probe build a capture launch triggers belongs to launch-env.test.ts and the provider
19+
// scenario; here the cache answers with a fixed path so no test in this file reaches clang.
20+
vi.mock('../../network/probe-cache.ts', () => ({
21+
ensureNetworkCaptureProbe: vi.fn(),
22+
}));
1523

1624
const execActual = await vi.importActual<typeof import('@agent-device/host-kit/command')>(
1725
'@agent-device/host-kit/command',
@@ -20,9 +28,12 @@ const simulatorActual = await vi.importActual<typeof import('../simulator.ts')>(
2028

2129
import { openIosApp } from '../app-launch.ts';
2230
import { ensureBootedSimulator } from '../simulator.ts';
23-
import { IOS_SIMULATOR_OPENURL_TIMEOUT_MS } from '../config.ts';
31+
import { ensureNetworkCaptureProbe } from '../../network/probe-cache.ts';
32+
import { IOS_SIMULATOR_CONSOLE_CAPTURE_MS, IOS_SIMULATOR_OPENURL_TIMEOUT_MS } from '../config.ts';
2433
import type { DeviceInfo } from '@agent-device/kernel/device';
25-
import { runCmd } from '@agent-device/host-kit/command';
34+
import { AppError } from '@agent-device/kernel/errors';
35+
import { runCmd, withCommandHost } from '@agent-device/host-kit/command';
36+
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
2637
import { assertRejectsAppError } from '../../__tests__/app-error.ts';
2738
import { withFakeAppleTool } from '../../__tests__/fake-apple-tool.ts';
2839
import {
@@ -33,6 +44,17 @@ import {
3344

3445
const mockRunCmd = vi.mocked(runCmd);
3546
const mockEnsureBootedSimulator = vi.mocked(ensureBootedSimulator);
47+
const mockEnsureNetworkCaptureProbe = vi.mocked(ensureNetworkCaptureProbe);
48+
const mockEmitDiagnostic = vi.mocked(emitDiagnostic);
49+
50+
/** The one diagnostic this file's timeout branches emit, by phase. */
51+
function findDiagnostic(phase: string): { level: string; data: Record<string, unknown> } {
52+
const event = mockEmitDiagnostic.mock.calls
53+
.map(([event]) => event)
54+
.find((event) => event.phase === phase);
55+
assert.ok(event, `expected a diagnostic for phase ${phase}`);
56+
return event as unknown as { level: string; data: Record<string, unknown> };
57+
}
3658

3759
beforeEach(() => {
3860
vi.resetAllMocks();
@@ -126,12 +148,15 @@ test('openIosApp captures iOS simulator launch console output when requested', a
126148
}
127149
return '';
128150
},
129-
async ({ calls }) => {
151+
async ({ calls, execOptions }) => {
130152
await openIosApp(IOS_TEST_SIMULATOR, 'MyApp', {
131153
appBundleId: 'com.example.app',
132154
launchConsole: launchConsolePath,
133155
});
134156
assert.deepEqual(calls, [['simctl', 'launch', '--console-pty', 'sim-1', 'com.example.app']]);
157+
// The closest negative to the capture launch: with no capture requested the console
158+
// launch spawns through the bare seam and no environment reaches it.
159+
assert.equal(execOptions[0]?.env, undefined);
135160
assert.equal(
136161
await fs.readFile(launchConsolePath, 'utf8'),
137162
'console stdout\nconsole stderr\n',
@@ -140,6 +165,188 @@ test('openIosApp captures iOS simulator launch console output when requested', a
140165
);
141166
});
142167

168+
test('openIosApp with a console and a network capture spawns the merged host environment', async () => {
169+
const tmpDir = await mkdtempForTest('agent-device-ios-console-capture-test-');
170+
const launchConsolePath = path.join(tmpDir, 'console.log');
171+
mockEnsureBootedSimulator.mockResolvedValue();
172+
mockEnsureNetworkCaptureProbe.mockResolvedValue({ path: '/cache/ADNetworkCapture.dylib' });
173+
174+
await withCommandHost(
175+
{ host: { env: { PATH: '/usr/bin' }, cwd: tmpDir }, requestEnv: undefined },
176+
async () =>
177+
await withFakeAppleTool(
178+
() => ({ stdout: 'captured stdout', stderr: '' }),
179+
async ({ calls, execOptions }) => {
180+
await openIosApp(IOS_TEST_SIMULATOR, 'MyApp', {
181+
appBundleId: 'com.example.app',
182+
launchConsole: launchConsolePath,
183+
networkCapturePath: '/sessions/one/network.jsonl',
184+
});
185+
assert.deepEqual(calls, [
186+
[
187+
'simctl',
188+
'launch',
189+
'--console-pty',
190+
'--terminate-running-process',
191+
'sim-1',
192+
'com.example.app',
193+
],
194+
]);
195+
// A capture launch crosses the host-env spawn boundary even for the console variant:
196+
// the host environment carries along and the two SIMCTL_CHILD overrides land on top.
197+
assert.equal(execOptions[0]?.timeoutMs, IOS_SIMULATOR_CONSOLE_CAPTURE_MS);
198+
assert.equal(execOptions[0]?.env?.PATH, '/usr/bin');
199+
assert.equal(
200+
execOptions[0]?.env?.SIMCTL_CHILD_DYLD_INSERT_LIBRARIES,
201+
'/cache/ADNetworkCapture.dylib',
202+
);
203+
assert.equal(
204+
execOptions[0]?.env?.SIMCTL_CHILD_AGENT_DEVICE_NETWORK_CAPTURE_PATH,
205+
'/sessions/one/network.jsonl',
206+
);
207+
assert.equal(await fs.readFile(launchConsolePath, 'utf8'), 'captured stdout');
208+
},
209+
),
210+
);
211+
});
212+
213+
test('openIosApp with a network capture and no console spawns the plain launch with that environment', async () => {
214+
mockEnsureBootedSimulator.mockResolvedValue();
215+
mockEnsureNetworkCaptureProbe.mockResolvedValue({ path: '/cache/ADNetworkCapture.dylib' });
216+
217+
await withCommandHost(
218+
{
219+
host: { env: { PATH: '/usr/bin' }, cwd: process.cwd() },
220+
requestEnv: undefined,
221+
},
222+
async () =>
223+
await withFakeAppleTool(
224+
() => '',
225+
async ({ calls, execOptions }) => {
226+
await openIosApp(IOS_TEST_SIMULATOR, 'MyApp', {
227+
appBundleId: 'com.example.app',
228+
networkCapturePath: '/sessions/one/network.jsonl',
229+
});
230+
// Capture forces a fresh process but does not ask for the console pipe.
231+
assert.deepEqual(calls, [
232+
['simctl', 'launch', '--terminate-running-process', 'sim-1', 'com.example.app'],
233+
]);
234+
assert.equal(
235+
execOptions[0]?.env?.SIMCTL_CHILD_DYLD_INSERT_LIBRARIES,
236+
'/cache/ADNetworkCapture.dylib',
237+
);
238+
assert.equal(execOptions[0]?.env?.PATH, '/usr/bin');
239+
},
240+
),
241+
);
242+
});
243+
244+
test('openIosApp keeps the open alive when the console capture bound fires and logs what was printed', async () => {
245+
const tmpDir = await mkdtempForTest('agent-device-ios-console-timeout-test-');
246+
const launchConsolePath = path.join(tmpDir, 'console.log');
247+
mockEnsureBootedSimulator.mockResolvedValue();
248+
249+
await withFakeAppleTool(
250+
(args) => {
251+
if (args[0] === 'simctl' && args[1] === 'launch') {
252+
throw new AppError('COMMAND_FAILED', 'xcrun timed out after 25000ms', {
253+
stdout: 'partial output',
254+
stderr: 'truncated stderr',
255+
exitCode: 124,
256+
timeoutMs: IOS_SIMULATOR_CONSOLE_CAPTURE_MS,
257+
});
258+
}
259+
return '';
260+
},
261+
async () => {
262+
// The bound on the console pipe is a capture budget, not a launch failure: the open
263+
// resolves, the bytes killed with the child land in the log, and the diagnostic tells.
264+
await openIosApp(IOS_TEST_SIMULATOR, 'MyApp', {
265+
appBundleId: 'com.example.app',
266+
launchConsole: launchConsolePath,
267+
});
268+
assert.equal(
269+
await fs.readFile(launchConsolePath, 'utf8'),
270+
'partial output\ntruncated stderr',
271+
);
272+
const diagnostic = findDiagnostic('ios_simulator_launch_console_capture_timeout');
273+
expect(diagnostic).toMatchObject({
274+
level: 'warn',
275+
data: {
276+
timeoutMs: IOS_SIMULATOR_CONSOLE_CAPTURE_MS,
277+
logPath: launchConsolePath,
278+
stdoutBytes: 14,
279+
stderrBytes: 16,
280+
},
281+
});
282+
},
283+
);
284+
});
285+
286+
test('openIosApp logs an empty console when the capture-bound error carries no output at all', async () => {
287+
const tmpDir = await mkdtempForTest('agent-device-ios-console-timeout-empty-test-');
288+
const launchConsolePath = path.join(tmpDir, 'console.log');
289+
mockEnsureBootedSimulator.mockResolvedValue();
290+
mockEnsureNetworkCaptureProbe.mockResolvedValue({ path: '/cache/ADNetworkCapture.dylib' });
291+
292+
await withCommandHost(
293+
{ host: { env: { PATH: '/usr/bin' }, cwd: tmpDir }, requestEnv: undefined },
294+
async () =>
295+
await withFakeAppleTool(
296+
(args) => {
297+
if (args[0] === 'simctl' && args[1] === 'launch') {
298+
throw new AppError('COMMAND_FAILED', 'xcrun timed out after 25000ms', {
299+
timeoutMs: IOS_SIMULATOR_CONSOLE_CAPTURE_MS,
300+
});
301+
}
302+
return '';
303+
},
304+
async ({ execOptions }) => {
305+
await openIosApp(IOS_TEST_SIMULATOR, 'MyApp', {
306+
appBundleId: 'com.example.app',
307+
launchConsole: launchConsolePath,
308+
networkCapturePath: '/sessions/one/network.jsonl',
309+
});
310+
// The timeout branch reads missing stdout/stderr as empty and still writes the log,
311+
// on the capture spawn variant.
312+
assert.equal(
313+
execOptions[0]?.env?.SIMCTL_CHILD_DYLD_INSERT_LIBRARIES,
314+
'/cache/ADNetworkCapture.dylib',
315+
);
316+
assert.equal(await fs.readFile(launchConsolePath, 'utf8'), '');
317+
const diagnostic = findDiagnostic('ios_simulator_launch_console_capture_timeout');
318+
expect(diagnostic).toMatchObject({
319+
data: { stdoutBytes: 0, stderrBytes: 0 },
320+
});
321+
},
322+
),
323+
);
324+
});
325+
326+
test('openIosApp rethrows a console launch failure that is not the capture bound', async () => {
327+
const tmpDir = await mkdtempForTest('agent-device-ios-console-error-test-');
328+
const launchConsolePath = path.join(tmpDir, 'console.log');
329+
mockEnsureBootedSimulator.mockResolvedValue();
330+
331+
await assert.rejects(
332+
withFakeAppleTool(
333+
(args) => {
334+
if (args[0] === 'simctl' && args[1] === 'launch') {
335+
throw new Error('console pipe exploded');
336+
}
337+
return '';
338+
},
339+
async () => {
340+
await openIosApp(IOS_TEST_SIMULATOR, 'MyApp', {
341+
appBundleId: 'com.example.app',
342+
launchConsole: launchConsolePath,
343+
});
344+
},
345+
),
346+
/console pipe exploded/,
347+
);
348+
});
349+
143350
test('openIosApp emits a clean simctl launch when launchArgs is an empty array', async () => {
144351
mockEnsureBootedSimulator.mockResolvedValue();
145352

‎packages/platform-apple/src/core/app-launch.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from '@agent-device/contracts/command';
2020
import {
2121
IOS_APP_LAUNCH_TIMEOUT_MS,
22+
IOS_SIMULATOR_CONSOLE_CAPTURE_MS,
2223
IOS_SIMULATOR_OPENURL_TIMEOUT_MS,
2324
IOS_SIMULATOR_TERMINATE_TIMEOUT_MS,
2425
} from './config.ts';
@@ -42,7 +43,6 @@ import { closeMacOsApp, openMacOsApp } from '../os/macos/apps.ts';
4243
import { resolveIosApp } from './app-resolution.ts';
4344
import { buildSimctlArgsForDevice } from './simctl.ts';
4445

45-
const IOS_SIMULATOR_CONSOLE_CAPTURE_MS = 25_000;
4646
const IOS_SIMULATOR_LAUNCH_ARGS_WITH_URL_MESSAGE =
4747
'--launch-args is not supported with iOS simulator URL opens (simctl openurl ignores launch args). Launch the app first with --launch-args, then issue the URL open in a separate call.';
4848

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export const IOS_SIMULATOR_TERMINATE_TIMEOUT_MS = 15_000;
2222
*/
2323
export const IOS_SIMULATOR_OPENURL_TIMEOUT_MS = 20_000;
2424

25+
/** Bound on one `simctl launch --console-pty` read; a console launch is kept alive past this with a diagnostic. */
26+
export const IOS_SIMULATOR_CONSOLE_CAPTURE_MS = 25_000;
27+
2528
export const IOS_SIMULATOR_SCREENSHOT_TIMEOUT_MS = 20_000;
2629

2730
// The CoreDevice panel probe runs on the same request budget as the capture it

0 commit comments

Comments
 (0)