|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { mkdtemp, rm, writeFile } from 'node:fs/promises'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { test } from 'vitest'; |
| 6 | +import { createSnapshotSourceDeadline } from './deadline.ts'; |
| 7 | +import { createSnapshotSourceHost } from './host.ts'; |
| 8 | +import { resolveSnapshotSourceLimits } from './limits.ts'; |
| 9 | +import { |
| 10 | + createSnapshotBridgePreparation, |
| 11 | + SNAPSHOT_BRIDGE_PREPARATION_RETRY_AFTER_MS, |
| 12 | +} from './preparation.ts'; |
| 13 | +import type { SnapshotSourceHost } from './types.ts'; |
| 14 | + |
| 15 | +const BRIDGE_SOURCES = [ |
| 16 | + 'SnapshotBridge.m', |
| 17 | + 'SnapshotBridgeRuntime.m', |
| 18 | + 'SnapshotBridgeRuntime.h', |
| 19 | + 'SnapshotBridgeCapture.h', |
| 20 | + 'SnapshotBridgeCapture.m', |
| 21 | +]; |
| 22 | + |
| 23 | +test('one detached preparation answers every capture that arrives while it runs', async () => { |
| 24 | + const root = await writeBridgeSource('preparation-shared-'); |
| 25 | + let releaseBuild = () => {}; |
| 26 | + const gate = new Promise<void>((resolve) => { |
| 27 | + releaseBuild = resolve; |
| 28 | + }); |
| 29 | + const fixture = createPreparationHost({ build: 'pending', gate }); |
| 30 | + const preparation = createSnapshotBridgePreparation({ |
| 31 | + host: fixture.host, |
| 32 | + limits: resolveSnapshotSourceLimits({}), |
| 33 | + producer: 'test', |
| 34 | + sourceRoot: path.join(root, 'source'), |
| 35 | + cacheRoot: path.join(root, 'cache'), |
| 36 | + }); |
| 37 | + |
| 38 | + try { |
| 39 | + const pending = await Promise.all([ |
| 40 | + preparation.readyBinary('ios-simulator', deadline(300), 'test-deadline').then( |
| 41 | + () => undefined, |
| 42 | + (error: unknown) => failureOf(error), |
| 43 | + ), |
| 44 | + preparation.readyBinary('ios-simulator', deadline(300), 'test-deadline').then( |
| 45 | + () => undefined, |
| 46 | + (error: unknown) => failureOf(error), |
| 47 | + ), |
| 48 | + ]); |
| 49 | + for (const failure of pending) { |
| 50 | + assert.equal(failure?.kind, 'preparing'); |
| 51 | + assert.equal(failure?.code, 'bridge-preparation-pending'); |
| 52 | + } |
| 53 | + assert.equal(fixture.builds, 1); |
| 54 | + |
| 55 | + releaseBuild(); |
| 56 | + const binary = await preparation.readyBinary('ios-simulator', deadline(5_000), 'test-deadline'); |
| 57 | + assert.ok(binary.path.startsWith(path.join(root, 'cache'))); |
| 58 | + assert.ok(binary.cacheKey.length > 0); |
| 59 | + assert.equal(fixture.builds, 1); |
| 60 | + } finally { |
| 61 | + await rm(root, { recursive: true, force: true }); |
| 62 | + } |
| 63 | +}); |
| 64 | + |
| 65 | +test('a failed preparation is reported to each capture without rebuilding per capture', async () => { |
| 66 | + const root = await writeBridgeSource('preparation-failed-'); |
| 67 | + const fixture = createPreparationHost({ build: 'fails', gate: undefined }); |
| 68 | + let clockMs = 0; |
| 69 | + const preparation = createSnapshotBridgePreparation({ |
| 70 | + host: fixture.host, |
| 71 | + limits: resolveSnapshotSourceLimits({}), |
| 72 | + producer: 'test', |
| 73 | + sourceRoot: path.join(root, 'source'), |
| 74 | + cacheRoot: path.join(root, 'cache'), |
| 75 | + now: () => clockMs, |
| 76 | + }); |
| 77 | + |
| 78 | + try { |
| 79 | + const first = await preparation |
| 80 | + .readyBinary( |
| 81 | + 'ios-simulator', |
| 82 | + deadline(5_000, () => clockMs), |
| 83 | + 'test-deadline', |
| 84 | + ) |
| 85 | + .then( |
| 86 | + () => undefined, |
| 87 | + (error: unknown) => failureOf(error), |
| 88 | + ); |
| 89 | + assert.equal(first?.kind, 'unsupported'); |
| 90 | + assert.equal(first?.code, 'native-build-failed'); |
| 91 | + assert.equal(fixture.builds, 1); |
| 92 | + |
| 93 | + const again = await preparation |
| 94 | + .readyBinary( |
| 95 | + 'ios-simulator', |
| 96 | + deadline(5_000, () => clockMs), |
| 97 | + 'test-deadline', |
| 98 | + ) |
| 99 | + .then( |
| 100 | + () => undefined, |
| 101 | + (error: unknown) => failureOf(error), |
| 102 | + ); |
| 103 | + assert.deepEqual(again, first); |
| 104 | + assert.equal(fixture.builds, 1); |
| 105 | + |
| 106 | + clockMs += SNAPSHOT_BRIDGE_PREPARATION_RETRY_AFTER_MS + 1; |
| 107 | + const retried = await preparation |
| 108 | + .readyBinary( |
| 109 | + 'ios-simulator', |
| 110 | + deadline(5_000, () => clockMs), |
| 111 | + 'test-deadline', |
| 112 | + ) |
| 113 | + .then( |
| 114 | + () => undefined, |
| 115 | + (error: unknown) => failureOf(error), |
| 116 | + ); |
| 117 | + assert.equal(retried?.code, 'native-build-failed'); |
| 118 | + assert.equal(fixture.builds, 2); |
| 119 | + } finally { |
| 120 | + await rm(root, { recursive: true, force: true }); |
| 121 | + } |
| 122 | +}); |
| 123 | + |
| 124 | +async function writeBridgeSource(prefix: string): Promise<string> { |
| 125 | + const root = await mkdtemp(path.join(os.tmpdir(), `agent-device-bridge-${prefix}`)); |
| 126 | + const sourceRoot = path.join(root, 'source'); |
| 127 | + await (await import('@agent-device/host-kit/host-file')).ensureHostDirectory(sourceRoot); |
| 128 | + for (const name of BRIDGE_SOURCES) { |
| 129 | + await writeFile(path.join(sourceRoot, name), 'native source'); |
| 130 | + } |
| 131 | + return root; |
| 132 | +} |
| 133 | + |
| 134 | +function deadline(timeoutMs: number, now: () => number = Date.now) { |
| 135 | + return createSnapshotSourceDeadline(timeoutMs, undefined, now); |
| 136 | +} |
| 137 | + |
| 138 | +function failureOf(error: unknown): Readonly<{ kind: string; code: string }> | undefined { |
| 139 | + const failure = (error as Readonly<{ failureKind?: unknown; failureCode?: unknown }>) ?? {}; |
| 140 | + return typeof failure.failureKind === 'string' && typeof failure.failureCode === 'string' |
| 141 | + ? { kind: failure.failureKind, code: failure.failureCode } |
| 142 | + : undefined; |
| 143 | +} |
| 144 | + |
| 145 | +function createPreparationHost( |
| 146 | + build: Readonly<{ build: 'pending' | 'fails'; gate: Promise<void> | undefined }>, |
| 147 | +): Readonly<{ host: SnapshotSourceHost; builds: number }> { |
| 148 | + const fixture: { host: SnapshotSourceHost; builds: number } = { |
| 149 | + host: undefined as never, |
| 150 | + builds: 0, |
| 151 | + }; |
| 152 | + const host: SnapshotSourceHost = { |
| 153 | + ...createSnapshotSourceHost(), |
| 154 | + run: async (command, args) => { |
| 155 | + if (command === 'xcrun' && args.includes('clang')) { |
| 156 | + fixture.builds += 1; |
| 157 | + if (build.build === 'pending') await build.gate; |
| 158 | + if (build.build === 'fails') return { stdout: '', stderr: 'boom', exitCode: 1 }; |
| 159 | + await writeFile(args.at(-1)!, 'bridge-binary'); |
| 160 | + return { stdout: '', stderr: '', exitCode: 0 }; |
| 161 | + } |
| 162 | + return { |
| 163 | + stdout: |
| 164 | + command === 'xcodebuild' |
| 165 | + ? 'Xcode 16.4\nBuild version 16F6' |
| 166 | + : command === 'sw_vers' |
| 167 | + ? '15.6' |
| 168 | + : command === 'uname' |
| 169 | + ? 'arm64' |
| 170 | + : '26.2', |
| 171 | + stderr: '', |
| 172 | + exitCode: 0, |
| 173 | + }; |
| 174 | + }, |
| 175 | + }; |
| 176 | + fixture.host = host; |
| 177 | + return fixture; |
| 178 | +} |
0 commit comments