import { spawn } from 'node:child_process'; import { mkdtemp, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join, resolve } from 'node:path'; import { bundlePiRuntime } from './bundle-pi-runtime.mjs'; import { defaultPiBundleTarget } from './lib/pi-runtime-bundle.mjs'; function parseArgs(argv) { const options = { runtimeRoot: undefined, electronExecutable: undefined }; for (let index = 0; index < argv.length; index += 1) { const argument = argv[index]; const value = argv[index + 1]; if (!value || value.startsWith('--')) throw new Error(`${argument} requires a value`); if (argument === '--runtime-root') options.runtimeRoot = resolve(value); else if (argument === '--electron-executable') options.electronExecutable = resolve(value); else throw new Error(`Unknown argument: ${argument}`); index += 1; } if (Boolean(options.runtimeRoot) !== Boolean(options.electronExecutable)) { throw new Error('--runtime-root and --electron-executable must be provided together'); } return options; } function runVitest(runtimeRoot, electronExecutable) { return new Promise((resolvePromise, reject) => { const child = spawn(process.execPath, [ resolve('node_modules/vitest/vitest.mjs'), 'run', 'tests/unit/pi-worker-process-real.test.ts', ], { cwd: process.cwd(), env: { ...process.env, MAKELORE_PI_STAGED_RUNTIME_ROOT: runtimeRoot, ...(electronExecutable ? { MAKELORE_PI_ELECTRON_EXECUTABLE: electronExecutable } : {}), }, stdio: 'inherit', windowsHide: true, }); child.once('error', reject); child.once('exit', (code, signal) => { if (code === 0) resolvePromise(); else { reject(new Error( `Packaged subagent smoke failed with code ${code ?? 'null'} signal ${signal ?? 'none'}`, )); } }); }); } function runElectronProductTools(electronExecutable) { return new Promise((resolvePromise, reject) => { const child = spawn(process.execPath, [ resolve('scripts/run-electron-vitest.mjs'), 'tests/unit/pi-extension-bundle.test.ts', ], { cwd: process.cwd(), env: { ...process.env, ...(electronExecutable ? { MAKELORE_ELECTRON_EXECUTABLE: electronExecutable, MAKELORE_PI_ELECTRON_EXECUTABLE: electronExecutable, } : {}), }, stdio: 'inherit', windowsHide: true, }); child.once('error', reject); child.once('exit', (code, signal) => { if (code === 0) resolvePromise(); else { reject(new Error( `Packaged product-tools Electron smoke failed with code ${code ?? 'null'} signal ${signal ?? 'none'}`, )); } }); }); } const options = parseArgs(process.argv.slice(2)); const outputRoot = options.runtimeRoot ? null : await mkdtemp(join(tmpdir(), 'makelore-pi-subagent-package-')); try { let runtimeRoot = options.runtimeRoot; if (!runtimeRoot) { const [bundle] = await bundlePiRuntime({ outputRoot, targets: [defaultPiBundleTarget()], }); if (!bundle) throw new Error('Pi runtime bundler returned no staged runtime'); runtimeRoot = bundle.destination; } await runVitest(runtimeRoot, options.electronExecutable); await runElectronProductTools(options.electronExecutable); } finally { if (outputRoot) { await rm(outputRoot, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 }); } }