// Called only by the owned standalone QA runner after package/schema/recovery checks. import fs from 'node:fs'; import path from 'node:path'; import crypto from 'node:crypto'; import { spawn, spawnSync } from 'node:child_process'; import { createServer } from 'node:net'; export async function reviewStandaloneFinance({ workspace, root, database, environment, assets, manifestPin }) { const parent = fs.realpathSync(path.join(workspace, 'storage/framework/testing')); const owned = fs.realpathSync(root); const match = /^compras_standalone_qa_([a-f0-9]{12})$/.exec(database); if (!match || path.dirname(owned) !== parent || path.basename(owned) !== `rq01-ledger-qa-${match[1]}` || environment.DB_DATABASE !== database || environment.DB_USERNAME !== `compras_standalone_app_${match[1]}` || environment.COMPRAS_RUNTIME_ENABLED !== 'true' || environment.COMPRAS_RUNTIME_PREPARE_ONLY !== 'false' || environment.COMPRAS_RUNTIME_SYNTHETIC_ONLY !== 'true') throw Error('Owned synthetic standalone preview required'); const worker = mode => { const result = spawnSync('php', ['tests/Support/standalone-runtime-worker.php', database, mode], { cwd: workspace, windowsHide: true, encoding: 'utf8', env: environment, maxBuffer: 1024 * 1024, }); if (result.status !== 0) throw Error('Standalone UI fixture/conservation failed'); return JSON.parse(result.stdout.trim()); }; for (const relative of ['public', 'storage/framework/sessions', 'storage/framework/views', 'storage/framework/cache/data', 'storage/logs', 'storage/app/private']) { fs.mkdirSync(path.join(root, relative), { recursive: true }); } const fixture = worker('prepare-ui'); if (fixture.ui_prepared !== true || fixture.no_initial_financial_grants !== true) throw Error('UI setup inherited grants'); const bytes = fs.readFileSync(path.join(assets, 'manifest.json')); if (crypto.createHash('sha256').update(bytes).digest('hex') !== manifestPin) throw Error('Pinned UI build required'); for (const entry of Object.values(JSON.parse(bytes))) { if (!fs.existsSync(path.join(assets, entry.file))) throw Error('UI build entry missing'); } fs.cpSync(assets, path.join(root, 'public/build'), { recursive: true, errorOnExist: true }); for (const file of ['favicon.ico', 'favicon.svg', 'apple-touch-icon.png']) { fs.copyFileSync(path.join(workspace, 'public', file), path.join(root, 'public', file)); } const port = await new Promise((resolve, reject) => { const socket = createServer(); socket.on('error', reject); socket.listen(0, '127.0.0.1', () => { const address = socket.address(); socket.close(() => resolve(address.port)); }); }); const url = `http://localhost:${port}`; const server = spawn('php', ['-S', `127.0.0.1:${port}`, '-t', path.join(root, 'public'), 'tests/Support/standalone-qa-http-router.php'], { cwd: workspace, windowsHide: true, stdio: ['ignore', 'ignore', 'ignore'], env: { ...environment, APP_URL: url, SGC_STANDALONE_QA_PORT: String(port), SESSION_DRIVER: 'file', SESSION_DOMAIN: '', SESSION_SECURE_COOKIE: 'false' }, }); try { let ready = false; for (let attempt = 0; attempt < 20 && !ready; attempt++) { try { const response = await fetch(url+'/login'); ready = response.status === 200; await response.arrayBuffer(); } catch {} if (!ready) await new Promise(resolve => setTimeout(resolve, 200)); } if (!ready) throw Error('Standalone UI did not start'); const stop = path.join(root, 'stop-finance-review'); console.log(JSON.stringify({ standalone_finance_preview: true, url, stop_file: stop, manifest_sha256: manifestPin, ...fixture })); const until = Date.now() + 20 * 60 * 1000; while (!fs.existsSync(stop) && Date.now() < until) { if (server.exitCode !== null) throw Error('Standalone UI stopped unexpectedly'); await new Promise(resolve => setTimeout(resolve, 200)); } const proof = worker('read-ui'); if (proof.ui_conservation_verified !== true) throw Error('UI conservation not verified'); console.log(JSON.stringify(proof)); } finally { if (server.exitCode === null) { const closed = new Promise(resolve => server.once('exit', resolve)); server.kill(); await closed; } } }