112 lines
6.1 KiB
JavaScript
112 lines
6.1 KiB
JavaScript
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-operation-identity-unit-"));
|
|
const sourcePath = path.join(root, "web/protocol/library-operation-identity.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-operation-identity.mjs"), transpiled.outputText);
|
|
const identity = await import(pathToFileURL(path.join(temporary, "library-operation-identity.mjs")));
|
|
const golden = JSON.parse(fs.readFileSync(path.join(root, "tests/golden/M12-03B/library-operation-bindings.json"), "utf8"));
|
|
const manifest = JSON.parse(fs.readFileSync(path.join(root, "tests/golden/M12-03B/manifest.json"), "utf8"));
|
|
const sha256 = (value) => crypto.createHash("sha256").update(value).digest("hex");
|
|
|
|
const bindingInput = (binding) => {
|
|
const { invalidationToken, ...input } = structuredClone(binding);
|
|
return input;
|
|
};
|
|
const stateFor = (binding) => ({
|
|
sourceLibraryId: binding.source.sourceLibraryId,
|
|
sourceSha256: binding.source.sourceSha256,
|
|
sourceGeneration: binding.sourceGeneration,
|
|
sourceRevision: binding.sourceRevision,
|
|
dependencyClosureSha256: binding.dependencyClosureSha256,
|
|
});
|
|
|
|
test("M12-03B binds the inventory, protocol, golden, and unit artifacts", () => {
|
|
assert.equal(manifest.task, "M12-03B");
|
|
assert.equal(manifest.enablingTask, true);
|
|
assert.equal(manifest.parityStateChange, false);
|
|
assert.equal(manifest.nextTask, "M12-03C");
|
|
for (const artifact of Object.values(manifest.artifacts)) {
|
|
assert.equal(sha256(fs.readFileSync(path.join(root, artifact.path))), artifact.sha256, artifact.path);
|
|
}
|
|
});
|
|
|
|
test("M12-03B derives a stable source library ID from locator and bytes", async () => {
|
|
const source = await identity.createLibrarySourceIdentity({
|
|
sourceLocator: golden.source.sourceLocator,
|
|
sourceSha256: golden.source.sourceSha256,
|
|
});
|
|
assert.deepEqual(source, golden.source);
|
|
assert.deepEqual(await identity.parseLibrarySourceIdentity(source), source);
|
|
assert.equal(source.sourceLibraryId, await identity.computeLibrarySourceId(source));
|
|
assert.notEqual(source.sourceLibraryId, (await identity.createLibrarySourceIdentity({
|
|
sourceLocator: `${source.sourceLocator}.moved`,
|
|
sourceSha256: source.sourceSha256,
|
|
})).sourceLibraryId);
|
|
});
|
|
|
|
test("M12-03B fixes operation-specific owner and read-only semantics", async () => {
|
|
const expected = [
|
|
["APPEND", "LOCAL_MAIN", false, false],
|
|
["LINK", "SOURCE_LIBRARY", true, true],
|
|
["LIBRARY_OVERRIDE", "LOCAL_OVERRIDE", false, true],
|
|
];
|
|
for (let index = 0; index < golden.bindings.length; index++) {
|
|
const binding = golden.bindings[index];
|
|
assert.deepEqual(await identity.createLibraryOperationBinding(bindingInput(binding)), binding);
|
|
assert.deepEqual(await identity.parseLibraryOperationBinding(binding), binding);
|
|
assert.deepEqual(await identity.assertLibraryOperationBindingCurrent(binding, stateFor(binding)), binding);
|
|
assert.deepEqual([binding.operation, binding.owner.kind, binding.readOnly, binding.referenceReadOnly], expected[index]);
|
|
}
|
|
assert.equal(new Set(golden.bindings.map((binding) => binding.invalidationToken)).size, 3);
|
|
});
|
|
|
|
test("M12-03B rejects owner, read-only, source, and token substitution", async () => {
|
|
const [append, link, override] = golden.bindings;
|
|
await assert.rejects(identity.createLibraryOperationBinding({ ...bindingInput(append), readOnly: true }), { code: "ASSET_MANIFEST_INVALID" });
|
|
await assert.rejects(identity.createLibraryOperationBinding({ ...bindingInput(link), owner: append.owner }), { code: "ASSET_MANIFEST_INVALID" });
|
|
await assert.rejects(identity.createLibraryOperationBinding({ ...bindingInput(override), referenceReadOnly: false }), { code: "ASSET_MANIFEST_INVALID" });
|
|
await assert.rejects(identity.createLibraryOperationBinding({
|
|
...bindingInput(override),
|
|
owner: { ...override.owner, referenceSourceDataBlockId: "Object/Other" },
|
|
}), { code: "ASSET_SOURCE_HASH_MISMATCH" });
|
|
await assert.rejects(identity.parseLibrarySourceIdentity({ ...golden.source, sourceSha256: "f".repeat(64) }), { code: "ASSET_SOURCE_HASH_MISMATCH" });
|
|
await assert.rejects(identity.parseLibraryOperationBinding({ ...append, sourceRevision: 8 }), { code: "REVISION_CONFLICT" });
|
|
await assert.rejects(identity.parseLibraryOperationBinding({ ...link, owner: { ...link.owner, sourceDataBlockId: "Object/Other" } }), { code: "ASSET_SOURCE_HASH_MISMATCH" });
|
|
await assert.rejects(identity.parseLibraryOperationBinding({ ...override, future: true }), { code: "ASSET_MANIFEST_INVALID" });
|
|
});
|
|
|
|
test("M12-03B invalidates generation, revision, source, and dependency closure drift", async () => {
|
|
const binding = golden.bindings[2];
|
|
for (const drift of [
|
|
{ sourceGeneration: 4 },
|
|
{ sourceRevision: 8 },
|
|
{ sourceSha256: "c".repeat(64) },
|
|
{ dependencyClosureSha256: "d".repeat(64) },
|
|
{ sourceLibraryId: `library:${"e".repeat(64)}` },
|
|
]) {
|
|
await assert.rejects(identity.assertLibraryOperationBindingCurrent(binding, { ...stateFor(binding), ...drift }), { code: "REVISION_CONFLICT" });
|
|
}
|
|
});
|
|
|
|
test("M12-03B enforces exact schemas and UTF-8 budgets", async () => {
|
|
await assert.rejects(identity.parseLibrarySourceIdentity({ schemaVersion: 2 }), { code: "PROTOCOL_MISMATCH" });
|
|
await assert.rejects(identity.createLibrarySourceIdentity({ sourceLocator: "x".repeat(4_097), sourceSha256: "a".repeat(64) }), { code: "ASSET_MANIFEST_INVALID" });
|
|
await assert.rejects(identity.createLibraryOperationBinding({ ...bindingInput(golden.bindings[0]), sourceGeneration: 0 }), { code: "ASSET_MANIFEST_INVALID" });
|
|
});
|
|
|
|
test.after(() => fs.rmSync(temporary, { recursive: true, force: true }));
|