Files
crosscode-platform/packages/contracts/tests/event.test.ts
joshuaandClaude Opus 5.5 ea1575f716 Contracts: event, event feed, church, identity, sign-in return, Hub protocol v2
- crosscode.event/1 and crosscode.event-feed/1 (feed, event.changed
  webhook), with session and registration rules; parseEventFeed skips
  and reports bad events instead of failing the whole feed.
- crosscode.church/1: branding, campuses, service times, contact, links.
- crosscode.identity/1: sign-in claims and the user.updated /
  user.deleted / user.export back-channel calls and replies.
- Sign-in return helpers (isSafeReturnPath, encodeState/decodeState,
  isRegisteredRedirectUri) so every product returns people to the page
  they came from in the same safe way.
- Hub protocol v2: product on register/heartbeat, stats per product,
  install public key, linked products, customerId/product in licences.
- @crosscode/contracts/signing (Node only): the Hub's HMAC scheme.
- Samples for every contract in samples/, checked by the tests (120).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 09:47:40 +02:00

103 lines
4.7 KiB
TypeScript

import { eventKey, eventSchema, parseEventFeed } from "../src";
import type { CrosscodeEvent } from "../src";
import { sample } from "./helpers";
const base = () => sample<CrosscodeEvent>("event.json");
const simple = () => sample<CrosscodeEvent>("event-simple.json");
const messages = (input: unknown) => {
const r = eventSchema.safeParse(input);
return r.success ? [] : r.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`);
};
describe("crosscode.event/1", () => {
it("ignores fields it doesn't know (a newer producer never breaks an older consumer)", () => {
const parsed = eventSchema.parse({ ...base(), someFutureField: { a: 1 } });
expect(parsed).not.toHaveProperty("someFutureField");
});
it("refuses another schema or version", () => {
expect(messages({ ...base(), schema: "crosscode.event/2" })).not.toEqual([]);
expect(messages({ ...base(), schema: "crosscode.church/1" })).not.toEqual([]);
});
it("refuses an unknown origin product", () => {
const e = base();
expect(messages({ ...e, origin: { ...e.origin, product: "accounts" } })).not.toEqual([]);
});
it("checks startsAt / endsAt against the sessions", () => {
expect(messages({ ...base(), startsAt: "2027-03-14T07:00:00.000Z" })).toContain("startsAt: startsAt must be the first session's start");
expect(messages({ ...base(), endsAt: "2027-03-16T13:00:00.000Z" })).toContain("endsAt: endsAt must be the last session's end");
});
it("leaves cancelled sessions out of the first-start / last-end check", () => {
const e = base();
e.sessions[2]!.cancelled = true;
expect(messages(e)).toContain("endsAt: endsAt must be the last session's end");
e.endsAt = e.sessions[1]!.endsAt;
expect(messages(e)).toEqual([]);
});
it("refuses an event or session that ends before it starts", () => {
const e = simple();
expect(messages({ ...e, endsAt: "2026-12-20T15:00:00.000Z" })).toContain("endsAt: endsAt is before startsAt");
const b = base();
b.sessions[0]!.endsAt = "2027-03-14T05:00:00.000Z";
expect(messages(b)).toContain("sessions.0.endsAt: Session ends before it starts");
});
it("only lets Cross Events take registrations", () => {
const e = simple();
expect(messages({ ...e, registration: { ...e.registration, mode: "origin", url: e.origin.url } })).toContain(
'registration.mode: Only Cross Events takes registrations (mode "origin")',
);
expect(messages({ ...e, registration: { ...e.registration, status: "full" } })).toContain(
'registration.status: Only Cross Events sends a registration status other than "open"',
);
});
it("needs a url for origin / external registration and none otherwise", () => {
const e = base();
expect(messages({ ...e, registration: { ...e.registration, url: null } })).toContain('registration.url: A "origin" registration needs a url');
const s = simple();
expect(messages({ ...s, registration: { ...s.registration, url: "https://example.com" } })).toContain('registration.url: A "contact" registration has no url');
expect(messages({ ...s, registration: { ...s.registration, mode: "external", url: "https://forms.example.com/carols" } })).toEqual([]);
});
it("refuses a negative or fractional price, and a currency that isn't ISO 4217", () => {
const e = base();
for (const priceFrom of [{ amountCents: -1, currency: "ZAR" }, { amountCents: 1.5, currency: "ZAR" }, { amountCents: 100, currency: "rand" }]) {
expect(messages({ ...e, registration: { ...e.registration, priceFrom } })).not.toEqual([]);
}
});
it("refuses non-UTC-ISO times and non-http URLs", () => {
expect(messages({ ...base(), updatedAt: "3 November 2026" })).not.toEqual([]);
expect(messages({ ...simple(), imageUrl: "javascript:alert(1)" })).not.toEqual([]);
expect(messages({ ...simple(), imageUrl: "ftp://example.com/a.jpg" })).not.toEqual([]);
});
it("identifies an event by origin product and id", () => {
expect(eventKey(base())).toBe("events:8f1c2a4e-5b6d-4c7e-9f80-a1b2c3d4e5f6");
});
});
describe("parseEventFeed", () => {
it("skips a bad event and reports it, keeping the rest", () => {
const feed = sample<{ events: unknown[] }>("event-feed.json");
feed.events.push({ ...simple(), title: "" }, "not an event", simple());
const result = parseEventFeed(feed);
if (!result.ok) throw result.error;
expect(result.feed.events).toHaveLength(2);
expect(result.rejected.map((r) => [r.index, r.id])).toEqual([
[1, simple().id],
[2, null],
]);
});
it("fails when the envelope itself is wrong", () => {
expect(parseEventFeed({ schema: "crosscode.event-feed/1", events: [] }).ok).toBe(false);
expect(parseEventFeed(null).ok).toBe(false);
});
});