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-freshness-unit-")); const sourcePath = path.join(root, "web/protocol/library-override-freshness.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-freshness.mjs"), transpiled.outputText); const freshness = await import(pathToFileURL(path.join(temporary, "library-override-freshness.mjs"))); const manifest = JSON.parse(fs.readFileSync(path.join(root, "tests/golden/M12-03L/manifest.json"), "utf8")); const sha256 = (file) => crypto.createHash("sha256").update(fs.readFileSync(path.join(root, file))).digest("hex"); const library = "library:" + "a".repeat(64); const digest = "b".repeat(64); const token = "override-token:" + "c".repeat(64); const state = (revision = 7) => ({ schemaVersion: 1, sourceLibraryId: library, sourceGeneration: 4, sourceRevision: revision, dependencyClosureSha256: digest, invalidationToken: token, localDataBlockId: "Object/M12 Override Object", referenceSourceDataBlockId: "Object/M12 Override Object", hierarchyRootDataBlockId: "Object/M12 Override Object", owner: "LOCAL_OVERRIDE", readOnly: false, referenceReadOnly: true, }); const request = (overrides = {}) => ({ schemaVersion: 1, operation: "COMMIT_OVERRIDE", sourceLibraryId: library, sourceGeneration: 4, sourceRevision: 7, dependencyClosureSha256: digest, invalidationToken: token, baseRevision: 7, localDataBlockId: "Object/M12 Override Object", referenceSourceDataBlockId: "Object/M12 Override Object", hierarchyRootDataBlockId: "Object/M12 Override Object", owner: "LOCAL_OVERRIDE", readOnly: false, referenceReadOnly: true, ...overrides, }); test("M12-03L binds the override freshness gate and evidence artifacts", () => { assert.equal(manifest.task, "M12-03L"); assert.equal(manifest.parentTask, "M12-03K"); assert.equal(manifest.nextTask, "M12-03M"); for (const artifact of Object.values(manifest.artifacts)) assert.equal(sha256(artifact.path), artifact.sha256, artifact.path); }); test("M12-03L allows only a fully matching source generation/revision commit", () => { assert.deepEqual(freshness.gateOverrideFreshness(state(), request()), { status: "READY", code: null }); }); test("M12-03L blocks stale generation, revision, closure, token, and identity before Main", () => { for (const overrides of [ { sourceGeneration: 3 }, { sourceRevision: 6, baseRevision: 6 }, { dependencyClosureSha256: "d".repeat(64) }, { invalidationToken: "override-token:" + "e".repeat(64) }, ]) assert.deepEqual(freshness.gateOverrideFreshness(state(), request(overrides)), { status: "BLOCKED", code: "REVISION_CONFLICT" }); assert.deepEqual(freshness.gateOverrideFreshness(state(), request({ localDataBlockId: "Object/Other" })), { status: "BLOCKED", code: "ASSET_SOURCE_HASH_MISMATCH" }); }); test("M12-03L rejects linked ownership, alternate operations, malformed tokens, and extra fields", () => { assert.equal(freshness.gateOverrideFreshness(state(), request({ owner: "SOURCE_LIBRARY", readOnly: true })).code, "LINKED_DATA_MUTATION_BLOCKED"); assert.equal(freshness.gateOverrideFreshness(state(), request({ operation: "SET_LOCATION" })).code, "TASK_VALIDATION_FAILED"); assert.equal(freshness.gateOverrideFreshness(state(), request({ invalidationToken: "bad" })).code, "TASK_VALIDATION_FAILED"); assert.equal(freshness.gateOverrideFreshness(state(), { ...request(), future: true }).code, "TASK_VALIDATION_FAILED"); }); test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));