diff --git a/tests/helpers/external-contract-fixture.ts b/tests/helpers/external-contract-fixture.ts index b483e0b4..9c8ec981 100644 --- a/tests/helpers/external-contract-fixture.ts +++ b/tests/helpers/external-contract-fixture.ts @@ -3,6 +3,7 @@ import { z } from "zod"; import type { CommandEffectDescriptor, InstalledContractContribution, + InstalledEventContract, InstalledHttpContract, RuntimeValidator, } from "../../src/contracts/external-contract-runtime.ts"; @@ -197,6 +198,23 @@ export const TEST_CREATE_HTTP_CONTRACT: InstalledHttpContract< }), }); +/** + * An event contract an external package could install. The shared + * contribution below keeps `events` empty, so tests that check event + * composition put this row into a copy of it. + */ +export const TEST_EVENT_CONTRACT: InstalledEventContract< + Readonly<{ type: string; payload: unknown }>, + z.output +> = Object.freeze({ + eventType: "test.entity.changed", + envelopeValidator: zodValidator( + "TestEventEnvelope", + z.object({ type: z.string().min(1), payload: z.unknown() }).strip(), + ), + payloadValidator: zodValidator("TestEntity", entitySchema), +}); + export const TEST_CONTRACT_CONTRIBUTION: InstalledContractContribution = Object.freeze({ contributionId: "test-http-v1", diff --git a/tests/unit/contract-registry-immutability.test.ts b/tests/unit/contract-registry-immutability.test.ts index f62746c0..a248b2b9 100644 --- a/tests/unit/contract-registry-immutability.test.ts +++ b/tests/unit/contract-registry-immutability.test.ts @@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest"; import { composeContractContributions, + ContractContributionError, type ComposedContractContributions, } from "../../src/contracts/external-contract-runtime.ts"; import { @@ -9,7 +10,10 @@ import { INSTALLED_REST_AUTH_PROFILES, REST_AUTH_PROFILES, } from "../../src/contracts/rest-profiles.ts"; -import { TEST_CONTRACT_CONTRIBUTION } from "../helpers/external-contract-fixture.ts"; +import { + TEST_CONTRACT_CONTRIBUTION, + TEST_EVENT_CONTRACT, +} from "../helpers/external-contract-fixture.ts"; /** * LIVE-02 / LIVE-03. `Object.freeze(new Map(...))` freezes the wrapper object, @@ -318,20 +322,95 @@ describe("LIVE-03 composed contract registry", () => { ).toThrow(); } }); + }); - it("refuses an event contract whose own shape is not exact", () => { - const events = TEST_CONTRACT_CONTRIBUTION.events; - if (events.length === 0) return; - for (const event of [ - { ...events[0]!, injected: true }, - Object.assign(Object.create({ injected: true }), { ...events[0]! }), - ]) { - expect(() => - composeContractContributions([ - { ...TEST_CONTRACT_CONTRIBUTION, events: [event] }, - ] as never), - ).toThrow(); - } + describe("event contracts", () => { + // 10-01. The shared contribution has no events, so every case here puts + // TEST_EVENT_CONTRACT (or a broken copy of it) into a copy of it. + const withEvents = (...events: unknown[]) => + [{ ...TEST_CONTRACT_CONTRIBUTION, events }] as never; + + it("installs a well-formed event contract as its own snapshot", () => { + const composed = composeContractContributions( + withEvents(TEST_EVENT_CONTRACT), + ); + const installed = composed.eventByType.get(TEST_EVENT_CONTRACT.eventType); + expect(installed).toEqual(TEST_EVENT_CONTRACT); + expect(installed).not.toBe(TEST_EVENT_CONTRACT); + expect(Object.isFrozen(installed)).toBe(true); + expect(composed.contributions[0]!.events).toEqual([installed]); + }); + + const hostileEvents = [ + { + label: "an extra own field", + event: () => ({ ...TEST_EVENT_CONTRACT, injected: true }), + }, + { + label: "a custom prototype", + event: () => + Object.assign(Object.create({ injected: true }), { + ...TEST_EVENT_CONTRACT, + }), + }, + { + label: "an accessor eventType", + event: () => + Object.defineProperty({ ...TEST_EVENT_CONTRACT }, "eventType", { + enumerable: true, + get: () => TEST_EVENT_CONTRACT.eventType, + }), + }, + { + label: "an empty eventType", + event: () => ({ ...TEST_EVENT_CONTRACT, eventType: "" }), + }, + { + label: "an eventType with a space", + event: () => ({ ...TEST_EVENT_CONTRACT, eventType: "test entity" }), + }, + { + label: "an eventType with a control character", + event: () => ({ + ...TEST_EVENT_CONTRACT, + eventType: "test\u0085entity", + }), + }, + { + label: "an eventType over 160 UTF-8 bytes", + // 54 three-byte characters = 162 bytes in 54 UTF-16 units. + event: () => ({ ...TEST_EVENT_CONTRACT, eventType: "가".repeat(54) }), + }, + { + label: "a missing envelope validator", + event: () => ({ ...TEST_EVENT_CONTRACT, envelopeValidator: null }), + }, + { + label: "a payload validator without a schemaId", + event: () => ({ + ...TEST_EVENT_CONTRACT, + payloadValidator: { + ...TEST_EVENT_CONTRACT.payloadValidator, + schemaId: "", + }, + }), + }, + ]; + + for (const { label, event } of hostileEvents) { + it(`refuses an event contract with ${label}`, () => { + expect(() => composeContractContributions(withEvents(event()))).toThrow( + ContractContributionError, + ); + }); + } + + it("refuses two event contracts with the same eventType", () => { + expect(() => + composeContractContributions( + withEvents(TEST_EVENT_CONTRACT, { ...TEST_EVENT_CONTRACT }), + ), + ).toThrow(ContractContributionError); }); }); });