Files
2026-07-08 16:15:37 +02:00

128 lines
5.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { REALTIME_EVENT_NAMES, validateRealtimeEvent, type NotificationStreamItem } from "./events.js";
const item: NotificationStreamItem = {
id: "delivery_b_1",
cursor: "2026-05-08T20:00:00.000Z:delivery_b_1",
tenantId: "tenant_1",
userId: "user_1",
channel: "IN_APP",
category: "billing",
messageKey: "invoice.ready",
status: "SENT",
attemptsMade: 1,
createdAt: "2026-05-08T19:59:00.000Z",
updatedAt: "2026-05-08T20:00:00.000Z",
sentAt: "2026-05-08T20:00:00.000Z",
readAt: null,
};
function snapshot(overrides: Record<string, unknown> = {}): Record<string, unknown> {
return {
event: REALTIME_EVENT_NAMES.notificationSnapshot,
cursor: item.cursor,
requestId: "req_b_1",
items: [item],
...overrides,
};
}
describe("realtime event contract edge cases", () => {
it("rejects top-level inputs that are not plain objects", () => {
expect(() => validateRealtimeEvent(null)).toThrow(/realtime event must be an object/);
expect(() => validateRealtimeEvent(42)).toThrow(/realtime event must be an object/);
expect(() => validateRealtimeEvent("notification.update")).toThrow(/realtime event must be an object/);
// Arrays are records-of-numbers but explicitly disallowed by isRecord.
expect(() => validateRealtimeEvent([item])).toThrow(/realtime event must be an object/);
});
it("rejects unsupported (but non-secret) envelope keys", () => {
expect(() =>
validateRealtimeEvent({ event: REALTIME_EVENT_NAMES.keepalive, cursor: item.cursor, requestId: "req_b_1", region: "eu" }),
).toThrow(/keepalive event contains unsupported key: region/);
});
it("rejects strings that exceed the maximum length", () => {
expect(() =>
validateRealtimeEvent({ event: REALTIME_EVENT_NAMES.keepalive, cursor: item.cursor, requestId: "x".repeat(501) }),
).toThrow(/requestId is too long/);
});
it("rejects snapshot payloads whose items are not an array", () => {
expect(() => validateRealtimeEvent(snapshot({ items: "not-an-array" }))).toThrow(/snapshot event items must be an array/);
expect(() => validateRealtimeEvent(snapshot({ items: { length: 1 } }))).toThrow(/snapshot event items must be an array/);
});
it("rejects snapshot payloads that exceed the per-event item cap", () => {
const tooMany = Array.from({ length: 101 }, () => item);
expect(() => validateRealtimeEvent(snapshot({ items: tooMany }))).toThrow(/too many items/);
});
it("rejects items that are not objects", () => {
expect(() => validateRealtimeEvent(snapshot({ items: [42] }))).toThrow(/notification item must be an object/);
expect(() =>
validateRealtimeEvent({ event: REALTIME_EVENT_NAMES.notificationUpdate, cursor: item.cursor, requestId: "req_b_1", item: null }),
).toThrow(/notification item must be an object/);
});
it("rejects non-negative-integer violations on attemptsMade", () => {
expect(() => validateRealtimeEvent(snapshot({ items: [{ ...item, attemptsMade: -1 }] }))).toThrow(/non-negative integer/);
expect(() => validateRealtimeEvent(snapshot({ items: [{ ...item, attemptsMade: 1.5 }] }))).toThrow(/non-negative integer/);
expect(() => validateRealtimeEvent(snapshot({ items: [{ ...item, attemptsMade: "1" }] }))).toThrow(/non-negative integer/);
});
it("rejects ISO date fields that fail the pattern or are unparseable", () => {
// Fails the strict ISO pattern entirely.
expect(() => validateRealtimeEvent(snapshot({ items: [{ ...item, createdAt: "08.05.2026" }] }))).toThrow(/must be an ISO timestamp/);
// Matches the pattern shape but is not a real calendar instant (Date.parse -> NaN).
expect(() => validateRealtimeEvent(snapshot({ items: [{ ...item, updatedAt: "2026-13-40T25:61:61.000Z" }] }))).toThrow(
/must be an ISO timestamp/,
);
});
it("normalizes explicit null envelope tenant/user scope to null", () => {
expect(
validateRealtimeEvent({
event: REALTIME_EVENT_NAMES.keepalive,
cursor: item.cursor,
requestId: "req_b_1",
tenantId: null,
userId: null,
}),
).toEqual({ event: REALTIME_EVENT_NAMES.keepalive, cursor: item.cursor, requestId: "req_b_1", tenantId: null, userId: null });
});
it("defaults omitted optional item fields to null/empty without throwing", () => {
const sparseItem = {
id: "delivery_sparse_1",
cursor: "2026-05-08T20:00:00.000Z:delivery_sparse_1",
channel: "IN_APP",
category: "billing",
messageKey: "invoice.ready",
status: "PENDING",
attemptsMade: 0,
};
const result = validateRealtimeEvent({
event: REALTIME_EVENT_NAMES.notificationUpdate,
cursor: item.cursor,
requestId: "req_b_1",
item: sparseItem,
});
expect(result).toMatchObject({
event: REALTIME_EVENT_NAMES.notificationUpdate,
item: {
id: "delivery_sparse_1",
tenantId: null,
userId: null,
createdAt: "",
updatedAt: "",
sentAt: null,
readAt: null,
},
});
});
});