import { createHmac } from "node:crypto"; import { hub } from "../src"; import { HUB_HEADERS, LINK_HEADERS, sign, signedHeaders, verify } from "../src/signing"; import { sample } from "./helpers"; describe("Hub protocol v2", () => { it("picks the stats by product", () => { const events = sample>("hub-heartbeat-events.json"); const website = sample>("hub-heartbeat-website.json"); expect(hub.heartbeatRequestSchema.safeParse({ ...events, stats: website.stats }).success).toBe(false); }); it("refuses protocol v1 bodies (the Hub routes those to its v1 handler)", () => { expect(hub.heartbeatRequestSchema.safeParse({ ...sample("hub-heartbeat-events.json"), protocolVersion: 1 }).success).toBe(false); }); it("accepts a licence token payload from a Hub before 2.0 (no customerId or product)", () => { const payload = sample>("hub-licence-payload.json"); delete payload.customerId; delete payload.product; expect(hub.licencePayloadSchema.safeParse(payload).success).toBe(true); }); it("only sends income as a band", () => { const hb = sample<{ stats: { income: Record } }>("hub-heartbeat-events.json"); hb.stats.income.last30d = 12345; expect(hub.heartbeatRequestSchema.safeParse(hb).success).toBe(false); }); it.each([ [0, "0–100"], [100, "0–100"], [101, "101–300"], [750, "301–750"], [751, "751–2 000"], [5000, "2 001–5 000"], [5001, "over 5 000"], ] as const)("puts %d active members in the %s band", (n, band) => { expect(hub.activeMemberBand(n)).toBe(band); }); }); describe("signing", () => { const secret = "s3cret-for-tests"; const body = JSON.stringify({ hello: "world" }); const now = new Date("2026-11-03T10:00:00.000Z"); it("signs exactly like the Hub protocol v1 heartbeat", () => { const timestamp = now.toISOString(); const expected = createHmac("sha256", secret).update(`${timestamp}.${body}`).digest("hex"); expect(sign(secret, timestamp, body)).toBe(expected); expect(sign(secret, timestamp, new TextEncoder().encode(body))).toBe(expected); }); it("builds the headers and verifies them", () => { const h = signedHeaders(LINK_HEADERS, { senderId: "inst-1", secret, rawBody: body, now }); expect(h[LINK_HEADERS.sender]).toBe("inst-1"); expect(verify({ secret, timestamp: h[LINK_HEADERS.timestamp], signature: h[LINK_HEADERS.signature], rawBody: body, now })).toEqual({ ok: true }); expect(Object.keys(signedHeaders(HUB_HEADERS, { senderId: "i", secret, rawBody: body, now }))).toEqual(["x-hub-install", "x-hub-timestamp", "x-hub-signature"]); }); it("refuses a changed body, a wrong secret or a bad signature", () => { const timestamp = now.toISOString(); const signature = sign(secret, timestamp, body); expect(verify({ secret, timestamp, signature, rawBody: body + " ", now })).toEqual({ ok: false, code: "BAD_SIGNATURE" }); expect(verify({ secret: "other", timestamp, signature, rawBody: body, now })).toEqual({ ok: false, code: "BAD_SIGNATURE" }); expect(verify({ secret, timestamp, signature: "zz", rawBody: body, now })).toEqual({ ok: false, code: "BAD_SIGNATURE" }); expect(verify({ secret, timestamp, signature: undefined, rawBody: body, now })).toEqual({ ok: false, code: "BAD_SIGNATURE" }); }); it("refuses requests more than five minutes off the clock", () => { const old = new Date(now.getTime() - 5 * 60 * 1000 - 1).toISOString(); expect(verify({ secret, timestamp: old, signature: sign(secret, old, body), rawBody: body, now })).toEqual({ ok: false, code: "STALE_REQUEST" }); const edge = new Date(now.getTime() - 5 * 60 * 1000).toISOString(); expect(verify({ secret, timestamp: edge, signature: sign(secret, edge, body), rawBody: body, now })).toEqual({ ok: true }); expect(verify({ secret, timestamp: "yesterday", signature: sign(secret, "yesterday", body), rawBody: body, now })).toEqual({ ok: false, code: "STALE_REQUEST" }); }); });