|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { EventEmitter } from 'node:events'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import { IOS_SIMULATOR } from '../../../../__tests__/test-utils/index.ts'; |
| 5 | +import { mkdtempForTestSync } from '../../../../__tests__/test-utils/tmp-dir.ts'; |
| 6 | +import { |
| 7 | + flushDiagnosticsToSessionFile, |
| 8 | + withDiagnosticsScope, |
| 9 | +} from '../../../../utils/diagnostics.ts'; |
| 10 | +import { RUNNER_OWNER_START_TIME, type RunnerLease } from '../runner/runner-lease.ts'; |
| 11 | +import type { RunnerSession } from '../runner/runner-session-types.ts'; |
| 12 | + |
| 13 | +// Fabricated runner sessions, leases, background children, and transport |
| 14 | +// payloads shared by the runner-session tests. The child pids here are made up |
| 15 | +// (`4242`): nothing in a test may deliver a real signal to them, so the owning |
| 16 | +// tests mock the signal seam in `src/utils/host-process.ts` — see |
| 17 | +// `src/__tests__/hermetic-signal-setup.ts` and #1824. |
| 18 | + |
| 19 | +export function makeRunnerSession(overrides: Partial<RunnerSession> = {}): RunnerSession { |
| 20 | + return { |
| 21 | + sessionId: `session-${overrides.port ?? 8100}`, |
| 22 | + device: IOS_SIMULATOR, |
| 23 | + deviceId: IOS_SIMULATOR.id, |
| 24 | + port: 8100, |
| 25 | + xctestrunPath: '/tmp/runner.xctestrun', |
| 26 | + jsonPath: '/tmp/runner.json', |
| 27 | + testPromise: Promise.resolve({ exitCode: 0, stdout: '', stderr: '' }), |
| 28 | + child: { pid: 1234, exitCode: null }, |
| 29 | + ready: true, |
| 30 | + ...overrides, |
| 31 | + } as RunnerSession; |
| 32 | +} |
| 33 | + |
| 34 | +export function makeRunnerLease( |
| 35 | + overrides: Partial<RunnerLease> & { deviceId: string; ownerToken?: string | undefined }, |
| 36 | +): RunnerLease { |
| 37 | + const ownerToken = overrides.ownerToken ?? `owner-${process.pid}-test`; |
| 38 | + const lease: RunnerLease = { |
| 39 | + schemaVersion: 1, |
| 40 | + deviceId: overrides.deviceId, |
| 41 | + ownerToken, |
| 42 | + ownerPid: process.pid, |
| 43 | + ownerStartTime: RUNNER_OWNER_START_TIME, |
| 44 | + sessionId: `session-${overrides.deviceId}`, |
| 45 | + runnerPid: 4242, |
| 46 | + port: 8123, |
| 47 | + xctestrunPath: `/tmp/AgentDeviceRunner.env.session-${overrides.deviceId}-${ownerToken}-8123.xctestrun`, |
| 48 | + jsonPath: `/tmp/AgentDeviceRunner.env.session-${overrides.deviceId}-${ownerToken}-8123.json`, |
| 49 | + createdAtMs: Date.now(), |
| 50 | + }; |
| 51 | + return { ...lease, ...overrides, ownerToken }; |
| 52 | +} |
| 53 | + |
| 54 | +export function makeBackgroundRunner(pid: number) { |
| 55 | + return { |
| 56 | + child: { |
| 57 | + pid, |
| 58 | + exitCode: null, |
| 59 | + stdout: new EventEmitter(), |
| 60 | + stderr: new EventEmitter(), |
| 61 | + }, |
| 62 | + wait: Promise.resolve({ exitCode: 0, stdout: '', stderr: '' }), |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +export function runnerResponse(data: Record<string, unknown>): Response { |
| 67 | + return new Response(JSON.stringify({ ok: true, data })); |
| 68 | +} |
| 69 | + |
| 70 | +export function runnerError(error: { code: string; message: string }): Response { |
| 71 | + return new Response(JSON.stringify({ ok: false, error })); |
| 72 | +} |
| 73 | + |
| 74 | +export async function captureDiagnostics(callback: () => Promise<void>): Promise<string> { |
| 75 | + const previousHome = process.env.HOME; |
| 76 | + process.env.HOME = mkdtempForTestSync('agent-device-runner-diag-'); |
| 77 | + try { |
| 78 | + return await withDiagnosticsScope( |
| 79 | + { session: 'runner-session-test', requestId: 'request-1', command: 'tap' }, |
| 80 | + async () => { |
| 81 | + await callback(); |
| 82 | + const diagnosticsPath = flushDiagnosticsToSessionFile({ force: true })?.path; |
| 83 | + assert.ok(diagnosticsPath); |
| 84 | + return fs.readFileSync(diagnosticsPath, 'utf8'); |
| 85 | + }, |
| 86 | + ); |
| 87 | + } finally { |
| 88 | + process.env.HOME = previousHome; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +export function assertRunnerCommand( |
| 93 | + actual: unknown, |
| 94 | + expected: Record<string, unknown>, |
| 95 | + options: { commandId?: boolean } = {}, |
| 96 | +): asserts actual is Record<string, unknown> { |
| 97 | + assert.equal(typeof actual, 'object'); |
| 98 | + assert.notEqual(actual, null); |
| 99 | + const command = actual as Record<string, unknown>; |
| 100 | + const commandId = command.commandId; |
| 101 | + if (options.commandId === false) { |
| 102 | + assert.equal(commandId, undefined); |
| 103 | + assert.deepEqual(command, expected); |
| 104 | + return; |
| 105 | + } |
| 106 | + if (typeof commandId !== 'string') { |
| 107 | + assert.fail('expected runner commandId'); |
| 108 | + } |
| 109 | + assert.match(commandId, /^runner-/); |
| 110 | + assert.deepEqual({ ...command, commandId: undefined }, { ...expected, commandId: undefined }); |
| 111 | +} |
0 commit comments