|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { readFile, rm, writeFile } from 'node:fs/promises'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { describe, test } from 'vitest'; |
| 5 | +import { runCmd } from '@agent-device/host-kit/command'; |
| 6 | +import { mkdtempForTest } from '../__tests__/tmp-dir.ts'; |
| 7 | +import { execKillTimeoutError } from '../snapshot-source/__tests__/exec-timeout-fixture.ts'; |
| 8 | +import { createSnapshotSourceHost } from '../snapshot-source/host.ts'; |
| 9 | +import type { SnapshotSourceHost } from '../snapshot-source/types.ts'; |
| 10 | +import { |
| 11 | + buildFoldHelperCompileArgv, |
| 12 | + ensureFoldHelperBinary, |
| 13 | + FOLD_HELPER_BUILD_TIMEOUT_MS, |
| 14 | +} from './fold-helper-cache.ts'; |
| 15 | + |
| 16 | +function fakeFoldHelperHost( |
| 17 | + binary: () => string, |
| 18 | + xcodeVersion: () => string = () => 'Xcode 16.4\nBuild version 16F6', |
| 19 | +): SnapshotSourceHost { |
| 20 | + const real = createSnapshotSourceHost(); |
| 21 | + return { |
| 22 | + ...real, |
| 23 | + run: async (command, args) => { |
| 24 | + if (command === 'xcrun' && args.includes('clang')) { |
| 25 | + const outputPath = args.at(-1)!; |
| 26 | + await writeFile(outputPath, binary()); |
| 27 | + return { stdout: '', stderr: '', exitCode: 0 }; |
| 28 | + } |
| 29 | + const stdout = |
| 30 | + command === 'xcodebuild' |
| 31 | + ? xcodeVersion() |
| 32 | + : command === 'sw_vers' |
| 33 | + ? args.includes('-buildVersion') |
| 34 | + ? '24G90' |
| 35 | + : '15.6' |
| 36 | + : command === 'uname' |
| 37 | + ? 'arm64' |
| 38 | + : ''; |
| 39 | + return { stdout, stderr: '', exitCode: 0 }; |
| 40 | + }, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +test('a cache hit does not build, and a source or toolchain change does', async () => { |
| 45 | + const root = await mkdtempForTest('agent-device-fold-helper-cache-'); |
| 46 | + const sourceRoot = path.join(root, 'source'); |
| 47 | + const cacheRoot = path.join(root, 'cache'); |
| 48 | + await (await import('@agent-device/host-kit/host-file')).ensureHostDirectory(sourceRoot); |
| 49 | + await writeFile(path.join(sourceRoot, 'Fold.m'), 'fold source v1'); |
| 50 | + |
| 51 | + let builds = 0; |
| 52 | + let xcodeVersion = 'Xcode 16.4\nBuild version 16F6'; |
| 53 | + const host = fakeFoldHelperHost( |
| 54 | + () => { |
| 55 | + builds += 1; |
| 56 | + return `binary-${builds}`; |
| 57 | + }, |
| 58 | + () => xcodeVersion, |
| 59 | + ); |
| 60 | + |
| 61 | + try { |
| 62 | + const first = await ensureFoldHelperBinary({ host, sourceRoot, cacheRoot }); |
| 63 | + assert.equal(builds, 1); |
| 64 | + assert.equal(await readFile(first.path, 'utf8'), 'binary-1'); |
| 65 | + |
| 66 | + const hit = await ensureFoldHelperBinary({ host, sourceRoot, cacheRoot }); |
| 67 | + assert.equal(hit.path, first.path); |
| 68 | + assert.equal(builds, 1); |
| 69 | + |
| 70 | + await writeFile(path.join(sourceRoot, 'Fold.m'), 'fold source v2'); |
| 71 | + const sourceChanged = await ensureFoldHelperBinary({ host, sourceRoot, cacheRoot }); |
| 72 | + assert.notEqual(sourceChanged.path, first.path); |
| 73 | + assert.equal(builds, 2); |
| 74 | + |
| 75 | + xcodeVersion = 'Xcode 16.5\nBuild version 16F5'; |
| 76 | + const toolchainChanged = await ensureFoldHelperBinary({ host, sourceRoot, cacheRoot }); |
| 77 | + assert.notEqual(toolchainChanged.path, sourceChanged.path); |
| 78 | + assert.equal(builds, 3); |
| 79 | + } finally { |
| 80 | + await rm(root, { recursive: true, force: true }); |
| 81 | + } |
| 82 | +}); |
| 83 | + |
| 84 | +test('a compile exec killed at its budget reports the fold-helper build, not the exec layer', async () => { |
| 85 | + const root = await mkdtempForTest('agent-device-fold-helper-cache-stall-'); |
| 86 | + const sourceRoot = path.join(root, 'source'); |
| 87 | + const cacheRoot = path.join(root, 'cache'); |
| 88 | + await (await import('@agent-device/host-kit/host-file')).ensureHostDirectory(sourceRoot); |
| 89 | + await writeFile(path.join(sourceRoot, 'Fold.m'), 'fold source'); |
| 90 | + const okHost = fakeFoldHelperHost(() => 'binary'); |
| 91 | + const host: SnapshotSourceHost = { |
| 92 | + ...okHost, |
| 93 | + run: async (command, args, options) => { |
| 94 | + if (command === 'xcrun' && args.includes('clang')) throw await execKillTimeoutError(); |
| 95 | + return await okHost.run(command, args, options); |
| 96 | + }, |
| 97 | + }; |
| 98 | + |
| 99 | + try { |
| 100 | + await assert.rejects( |
| 101 | + ensureFoldHelperBinary({ host, sourceRoot, cacheRoot }), |
| 102 | + (error: unknown) => { |
| 103 | + assert.ok(error instanceof Error); |
| 104 | + assert.equal((error as { code?: string }).code, 'COMMAND_FAILED'); |
| 105 | + assert.equal( |
| 106 | + (error as { details?: { reason?: string } }).details?.reason, |
| 107 | + 'fold-helper-build-failed', |
| 108 | + ); |
| 109 | + assert.equal( |
| 110 | + (error as { details?: { cause?: string } }).details?.cause, |
| 111 | + 'native-build-stalled', |
| 112 | + ); |
| 113 | + return true; |
| 114 | + }, |
| 115 | + ); |
| 116 | + } finally { |
| 117 | + await rm(root, { recursive: true, force: true }); |
| 118 | + } |
| 119 | +}); |
| 120 | + |
| 121 | +test('a build failure reports COMMAND_FAILED with the fold-helper-build-failed reason', async () => { |
| 122 | + const root = await mkdtempForTest('agent-device-fold-helper-cache-failure-'); |
| 123 | + const sourceRoot = path.join(root, 'source'); |
| 124 | + const cacheRoot = path.join(root, 'cache'); |
| 125 | + await (await import('@agent-device/host-kit/host-file')).ensureHostDirectory(sourceRoot); |
| 126 | + await writeFile(path.join(sourceRoot, 'Fold.m'), 'fold source'); |
| 127 | + const okHost = fakeFoldHelperHost(() => 'binary'); |
| 128 | + const host: SnapshotSourceHost = { |
| 129 | + ...okHost, |
| 130 | + run: async (command, args, options) => { |
| 131 | + if (command === 'xcrun' && args.includes('clang')) { |
| 132 | + return { stdout: '', stderr: 'clang: error: build failed', exitCode: 1 }; |
| 133 | + } |
| 134 | + return await okHost.run(command, args, options); |
| 135 | + }, |
| 136 | + }; |
| 137 | + |
| 138 | + try { |
| 139 | + await assert.rejects( |
| 140 | + ensureFoldHelperBinary({ host, sourceRoot, cacheRoot }), |
| 141 | + (error: unknown) => { |
| 142 | + assert.ok(error instanceof Error); |
| 143 | + assert.equal((error as { code?: string }).code, 'COMMAND_FAILED'); |
| 144 | + assert.equal( |
| 145 | + (error as { details?: { reason?: string } }).details?.reason, |
| 146 | + 'fold-helper-build-failed', |
| 147 | + ); |
| 148 | + return true; |
| 149 | + }, |
| 150 | + ); |
| 151 | + } finally { |
| 152 | + await rm(root, { recursive: true, force: true }); |
| 153 | + } |
| 154 | +}); |
| 155 | + |
| 156 | +// #2796: the production compile drops -Werror so a stale toolchain warning cannot fail a build; |
| 157 | +// this is the gate that keeps a new Fold.m warning from passing CI unnoticed. It runs the |
| 158 | +// production argv (`buildFoldHelperCompileArgv`) against the real iphonesimulator SDK with |
| 159 | +// -Werror appended, so a warning fails here instead of nowhere. |
| 160 | +describe.skipIf(process.platform !== 'darwin')('fold helper warning gate', () => { |
| 161 | + test( |
| 162 | + 'the production fold helper argv compiles clean under -Werror', |
| 163 | + async () => { |
| 164 | + const sourceRoot = path.resolve(import.meta.dirname, '../../../../apple/fold-helper'); |
| 165 | + const binary = path.join(await mkdtempForTest('fold-helper-werror-'), 'fold-helper'); |
| 166 | + const argv = buildFoldHelperCompileArgv({ sourceRoot, outputPath: binary }); |
| 167 | + const wextraIndex = argv.indexOf('-Wextra'); |
| 168 | + assert.ok(wextraIndex >= 0, 'the production argv carries -Wextra'); |
| 169 | + const werrorArgv = [ |
| 170 | + ...argv.slice(0, wextraIndex + 1), |
| 171 | + '-Werror', |
| 172 | + ...argv.slice(wextraIndex + 1), |
| 173 | + ]; |
| 174 | + const compiled = await runCmd('xcrun', werrorArgv, { |
| 175 | + allowFailure: true, |
| 176 | + timeoutMs: FOLD_HELPER_BUILD_TIMEOUT_MS, |
| 177 | + }); |
| 178 | + assert.equal(compiled.exitCode, 0, compiled.stderr); |
| 179 | + }, |
| 180 | + FOLD_HELPER_BUILD_TIMEOUT_MS + 30_000, |
| 181 | + ); |
| 182 | +}); |
0 commit comments