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 { spawnSync } from "node:child_process"; import { fileURLToPath } from "node:url"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const manifestPath = path.join(root, "tests/golden/M12-07B/manifest.json"); const reportPath = path.join(root, "tests/golden/M12-07B/desktop-fixtures.json"); const fixtureRoot = path.join(root, "tests/files/web/m12_obj_multi_v1"); const generator = path.join(root, "tools/web/generate-obj-multi-negative-fixtures.py"); const blender = process.env.BLENDER_BIN ?? path.join(root, "build_blender_5.2.0/bin/blender"); const sha256 = (bytes) => crypto.createHash("sha256").update(bytes).digest("hex"); const fileHash = (file) => sha256(fs.readFileSync(file)); const readJson = (file) => JSON.parse(fs.readFileSync(file, "utf8")); function parseObj(file) { const positions = []; const texcoords = []; const normals = []; const faces = []; const groups = []; let object = null; let material = null; for (const raw of fs.readFileSync(file, "utf8").split(/\r?\n/)) { const line = raw.trim(); if (!line || line.startsWith("#")) continue; const parts = line.split(/\s+/); if (parts[0] === "v") positions.push(parts.slice(1)); else if (parts[0] === "vt") texcoords.push(parts.slice(1)); else if (parts[0] === "vn") normals.push(parts.slice(1)); else if (parts[0] === "g") { for (const group of parts.slice(1)) { groups.push(group); if (group.endsWith("_Mesh")) object = group; } } else if (parts[0] === "usemtl") material = parts.slice(1).join(" "); else if (parts[0] === "f") faces.push({ object, material, tokens: parts.slice(1) }); } return { positions, texcoords, normals, groups, objects: [...new Set(faces.map((face) => face.object).filter(Boolean))], faces }; } const manifest = readJson(manifestPath); const report = readJson(reportPath); assert.deepEqual( { schemaVersion: manifest.schemaVersion, task: manifest.task, parentTask: manifest.parentTask, nextTask: manifest.nextTask }, { schemaVersion: 1, task: "M12-07B", parentTask: "M12-07A", nextTask: "M12-07C" }, ); assert.deepEqual( { schemaVersion: report.schemaVersion, task: report.task, operation: report.operation, nextTask: report.nextTask }, { schemaVersion: 1, task: "M12-07B", operation: "DESKTOP_OBJ_MULTI_OBJECT_AND_NEGATIVE_FIXTURES", nextTask: "M12-07C" }, ); for (const artifact of Object.values(manifest.artifacts)) { if (artifact.path === manifestPath) continue; const absolute = path.join(root, artifact.path); assert.ok(fs.existsSync(absolute), `missing artifact ${artifact.path}`); assert.equal(fileHash(absolute), artifact.sha256, `hash mismatch ${artifact.path}`); } const inventory = readJson(path.join(root, "tests/golden/M12-05A/format-inventory.json")); for (const key of ["blenderVersion", "versionTuple", "buildDate", "buildTime", "buildHash", "buildBranch", "buildPlatform", "buildType", "binarySha256"]) { assert.deepEqual(report.runtime[key], inventory.runtime[key], `runtime drift in ${key}`); } assert.equal(report.semantic.objects.length, 2); assert.equal(report.semantic.positions.length, 6); assert.equal(report.semantic.texcoords.length, 6); assert.equal(report.semantic.normals.length, 2); assert.equal(report.semantic.faces.length, 2); assert.equal(report.semantic.materials.length, 2); assert.deepEqual(report.textureOrigin, { mtlMapKd: ["m12_obj_texture.png", "m12_obj_texture.png"], relative: true, textureFile: "m12_obj_texture.png" }); assert.deepEqual(report.negativeIndex, { file: "negative-index.obj", expectedStatus: "ACCEPT_WITH_NEGATIVE_INDICES", faceCount: 2 }); assert.deepEqual(report.malformedFace, { file: "malformed-face.obj", expectedCode: "OBJ_FACE_ARITY_INVALID" }); const positive = parseObj(path.join(fixtureRoot, "multi-object.obj")); assert.equal(positive.objects.length, 2); assert.equal(positive.faces.length, 2); assert.ok(positive.faces.every((face) => face.tokens.length === 3)); assert.ok(positive.faces.every((face) => face.tokens.every((token) => token.split("/").length === 3))); const negative = parseObj(path.join(fixtureRoot, "negative-index.obj")); assert.ok(negative.faces.every((face) => face.tokens.every((token) => token.split("/").every((index) => Number(index) < 0)))); assert.equal(negative.faces.length, 2); const malformed = parseObj(path.join(fixtureRoot, "malformed-face.obj")); assert.equal(malformed.faces[0].tokens.length, 2); assert.equal(malformed.faces[0].tokens.length === 3 ? "ACCEPTED" : "OBJ_FACE_ARITY_INVALID", "OBJ_FACE_ARITY_INVALID"); const texture = fs.readFileSync(path.join(fixtureRoot, "m12_obj_texture.png")); assert.deepEqual([...texture.subarray(0, 8)], [137, 80, 78, 71, 13, 10, 26, 10]); for (const file of report.files) { const absolute = path.join(fixtureRoot, file.name); assert.equal(fileHash(absolute), file.sha256, `${file.name} hash`); assert.equal(fs.statSync(absolute).size, file.byteLength, `${file.name} byte length`); } const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "m12-07b-obj-")); try { const regeneratedReport = path.join(temporary, "desktop-fixtures.json"); const result = spawnSync(blender, ["-b", "--factory-startup", "--python", generator, "--", temporary, regeneratedReport], { cwd: root, encoding: "utf8", maxBuffer: 20 * 1024 * 1024, }); assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`); assert.deepEqual(readJson(regeneratedReport), report, "OBJ multi/negative report is not deterministic"); for (const file of report.files) assert.deepEqual(fs.readFileSync(path.join(temporary, file.name)), fs.readFileSync(path.join(fixtureRoot, file.name)), `${file.name} bytes are not deterministic`); } finally { fs.rmSync(temporary, { recursive: true, force: true }); } process.stdout.write(`obj-multi-negative-fixtures-ok objects=${report.semantic.objects.length} negative=true malformed=${report.malformedFace.expectedCode} textureOrigin=relative deterministic=true next=${manifest.nextTask}\n`);