73 lines
5.5 KiB
JavaScript
73 lines
5.5 KiB
JavaScript
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-family-inventory.py");
|
|
const expectedPath = path.join(root, "tests/golden/M15-01C/blender-family-inventory.json");
|
|
const reportPath = path.join(root, "tests/golden/M15-01C/family-inventory-check-report.json");
|
|
const manifestPath = path.join(root, "tests/golden/M15-01C/manifest.json");
|
|
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "blender-family-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 family inventory is not deterministic");
|
|
const inventory = JSON.parse(expectedBytes);
|
|
assert.equal(inventory.schemaVersion, 1);
|
|
assert.equal(inventory.task, "M15-01C");
|
|
assert.equal(inventory.operation, "BLENDER_FAMILY_INVENTORY");
|
|
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.modifierCount >= 80);
|
|
assert.ok(inventory.summary.constraintCount >= 25);
|
|
assert.ok(inventory.summary.nodeCount >= 400);
|
|
assert.deepEqual(Object.keys(inventory.summary.nodesByFamily).sort(), ["Compositor", "Geometry", "Shader"]);
|
|
assert.equal(inventory.nodes.length, inventory.summary.nodeCount);
|
|
for (const [key, value] of Object.entries(inventory.summary.nodesByFamily)) assert.equal(inventory.nodes.filter((node) => node.family === key).length, value);
|
|
const checkSortedUnique = (values, label) => {
|
|
assert.deepEqual(values, [...values].sort(), `${label} are not sorted`);
|
|
assert.equal(new Set(values).size, values.length, `${label} are not unique`);
|
|
};
|
|
checkSortedUnique(inventory.modifiers.map((entry) => entry.identifier), "modifier identifiers");
|
|
checkSortedUnique(inventory.constraints.map((entry) => entry.identifier), "constraint identifiers");
|
|
checkSortedUnique(inventory.nodes.map((entry) => `${entry.family}:${entry.rnaIdentifier}`), "node identifiers");
|
|
for (const entry of [...inventory.modifiers, ...inventory.constraints]) {
|
|
assert.match(entry.identifier, /^[A-Z0-9_]+$/);
|
|
assert.equal(typeof entry.name, "string");
|
|
assert.equal(typeof entry.description, "string");
|
|
assert.equal(typeof entry.value, "number");
|
|
}
|
|
for (const entry of inventory.nodes) {
|
|
assert.match(entry.family, /^(Shader|Geometry|Compositor)$/);
|
|
assert.match(entry.rnaIdentifier, /^(Shader|Geometry|Compositor)Node[A-Za-z0-9_]+$/);
|
|
assert.ok(Array.isArray(entry.properties));
|
|
checkSortedUnique(entry.properties.map((property) => property.identifier), `${entry.rnaIdentifier} properties`);
|
|
}
|
|
const report = { schemaVersion: 1, task: "M15-01C", operation: "BLENDER_FAMILY_INVENTORY_CHECK", runtime: inventory.runtime, summary: inventory.summary, firstModifier: inventory.modifiers[0].identifier, lastNode: inventory.nodes.at(-1).rnaIdentifier, inventorySha256: sha256(expectedBytes), nextTask: inventory.nextTask };
|
|
if (process.env.UPDATE_M15_01C_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-01B/manifest.json"), generator, checker: path.join(root, "tools/web/check-blender-family-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-01C", parentTask: "M15-01B", enablingTask: false, parityStateChange: false, runtime: "BLENDER_5_2_FAMILY", operation: "BLENDER_FAMILY_INVENTORY", artifacts, nextTask: "M15-01D" }, null, 2)}\n`);
|
|
}
|
|
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
assert.deepEqual({ task: manifest.task, parentTask: manifest.parentTask, nextTask: manifest.nextTask }, { task: "M15-01C", parentTask: "M15-01B", nextTask: "M15-01D" });
|
|
for (const artifact of Object.values(manifest.artifacts)) assert.equal(fileSha256(path.join(root, artifact.path)), artifact.sha256, artifact.path);
|
|
process.stdout.write(`blender-family-inventory-ok modifiers=${inventory.summary.modifierCount} constraints=${inventory.summary.constraintCount} nodes=${inventory.summary.nodeCount} inventory=${sha256(expectedBytes)} next=${manifest.nextTask}\n`);
|
|
} finally {
|
|
fs.rmSync(temporary, { recursive: true, force: true });
|
|
}
|