import assert from "node:assert/strict"; import crypto from "node:crypto"; import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { execFileSync } from "node:child_process"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const task = process.argv[2]; const config = { "M15-02C": { mode: "LOCAL_EXACT", nextTask: "M15-02D", parent: "M15-02B", dir: "M15-02C", file: "local-exact-audit" }, "M15-02D": { mode: "LOCAL_EQUIVALENT", nextTask: "M15-02E", parent: "M15-02C", dir: "M15-02D", file: "local-equivalent-audit" }, "M15-02E": { mode: "SERVER_EXACT", nextTask: "M15-02F", parent: "M15-02D", dir: "M15-02E", file: "server-exact-audit" }, "M15-02F": { mode: "UNKNOWN_DATA_PRESERVATION", nextTask: "M15-03A", parent: "M15-02E", dir: "M15-02F", file: "unknown-data-audit" }, }; const c = config[task]; if (!c) throw new Error("usage: node check-blender-contract-audit.mjs M15-02C"); const generator = path.join(root, "tools/web/generate-blender-contract-audit.mjs"); const auditPath = path.join(root, `tests/golden/${c.dir}/${c.file}.json`); const reportPath = path.join(root, `tests/golden/${c.dir}/${c.file}-check-report.json`); const manifestPath = path.join(root, `tests/golden/${c.dir}/manifest.json`); const sha256 = (bytes) => crypto.createHash("sha256").update(bytes).digest("hex"); const fileSha256 = (file) => sha256(fs.readFileSync(file)); const relative = (file) => path.relative(root, file).replaceAll(path.sep, "/"); const audit = JSON.parse(fs.readFileSync(auditPath, "utf8")); assert.equal(audit.schemaVersion, 1); assert.equal(audit.task, task); assert.equal(audit.operation, "BLENDER_PARITY_CONTRACT_AUDIT"); assert.equal(audit.contract.mode, c.mode); assert.equal(audit.contract.failClosed, true); assert.equal(audit.summary.inventoried, 6900); assert.equal(audit.summary.applicable, 0); assert.equal(audit.summary.blocked, 0); assert.equal(audit.summary.ready, 0); assert.equal(audit.summary.notApplicable, audit.summary.inventoried); assert.equal(audit.interpretation, "NO_CLAIMS_DECLARED"); assert.deepEqual(audit.blockedIds, []); assert.deepEqual(audit.readyIds, []); assert.equal(fileSha256(path.join(root, audit.source.path)), audit.source.sha256); const temporary = path.join(root, `.tmp-${task}.json`); try { execFileSync(process.execPath, [generator, task, temporary], { cwd: root, stdio: "pipe" }); assert.deepEqual(fs.readFileSync(temporary), fs.readFileSync(auditPath), `${task} audit is not deterministic`); } finally { fs.rmSync(temporary, { force: true }); } const report = { schemaVersion: 1, task, operation: "BLENDER_PARITY_CONTRACT_AUDIT_CHECK", contract: audit.contract, summary: audit.summary, auditSha256: fileSha256(auditPath), nextTask: audit.nextTask }; if (process.env[`UPDATE_${task.replaceAll("-", "_")}_MANIFEST`] === "1") { fs.mkdirSync(path.dirname(reportPath), { recursive: true }); fs.writeFileSync(reportPath, `${JSON.stringify(report, null, 2)}\n`); const artifactPaths = { parentManifest: path.join(root, `tests/golden/${c.parent}/manifest.json`), generator, checker: path.join(root, "tools/web/check-blender-contract-audit.mjs"), audit: auditPath, report: reportPath, package: path.join(root, "web/package.json") }; const artifacts = Object.fromEntries(Object.entries(artifactPaths).map(([name, file]) => [name, { path: relative(file), sha256: fileSha256(file) }])); fs.writeFileSync(manifestPath, `${JSON.stringify({ schemaVersion: 1, task, parentTask: c.parent, enablingTask: false, parityStateChange: false, runtime: "BLENDER_5_2_PARITY", operation: "BLENDER_PARITY_CONTRACT_AUDIT", artifacts, nextTask: c.nextTask }, null, 2)}\n`); } const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); assert.deepEqual({ task: manifest.task, parentTask: manifest.parentTask, nextTask: manifest.nextTask }, { task, parentTask: c.parent, nextTask: c.nextTask }); for (const artifact of Object.values(manifest.artifacts)) assert.equal(fileSha256(path.join(root, artifact.path)), artifact.sha256, artifact.path); process.stdout.write(`blender-contract-audit-ok task=${task} mode=${c.mode} applicable=0 blocked=0 notApplicable=${audit.summary.notApplicable} next=${manifest.nextTask}\n`);