Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/realtime/src/guards.test.ts
T

91 lines
3.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { REALTIME_EVENT_NAMES, type NotificationStreamItem } from "./events.js";
import {
assertNotificationEventInAppScope,
assertNotificationItemInAppScope,
normalizeNotificationStreamScope,
redactNotificationGuardDiagnostic,
} from "./guards.js";
const item: NotificationStreamItem = {
id: "delivery_guard_1",
cursor: "2026-05-08T20:00:00.000Z:delivery_guard_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 scope = { tenantId: "tenant_1", userId: "user_1" };
describe("notification stream guards", () => {
it("normalizes trimmed valid stream scope", () => {
expect(normalizeNotificationStreamScope({ tenantId: " tenant_1 ", userId: " user_1 ", afterCursor: " cursor_1 " })).toEqual({
tenantId: "tenant_1",
userId: "user_1",
afterCursor: "cursor_1",
});
});
it("rejects missing or blank tenant and user scope", () => {
expect(() => normalizeNotificationStreamScope({ userId: "user_1" })).toThrow(/tenantId is required/);
expect(() => normalizeNotificationStreamScope({ tenantId: " ", userId: "user_1" })).toThrow(/tenantId must not be empty/);
expect(() => normalizeNotificationStreamScope({ tenantId: "tenant_1" })).toThrow(/userId is required/);
expect(() => normalizeNotificationStreamScope({ tenantId: "tenant_1", userId: "\t" })).toThrow(/userId must not be empty/);
});
it("rejects non-IN_APP notification items", () => {
expect(() => assertNotificationItemInAppScope({ ...item, channel: "EMAIL" as "IN_APP" }, scope)).toThrow(/IN_APP/);
});
it("rejects tenant and user mismatches between stream scope and items", () => {
expect(() => assertNotificationItemInAppScope({ ...item, tenantId: "tenant_2" }, scope)).toThrow(/item\.tenantId/);
expect(() => assertNotificationItemInAppScope({ ...item, userId: "user_2" }, scope)).toThrow(/item\.userId/);
});
it("rejects tenant and user mismatches between stream scope and events", () => {
expect(() =>
assertNotificationEventInAppScope(
{
event: REALTIME_EVENT_NAMES.notificationSnapshot,
cursor: item.cursor,
requestId: "req_guard_1",
tenantId: "tenant_2",
userId: "user_1",
items: [item],
},
scope,
),
).toThrow(/event\.tenantId/);
expect(() =>
assertNotificationEventInAppScope(
{
event: REALTIME_EVENT_NAMES.notificationUpdate,
cursor: item.cursor,
requestId: "req_guard_2",
tenantId: "tenant_1",
userId: "user_1",
item: { ...item, userId: "user_2" },
},
scope,
),
).toThrow(/item\.userId/);
});
it("redacts secret-like guard diagnostic text", () => {
const diagnostic = redactNotificationGuardDiagnostic(new Error("provider payload token=secret Bearer abc123 should not leak"));
expect(diagnostic).toContain("[redacted]");
expect(diagnostic).not.toMatch(/provider|payload|token=secret|Bearer abc123/i);
});
});