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-mutation-unit-")); const transpile = (sourceName, outputName, replacements = []) => { const sourcePath = path.join(root, "web/protocol", sourceName); 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, outputName), replacements.reduce((source, [from, to]) => source.replaceAll(from, to), transpiled.outputText)); }; transpile("capability-gates.ts", "capability-gates.mjs"); transpile("library-linked-mutation.ts", "library-linked-mutation.mjs", [["from \"./capability-gates\"", "from \"./capability-gates.mjs\""]]); const linked = await import(pathToFileURL(path.join(temporary, "library-linked-mutation.mjs"))); const manifest = JSON.parse(fs.readFileSync(path.join(root, "tests/golden/M12-03G/manifest.json"), "utf8")); const sha256 = (file) => crypto.createHash("sha256").update(fs.readFileSync(path.join(root, file))).digest("hex"); const base = (operation = "MESH_GEOMETRY") => ({ schemaVersion: 1, operation, dataBlockId: "mesh:M12 Link Mesh", baseRevision: 7, owner: "SOURCE_LIBRARY", linkedLibrary: true, readOnly: true, }); test("M12-03G binds the linked writer protocol and evidence artifacts", () => { assert.equal(manifest.task, "M12-03G"); assert.equal(manifest.parentTask, "M12-03F"); assert.equal(manifest.nextTask, "M12-03H"); for (const artifact of Object.values(manifest.artifacts)) assert.equal(sha256(artifact.path), artifact.sha256, artifact.path); }); test("M12-03G blocks every linked data writer with the stable mutation code", () => { assert.equal(linked.LINKED_DATA_WRITER_OPERATIONS.length, 6); for (const operation of linked.LINKED_DATA_WRITER_OPERATIONS) { const gate = linked.gateLinkedDataMutation(base(operation), 7); assert.equal(gate.status, "BLOCKED"); assert.deepEqual(gate.issues.map((issue) => issue.code), ["LINKED_DATA_MUTATION_BLOCKED"]); assert.equal(gate.issues[0].recoverable, false); assert.deepEqual(linked.parseLinkedDataMutation(base(operation)), { ...base(operation) }); } }); test("M12-03G rejects stale, malformed, and ownership-substituted writes before Main", () => { assert.equal(linked.gateLinkedDataMutation(base(), 8).issues[0].code, "REVISION_CONFLICT"); assert.equal(linked.gateLinkedDataMutation({ ...base(), owner: "LOCAL_MAIN", linkedLibrary: false, readOnly: false }, 7).issues[0].code, "LINKED_DATA_MUTATION_BLOCKED"); assert.equal(linked.gateLinkedDataMutation({ ...base(), future: true }, 7).issues[0].code, "TASK_VALIDATION_FAILED"); assert.equal(linked.gateLinkedDataMutation({ ...base(), operation: "UNKNOWN" }, 7).issues[0].code, "TASK_VALIDATION_FAILED"); });