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-06B/manifest.json"); const reportPath = path.join(root, "tests/golden/M12-06B/web-import-report.json"); const generator = path.join(root, "tools/web/generate-glb-desktop-import-report.mjs"); const sha256 = (bytes) => crypto.createHash("sha256").update(bytes).digest("hex"); const fileHash = (file) => sha256(fs.readFileSync(file)); const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); const report = JSON.parse(fs.readFileSync(reportPath, "utf8")); assert.equal(manifest.schemaVersion, 1); assert.equal(manifest.task, "M12-06B"); assert.equal(manifest.parentTask, "M12-06A"); assert.equal(manifest.nextTask, "M12-06C"); for (const artifact of Object.values(manifest.artifacts)) { assert.equal(fileHash(path.join(root, artifact.path)), artifact.sha256, artifact.path); } assert.equal(report.schemaVersion, 1); assert.equal(report.task, "M12-06B"); assert.equal(report.operation, "WEB_GLB_IMPORT_SEMANTIC_COMPARISON"); assert.equal(report.parentManifestSha256, fileHash(path.join(root, "tests/golden/M12-06A/manifest.json"))); assert.equal(report.fixtureReportSha256, fileHash(path.join(root, "tests/golden/M12-06A/desktop-fixtures.json"))); assert.equal(report.fixtureCount, 5); assert.deepEqual(report.comparedDomains, ["topology", "attributes", "materials", "nodes", "animations"]); assert.equal(report.allCompatible, true); assert.equal(report.routeState, "BLOCKED_UNTIL_MAIN_PERSISTENCE"); assert.equal(report.nextTask, "M12-06C"); assert.deepEqual(report.comparisons.map((item) => item.id), ["mesh", "pbr", "uv", "skin", "animation"]); assert(report.comparisons.every((item) => item.compatible && item.mismatchCount === 0 && item.desktopSemanticSha256 === item.webSemanticSha256)); assert.deepEqual(report.comparisons.find((item) => item.id === "mesh").attributes, ["COLOR_0", "NORMAL", "POSITION"]); assert.equal(report.comparisons.find((item) => item.id === "pbr").materials.pbrCount, 1); assert.equal(report.comparisons.find((item) => item.id === "uv").materials.texturedCount, 1); assert.equal(report.comparisons.find((item) => item.id === "skin").nodes.skinnedCount, 1); assert.deepEqual(report.comparisons.find((item) => item.id === "animation").animations.channelPaths, ["rotation", "translation"]); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "m12-06b-import-check-")); try { const regenerated = path.join(temporary, "web-import-report.json"); const result = spawnSync(process.execPath, [generator, regenerated], { cwd: root, encoding: "utf8", maxBuffer: 20 * 1024 * 1024 }); assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`); assert.deepEqual(fs.readFileSync(regenerated), fs.readFileSync(reportPath), "Web import report is not deterministic"); } finally { fs.rmSync(temporary, { recursive: true, force: true }); } process.stdout.write(`glb-desktop-import-ok fixtures=${report.fixtureCount} domains=${report.comparedDomains.length} compatible=${report.allCompatible} route=${report.routeState} next=${manifest.nextTask}\n`);