import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import { createHash } from 'node:crypto'; import { spawn } from 'node:child_process'; const workspace = process.cwd(); const evidence = JSON.parse(fs.readFileSync('.qa/archives-validation.json', 'utf8')); assert.equal(evidence.success, true); assert.ok(evidence.extractionRoot, 'Run python scripts/verify-archives.py --extract-source first.'); const sourceRoot = fs.realpathSync.native(evidence.extractionRoot); assert.ok(sourceRoot.startsWith(path.resolve('.qa') + path.sep)); const npmCli = path.join(path.dirname(process.execPath), 'node_modules/npm/bin/npm-cli.js'); assert.ok(fs.existsSync(npmCli), 'Use Node with its bundled npm.'); const files = (root, relative = '') => fs.readdirSync(path.join(root, relative), { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name)) .flatMap(entry => entry.isDirectory() ? files(root, path.join(relative, entry.name)) : [path.join(relative, entry.name)]); const digest = file => createHash('sha256').update(fs.readFileSync(file)).digest('hex'); const expected = Object.fromEntries(['dist-client', 'dist-server'].flatMap(folder => files(path.join(workspace, folder)).map(relative => [path.join(folder, relative), digest(path.join(workspace, folder, relative))]))); const allowedEnv = /^(path|pathext|systemroot|systemdrive|windir|comspec|temp|tmp|userprofile|homedrive|homepath|home|appdata|localappdata|programfiles(?:\(x86\))?|commonprogramfiles(?:\(x86\))?|lang|lc_all)$/i; const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => allowedEnv.test(key))); let stage = 'install'; const resultFile = path.join(workspace, '.qa/source-package-validation.json'); function run(args, logName) { console.log('Source package: ' + stage); return new Promise((resolve, reject) => { const outputFile = fs.createWriteStream(path.join(sourceRoot, logName)); const child = spawn(process.execPath, args, { cwd: sourceRoot, env, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] }); let tail = ''; const capture = bytes => { outputFile.write(bytes); tail = (tail + bytes.toString('utf8')).slice(-12000); }; child.stdout.on('data', capture); child.stderr.on('data', capture); const timer = setTimeout(() => child.kill(), 600000); child.once('error', error => { clearTimeout(timer); outputFile.end(); reject(error); }); child.once('close', code => { clearTimeout(timer); outputFile.end(); code === 0 ? resolve(tail) : reject(new Error(stage + ' failed (' + code + '): ' + tail)); }); }); } try { await run([npmCli, 'ci', '--offline', '--no-audit', '--no-fund'], 'install.log'); stage = 'build'; await run([npmCli, 'run', 'build'], 'build.log'); const actualFiles = ['dist-client', 'dist-server'].flatMap(folder => files(path.join(sourceRoot, folder)).map(relative => path.join(folder, relative))); assert.deepEqual(actualFiles.slice().sort(), Object.keys(expected).sort()); for (const [relative, sha256] of Object.entries(expected)) assert.equal(digest(path.join(sourceRoot, relative)), sha256, relative); stage = 'tests'; const output = await run(['--test', '--test-concurrency=2', '--test-reporter=tap', ...files(path.join(sourceRoot, 'tests')).filter(file => !file.includes(path.sep) && file.endsWith('.test.mjs')).map(file => path.join('tests', file))], 'tests.tap'); const stats = Object.fromEntries([...output.matchAll(/^# (tests|pass|fail|cancelled|skipped|todo) (\d+)\s*$/gm)].map(match => [match[1], Number(match[2])])); assert.ok(stats.tests > 0); assert.equal(stats.pass, stats.tests); for (const key of ['fail', 'cancelled', 'skipped', 'todo']) assert.equal(stats[key], 0); const result = { success: true, version: evidence.version, testedAt: new Date().toISOString(), sourceRoot, offlineInstall: true, buildMatchedFiles: Object.keys(expected).length, tests: stats, credentialsHandling: 'Only build/runtime environment paths are inherited; no model configuration or credentials are used.' }; fs.writeFileSync(resultFile, JSON.stringify(result, null, 2)); console.log(JSON.stringify(result, null, 2)); } catch (error) { fs.writeFileSync(resultFile, JSON.stringify({ success: false, version: evidence.version, stage, error: error.message, sourceRoot }, null, 2)); throw error; }