import { z } from "zod"; import type { CommandEffectDescriptor, InstalledContractContribution, InstalledEventContract, InstalledHttpContract, } from "../../src/contracts/external-contract-runtime.ts"; import { zodValidator } from "../../src/contracts/zod-validator.ts"; const entitySchema = z .object({ id: z.string().min(1), name: z.string().min(1), }) .strip(); const problemSchema = z .object({ type: z.string().min(1), title: z.string().min(1), status: z.int().min(100).max(599), code: z.string().min(1).optional(), }) .strip(); type TestProblem = z.output; const problemValidator = zodValidator("TestProblem", problemSchema); const readPolicy = Object.freeze({ policyId: "TEST_READ_V1", requestByteLimit: 0, responseByteLimit: 32_768, totalDeadlineMs: 10_000, retryBudget: 2 as const, authProfileId: "ANONYMOUS", diagnosticsOperation: "test.read", }); export const TEST_LIST_HTTP_CONTRACT: InstalledHttpContract< Readonly<{ limit: number }>, readonly z.output[], TestProblem > = Object.freeze({ contract: Object.freeze({ operationId: "TEST_LIST_ENTITIES", method: "GET" as const, pathTemplate: "/api/test-entities", inputValidator: zodValidator( "TestEntityListQuery", z .object({ limit: z.coerce.number().int().min(1).max(100).default(20), }) .strict(), ), outputValidator: zodValidator( "TestEntityListPayload", z.array(entitySchema).max(100), ), problemValidator, acceptedStatuses: Object.freeze([200]), emptyBodyStatuses: Object.freeze([]), retrySemantics: "SAFE" as const, requestBody: "NONE" as const, responseBody: "REQUIRED_JSON" as const, responseEnvelope: "NONE" as const, commandRecovery: null, commandEffect: null, projectRequest(input: Readonly<{ limit: number }>) { return Object.freeze({ pathValues: Object.freeze({}), queryEntries: Object.freeze([ Object.freeze(["limit", String(input.limit)] as const), ]), body: null, }); }, }), frontend: readPolicy, }); export const TEST_DETAIL_HTTP_CONTRACT: InstalledHttpContract< Readonly<{ entityId: string }>, z.output, TestProblem > = Object.freeze({ contract: Object.freeze({ operationId: "TEST_GET_ENTITY", method: "GET" as const, pathTemplate: "/api/test-entities/{entityId}", inputValidator: zodValidator( "TestEntityParams", z.object({ entityId: z.string().min(1) }).strict(), ), outputValidator: zodValidator("TestEntityPayload", entitySchema), problemValidator, acceptedStatuses: Object.freeze([200]), emptyBodyStatuses: Object.freeze([]), retrySemantics: "SAFE" as const, requestBody: "NONE" as const, responseBody: "REQUIRED_JSON" as const, responseEnvelope: "NONE" as const, commandRecovery: null, commandEffect: null, projectRequest(input: Readonly<{ entityId: string }>) { return Object.freeze({ pathValues: Object.freeze({ entityId: input.entityId }), queryEntries: Object.freeze([]), body: null, }); }, }), frontend: Object.freeze({ ...readPolicy, policyId: "TEST_DETAIL_V1", diagnosticsOperation: "test.detail", }), }); const commandEffect: CommandEffectDescriptor = Object.freeze({ successEffect: "APPLIED_CONFIRMED" as const, classifyProblem({ status, }: Readonly<{ status: number; problem: TestProblem }>) { return status === 400 ? "NOT_APPLIED" : "MAYBE_APPLIED"; }, }); export const TEST_CREATE_HTTP_CONTRACT: InstalledHttpContract< Readonly<{ name: string }>, z.output, TestProblem > = Object.freeze({ contract: Object.freeze({ operationId: "TEST_CREATE_ENTITY", method: "POST" as const, pathTemplate: "/api/test-entities", inputValidator: zodValidator( "TestCreateEntityCommand", z.object({ name: z.string().trim().min(1) }).strict(), ), outputValidator: zodValidator("TestEntityPayload", entitySchema), problemValidator, acceptedStatuses: Object.freeze([201]), emptyBodyStatuses: Object.freeze([]), retrySemantics: "KEYED" as const, requestBody: "JSON" as const, responseBody: "REQUIRED_JSON" as const, responseEnvelope: "NONE" as const, commandRecovery: Object.freeze({ mode: "IDEMPOTENCY_REPLAY" as const, operationIdentityField: "idempotencyKey", }), commandEffect, projectRequest(input: Readonly<{ name: string }>) { return Object.freeze({ pathValues: Object.freeze({}), queryEntries: Object.freeze([]), body: Object.freeze({ ...input }), }); }, }), frontend: Object.freeze({ policyId: "TEST_CREATE_V1", requestByteLimit: 32_768, responseByteLimit: 32_768, totalDeadlineMs: 10_000, retryBudget: 0 as const, authProfileId: "ANONYMOUS", diagnosticsOperation: "test.create", }), }); /** * The create command as a package that classifies its effects describes it, * the way the HTTP scenario catalog (`tests/mocks/scenarios/catalog.ts`) * expects (testing strategy ยง11.2): 401, 403 and 429 are rejected before any * state changes, 400, 422 and the declared `409 CONFLICT` are NOT_APPLIED, and * any other problem (`409 IDEMPOTENT_IN_FLIGHT`, 404, 503) stays MAYBE_APPLIED. * TEST_CREATE_HTTP_CONTRACT keeps the bare classifier the executor tests * extend. */ export const TEST_CLASSIFIED_CREATE_HTTP_CONTRACT: typeof TEST_CREATE_HTTP_CONTRACT = Object.freeze({ ...TEST_CREATE_HTTP_CONTRACT, contract: Object.freeze({ ...TEST_CREATE_HTTP_CONTRACT.contract, commandEffect: Object.freeze({ successEffect: "APPLIED_CONFIRMED" as const, classifyProblem({ status, problem, }: Readonly<{ status: number; problem: TestProblem }>) { if (status === 400 || status === 422) return "NOT_APPLIED"; if (status === 409 && problem.code === "CONFLICT") return "NOT_APPLIED"; return "MAYBE_APPLIED"; }, classifyStatus: () => "NOT_APPLIED" as const, }), }), }); /** * 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", featureId: "test-feature", source: Object.freeze({ kind: "EXTERNAL_PACKAGE" as const, package: Object.freeze({ packageId: "@test/contracts", version: "1.0.0", digest: `sha256:${"a".repeat(64)}`, runtimeProtocolVersion: 1 as const, sourceRevision: "abcdef1", }), }), http: Object.freeze([ TEST_LIST_HTTP_CONTRACT, TEST_DETAIL_HTTP_CONTRACT, TEST_CREATE_HTTP_CONTRACT, ]) as readonly InstalledHttpContract[], events: Object.freeze([]), });