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