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 { pathToFileURL, fileURLToPath } from "node:url"; import ts from "../../web/node_modules/typescript/lib/typescript.js"; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const outputPath = path.resolve(process.argv[2] ?? path.join(root, "tests/golden/M12-06B/web-import-report.json")); const parentManifestPath = path.join(root, "tests/golden/M12-06A/manifest.json"); const fixtureReportPath = path.join(root, "tests/golden/M12-06A/desktop-fixtures.json"); const fixtureRoot = path.join(root, "tests/files/web/m12_glb_desktop_v1"); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "m12-06b-import-report-")); const sha256 = (bytes) => crypto.createHash("sha256").update(bytes).digest("hex"); const stableValue = (value) => Array.isArray(value) ? value.map(stableValue) : value && typeof value === "object" ? Object.fromEntries(Object.keys(value).sort().map((key) => [key, stableValue(value[key])])) : value; const stableSha256 = (value) => sha256(JSON.stringify(stableValue(value))); const arrayBuffer = (bytes) => bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); try { const sourcePath = path.join(root, "web/protocol/glb-import.ts"); const transpiled = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 }, fileName: sourcePath, reportDiagnostics: true, }); assert.deepEqual(transpiled.diagnostics, []); const modulePath = path.join(temporary, "glb-import.mjs"); fs.writeFileSync(modulePath, transpiled.outputText); const protocol = await import(pathToFileURL(modulePath)); const fixtures = JSON.parse(fs.readFileSync(fixtureReportPath, "utf8")).fixtures; const comparisons = fixtures.map((fixture) => { const bytes = fs.readFileSync(path.join(fixtureRoot, fixture.file)); assert.equal(sha256(bytes), fixture.sha256, `${fixture.id} source SHA-256`); const imported = protocol.importGLBDesktopFixtureSemantics(arrayBuffer(bytes)); const comparison = protocol.compareGLBDesktopFixtureSemantics(fixture.semantic, imported); const primitives = imported.meshes.flatMap((mesh) => mesh.primitives); return { id: fixture.id, file: fixture.file, sourceSha256: fixture.sha256, byteLength: bytes.byteLength, desktopSemanticSha256: stableSha256(fixture.semantic), webSemanticSha256: stableSha256(imported), compatible: comparison.compatible, mismatchCount: comparison.mismatches.length, topology: { meshCount: imported.meshes.length, primitiveCount: primitives.length, indexCount: primitives.reduce((sum, primitive) => sum + (primitive.indices?.count ?? 0), 0), modes: [...new Set(primitives.map((primitive) => primitive.mode))].sort((left, right) => left - right), }, attributes: [...new Set(primitives.flatMap((primitive) => Object.keys(primitive.attributes)))].sort(), materials: { count: imported.materials.length, pbrCount: imported.materials.filter((material) => material.pbr.metallicFactor !== null && material.pbr.roughnessFactor !== null).length, texturedCount: imported.materials.filter((material) => material.pbr.baseColorTexture !== null).length, }, nodes: { count: imported.nodes.length, namedCount: imported.nodes.filter((node) => node.name !== null).length, hierarchyEdges: imported.nodes.reduce((sum, node) => sum + node.children.length, 0), skinnedCount: imported.nodes.filter((node) => node.skin !== null).length, }, animations: { count: imported.animations.length, channelPaths: imported.animations.flatMap((animation) => animation.channels.map((channel) => channel.target.path)).sort(), sampleCounts: imported.animations.flatMap((animation) => animation.samplers.map((sampler) => sampler.input?.count ?? 0)), }, }; }); const report = { schemaVersion: 1, task: "M12-06B", operation: "WEB_GLB_IMPORT_SEMANTIC_COMPARISON", parentManifestSha256: sha256(fs.readFileSync(parentManifestPath)), fixtureReportSha256: sha256(fs.readFileSync(fixtureReportPath)), fixtureCount: comparisons.length, comparedDomains: ["topology", "attributes", "materials", "nodes", "animations"], allCompatible: comparisons.every((comparison) => comparison.compatible && comparison.mismatchCount === 0), comparisons, routeState: "BLOCKED_UNTIL_MAIN_PERSISTENCE", nextTask: "M12-06C", }; fs.mkdirSync(path.dirname(outputPath), { recursive: true }); fs.writeFileSync(outputPath, JSON.stringify(report, null, 2) + "\n"); process.stdout.write(`glb-desktop-import-report-generated fixtures=${report.fixtureCount} domains=${report.comparedDomains.length} compatible=${report.allCompatible} next=${report.nextTask}\n`); } finally { fs.rmSync(temporary, { recursive: true, force: true }); }