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, pathToFileURL } from "node:url"; import ts from "../../web/node_modules/typescript/lib/typescript.js"; const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); const registryPath = path.join(repoRoot, "web/app/src/capabilities/io-format-ui-registry.json"); const matrixPath = path.join(repoRoot, "tests/golden/M12-05B/capability-matrix.json"); const matrixProtocolPath = path.join(repoRoot, "web/protocol/io-format-capability-matrix.ts"); const gateProtocolPath = path.join(repoRoot, "web/protocol/io-format-ui-gate.ts"); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "io-format-ui-gate-check-")); const sha256 = (bytes) => crypto.createHash("sha256").update(bytes).digest("hex"); try { const registry = JSON.parse(fs.readFileSync(registryPath, "utf8")); const matrixBytes = fs.readFileSync(matrixPath); const transpile = (sourcePath, fileName) => { const result = ts.transpileModule(fs.readFileSync(sourcePath, "utf8"), { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 }, fileName: sourcePath, reportDiagnostics: true }); assert.deepEqual(result.diagnostics, []); const target = path.join(temporary, fileName); fs.writeFileSync(target, result.outputText); return target; }; const matrixProtocol = await import(pathToFileURL(transpile(matrixProtocolPath, "matrix.mjs"))); const gateProtocol = await import(pathToFileURL(transpile(gateProtocolPath, "gate.mjs"))); const matrix = matrixProtocol.parseIOFormatCapabilityMatrix(JSON.parse(matrixBytes)); gateProtocol.validateIOFormatUIRegistry(registry, matrix, sha256(matrixBytes)); assert.equal(registry.task, "M12-05C"); assert.equal(registry.parentMatrixSha256, sha256(matrixBytes)); assert.equal(registry.projectFileAccept, ".blend,application/octet-stream"); assert.deepEqual(registry.importRoutes, [], "no blocked import route may reach the file selector"); assert.deepEqual(registry.exportRoutes, [{ format: "GLB", operation: "EXPORT", execution: "LOCAL", extensions: [".glb"] }]); const regenerated = path.join(temporary, "registry.json"); execFileSync(process.execPath, [path.join(repoRoot, "tools/web/generate-io-format-ui-gate.mjs"), "--output", regenerated], { cwd: repoRoot }); assert.deepEqual(fs.readFileSync(regenerated), fs.readFileSync(registryPath), "UI registry is not deterministic"); process.stdout.write("io-format-ui-gate-ok import-routes=0 export-routes=1 file-accept=.blend,application/octet-stream fail-closed=true\n"); } finally { fs.rmSync(temporary, { recursive: true, force: true }); }