import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import { execFileSync } from 'node:child_process'; import { createHash } from 'node:crypto'; const directory = path.resolve(process.argv[2] ?? 'docs/live-validation-aa-evidence'); const hash = bytes => createHash('sha256').update(bytes).digest('hex'); const manifestBytes = fs.readFileSync(path.join(directory, 'MANIFEST.json')), manifest = JSON.parse(manifestBytes); for (const row of manifest.files) { assert.ok(!row.path.split(/[\\/]/).includes('..') && !path.isAbsolute(row.path)); const bytes = fs.readFileSync(path.join(directory, row.path)); assert.equal(bytes.length, row.bytes); assert.equal(hash(bytes), row.sha256); } assert.equal(manifest.filesCount, manifest.files.length); assert.equal(manifest.totalBytes, manifest.files.reduce((sum, row) => sum + row.bytes, 0)); const files = []; const walk = relative => { for (const entry of fs.readdirSync(path.join(directory, relative), { withFileTypes: true })) { const file = path.posix.join(relative, entry.name); entry.isDirectory() ? walk(file) : files.push(file); } }; walk(''); assert.deepEqual(files.filter(file => file !== 'MANIFEST.json').sort(), manifest.files.map(row => row.path).sort()); if (process.argv.includes('--index')) for (const row of [...manifest.files, { path: 'MANIFEST.json', bytes: manifestBytes.length, sha256: hash(manifestBytes) }]) { const relative = path.relative(process.cwd(), path.join(directory, row.path)).replaceAll('\\', '/'); const bytes = execFileSync('git', ['show', ':' + relative], { windowsHide: true, maxBuffer: Math.max(1024 * 1024, row.bytes + 1024) }); assert.equal(bytes.length, row.bytes, relative); assert.equal(hash(bytes), row.sha256, relative); } const result = { passed: true, files: manifest.filesCount, bytes: manifest.totalBytes, manifestSha256: hash(manifestBytes), gitIndex: process.argv.includes('--index'), modelRequests: 0 }; const receiptIndex = process.argv.indexOf('--receipt'); if (receiptIndex >= 0) fs.writeFileSync(process.argv[receiptIndex + 1], JSON.stringify(result, null, 2) + '\n', { flag: 'wx' }); console.log(JSON.stringify(result));