Skip to content

Commit f86926f

Browse files
committed
test(apple): make screenshot diagnostics tests hermetic
1 parent 422737d commit f86926f

3 files changed

Lines changed: 34 additions & 66 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { readFileSync } from 'node:fs';
2+
import path from 'node:path';
3+
import { withDiagnosticsScope } from '@agent-device/host-kit/diagnostics';
4+
import { mkdtempForTest } from '../../__tests__/tmp-dir.ts';
5+
6+
type DiagnosticEvent = {
7+
level: string;
8+
phase: string;
9+
data?: Record<string, unknown>;
10+
};
11+
12+
export async function statusBarDiagnosticEvents(run: () => Promise<unknown>) {
13+
const logPath = path.join(await mkdtempForTest('ios-status-bar-diagnostics-'), 'log.ndjson');
14+
await withDiagnosticsScope({ command: 'screenshot', debug: true, logPath }, run);
15+
return readFileSync(logPath, 'utf8')
16+
.trim()
17+
.split('\n')
18+
.map((line) => JSON.parse(line) as DiagnosticEvent)
19+
.filter((event) => event.phase.startsWith('ios_screenshot_status_bar_'))
20+
.map(({ level, phase, data }) => ({ level, phase, data }));
21+
}

‎packages/platform-apple/src/core/__tests__/screenshot-status-bar.test.ts‎

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import { beforeEach, test } from 'vitest';
22
import assert from 'node:assert/strict';
3-
import { readFileSync } from 'node:fs';
4-
import path from 'node:path';
5-
import { withDiagnosticsScope } from '@agent-device/host-kit/diagnostics';
63
import { AppError } from '@agent-device/kernel/errors';
74
import {
85
invalidateSimulatorStatusBarOverrideCache,
96
prepareSimulatorStatusBarForScreenshot,
107
} from '../screenshot-status-bar.ts';
118
import { withFakeAppleTool } from '../../__tests__/fake-apple-tool.ts';
12-
import { mkdtempForTest } from '../../__tests__/tmp-dir.ts';
139
import { IOS_TEST_SIMULATOR } from './apple-core-stub-helpers.ts';
10+
import { statusBarDiagnosticEvents } from './screenshot-diagnostic-log.ts';
1411

1512
// The fake tool provider installs through the production withAppleToolProvider
1613
// scope, so `calls` records the flat invocations the PATH-stub scripts saw.
@@ -35,25 +32,6 @@ function statusBarScript(listResponse: () => string | { stderr: string; exitCode
3532
};
3633
}
3734

38-
// A status bar step that fails best-effort has to reach the request log, or a debug run cannot
39-
// explain why a screenshot still shows a non-deterministic status bar.
40-
async function statusBarDiagnosticEvents(run: () => Promise<unknown>) {
41-
const logPath = path.join(
42-
await mkdtempForTest('agent-device-ios-status-bar-diag-'),
43-
'log.ndjson',
44-
);
45-
await withDiagnosticsScope({ command: 'screenshot', debug: true, logPath }, run);
46-
return readFileSync(logPath, 'utf8')
47-
.trim()
48-
.split('\n')
49-
.map(
50-
(line) =>
51-
JSON.parse(line) as { level: string; phase: string; data?: Record<string, unknown> },
52-
)
53-
.filter((event) => event.phase.startsWith('ios_screenshot_status_bar_'))
54-
.map(({ level, phase, data }) => ({ level, phase, data }));
55-
}
56-
5735
beforeEach(() => {
5836
invalidateSimulatorStatusBarOverrideCache(IOS_TEST_SIMULATOR);
5937
});
@@ -122,9 +100,6 @@ test('prepareSimulatorStatusBarForScreenshot still normalizes when snapshotting
122100
);
123101
});
124102

