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-override-writer-unit-")); const sourcePath = path.join(root, "web/protocol/library-override-writer.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-override-writer.mjs"), transpiled.outputText); const writer = await import(pathToFileURL(path.join(temporary, "library-override-writer.mjs"))); const manifest = JSON.parse(fs.readFileSync(path.join(root, "tests/golden/M12-03K/manifest.json"), "utf8")); const sha256 = (file) => crypto.createHash("sha256").update(fs.readFileSync(path.join(root, file))).digest("hex"); const state = (revision = 3, value = 2.5) => ({ schemaVersion: 1, revision, localDataBlockId: "Object/M12 Override Object", referenceSourceDataBlockId: "Object/M12 Override Object", hierarchyRootDataBlockId: "Object/M12 Override Object", owner: "LOCAL_OVERRIDE", readOnly: false, referenceReadOnly: true, propertyPath: '["m12_override_value"]', value, }); const request = (baseRevision = 3, value = 4.5) => ({ schemaVersion: 1, operation: "SET_M12_OVERRIDE_VALUE", baseRevision, localDataBlockId: "Object/M12 Override Object", referenceSourceDataBlockId: "Object/M12 Override Object", hierarchyRootDataBlockId: "Object/M12 Override Object", owner: "LOCAL_OVERRIDE", readOnly: false, referenceReadOnly: true, propertyPath: '["m12_override_value"]', value, }); test("M12-03K binds the single-property writer and evidence artifacts", () => { assert.equal(manifest.task, "M12-03K"); assert.equal(manifest.parentTask, "M12-03J"); assert.equal(manifest.nextTask, "M12-03L"); for (const artifact of Object.values(manifest.artifacts)) assert.equal(sha256(artifact.path), artifact.sha256, artifact.path); }); test("M12-03K applies exactly the verified override property and advances one revision", () => { const result = writer.applyOverrideWriter(state(), request()); assert.equal(result.status, "APPLIED"); assert.equal(result.code, null); assert.equal(result.state.revision, 4); assert.equal(result.state.value, 4.5); assert.equal(result.state.propertyPath, writer.LIBRARY_OVERRIDE_PROPERTY_PATH); assert.equal(result.state.owner, "LOCAL_OVERRIDE"); assert.equal(result.state.referenceReadOnly, true); }); test("M12-03K blocks stale, linked-owner, identity, and second-property writes", () => { assert.equal(writer.applyOverrideWriter(state(3), request(2)).code, "REVISION_CONFLICT"); assert.equal(writer.applyOverrideWriter(state(), { ...request(), owner: "SOURCE_LIBRARY", readOnly: true, referenceReadOnly: true }).code, "LINKED_DATA_MUTATION_BLOCKED"); assert.equal(writer.applyOverrideWriter(state(), { ...request(), localDataBlockId: "Object/Other" }).code, "ASSET_SOURCE_HASH_MISMATCH"); assert.equal(writer.applyOverrideWriter(state(), { ...request(), propertyPath: "location" }).code, "EDITOR_WRITER_UNAVAILABLE"); assert.equal(writer.applyOverrideWriter(state(), { ...request(), operation: "SET_LOCATION" }).code, "TASK_VALIDATION_FAILED"); }); test("M12-03K rejects malformed values and undeclared fields before the writer", () => { assert.equal(writer.applyOverrideWriter(state(), { ...request(), future: true }).code, "TASK_VALIDATION_FAILED"); assert.equal(writer.applyOverrideWriter(state(), { ...request(), value: Number.NaN }).code, "TASK_VALIDATION_FAILED"); assert.throws(() => writer.parseOverrideWriterState({ ...state(), propertyPath: "location" }), { code: "EDITOR_WRITER_UNAVAILABLE" }); assert.throws(() => writer.parseOverrideWriterRequest({ ...request(), value: 1e9 }), { code: "TASK_VALIDATION_FAILED" }); }); test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));