import { describe, expect, it } from "vitest"; import { CONTRACT_SET_ALGORITHM, computeContractSetDigest, } from "../../src/contracts/contract-set-canonical.ts"; import { verifyContractSet } from "../../src/contracts/contract-set.ts"; import { createRuntimeIdentityRegistry } from "../../src/contracts/query-keys.ts"; import { compareReleaseToRuntime } from "../../src/contracts/release-tokens.ts"; /** Q-6.7-12. Quiet passes and small defects in the release contracts. */ describe("release to runtime comparison", () => { it("reports the asset manifest hash as unverified, not as compared", () => { const result = compareReleaseToRuntime( { buildId: "build-a", configSchemaVersion: "1.0", apiContractVersion: "1.0", assetManifestHash: "assets-a", releaseId: "release-a", }, { BUILD_ID: "build-a", CONFIG_SCHEMA_VERSION: "1.0", API_CONTRACT_VERSION: "1.0", RELEASE_ID: "release-a", }, ); expect(result).toMatchObject({ compatible: true, mismatches: [] }); expect(result.unverified).toEqual(["assetManifestHash"]); }); }); describe("contract set verification", () => { const accounts = Object.freeze({ packageId: "@example/accounts", version: "1.2.3", digest: `sha256:${"1".repeat(64)}` as const, runtimeProtocolVersion: 1 as const, sourceRevision: "a".repeat(40), }); it("returns a failure code for a duplicated expected package", async () => { const manifest = { setAlgorithm: CONTRACT_SET_ALGORITHM, setDigest: await computeContractSetDigest([accounts]), packages: [accounts], }; await expect( verifyContractSet({ expected: [accounts, accounts], manifest }), ).resolves.toEqual({ ok: false, code: "CONTRACT_SET_DUPLICATE_PACKAGE" }); }); it("still admits a matching set", async () => { const manifest = { setAlgorithm: CONTRACT_SET_ALGORITHM, setDigest: await computeContractSetDigest([accounts]), packages: [accounts], }; await expect( verifyContractSet({ expected: [accounts], manifest }), ).resolves.toEqual({ ok: true }); }); }); describe("runtime identity registry eviction", () => { it("evicts the least recently touched released row first", () => { let sequence = 0; const registry = createRuntimeIdentityRegistry({ maxEntries: 2, tokenFactory: () => `identity-token-${String(sequence++).padStart(4, "0")}`, }); const first = registry.intern({ resource: "first" }).token; const second = registry.intern({ resource: "second" }).token; // Touch "first" again so "second" becomes the eviction candidate. expect(registry.intern({ resource: "first" }).token).toBe(first); registry.intern({ resource: "third" }); expect(registry.inspect().entries).toBe(2); expect(registry.intern({ resource: "first" }).token).toBe(first); expect(registry.intern({ resource: "second" }).token).not.toBe(second); }); });