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-migration-unit-")); const sources = ["asset-path.ts", "capability-gates.ts", "asset-library-io.ts", "asset-catalog-v2.ts", "asset-catalog-migration.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, []); const output = result.outputText.replaceAll(/from "\.\/([a-z0-9-]+)"/g, 'from "./$1.mjs"'); fs.writeFileSync(path.join(temporary, sourceName.replace(/\.ts$/, ".mjs")), output); } const migration = await import(pathToFileURL(path.join(temporary, "asset-catalog-migration.mjs"))); const source = JSON.parse(fs.readFileSync(path.join(repoRoot, "tests/golden/M12-01D/catalog-v1.json"), "utf8")); const target = JSON.parse(fs.readFileSync(path.join(repoRoot, "tests/golden/M12-01D/catalog-v2.json"), "utf8")); const report = JSON.parse(fs.readFileSync(path.join(repoRoot, "tests/golden/M12-01D/migration-report.json"), "utf8")); const manifest = JSON.parse(fs.readFileSync(path.join(repoRoot, "tests/golden/M12-01D/manifest.json"), "utf8")); test("M12-01D binds migration inputs, outputs, and production protocols", () => { assert.equal(manifest.nextTask, "M12-01E"); 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-01D migrates schema v1 to v2 without dropping source, preview, or library data", async () => { const actual = await migration.migrateAssetCatalogV1ToV2(source); assert.deepEqual(actual.manifest, target); assert.deepEqual(actual.report, report); assert.equal(actual.manifest.revision, 7); assert.deepEqual(actual.manifest.catalogs.map((item) => item.path), ["Animation", "Characters", "Characters/Heroes"]); assert.equal(actual.manifest.assets.filter((item) => item.preview !== null).length, 1); assert.deepEqual(actual.manifest.libraries.map((item) => item.libraryId), ["library:characters", "library:materials"]); assert.equal(actual.report.preserved.sourceBindings, 2); }); test("M12-01D preserves canonical UUIDs and deterministically maps legacy IDs", async () => { const first = await migration.migrateAssetCatalogV1ToV2(source); const second = await migration.migrateAssetCatalogV1ToV2(structuredClone(source)); assert.deepEqual(second, first); const canonical = first.report.catalogMappings.find((item) => item.legacyId.startsWith("4444")); assert.equal(canonical.catalogId, canonical.legacyId); assert.match(first.report.catalogMappings.find((item) => item.legacyId === "catalog:root").catalogId, /^[a-f0-9-]{36}$/); assert.equal(first.report.sourceManifestSha256, report.sourceManifestSha256); assert.equal(first.report.targetManifestSha256, report.targetManifestSha256); }); test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));