// Synthetic loopback preview, normal application authentication, no installed login shortcut. 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 reviewStandaloneInventory({ workspace, root, database, environment, cancellation = false, analytics = false, }) { const parent = fs.realpathSync( path.join(workspace, 'storage/framework/testing'), ); const match = /^compras_receipts_test_([a-f0-9]{12})$/.exec(database); if ( !match || path.dirname(fs.realpathSync(root)) !== parent || path.basename(root) !== `rq01-ledger-qa-${match[1]}` || environment.DB_USERNAME !== `payable_history_native_${match[1]}` || environment.DB_HOST !== '127.0.0.1' || environment.DB_PORT !== '55439' || environment.COMPRAS_RUNTIME_SYNTHETIC_ONLY !== 'true' ) throw Error('Owned inventory preview required'); if (cancellation) environment = { ...environment, COMPRAS_ORDER_CANCELLATION_ENABLED: 'true', }; const prepared = spawnSync( 'php', [ 'tests/Concurrency/unit-conversion-worker.php', database, analytics ? 'prepare-analytics-preview' : cancellation ? 'prepare-cancellation-preview' : 'prepare-preview', ], { cwd: workspace, windowsHide: true, encoding: 'utf8', env: { ...environment, APP_ENV: 'testing' }, }, ); if (prepared.status !== 0) throw Error('Synthetic inventory preview fixture failed'); const fixture = JSON.parse(prepared.stdout); 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 assets = path.join( workspace, 'storage/framework/testing', analytics ? 'sgc-analytics-print-qa-20261129' : 'sgc-standalone-finance-help-qa-20261129', ); const manifest = fs.readFileSync(path.join(assets, 'manifest.json')); if ( crypto.createHash('sha256').update(manifest).digest('hex') !== (analytics ? 'c3ac2c6443d73c228a3a8e8e96f2f32bb130fa5e5b03b2b284b02007c5502022' : '8653c147ab863447b523ddc03111265e3e6510bdddc31341777d6a3bdc46ce5b') ) throw Error('Pinned UI build required'); fs.cpSync(assets, path.join(root, 'public/build'), { recursive: true, errorOnExist: true, }); const verifyAnalytics = (mode) => { const check = spawnSync( 'php', ['scripts/check-analytics-preview.php', root, mode], { cwd: workspace, windowsHide: true, encoding: 'utf8' }, ); if (check.status !== 0) throw Error('Synthetic analytical conservation failed'); return JSON.parse(check.stdout); }; if (analytics) verifyAnalytics('--capture'); 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-inventory-http-router.php', ], { cwd: workspace, windowsHide: true, stdio: ['ignore', 'ignore', 'ignore'], env: { ...environment, APP_URL: url, ASSET_URL: url, SGC_STANDALONE_QA_ROOT: root, SGC_STANDALONE_QA_PORT: String(port), SESSION_DRIVER: 'file', SESSION_COOKIE: `sgc_inventory_qa_${match[1]}`, SESSION_DOMAIN: '', SESSION_SECURE_COOKIE: 'false', SGC_PRODUCT_ENTITLEMENTS: JSON.stringify({ [fixture.tenant_id]: ['procurement.internal'], }), }, }, ); try { let ready = false; for (let attempt = 0; attempt < 30 && !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('Inventory preview did not start'); const stop = path.join(root, 'stop-inventory-review'); console.log( JSON.stringify({ inventory_preview: !cancellation && !analytics, cancellation_preview: cancellation, analytics_preview: analytics, url, stop_file: stop, ...fixture, normal_authentication_required: true, }), ); const until = Date.now() + 20 * 60 * 1000; while (!fs.existsSync(stop) && Date.now() < until) { if (server.exitCode !== null) throw Error('Inventory preview stopped unexpectedly'); await new Promise((resolve) => setTimeout(resolve, 200)); } if (analytics) console.log( JSON.stringify({ analytical_conservation: verifyAnalytics('--verify'), }), ); } finally { if (server.exitCode === null) { const closed = new Promise((resolve) => server.once('exit', resolve), ); server.kill(); await closed; } } }