Skip to content

Commit 7a113cc

Browse files
committed
test(apple-runner): pin exchange accounting through real HTTP
1 parent 7be94ce commit 7a113cc

3 files changed

Lines changed: 121 additions & 14 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import assert from 'node:assert/strict';
2+
import type { ExecResult } from '@agent-device/host-kit/command';
3+
import { afterEach, test } from 'vitest';
4+
import { executeRunnerExchange } from '../runner-exchange.ts';
5+
import { RunnerCommandAccounting, type RunnerSession } from '../runner-session-types.ts';
6+
import { IOS_SIMULATOR } from './device-fixtures.ts';
7+
import { startFakeRunnerServer, type FakeRunnerServer } from './fake-runner-server.ts';
8+
9+
let server: FakeRunnerServer | undefined;
10+
11+
afterEach(async () => {
12+
await server?.close();
13+
server = undefined;
14+
});
15+
16+
function sessionFor(port: number): RunnerSession {
17+
return {
18+
sessionId: `exchange:${port}`,
19+
device: IOS_SIMULATOR,
20+
deviceId: IOS_SIMULATOR.id,
21+
port,
22+
xctestrunPath: '/tmp/fake.xctestrun',
23+
jsonPath: '/tmp/fake.json',
24+
testPromise: new Promise<ExecResult>(() => {}),
25+
child: { pid: process.pid, exitCode: null },
26+
state: 'ready',
27+
commandCharges: new RunnerCommandAccounting(),
28+
};
29+
}
30+
31+
test('the exchange preflights a mutation and settles only its answer', async () => {
32+
server = await startFakeRunnerServer({
33+
uptime: [{ kind: 'ok', data: { uptimeMs: 5 } }],
34+
tap: [{ kind: 'ok', data: { tapped: true } }],
35+
});
36+
const session = sessionFor(server.port);
37+
const invalidations: string[] = [];
38+
39+
const result = await executeRunnerExchange(
40+
IOS_SIMULATOR,
41+
session,
42+
{ command: 'tap', x: 10, y: 10, appBundleId: 'com.example.app' },
43+
undefined,
44+
10_000,
45+
async (reason) => {
46+
invalidations.push(reason);
47+
},
48+
);
49+
50+
assert.deepEqual(result, { tapped: true });
51+
assert.deepEqual(
52+
server.requests.map(({ command }) => command),
53+
['uptime', 'tap'],
54+
);
55+
assert.equal(session.commandCharges.hasOutstandingCharges, false);
56+
assert.equal(session.lastHealthyMutation?.appBundleId, 'com.example.app');
57+
assert.deepEqual(invalidations, []);
58+
});
59+
60+
test('the exchange awaits owner invalidation before returning a fatal answer', async () => {
61+
server = await startFakeRunnerServer({
62+
snapshot: [{ kind: 'ok', data: { runnerFatal: true, runnerFatalReason: 'ax_failed' } }],
63+
});
64+
const session = sessionFor(server.port);
65+
session.lastHealthyMutation = { atMs: Date.now(), appBundleId: 'com.example.app' };
66+
const order: string[] = [];
67+
68+
const result = await executeRunnerExchange(
69+
IOS_SIMULATOR,
70+
session,
71+
{ command: 'snapshot', appBundleId: 'com.example.app' },
72+
undefined,
73+
10_000,
74+
async (reason) => {
75+
await Promise.resolve();
76+
order.push(reason);
77+
},
78+
);
79+
order.push('returned');
80+
81+
assert.equal(result.runnerFatal, true);
82+
assert.deepEqual(order, ['ax_failed', 'returned']);
83+
assert.equal(session.lastHealthyMutation, undefined);
84+
assert.equal(session.commandCharges.hasOutstandingCharges, false);
85+
});

