Files
clean-architecture-frontend…/tests/unit/contract-release-checks.test.ts
T
DongHyeonkaandClaude Opus 5.5 a8c5eabcd9 fix(contracts): stop quiet passes in release checks and guard contracts imports (Q-6.7-12)
- release-tokens.ts: compareReleaseToRuntime filled the runtime-side
  assetManifestHash with the release's own value, so the field always
  compared equal and read as a pass. The result now also carries
  `unverified: ["assetManifestHash"]` (additive field; compatible and
  mismatches are unchanged).
- contract-set.ts: a duplicated package in `expected` without
  expectedSetDigest made the digest helper throw, so verifyContractSet
  rejected. It now returns CONTRACT_SET_DUPLICATE_PACKAGE, and any other
  throw from the expected-digest computation returns
  CONTRACT_SET_DIGEST_INVALID.
- query-keys.ts: the identity registry copied and sorted every row on
  each new intern; it now does so only when an eviction is needed.
- .dependency-cruiser.json: new rule
  contracts-do-not-know-presentation-or-bootstrap. No current
  violations; a temporary src/contracts probe importing presentation
  and bootstrap was reported by check:architecture and then removed.

Verified: tests/unit/contract-release-checks.test.ts (release-tokens
and contract-set cases RED before; eviction-order and matching-set
cases are characterization and passed before and after); gates green
except the known removal-fixture test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 13:45:37 +09:00

84 lines
2.9 KiB
TypeScript

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);
});
});