Files
crosscode-platform/packages/contracts/tests/json-schemas.test.ts
T
joshuaandClaude Opus 5.5 4952e2cb0f Contracts 1.4.0: crosscode.link/1 and a public key in the v2 heartbeat
- crosscode.link/1 (link.connect / link.connected / link.disconnect), with
  signLink / verifyLink, PEM conversion, link secrets and nonces.
- The v2 heartbeat can carry the install's publicKey; the reply says
  saved / matches / mismatch (Hub 2.2.0). Never a failed heartbeat.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 11:58:26 +02:00

87 lines
3.5 KiB
TypeScript

import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import Ajv2020 from "ajv/dist/2020";
import addFormats from "ajv-formats";
import * as contracts from "../src";
import { SAMPLES, sample } from "./helpers";
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { buildSchemas, serialise } = require("../scripts/json-schemas.cjs") as {
buildSchemas: (c: typeof contracts) => Record<string, object>;
serialise: (json: object) => string;
};
const SCHEMAS_DIR = join(__dirname, "..", "schemas");
const schemas = buildSchemas(contracts);
/** Every sample file and the JSON Schema (file name without .json) it must pass. */
const SAMPLE_SCHEMA: Record<string, string> = {
"event.json": "event",
"event-simple.json": "event",
"event-feed.json": "event-feed",
"event-changed.json": "event-changed",
"church.json": "church",
"identity-claims.json": "identity-claims",
"user-updated.json": "user-updated",
"user-deleted.json": "user-deleted",
"user-deleted-reply.json": "user-deleted-reply",
"user-export.json": "user-export",
"user-export-reply.json": "user-export-reply",
"hub-register-request.json": "hub-register-request",
"hub-register-response.json": "hub-register-response",
"hub-heartbeat-events.json": "hub-heartbeat-request",
"hub-heartbeat-website.json": "hub-heartbeat-request",
"hub-heartbeat-app.json": "hub-heartbeat-request",
"hub-heartbeat-accounts.json": "hub-heartbeat-request",
"hub-heartbeat-response.json": "hub-heartbeat-response",
"hub-licence-payload.json": "hub-licence-payload",
"church-export.json": "church-export",
"link-connect.json": "link-connect",
"link-connected.json": "link-connected",
"link-disconnect.json": "link-disconnect",
};
describe("JSON Schema (schemas/)", () => {
it("the files in schemas/ match the zod schemas (run npm run build after changing a contract)", () => {
const files = readdirSync(SCHEMAS_DIR).filter((f) => f.endsWith(".json")).sort();
expect(files).toEqual(Object.keys(schemas).map((n) => `${n}.json`).sort());
for (const [name, json] of Object.entries(schemas)) {
expect(readFileSync(join(SCHEMAS_DIR, `${name}.json`), "utf8").replace(/\r\n/g, "\n")).toBe(serialise(json));
}
});
const ajv = new Ajv2020({ allErrors: true, strict: true });
addFormats(ajv);
for (const json of Object.values(schemas)) ajv.addSchema(json);
const validatorFor = (name: string) => {
const validate = ajv.getSchema((schemas[name] as { $id: string }).$id);
if (!validate) throw new Error(`no JSON Schema "${name}"`);
return validate;
};
it("every JSON Schema compiles", () => {
for (const name of Object.keys(schemas)) expect(() => validatorFor(name)).not.toThrow();
});
it("every sample has a JSON Schema", () => {
const files = readdirSync(SAMPLES).filter((f) => f.endsWith(".json"));
expect(files.filter((f) => !(f in SAMPLE_SCHEMA))).toEqual([]);
});
it.each(Object.entries(SAMPLE_SCHEMA))("%s passes the JSON Schema %s", (file, name) => {
const validate = validatorFor(name);
validate(sample(file));
expect(validate.errors ?? []).toEqual([]);
});
it("refuses a payload with a wrong schema name", () => {
const validate = validatorFor("event");
expect(validate({ ...(sample("event.json") as object), schema: "crosscode.event/2" })).toBe(false);
});
it("allows fields it doesn't know (consumers drop them, never refuse them)", () => {
const validate = validatorFor("event");
expect(validate({ ...(sample("event.json") as object), somethingNew: true })).toBe(true);
});
});