‎packages/platform-apple/src/runner/runner-exchange.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { AppError } from '@agent-device/kernel/errors';
22
import type { DeviceInfo } from '@agent-device/kernel/device';
33
import { Deadline, emitDiagnostic, withDiagnosticTimer } from './host.ts';
4-
import { waitForRunner, RUNNER_STARTUP_TIMEOUT_MS } from './runner-startup-transport.ts';
4+
import {
5+
waitForRunner,
6+
RUNNER_STARTUP_TIMEOUT_MS,
7+
type RunnerConnectionSession,
8+
} from './runner-startup-transport.ts';
59
import { sendRunnerCommandOnce } from './runner-transport.ts';
610
import {
711
buildRunnerResponseError,
@@ -29,6 +33,12 @@ import {
2933
} from './runner-failure-diagnostics.ts';
3034
import { advanceRunnerSessionState, type RunnerSession } from './runner-session-types.ts';
3135

36+
type RunnerExchangeSession = RunnerConnectionSession &
37+
Pick<
38+
RunnerSession,
39+
'port' | 'commandCharges' | 'lastHealthyMutation' | 'runnerMainThreadBusy' | 'launchDeadline'
40+
>;
41+
3242
const RUNNER_READY_PREFLIGHT_TIMEOUT_MS = 1_000;
3343
const RUNNER_PREFLIGHT_SKIP_FRESHNESS_MS = 5_000;
3444

@@ -63,7 +73,7 @@ type RunnerReadinessPreflightDecision =
6373
*/
6474
export async function executeRunnerExchange(
6575
device: DeviceInfo,
66-
session: RunnerSession,
76+
session: RunnerExchangeSession,
6777
command: RunnerCommand,
6878
logPath: string | undefined,
6979
timeoutMs: number,
@@ -137,7 +147,7 @@ export async function executeRunnerExchange(
137147
* runner's occupancy report, and applies what the payload says about the session.
138148
*/
139149
async function settleRunnerAnsweredExchange(
140-
session: RunnerSession,
150+
session: RunnerExchangeSession,
141151
runnerCommand: RunnerCommand,
142152
data: Record<string, unknown>,
143153
invalidateFatalSession: (reason: string) => Promise<void>,
@@ -169,7 +179,7 @@ async function settleRunnerAnsweredExchange(
169179
* runner answered it.
170180
*/
171181
function recordUnansweredRunnerExchange(
172-
session: RunnerSession,
182+
session: RunnerExchangeSession,
173183
runnerCommand: RunnerCommand,
174184
error: unknown,
175185
): boolean {
@@ -199,7 +209,7 @@ function readRunnerMainThreadBusy(data: Record<string, unknown>): boolean | unde
199209

200210
function markSkippedPreflightTransportError(
201211
error: unknown,
202-
session: RunnerSession,
212+
session: RunnerExchangeSession,
203213
preflightDecision: RunnerReadinessPreflightDecision,
204214
): unknown {
205215
if (
@@ -218,7 +228,7 @@ function markSkippedPreflightTransportError(
218228

219229
async function sendRunnerCommandAfterPreflight(params: {
220230
device: DeviceInfo;
221-
session: RunnerSession;
231+
session: RunnerExchangeSession;
222232
runnerCommand: RunnerCommand;
223233
logPath: string | undefined;
224234
deadline: Deadline;
@@ -282,7 +292,7 @@ async function sendRunnerCommandAfterPreflight(params: {
282292

283293
async function runRunnerReadinessPreflight(params: {
284294
device: DeviceInfo;
285-
session: RunnerSession;
295+
session: RunnerExchangeSession;
286296
runnerCommand: RunnerCommand;
287297
logAttempt: RunnerLogAttempt | undefined;
288298
deadline: Deadline;
@@ -325,7 +335,7 @@ async function runRunnerReadinessPreflight(params: {
325335

326336
function emitRunnerReadinessPreflightSkipped(
327337
runnerCommand: RunnerCommand,
328-
session: RunnerSession,
338+
session: RunnerExchangeSession,
329339
decision: Extract<RunnerReadinessPreflightDecision, { action: 'skip' }>,
330340
): void {
331341
emitDiagnostic({
@@ -392,7 +402,7 @@ function resolveRunnerFatalReason(data: Record<string, unknown>): string | undef
392402
}
393403

394404
function resolveRunnerReadinessPreflightDecision(
395-
session: RunnerSession,
405+
session: RunnerExchangeSession,
396406
command: RunnerCommand,
397407
): RunnerReadinessPreflightDecision {
398408
const readOnlyCommand = isReadOnlyRunnerCommand(command);

‎packages/platform-apple/src/runner/runner-startup-transport.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ import {
3030
} from './runner-error-classification.ts';
3131
import type { RunnerCommand } from './runner-contract.ts';
3232
import type { RunnerSession } from './runner-session-types.ts';
33+
34+
export type RunnerConnectionSession = Pick<
35+
RunnerSession,
36+
| 'startupRetryWake'
37+
| 'child'
38+
| 'startupDeviceStates'
39+
| 'state'
40+
| 'testPromise'
41+
| 'readLogTail'
42+
| 'device'
43+
| 'runnerLogPath'
44+
>;
3345
import {
3446
runnerSimulatorSetFailureDetails,
3547
simulatorSetDestinationNotFoundMessage,
@@ -53,7 +65,7 @@ export async function waitForRunner(
5365
command: RunnerCommand,
5466
logPath?: string,
5567
timeoutMs: number = RUNNER_STARTUP_TIMEOUT_MS,
56-
session?: RunnerSession,
68+
session?: RunnerConnectionSession,
5769
signal?: AbortSignal,
5870
): Promise<Response> {
5971
const deadline = Deadline.fromTimeoutMs(timeoutMs);
@@ -148,7 +160,7 @@ async function attemptRunnerConnection(params: {
148160
command: RunnerCommand;
149161
timeoutMs: number;
150162
logPath?: string;
151-
session?: RunnerSession;
163+
session?: RunnerConnectionSession;
152164
route: RunnerCommandRoute;
153165
resolveRoute: RunnerRouteResolver;
154166
markUsbmuxUnattached: () => void;
@@ -172,7 +184,7 @@ async function ensureRunnerAttemptCanStart(params: {
172184
port: number;
173185
timeoutMs: number;
174186
logPath?: string;
175-
session?: RunnerSession;
187+
session?: RunnerConnectionSession;
176188
attemptDeadline?: Deadline;
177189
}): Promise<void> {
178190
if (params.attemptDeadline?.isExpired()) {
@@ -246,7 +258,7 @@ async function tryReadySimulatorEndpoint(params: {
246258
device: DeviceInfo;
247259
port: number;
248260
command: RunnerCommand;
249-
session?: RunnerSession;
261+
session?: RunnerConnectionSession;
250262
signal?: AbortSignal;
251263
attemptDeadline?: Deadline;
252264
setLastError: (error: unknown) => void;
@@ -516,7 +528,7 @@ function buildRunnerConnectError(params: {
516528
}
517529

518530
export async function buildRunnerEarlyExitError(params: {
519-
session: RunnerSession;
531+
session: RunnerConnectionSession;
520532
port: number;
521533
logPath?: string;
522534
}): Promise<AppError> {

0 commit comments

Comments
 (0)