Files
crosscode-platform/packages/contracts/tests/samples.test.ts
T

66 lines
2.3 KiB
TypeScript

import { readdirSync } from "node:fs";
import type { z } from "zod";
import {
accountsCallSchema,
churchExportSchema,
churchSchema,
eventChangedSchema,
eventFeedSchema,
eventSchema,
hub,
churchChangedSchema,
linkConnectSchema,
linkConnectedSchema,
linkDisconnectSchema,
identityClaimsSchema,
userDeletedReplySchema,
userExportReplySchema,
} from "../src";
import { SAMPLES, sample } from "./helpers";
/** Every sample file and the schema it must pass. A new sample without an entry fails the test. */
const SCHEMAS: Record<string, z.ZodType> = {
"event.json": eventSchema,
"event-simple.json": eventSchema,
"event-feed.json": eventFeedSchema,
"event-changed.json": eventChangedSchema,
"church.json": churchSchema,
"identity-claims.json": identityClaimsSchema,
"user-updated.json": accountsCallSchema,
"user-deleted.json": accountsCallSchema,
"user-deleted-reply.json": userDeletedReplySchema,
"user-export.json": accountsCallSchema,
"user-export-reply.json": userExportReplySchema,
"hub-register-request.json": hub.registerRequestSchema,
"hub-register-response.json": hub.registerResponseSchema,
"hub-heartbeat-events.json": hub.heartbeatRequestSchema,
"hub-heartbeat-website.json": hub.heartbeatRequestSchema,
"hub-heartbeat-app.json": hub.heartbeatRequestSchema,
"hub-heartbeat-accounts.json": hub.heartbeatRequestSchema,
"hub-heartbeat-response.json": hub.heartbeatResponseSchema,
"hub-licence-payload.json": hub.licencePayloadSchema,
"church-export.json": churchExportSchema,
"church-changed.json": churchChangedSchema,
"link-connect.json": linkConnectSchema,
"link-connected.json": linkConnectedSchema,
"link-disconnect.json": linkDisconnectSchema,
};
describe("samples", () => {
const files = readdirSync(SAMPLES).filter((f) => f.endsWith(".json"));
it("every sample has a schema", () => {
expect(files.filter((f) => !(f in SCHEMAS))).toEqual([]);
});
it.each(Object.entries(SCHEMAS))("%s is valid", (file, schema) => {
const result = schema.safeParse(sample(file));
expect(result.success ? [] : result.error.issues).toEqual([]);
});
it.each(Object.entries(SCHEMAS))("%s keeps every field it sends (no field is silently dropped)", (file, schema) => {
const input = sample(file);
expect(schema.parse(input)).toEqual(input);
});
});