Files
clean-architecture-frontend…/tests/unit/contract-registry-lookup.test.ts
T
DongHyeonkaandClaude Opus 5.5 3cc5039686 fix(contracts): look registries up by own key only (Q-6.7-11)
`registry[id]` walks the prototype chain: getApiOperation("constructor")
returned the Object function, getStorageDefinition("__proto__")
returned Object.prototype, validateWithRuntimeSchemaRegistry reported
an inherited id as SCHEMA_EXECUTION_FAILED instead of
SCHEMA_NOT_REGISTERED, and isAllowedOfflineTransition threw a TypeError
for a state that is not listed.

getApiOperation, getStorageDefinition, validateWithRuntimeSchemaRegistry
and mapWithBoundaryRegistry now check Object.hasOwn before reading, and
isAllowedOfflineTransition returns false for an unlisted state.

Verified: new tests/unit/contract-registry-lookup.test.ts (4 of 6 RED
before; the boundary-mapper case already failed closed and is kept as
characterization); 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

68 lines
2.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { getApiOperation } from "../../src/contracts/api-operations.ts";
import { mapWithBoundaryRegistry } from "../../src/contracts/boundary-mapper.ts";
import {
isAllowedOfflineTransition,
type OfflineCommandState,
} from "../../src/contracts/offline-command.ts";
import { validateWithRuntimeSchemaRegistry } from "../../src/contracts/schema-registry.ts";
import { getStorageDefinition } from "../../src/contracts/storage-keys.ts";
/**
* Q-6.7-11. A registry lookup answers only for its own keys. `registry[id]`
* walks the prototype chain, so `constructor` or `__proto__` resolved to
* `Object` / `Object.prototype` instead of "not registered".
*/
const INHERITED = ["constructor", "__proto__", "toString", "hasOwnProperty"];
describe("registry lookups ignore inherited keys", () => {
it("getApiOperation", () => {
for (const id of INHERITED) {
expect(() => getApiOperation(id), id).toThrow("Unregistered API operation");
}
});
it("getStorageDefinition", () => {
for (const id of INHERITED) {
expect(() => getStorageDefinition(id), id).toThrow(
"Unregistered storage key",
);
}
});
it("validateWithRuntimeSchemaRegistry", () => {
for (const id of INHERITED) {
expect(validateWithRuntimeSchemaRegistry(id, {}, {}), id).toEqual({
success: false,
issues: [{ path: "", code: "SCHEMA_NOT_REGISTERED" }],
});
}
});
it("mapWithBoundaryRegistry", () => {
for (const id of INHERITED) {
expect(mapWithBoundaryRegistry(id, {}, {}), id).toEqual({
ok: false,
code: "MAPPING_INVARIANT_REJECTED",
});
}
});
});
describe("offline command transitions", () => {
it("answers false for a state that is not listed", () => {
for (const from of ["UNKNOWN", ...INHERITED]) {
expect(
isAllowedOfflineTransition(from as OfflineCommandState, "PENDING"),
from,
).toBe(false);
}
});
it("keeps the listed transitions", () => {
expect(isAllowedOfflineTransition("PENDING", "LEASED")).toBe(true);
expect(isAllowedOfflineTransition("ACKED", "PENDING")).toBe(false);
});
});