Jobs of fixed types with strict argument schemas, signed by the Hub with Ed25519 over canonical JSON. Agent requests signed with the server's Ed25519 key. Config sealed to the server's X25519 key (X25519 + HKDF + AES-256-GCM; Node has no XSalsa20). Exports encrypted with a key that only travels in the download link. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
84 lines
3.4 KiB
TypeScript
84 lines
3.4 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",
|
|
};
|
|
|
|
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);
|
|
});
|
|
});
|