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 } from "node:url"; import test from "node:test"; import ts from "typescript"; const repoRoot = path.resolve(import.meta.dirname, "../../.."); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "asset-catalog-compatibility-unit-")); const sources = ["asset-path.ts", "capability-gates.ts", "asset-library-io.ts", "asset-catalog-v2.ts", "asset-catalog-compatibility.ts"]; for (const sourceName of sources) { const sourcePath = path.join(repoRoot, "web/protocol", sourceName); 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, []); fs.writeFileSync(path.join(temporary, sourceName.replace(/\.ts$/, ".mjs")), result.outputText.replaceAll(/from "\.\/([a-z0-9-]+)"/g, 'from "./$1.mjs"')); } const compatibility = await import(pathToFileURL(path.join(temporary, "asset-catalog-compatibility.mjs"))); const v1 = JSON.parse(fs.readFileSync(path.join(repoRoot, "tests/golden/M12-01D/catalog-v1.json"), "utf8")); const v2 = JSON.parse(fs.readFileSync(path.join(repoRoot, "tests/golden/M12-01D/catalog-v2.json"), "utf8")); const golden = JSON.parse(fs.readFileSync(path.join(repoRoot, "tests/golden/M12-01E/compatibility-report.json"), "utf8")); const manifest = JSON.parse(fs.readFileSync(path.join(repoRoot, "tests/golden/M12-01E/manifest.json"), "utf8")); test("M12-01E binds the compatibility policy and generated reports", () => { assert.equal(manifest.nextTask, "M12-01F"); for (const artifact of Object.values(manifest.artifacts)) { const actual = crypto.createHash("sha256").update(fs.readFileSync(path.join(repoRoot, artifact.path))).digest("hex"); assert.equal(actual, artifact.sha256, `${artifact.path} hash drifted`); } }); test("M12-01E exposes schema v2 to a v1 reader as a bounded read-only snapshot", async () => { const before = structuredClone(v2); const report = await compatibility.inspectAssetCatalogForLegacyReader(v2, "READ"); assert.deepEqual(report, golden.read); assert.equal(report.status, "READ_ONLY"); assert.equal(report.code, "ASSET_SCHEMA_DOWNGRADE_BLOCKED"); assert.deepEqual(report.snapshot.catalogs.map((item) => item.path), ["Animation", "Characters", "Characters/Heroes"]); assert.deepEqual(v2, before); }); test("M12-01E blocks every legacy write and save without changing the source hash", async () => { for (const [operation, key] of [["CATALOG_WRITE", "catalogWrite"], ["ASSET_WRITE", "assetWrite"], ["SAVE", "save"]]) { const report = await compatibility.inspectAssetCatalogForLegacyReader(v2, operation); assert.deepEqual(report, golden[key]); assert.equal(report.status, "BLOCKED"); assert.equal(report.code, "ASSET_SCHEMA_DOWNGRADE_BLOCKED"); assert.equal(report.sourceSha256, golden.read.sourceSha256); } }); test("M12-01E keeps native v1 ready and blocks unknown future schemas", async () => { assert.equal((await compatibility.inspectAssetCatalogForLegacyReader(v1, "SAVE")).status, "READY"); assert.deepEqual(await compatibility.inspectAssetCatalogForLegacyReader({ schemaVersion: 3 }, "READ"), golden.future); assert.equal(golden.future.code, "PROTOCOL_MISMATCH"); assert.equal(golden.future.recoverable, false); }); test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));