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 matrixPath = path.join(repoRoot, "tests/golden/M12-05B/capability-matrix.json"); const inventoryPath = path.join(repoRoot, "tests/golden/M12-05A/format-inventory.json"); const evidencePath = path.join(repoRoot, "tests/golden/M12-05B/manifest.json"); const sourcePath = path.join(repoRoot, "web/protocol/io-format-capability-matrix.ts"); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "io-format-capability-matrix-")); const sha256 = (bytes) => crypto.createHash("sha256").update(bytes).digest("hex"); const fileSha256 = (file) => sha256(fs.readFileSync(file)); try { 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, "io-format-capability-matrix.mjs"); fs.writeFileSync(modulePath, transpiled.outputText); const protocol = await import(pathToFileURL(modulePath)); const evidence = JSON.parse(fs.readFileSync(evidencePath, "utf8")); assert.equal(evidence.task, "M12-05B"); assert.equal(evidence.parentTask, "M12-05A"); assert.equal(evidence.nextTask, "M12-05C"); for (const artifact of Object.values(evidence.artifacts)) assert.equal(fileSha256(path.join(repoRoot, artifact.path)), artifact.sha256, artifact.path); const inventoryBytes = fs.readFileSync(inventoryPath); const matrixBytes = fs.readFileSync(matrixPath); const matrix = JSON.parse(matrixBytes); assert.equal(matrix.runtimeInventorySha256, sha256(inventoryBytes)); const inventory = JSON.parse(inventoryBytes); const parsed = protocol.parseIOFormatCapabilityMatrix(matrix); assert.deepEqual(parsed.formats.map((entry) => entry.format), inventory.formats.map((entry) => entry.format)); const runtimeByFormat = new Map(inventory.formats.map((entry) => [entry.format, entry])); for (const entry of parsed.formats) { const runtime = runtimeByFormat.get(entry.format); assert.equal(entry.runtimeImportStatus, runtime.import.runtimeStatus === "AVAILABLE" ? "AVAILABLE" : "OPERATOR_UNREGISTERED"); assert.equal(entry.runtimeExportStatus, runtime.export.runtimeStatus === "AVAILABLE" ? "AVAILABLE" : "OPERATOR_UNREGISTERED"); assert.equal(entry.operations.EXPORT.local.status, entry.format === "GLB" ? "READY" : "BLOCKED"); assert.equal(entry.operations.EXPORT.local.execution, entry.format === "GLB" ? "LOCAL" : "NONE"); assert.equal(entry.operations.EXPORT.server.status, "BLOCKED"); assert.equal(entry.operations.IMPORT.local.status, "BLOCKED"); assert.equal(entry.operations.IMPORT.server.status, "BLOCKED"); } const regenerated = path.join(temporary, "capability-matrix.json"); execFileSync(process.execPath, [path.join(repoRoot, "tools/web/generate-io-format-capability-matrix.mjs"), "--output", regenerated], { cwd: repoRoot }); assert.deepEqual(fs.readFileSync(regenerated), matrixBytes, "capability matrix is not deterministic"); process.stdout.write("io-format-capability-matrix-ok formats=7 local-glb-export=1 blocked-routes=27 runtime-bound=true\n"); } finally { fs.rmSync(temporary, { recursive: true, force: true }); }