125-
// The command list above proves a failed step is tolerated; the log line is what proves the same
126-
// run can be explained. One event per failed step, carrying the device it failed on and the
127-
// normalized xcrun failure.
128103
test('a failed status bar snapshot logs snapshot_failed with the tool failure', async () => {
129104
const events = await statusBarDiagnosticEvents(async () => {
130105
await withFakeAppleTool(
@@ -170,16 +145,7 @@ test('a failed status bar override logs prepare_failed and the step stays best-e
170145
async ({ calls }) => {
171146
const restore = await prepareSimulatorStatusBarForScreenshot(IOS_TEST_SIMULATOR);
172147
await restore();
173-
// The deterministic override is the step that fails, so the screenshot proceeds with
174-
// whatever status bar the simulator kept, and restoration still runs.
175-
assert.deepEqual(
176-
calls.map((args) => args.join(' ')),
177-
[
178-
'simctl status_bar sim-1 list',
179-
'simctl status_bar sim-1 override --time 9:41 --dataNetwork wifi --wifiMode active --wifiBars 3 --batteryState charged --batteryLevel 100',
180-
'simctl status_bar sim-1 clear',
181-
],
182-
);
148+
assert.equal(calls.at(-1)?.join(' '), 'simctl status_bar sim-1 clear');
183149
},
184150
);
185151
});

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

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { beforeEach, test, vi } from 'vitest';
22
import assert from 'node:assert/strict';
3-
import { promises as fs, existsSync, readFileSync } from 'node:fs';
3+
import { promises as fs, readFileSync } from 'node:fs';
44
import path from 'node:path';
55
import type { RunnerScreenCaptureMetadata } from '@agent-device/contracts/screen-capture-contract';
66
import { bindTestCommandHost } from '@agent-device/test-kit';
@@ -65,6 +65,7 @@ import { retryWithPolicy } from '@agent-device/host-kit/retry';
6565
import { AppError } from '@agent-device/kernel/errors';
6666

6767
import { IOS_TEST_SIMULATOR, MACOS_TEST_DEVICE } from './apple-core-stub-helpers.ts';
68+
import { statusBarDiagnosticEvents } from './screenshot-diagnostic-log.ts';
6869

6970
const mockRunCmd = vi.mocked(runCmd);
7071
const mockRetryWithPolicy = vi.mocked(retryWithPolicy);
@@ -109,15 +110,6 @@ async function waitForFileText(filePath: string, attempts = 20): Promise<string>
109110
throw lastError;
110111
}
111112

112-
function statusBarItemLogLines(logPath: string) {
113-
if (!existsSync(logPath)) return [];
114-
return readFileSync(logPath, 'utf8')
115-
.trim()
116-
.split('\n')
117-
.filter((line) => line.includes('"ios_screenshot_status_bar_'))
118-
.map((line) => JSON.parse(line) as { level: string; phase: string; data?: unknown });
119-
}
120-
121113
test('shouldRetryIosSimulatorScreenshot detects simulator screen-surface timeout', () => {
122114
const error = new AppError('COMMAND_FAILED', 'Detected file type from extension: PNG', {
123115
stderr: 'Timeout waiting for screen surfaces',
@@ -371,27 +363,16 @@ test('captureSimulatorScreenshotWithFallback ignores status bar restore failures
371363
assert.equal(mockRunAppleRunnerCommand.mock.calls.length, 0);
372364
});
373365

374-
// The screenshot flow owns the prepare and restore steps around a capture, so it owns reporting
375-
// that they failed. Both must go through the status bar module's emitter: a second emitter here
376-
// would drift on the phase vocabulary or drop the normalized tool failure.
377-
async function statusBarDiagnosticEvents(run: () => Promise<void>) {
378-
const tmpDir = await mkdtempForTest('agent-device-ios-screenshot-status-bar-diag-');
379-
const logPath = path.join(tmpDir, 'diag.ndjson');
380-
try {
381-
await withDiagnosticsScope({ command: 'screenshot', debug: true, logPath }, run);
382-
return statusBarItemLogLines(logPath);
383-
} finally {
384-
await fs.rm(tmpDir, { recursive: true, force: true });
385-
}
386-
}
387-
388-
function statusBarItemSummary(event: { level: string; phase: string; data?: unknown }) {
389-
const data = event.data as { deviceId?: string; exitCode?: number };
366+
function statusBarItemSummary(event: {
367+
level: string;
368+
phase: string;
369+
data?: Record<string, unknown>;
370+
}) {
390371
return {
391372
level: event.level,
392373
phase: event.phase,
393-
deviceId: data.deviceId,
394-
exitCode: data.exitCode,
374+
deviceId: event.data?.deviceId,
375+
exitCode: event.data?.exitCode,
395376
};
396377
}
397378

@@ -408,7 +389,7 @@ test('captureSimulatorScreenshotWithFallback logs a status bar preparation failu
408389
env: process.env,
409390
appBundleId: 'com.example.app',
410391
normalizeStatusBar: true,
411-
deps: { normalizeDensity: async () => {} },
392+
deps: { normalizeDensity: async () => {}, resolveCaptureTarget: async () => ({}) },
412393
});
413394
});
414395

@@ -435,7 +416,7 @@ test('captureSimulatorScreenshotWithFallback logs a status bar restoration failu
435416
env: process.env,
436417
appBundleId: 'com.example.app',
437418
normalizeStatusBar: true,
438-
deps: { normalizeDensity: async () => {} },
419+
deps: { normalizeDensity: async () => {}, resolveCaptureTarget: async () => ({}) },
439420
});
440421
});
441422

0 commit comments

Comments
 (0)