Skip to content

Commit ea981ab

Browse files
test(apple-runner): readiness preflight decisions get their own file
`runner-command-retry.test.ts` sits above the test-size tripwire and may not grow, and the four preflight verdicts did not belong in a retry aggregation anyway: they answer one question about what `executeRunnerCommand` does with a runner that refused before the command was written. `makeRunnerSession` comes from the fixtures module that already owns it. Co-authored-by: Apex by Callstack <[email protected]>
1 parent 8b250ca commit ea981ab

2 files changed

Lines changed: 179 additions & 93 deletions

File tree

‎packages/platform-apple/src/runner/__tests__/runner-command-retry.test.ts‎

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -342,99 +342,6 @@ test('mutating commands retry startup sessions with stale bundle cleanup', async
342342
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], freshSession);
343343
});
344344

345-
test('mutating commands restart stale sessions when readiness preflight fails before command send', async () => {
346-
const staleSession = makeRunnerSession({ port: 8100, ready: true });
347-
const freshSession = makeRunnerSession({ port: 8101, ready: false });
348-
349-
mockEnsureRunnerSession.mockResolvedValueOnce(staleSession).mockResolvedValueOnce(freshSession);
350-
mockExecuteRunnerCommandWithSession
351-
.mockRejectedValueOnce(
352-
new AppError('COMMAND_FAILED', 'fetch failed', {
353-
runnerReadinessPreflightFailed: true,
354-
}),
355-
)
356-
.mockResolvedValueOnce({ message: 'tapped' });
357-
358-
const result = await runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 });
359-
360-
assert.deepEqual(result, { message: 'tapped' });
361-
assert.equal(mockEnsureRunnerSession.mock.calls.length, 2);
362-
assert.deepEqual(mockInvalidateRunnerSession.mock.calls[0], [
363-
staleSession,
364-
'runner_readiness_preflight_failed_before_command_send',
365-
]);
366-
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls.length, 2);
367-
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], freshSession);
368-
});
369-
370-
test('mutating commands restart stale sessions when readiness preflight outlives its deadline', async () => {
371-
const staleSession = makeRunnerSession({ port: 8100, ready: true });
372-
const freshSession = makeRunnerSession({ port: 8101, ready: false });
373-
374-
mockEnsureRunnerSession.mockResolvedValueOnce(staleSession).mockResolvedValueOnce(freshSession);
375-
mockExecuteRunnerCommandWithSession
376-
.mockRejectedValueOnce(
377-
new AppError('COMMAND_FAILED', 'xcrun timed out after 45000ms', {
378-
cmd: 'xcrun',
379-
timeoutMs: 45_000,
380-
runnerReadinessPreflightFailed: true,
381-
}),
382-
)
383-
.mockResolvedValueOnce({ message: 'tapped' });
384-
385-
const result = await runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 });
386-
387-
assert.deepEqual(result, { message: 'tapped' });
388-
assert.equal(mockEnsureRunnerSession.mock.calls.length, 2);
389-
assert.deepEqual(mockInvalidateRunnerSession.mock.calls[0], [
390-
staleSession,
391-
'runner_readiness_preflight_failed_before_command_send',
392-
]);
393-
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls.length, 2);
394-
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], freshSession);
395-
});
396-
397-
test('a readiness preflight refusal that is neither transport-shaped nor deadline-shaped surfaces', async () => {
398-
const staleSession = makeRunnerSession({ port: 8100, ready: true });
399-
400-
mockEnsureRunnerSession.mockResolvedValueOnce(staleSession);
401-
mockExecuteRunnerCommandWithSession.mockRejectedValueOnce(
402-
new AppError('COMMAND_FAILED', 'Runner readiness refused', {
403-
runnerReadinessPreflightFailed: true,
404-
}),
405-
);
406-
407-
await assert.rejects(
408-
() => runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }),
409-
(error: unknown) => {
410-
assert.ok(error instanceof AppError);
411-
assert.equal(error.message, 'Runner readiness refused');
412-
return true;
413-
},
414-
);
415-
assert.equal(mockEnsureRunnerSession.mock.calls.length, 1);
416-
});
417-
418-
test('a boot that exited early does not wipe a restored runner artifact', async () => {
419-
const fixtures = makeBadCacheRecoveryFixtures();
420-
421-
mockEnsureRunnerSession.mockResolvedValueOnce(fixtures.restoredSession);
422-
mockExecuteRunnerCommandWithSession.mockRejectedValueOnce(
423-
new AppError('COMMAND_FAILED', 'Runner did not accept connection (xcodebuild exited early)'),
424-
);
425-
426-
await assert.rejects(
427-
() => prepareIosRunner(IOS_SIMULATOR, { healthTimeoutMs: 90_000 }),
428-
(error: unknown) => {
429-
assert.ok(error instanceof AppError);
430-
assert.equal(error.message, 'Runner did not accept connection (xcodebuild exited early)');
431-
return true;
432-
},
433-
);
434-
assert.equal(mockMarkRunnerXctestrunArtifactBadForRun.mock.calls.length, 0);
435-
assert.equal(mockEnsureRunnerSession.mock.calls.length, 1);
436-
});
437-
438345
test('mutating commands emit readiness recovery diagnostics after failed preflight restart succeeds', async () => {
439346
const staleSession = makeRunnerSession({ port: 8100, ready: true });
440347
const freshSession = makeRunnerSession({ port: 8101, ready: false });
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import assert from 'node:assert/strict';
2+
import { beforeEach, test, vi } from 'vitest';
3+
import { AppError } from '@agent-device/kernel/errors';
4+
import { appleRunnerTestHost } from '../test-host.ts';
5+
import type { RunnerXctestrunArtifact } from '../runner-xctestrun.ts';
6+
import { IOS_SIMULATOR } from './device-fixtures.ts';
7+
import { createTestRequestCancellation, makeRunnerSession } from './runner-session-fixtures.ts';
8+
9+
const {
10+
mockEnsureRunnerSession,
11+
mockExecuteRunnerCommandWithSession,
12+
mockEmitDiagnostic,
13+
mockGetRunnerSessionSnapshot,
14+
mockInvalidateRunnerSession,
15+
mockMarkRunnerXctestrunArtifactBadForRun,
16+
} = vi.hoisted(() => ({
17+
mockEnsureRunnerSession: vi.fn(),
18+
mockExecuteRunnerCommandWithSession: vi.fn(),
19+
mockEmitDiagnostic: vi.fn(),
20+
mockGetRunnerSessionSnapshot: vi.fn(),
21+
mockInvalidateRunnerSession: vi.fn(),
22+
mockMarkRunnerXctestrunArtifactBadForRun: vi.fn(),
23+
}));
24+
25+
vi.mock('../runner-session.ts', async () => {
26+
const actual =
27+
await vi.importActual<typeof import('../runner-session.ts')>('../runner-session.ts');
28+
return {
29+
...actual,
30+
ensureRunnerSession: mockEnsureRunnerSession,
31+
executeRunnerCommandWithSession: mockExecuteRunnerCommandWithSession,
32+
getRunnerSessionSnapshot: mockGetRunnerSessionSnapshot,
33+
invalidateRunnerSession: mockInvalidateRunnerSession,
34+
};
35+
});
36+
37+
vi.mock('../runner-xctestrun.ts', async () => {
38+
const actual =
39+
await vi.importActual<typeof import('../runner-xctestrun.ts')>('../runner-xctestrun.ts');
40+
return {
41+
...actual,
42+
markRunnerXctestrunArtifactBadForRun: mockMarkRunnerXctestrunArtifactBadForRun,
43+
};
44+
});
45+
46+
import { prepareIosRunner, runAppleRunnerCommand } from '../runner-client.ts';
47+
import { resetRunnerRecycleLedgerForTests } from '../runner-recycle-ledger.ts';
48+
49+
// What `executeRunnerCommand` decides when a readiness preflight refuses the runner
50+
// before the command was ever written: the table's own verdict axes say whether the
51+
// session restarts, whether the restored artifact is suspect, and whether the caller
52+
// simply hears the refusal.
53+
54+
const requestCancellation = createTestRequestCancellation();
55+
const { isRequestCanceled } = requestCancellation;
56+
57+
beforeEach(() => {
58+
vi.resetAllMocks();
59+
resetRunnerRecycleLedgerForTests();
60+
mockGetRunnerSessionSnapshot.mockReturnValue(null);
61+
mockMarkRunnerXctestrunArtifactBadForRun.mockResolvedValue(undefined);
62+
requestCancellation.reset();
63+
appleRunnerTestHost.update({
64+
emitDiagnostic: mockEmitDiagnostic,
65+
isRequestCanceled,
66+
getRequestSignal: () => undefined,
67+
});
68+
});
69+
70+
function makeRunnerArtifact(
71+
overrides: Partial<RunnerXctestrunArtifact> = {},
72+
): RunnerXctestrunArtifact {
73+
return {
74+
xctestrunPath: '/tmp/runner.xctestrun',
75+
derived: '/tmp/derived',
76+
cache: 'exact',
77+
artifact: 'valid',
78+
buildMs: 0,
79+
xctestrunPathSource: 'manifest',
80+
...overrides,
81+
};
82+
}
83+
84+
test('mutating commands restart stale sessions when readiness preflight fails before command send', async () => {
85+
const staleSession = makeRunnerSession({ port: 8100, ready: true });
86+
const freshSession = makeRunnerSession({ port: 8101, ready: false });
87+
88+
mockEnsureRunnerSession.mockResolvedValueOnce(staleSession).mockResolvedValueOnce(freshSession);
89+
mockExecuteRunnerCommandWithSession
90+
.mockRejectedValueOnce(
91+
new AppError('COMMAND_FAILED', 'fetch failed', {
92+
runnerReadinessPreflightFailed: true,
93+
}),
94+
)
95+
.mockResolvedValueOnce({ message: 'tapped' });
96+
97+
const result = await runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 });
98+
99+
assert.deepEqual(result, { message: 'tapped' });
100+
assert.equal(mockEnsureRunnerSession.mock.calls.length, 2);
101+
assert.deepEqual(mockInvalidateRunnerSession.mock.calls[0], [
102+
staleSession,
103+
'runner_readiness_preflight_failed_before_command_send',
104+
]);
105+
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls.length, 2);
106+
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], freshSession);
107+
});
108+
109+
test('mutating commands restart stale sessions when readiness preflight outlives its deadline', async () => {
110+
const staleSession = makeRunnerSession({ port: 8100, ready: true });
111+
const freshSession = makeRunnerSession({ port: 8101, ready: false });
112+
113+
mockEnsureRunnerSession.mockResolvedValueOnce(staleSession).mockResolvedValueOnce(freshSession);
114+
mockExecuteRunnerCommandWithSession
115+
.mockRejectedValueOnce(
116+
new AppError('COMMAND_FAILED', 'xcrun timed out after 45000ms', {
117+
cmd: 'xcrun',
118+
timeoutMs: 45_000,
119+
runnerReadinessPreflightFailed: true,
120+
}),
121+
)
122+
.mockResolvedValueOnce({ message: 'tapped' });
123+
124+
const result = await runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 });
125+
126+
assert.deepEqual(result, { message: 'tapped' });
127+
assert.equal(mockEnsureRunnerSession.mock.calls.length, 2);
128+
assert.deepEqual(mockInvalidateRunnerSession.mock.calls[0], [
129+
staleSession,
130+
'runner_readiness_preflight_failed_before_command_send',
131+
]);
132+
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls.length, 2);
133+
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], freshSession);
134+
});
135+
136+
test('a readiness preflight refusal that is neither transport-shaped nor deadline-shaped surfaces', async () => {
137+
const staleSession = makeRunnerSession({ port: 8100, ready: true });
138+
139+
mockEnsureRunnerSession.mockResolvedValueOnce(staleSession);
140+
mockExecuteRunnerCommandWithSession.mockRejectedValueOnce(
141+
new AppError('COMMAND_FAILED', 'Runner readiness refused', {
142+
runnerReadinessPreflightFailed: true,
143+
}),
144+
);
145+
146+
await assert.rejects(
147+
() => runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }),
148+
(error: unknown) => {
149+
assert.ok(error instanceof AppError);
150+
assert.equal(error.message, 'Runner readiness refused');
151+
return true;
152+
},
153+
);
154+
assert.equal(mockEnsureRunnerSession.mock.calls.length, 1);
155+
});
156+
157+
test('a boot that exited early does not wipe a restored runner artifact', async () => {
158+
const restoredSession = makeRunnerSession({
159+
port: 8100,
160+
xctestrunPath: '/tmp/restored.xctestrun',
161+
xctestrunArtifact: makeRunnerArtifact({ xctestrunPath: '/tmp/restored.xctestrun' }),
162+
});
163+
164+
mockEnsureRunnerSession.mockResolvedValueOnce(restoredSession);
165+
mockExecuteRunnerCommandWithSession.mockRejectedValueOnce(
166+
new AppError('COMMAND_FAILED', 'Runner did not accept connection (xcodebuild exited early)'),
167+
);
168+
169+
await assert.rejects(
170+
() => prepareIosRunner(IOS_SIMULATOR, { healthTimeoutMs: 90_000 }),
171+
(error: unknown) => {
172+
assert.ok(error instanceof AppError);
173+
assert.equal(error.message, 'Runner did not accept connection (xcodebuild exited early)');
174+
return true;
175+
},
176+
);
177+
assert.equal(mockMarkRunnerXctestrunArtifactBadForRun.mock.calls.length, 0);
178+
assert.equal(mockEnsureRunnerSession.mock.calls.length, 1);
179+
});

0 commit comments

Comments
 (0)