import assert from "node:assert/strict"; import crypto from "node:crypto"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { spawnSync } from "node:child_process"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const generator = path.join(root, "tools/web/generate-blender-operator-inventory.py"); const expectedPath = path.join(root, "tests/golden/M15-01B/blender-operator-inventory.json"); const reportPath = path.join(root, "tests/golden/M15-01B/operator-inventory-check-report.json"); const manifestPath = path.join(root, "tests/golden/M15-01B/manifest.json"); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "blender-operator-inventory-")); const regeneratedPath = path.join(temporary, "inventory.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, "/"); try { const blender = process.env.BLENDER_BIN ?? path.join(root, "build_blender_5.2.0/bin/blender"); const run = spawnSync(blender, ["-b", "--factory-startup", "--python", generator, "--", regeneratedPath], { cwd: root, encoding: "utf8", maxBuffer: 32 * 1024 * 1024 }); assert.equal(run.status, 0, `${run.stdout ?? ""}\n${run.stderr ?? ""}`); const expectedBytes = fs.readFileSync(expectedPath); assert.deepEqual(fs.readFileSync(regeneratedPath), expectedBytes, "Blender operator inventory is not deterministic"); const inventory = JSON.parse(expectedBytes); assert.deepEqual(inventory, JSON.parse(fs.readFileSync(regeneratedPath, "utf8"))); assert.equal(inventory.schemaVersion, 1); assert.equal(inventory.task, "M15-01B"); assert.deepEqual(inventory.runtime.versionTuple, [5, 2, 0]); assert.equal(inventory.runtime.blenderVersion, "5.2.0 LTS"); assert.match(inventory.runtime.binarySha256, /^[a-f0-9]{64}$/); assert.ok(inventory.summary.count >= 500, `unexpectedly small operator inventory: ${inventory.summary.count}`); assert.equal(inventory.operators.length, inventory.summary.count); const operatorIds = inventory.operators.map((entry) => entry.operator); assert.deepEqual(operatorIds, [...operatorIds].sort(), "operator IDs are not sorted"); assert.equal(new Set(operatorIds).size, operatorIds.length, "operator IDs are not unique"); assert.equal(inventory.summary.registered, inventory.operators.filter((entry) => entry.registered).length); assert.equal(inventory.summary.pollTrue + inventory.summary.pollFalse + inventory.summary.pollUnknown, inventory.summary.count); for (const entry of inventory.operators) { assert.match(entry.operator, /^[a-z0-9_]+\.[a-z0-9_]+$/); assert.equal(entry.registered, true); assert.match(entry.rnaIdentifier, /^[A-Za-z0-9_]+$/); assert.ok(Array.isArray(entry.properties)); const properties = entry.properties.map((property) => property.identifier); assert.deepEqual(properties, [...properties].sort(), `${entry.operator} properties are not sorted`); } const report = { schemaVersion: 1, task: "M15-01B", operation: "BLENDER_OPERATOR_INVENTORY_CHECK", runtime: inventory.runtime, count: inventory.summary.count, registered: inventory.summary.registered, poll: { true: inventory.summary.pollTrue, false: inventory.summary.pollFalse, unknown: inventory.summary.pollUnknown }, first: operatorIds[0], last: operatorIds.at(-1), inventorySha256: sha256(expectedBytes), nextTask: inventory.nextTask }; if (process.env.UPDATE_M15_01B_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/M15-01A/manifest.json"), generator, checker: path.join(root, "tools/web/check-blender-operator-inventory.mjs"), inventory: expectedPath, report: reportPath, runtime: blender, 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: "M15-01B", parentTask: "M15-01A", enablingTask: false, parityStateChange: false, runtime: "BLENDER_5_2_OPERATOR", operation: "BLENDER_OPERATOR_INVENTORY", artifacts, nextTask: "M15-01C" }, null, 2)}\n`); } const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); assert.deepEqual({ task: manifest.task, parentTask: manifest.parentTask, nextTask: manifest.nextTask }, { task: "M15-01B", parentTask: "M15-01A", nextTask: "M15-01C" }); for (const artifact of Object.values(manifest.artifacts)) assert.equal(fileSha256(path.join(root, artifact.path)), artifact.sha256, artifact.path); process.stdout.write(`blender-operator-inventory-ok count=${inventory.summary.count} registered=${inventory.summary.registered} pollUnknown=${inventory.summary.pollUnknown} inventory=${sha256(expectedBytes)} next=${manifest.nextTask}\n`); } finally { fs.rmSync(temporary, { recursive: true, force: true }); }