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 root = path.resolve(import.meta.dirname, "../../.."); const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "library-linked-reload-unit-")); const sourcePath = path.join(root, "web/protocol/library-linked-reload.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, []); fs.writeFileSync(path.join(temporary, "library-linked-reload.mjs"), transpiled.outputText); const reload = await import(pathToFileURL(path.join(temporary, "library-linked-reload.mjs"))); const manifest = JSON.parse(fs.readFileSync(path.join(root, "tests/golden/M12-03H/manifest.json"), "utf8")); const sha256 = (file) => crypto.createHash("sha256").update(fs.readFileSync(path.join(root, file))).digest("hex"); const library = (value) => `library:${String(value).repeat(64).slice(0, 64)}`; const digest = (value) => crypto.createHash("sha256").update(String(value)).digest("hex"); const snapshot = (sourceLibraryId, generation, revision, marker) => ({ sourceLibraryId, sourceGeneration: generation, sourceRevision: revision, dependencyClosureSha256: digest(marker), graphSha256: digest(`${marker}-graph`), dataBlocks: [ { dataBlockId: `Object/${marker}`, owner: "SOURCE_LIBRARY", readOnly: true }, { dataBlockId: `Mesh/${marker}`, owner: "SOURCE_LIBRARY", readOnly: true }, ], }); test("M12-03H binds the reload protocol and evidence artifacts", () => { assert.equal(manifest.task, "M12-03H"); assert.equal(manifest.parentTask, "M12-03G"); assert.equal(manifest.nextTask, "M12-03I"); for (const artifact of Object.values(manifest.artifacts)) assert.equal(sha256(artifact.path), artifact.sha256, artifact.path); }); test("M12-03H replaces only the matching library generation and preserves every other snapshot", () => { const sourceLibraryId = library("a"); const otherLibraryId = library("b"); const current = snapshot(sourceLibraryId, 3, 7, "old"); const otherGeneration = snapshot(sourceLibraryId, 9, 2, "future"); const otherLibrary = snapshot(otherLibraryId, 1, 4, "other"); const state = { schemaVersion: 1, snapshots: [current, otherGeneration, otherLibrary] }; const replacement = snapshot(sourceLibraryId, 4, 8, "new"); const decision = reload.reloadMatchingLinkedSnapshot(state, { schemaVersion: 1, operation: "RELOAD", sourceLibraryId, expectedGeneration: 3, expectedRevision: 7, replacement, }); assert.equal(decision.status, "REPLACED"); assert.equal(decision.code, null); assert.deepEqual(decision.state.snapshots, [replacement, otherGeneration, otherLibrary]); assert.deepEqual(state.snapshots, [current, otherGeneration, otherLibrary]); assert(decision.state.snapshots.every((item) => item.dataBlocks.every((block) => block.owner === "SOURCE_LIBRARY" && block.readOnly))); }); test("M12-03H rejects stale generations and keeps state byte-for-byte equivalent", () => { const sourceLibraryId = library("c"); const state = { schemaVersion: 1, snapshots: [snapshot(sourceLibraryId, 5, 11, "stable")] }; const request = { schemaVersion: 1, operation: "RELOAD", sourceLibraryId, expectedGeneration: 4, expectedRevision: 10, replacement: snapshot(sourceLibraryId, 5, 12, "late"), }; const decision = reload.reloadMatchingLinkedSnapshot(state, request); assert.deepEqual(decision, { status: "STALE", code: "REVISION_CONFLICT", sourceLibraryId, replacedGeneration: 4, replacementGeneration: 5, state, }); }); test("M12-03H fails closed for malformed, substituted, duplicate, and non-adjacent reloads", () => { const sourceLibraryId = library("d"); const current = snapshot(sourceLibraryId, 1, 1, "current"); const baseRequest = { schemaVersion: 1, operation: "RELOAD", sourceLibraryId, expectedGeneration: 1, expectedRevision: 1, replacement: snapshot(sourceLibraryId, 2, 2, "replacement"), }; assert.throws(() => reload.parseLinkedReloadRequest({ ...baseRequest, future: true }), { code: "TASK_VALIDATION_FAILED" }); assert.throws(() => reload.parseLinkedReloadRequest({ ...baseRequest, replacement: { ...baseRequest.replacement, sourceLibraryId: library("e") } }), { code: "TASK_VALIDATION_FAILED" }); assert.throws(() => reload.parseLinkedReloadState({ schemaVersion: 1, snapshots: [current, current] }), { code: "TASK_VALIDATION_FAILED" }); const decision = reload.reloadMatchingLinkedSnapshot({ schemaVersion: 1, snapshots: [current] }, { ...baseRequest, replacement: snapshot(sourceLibraryId, 3, 3, "skip"), }); assert.equal(decision.status, "STALE"); assert.equal(decision.code, "REVISION_CONFLICT"); }); test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));