Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/realtime/src/server.test.ts
T
2026-07-08 16:15:37 +02:00

116 lines
4.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { REALTIME_EVENT_NAMES, type NotificationStreamItem } from "./events.js";
import { serializeRealtimeComment, serializeRealtimeEvent } from "./server.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,
};
const baseEvent = {
cursor: item.cursor,
requestId: "req_1",
tenantId: "tenant_1",
userId: "user_1",
};
describe("SSE realtime serialization", () => {
it("serializes deterministic snapshot frames with cursor ids", () => {
expect(
serializeRealtimeEvent({
...baseEvent,
event: REALTIME_EVENT_NAMES.notificationSnapshot,
items: [item],
}),
).toBe(
`id: ${item.cursor}\nevent: notification.snapshot\ndata: {"event":"notification.snapshot","cursor":"${item.cursor}","requestId":"req_1","tenantId":"tenant_1","userId":"user_1","items":[{"id":"delivery_1","cursor":"${item.cursor}","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,"deeplink":null,"lastError":null}]}\n\n`,
);
});
it("serializes updates with retry hints before event data", () => {
expect(
serializeRealtimeEvent(
{
...baseEvent,
event: REALTIME_EVENT_NAMES.notificationUpdate,
item,
},
{ retry: 5000 },
),
).toBe(
`retry: 5000\nid: ${item.cursor}\nevent: notification.update\ndata: {"event":"notification.update","cursor":"${item.cursor}","requestId":"req_1","tenantId":"tenant_1","userId":"user_1","item":{"id":"delivery_1","cursor":"${item.cursor}","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,"deeplink":null,"lastError":null}}\n\n`,
);
});
it("serializes keepalive comments and keepalive events", () => {
expect(serializeRealtimeComment("keepalive req_1")).toBe(": keepalive req_1\n\n");
expect(
serializeRealtimeEvent({
event: REALTIME_EVENT_NAMES.keepalive,
cursor: item.cursor,
requestId: "req_keepalive",
}),
).toBe(`id: ${item.cursor}\nevent: keepalive\ndata: {"event":"keepalive","cursor":"${item.cursor}","requestId":"req_keepalive"}\n\n`);
});
it("serializes redacted error diagnostics without secret material", () => {
const frame = serializeRealtimeEvent({
event: REALTIME_EVENT_NAMES.error,
cursor: item.cursor,
requestId: "req_error",
code: "STREAM_FAILED",
message: "Notification stream failed.",
});
expect(frame).toBe(
`id: ${item.cursor}\nevent: error\ndata: {"event":"error","cursor":"${item.cursor}","requestId":"req_error","code":"STREAM_FAILED","message":"Notification stream failed."}\n\n`,
);
expect(frame).not.toContain("token");
expect(frame).not.toContain("authorization");
});
it("fails validation before serializing invalid events and secret-like fields", () => {
expect(() =>
serializeRealtimeEvent({
...baseEvent,
event: REALTIME_EVENT_NAMES.notificationSnapshot,
items: [{ ...item, providerPayload: "token=secret" }],
}),
).toThrow(/secret-like key/);
expect(() =>
serializeRealtimeEvent({
event: REALTIME_EVENT_NAMES.error,
cursor: item.cursor,
requestId: "req_error",
code: "STREAM_FAILED",
message: "Notification stream failed.",
authorizationHeader: "Bearer secret",
}),
).toThrow(/secret-like key/);
});
it("rejects invalid retry hints and newline injection", () => {
expect(() => serializeRealtimeEvent({ event: REALTIME_EVENT_NAMES.keepalive, cursor: item.cursor, requestId: "req_1" }, { retry: -1 })).toThrow(
/retry/,
);
expect(() => serializeRealtimeComment("keepalive\nevent: error")).toThrow(/newline/);
expect(() => serializeRealtimeEvent({ event: REALTIME_EVENT_NAMES.keepalive, cursor: `${item.cursor}\nevent: error`, requestId: "req_1" })).toThrow(
/newline/,
);
});
});