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 { execFileSync } from "node:child_process"; import { fileURLToPath } from "node:url"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const read = (relativePath) => fs.readFileSync(path.join(root, relativePath)); const sha256 = (value) => crypto.createHash("sha256").update(value).digest("hex"); const manifest = JSON.parse(read("tests/golden/M12-03F/manifest.json")); const report = JSON.parse(read("tests/golden/M12-03F/desktop-link-report.json")); const fixtureRoot = path.join(root, "tests/files/web/m12_library_link_v1"); const generator = path.join(root, "tools/web/generate-library-link-fixture.py"); const blender = process.env.BLENDER_BIN ?? path.join(root, "build_blender_5.2.0/bin/blender"); assert.equal(manifest.task, "M12-03F"); assert.equal(manifest.parentTask, "M12-03E"); assert.equal(manifest.nextTask, "M12-03G"); assert.equal(manifest.operation, "LINK"); for (const artifact of Object.values(manifest.artifacts)) { assert.equal(sha256(read(artifact.path)), artifact.sha256, artifact.path); } assert.equal(report.schemaVersion, 1); assert.equal(report.task, "M12-03F"); assert.equal(report.operation, "LINK"); assert.equal(report.blenderVersion, "5.2.0"); assert.equal(report.nextTask, "M12-03G"); assert.deepEqual(report.selectedRoots, ["Object/M12 Link Object"]); assert.deepEqual(report.sourceGraph, { edges: [ { from: "Object/M12 Link Object", relation: "OBJECT_DATA", to: "Mesh/M12 Link Mesh" }, { from: "Mesh/M12 Link Mesh", relation: "MATERIAL_SLOT[0]", to: "Material/M12 Link Material" }, { from: "Material/M12 Link Material", relation: "NODE_IMAGE[M12 Link Image Node]", to: "Image/M12 Link Image" }, ], geometry: { edges: 4, loops: 4, materialSlots: ["M12 Link Material"], polygons: 1, uvLayers: ["UVMap"], vertices: 4 }, ids: { IMAGE: { idType: "IMAGE", isLibraryOverride: false, library: null, name: "M12 Link Image", nameFull: "M12 Link Image" }, MATERIAL: { idType: "MATERIAL", isLibraryOverride: false, library: null, name: "M12 Link Material", nameFull: "M12 Link Material" }, MESH: { idType: "MESH", isLibraryOverride: false, library: null, name: "M12 Link Mesh", nameFull: "M12 Link Mesh" }, OBJECT: { idType: "OBJECT", isLibraryOverride: false, library: null, name: "M12 Link Object", nameFull: "M12 Link Object" }, }, image: { channels: 4, colorspace: "sRGB", packed: true, pixelFloat32Sha256: "6f0f8c231d65149e69e6ed12d370bcfa095ef90adc202065492ea8c8ef17e45a", size: [2, 2] }, root: { idType: "OBJECT", isLibraryOverride: false, library: null, name: "M12 Link Object", nameFull: "M12 Link Object" }, sourceMarker: "M12-03F", }); assert.deepEqual(report.linkedGraph.edges, report.sourceGraph.edges); assert.deepEqual(report.linkedGraph.geometry, report.sourceGraph.geometry); assert.deepEqual(report.linkedGraph.image, report.sourceGraph.image); assert.equal(report.linkedGraph.ids.OBJECT.library, "m12_link_source.blend"); assert.equal(report.linkedGraph.ids.OBJECT.nameFull, "M12 Link Object [m12_link_source.blend]"); for (const value of Object.values(report.linkedGraph.ids)) { assert.equal(value.library, "m12_link_source.blend"); assert.equal(value.isLibraryOverride, false); } assert.deepEqual(report.stableMapping.map((item) => [item.owner, item.readOnly]), [ ["SOURCE_LIBRARY", true], ["SOURCE_LIBRARY", true], ["SOURCE_LIBRARY", true], ["SOURCE_LIBRARY", true], ]); assert.deepEqual(report.stableMapping.map((item) => item.source), [ "Object/M12 Link Object", "Mesh/M12 Link Mesh", "Material/M12 Link Material", "Image/M12 Link Image", ]); assert.deepEqual(report.stableMapping.map((item) => item.local), report.stableMapping.map((item) => item.source)); assert.equal(sha256(fs.readFileSync(path.join(fixtureRoot, report.source.file))), report.source.sha256); assert.equal(sha256(fs.readFileSync(path.join(fixtureRoot, report.target.file))), report.target.sha256); const normalizeContainerHashes = (value) => ({ ...value, source: { ...value.source, sha256: "" }, target: { ...value.target, sha256: "" }, }); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "m12-library-link-")); try { const generatedFixtureRoot = path.join(temporary, "files"); const generatedReportPath = path.join(temporary, "report.json"); const output = execFileSync(blender, [ "--background", "--factory-startup", "--python", generator, "--", generatedFixtureRoot, generatedReportPath, ], { cwd: root, encoding: "utf8" }); assert.match(output, /library-link-fixture-ok roots=1 mapping=4/); assert.match(output, /next=M12-03G/); const generated = JSON.parse(fs.readFileSync(generatedReportPath, "utf8")); assert.deepEqual(normalizeContainerHashes(generated), normalizeContainerHashes(report)); assert.equal(sha256(fs.readFileSync(path.join(generatedFixtureRoot, generated.source.file))), generated.source.sha256); assert.equal(sha256(fs.readFileSync(path.join(generatedFixtureRoot, generated.target.file))), generated.target.sha256); } finally { fs.rmSync(temporary, { recursive: true, force: true }); } process.stdout.write( `library-link-fixture-check-ok roots=${report.selectedRoots.length} mapping=${report.stableMapping.length} ` + `library=${report.linkedGraph.ids.OBJECT.library} readOnly=${report.stableMapping.every((item) => item.readOnly)} next=${report.nextTask}\n`, );