147 lines
4.7 KiB
TypeScript
147 lines
4.7 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { REALTIME_EVENT_NAMES, isRealtimeEvent, validateRealtimeEvent, type NotificationStreamItem } from "./events.js";
|
|
|
|
const item: NotificationStreamItem = {
|
|
id: "delivery_1",
|
|
cursor: "2026-05-08T20:00:00.000Z:delivery_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,
|
|
};
|
|
|
|
describe("realtime event contract", () => {
|
|
it("accepts and normalizes a public notification snapshot", () => {
|
|
expect(
|
|
validateRealtimeEvent({
|
|
event: REALTIME_EVENT_NAMES.notificationSnapshot,
|
|
cursor: "2026-05-08T20:00:00.000Z:delivery_1",
|
|
requestId: "req_1",
|
|
tenantId: "tenant_1",
|
|
userId: "user_1",
|
|
items: [item],
|
|
}),
|
|
).toEqual({
|
|
event: REALTIME_EVENT_NAMES.notificationSnapshot,
|
|
cursor: "2026-05-08T20:00:00.000Z:delivery_1",
|
|
requestId: "req_1",
|
|
tenantId: "tenant_1",
|
|
userId: "user_1",
|
|
items: [item],
|
|
});
|
|
});
|
|
|
|
it("accepts update, keepalive, and redacted error envelopes", () => {
|
|
expect(
|
|
validateRealtimeEvent({
|
|
event: REALTIME_EVENT_NAMES.notificationUpdate,
|
|
cursor: item.cursor,
|
|
requestId: "req_skipped",
|
|
item: { ...item, status: "SKIPPED", attemptsMade: 0, sentAt: null },
|
|
}),
|
|
).toMatchObject({ event: REALTIME_EVENT_NAMES.notificationUpdate, item: expect.objectContaining({ status: "SKIPPED" }) });
|
|
|
|
expect(
|
|
validateRealtimeEvent({
|
|
event: REALTIME_EVENT_NAMES.notificationUpdate,
|
|
cursor: item.cursor,
|
|
requestId: "req_2",
|
|
item,
|
|
}),
|
|
).toMatchObject({ event: REALTIME_EVENT_NAMES.notificationUpdate, item });
|
|
|
|
expect(
|
|
validateRealtimeEvent({
|
|
event: REALTIME_EVENT_NAMES.keepalive,
|
|
cursor: item.cursor,
|
|
requestId: "req_3",
|
|
}),
|
|
).toEqual({ event: REALTIME_EVENT_NAMES.keepalive, cursor: item.cursor, requestId: "req_3" });
|
|
|
|
expect(
|
|
validateRealtimeEvent({
|
|
event: REALTIME_EVENT_NAMES.error,
|
|
cursor: item.cursor,
|
|
requestId: "req_4",
|
|
code: "STREAM_FAILED",
|
|
message: "Notification stream failed.",
|
|
}),
|
|
).toEqual({
|
|
event: REALTIME_EVENT_NAMES.error,
|
|
cursor: item.cursor,
|
|
requestId: "req_4",
|
|
code: "STREAM_FAILED",
|
|
message: "Notification stream failed.",
|
|
});
|
|
});
|
|
|
|
it("rejects invalid event names and malformed identifiers", () => {
|
|
expect(() => validateRealtimeEvent({ event: "notification", cursor: item.cursor, requestId: "req_1", items: [] })).toThrow(
|
|
/supported realtime event name/,
|
|
);
|
|
expect(() => validateRealtimeEvent({ event: REALTIME_EVENT_NAMES.keepalive, cursor: 123, requestId: "req_1" })).toThrow(/cursor/);
|
|
expect(() => validateRealtimeEvent({ event: REALTIME_EVENT_NAMES.keepalive, cursor: item.cursor, requestId: "" })).toThrow(/requestId/);
|
|
expect(isRealtimeEvent({ event: "notification", cursor: item.cursor, requestId: "req_1" })).toBe(false);
|
|
});
|
|
|
|
it("rejects malformed items and unsupported public fields", () => {
|
|
expect(() =>
|
|
validateRealtimeEvent({
|
|
event: REALTIME_EVENT_NAMES.notificationSnapshot,
|
|
cursor: item.cursor,
|
|
requestId: "req_1",
|
|
items: [{ ...item, channel: "EMAIL" }],
|
|
}),
|
|
).toThrow(/IN_APP/);
|
|
|
|
expect(() =>
|
|
validateRealtimeEvent({
|
|
event: REALTIME_EVENT_NAMES.notificationSnapshot,
|
|
cursor: item.cursor,
|
|
requestId: "req_1",
|
|
items: [{ ...item, status: "DELIVERED" }],
|
|
}),
|
|
).toThrow(/supported notification status/);
|
|
|
|
expect(() =>
|
|
validateRealtimeEvent({
|
|
event: REALTIME_EVENT_NAMES.notificationSnapshot,
|
|
cursor: item.cursor,
|
|
requestId: "req_1",
|
|
items: [{ ...item, providerMessageId: "provider_1" }],
|
|
}),
|
|
).toThrow(/secret-like key/);
|
|
});
|
|
|
|
it("rejects metadata leakage attempts on envelopes and error diagnostics", () => {
|
|
expect(() =>
|
|
validateRealtimeEvent({
|
|
event: REALTIME_EVENT_NAMES.notificationUpdate,
|
|
cursor: item.cursor,
|
|
requestId: "req_1",
|
|
item,
|
|
authorizationHeader: "Bearer token",
|
|
}),
|
|
).toThrow(/secret-like key/);
|
|
|
|
expect(() =>
|
|
validateRealtimeEvent({
|
|
event: REALTIME_EVENT_NAMES.error,
|
|
cursor: item.cursor,
|
|
requestId: "req_1",
|
|
code: "STREAM_FAILED",
|
|
message: "Notification stream failed.",
|
|
stack: "Error: provider token abc",
|
|
}),
|
|
).toThrow(/secret-like key/);
|
|
});
|
|
});
|