Track HiveGrid Cloud planning reference

This commit is contained in:
david committed 2026-06-26 20:48:01 +02:00
1 parent b3d5ea6764
commit e341c5938a
5784 files changed
+1104929 -3

No files matched your search

@@ -0,0 +1,180 @@
import { describe, expect, it } from "vitest";
import {
REALTIME_EVENT_NAMES,
isRealtimeEvent,
serializeRealtimeComment,
serializeRealtimeEvent,
subscribeToRealtime,
validateRealtimeEvent,
type NotificationStreamItem,
type RealtimeMessageEvent,
} from "./index.js";
const item: NotificationStreamItem = {
id: "delivery_boundary_1",
cursor: "2026-05-08T20:00:00.000Z:delivery_boundary_1",
tenantId: "tenant_1",
userId: "user_1",
channel: "IN_APP",
category: "billing",
messageKey: "invoice.ready",
status: "PENDING",
attemptsMade: 0,
createdAt: "2026-05-08T19:59:00.000Z",
updatedAt: "2026-05-08T20:00:00.000Z",
sentAt: null,
readAt: null,
};
const baseEvent = {
cursor: item.cursor,
requestId: "req_boundary_1",
tenantId: "tenant_1",
userId: "user_1",
};
class FakeEventSource {
static instances: FakeEventSource[] = [];
onopen: (() => void) | null = null;
onmessage: ((event: RealtimeMessageEvent) => void) | null = null;
onerror: ((event: unknown) => void) | null = null;
closed = false;
constructor(readonly url: string | URL) {
FakeEventSource.instances.push(this);
}
emitMessage(data: unknown): void {
this.onmessage?.({ data: typeof data === "string" ? data : JSON.stringify(data) });
}
close(): void {
this.closed = true;
}
}
describe("public realtime package boundary", () => {
it("subscribes to notification updates through the entrypoint with redacted invalid diagnostics", () => {
FakeEventSource.instances = [];
const updates: NotificationStreamItem[] = [];
const diagnostics: unknown[] = [];
const subscriber = subscribeToRealtime({
url: "/api/notifications/stream",
EventSource: FakeEventSource,
reconnect: { enabled: false },
onUpdate: (event) => updates.push(event.item),
onDiagnostic: (diagnostic) => diagnostics.push(diagnostic),
});
const source = FakeEventSource.instances[0];
expect(source?.url).toBe("/api/notifications/stream");
expect(subscriber.currentSource).toBe(source);
source.emitMessage({ ...baseEvent, event: REALTIME_EVENT_NAMES.notificationUpdate, item });
source.emitMessage({ ...baseEvent, event: REALTIME_EVENT_NAMES.notificationUpdate, item: { ...item, providerPayload: "token=secret" } });
expect(updates).toEqual([item]);
expect(diagnostics).toEqual([
{
phase: "validation",
message: "notification item contains secret-like key: [redacted]",
eventName: REALTIME_EVENT_NAMES.notificationUpdate,
cursor: item.cursor,
requestId: "req_boundary_1",
},
]);
expect(JSON.stringify(diagnostics)).not.toMatch(/providerPayload|token=secret|authorization|Bearer/i);
subscriber.close();
expect(source.closed).toBe(true);
});
it("validates snapshot, update, error, and keepalive events from the entrypoint", () => {
expect(
validateRealtimeEvent({
...baseEvent,
event: REALTIME_EVENT_NAMES.notificationSnapshot,
items: [item],
}),
).toEqual({
...baseEvent,
event: REALTIME_EVENT_NAMES.notificationSnapshot,
items: [item],
});
expect(
validateRealtimeEvent({
...baseEvent,
event: REALTIME_EVENT_NAMES.notificationUpdate,
item,
}),
).toEqual({
...baseEvent,
event: REALTIME_EVENT_NAMES.notificationUpdate,
item,
});
expect(validateRealtimeEvent({ ...baseEvent, event: REALTIME_EVENT_NAMES.keepalive })).toEqual({ ...baseEvent, event: REALTIME_EVENT_NAMES.keepalive });
expect(validateRealtimeEvent({ ...baseEvent, event: REALTIME_EVENT_NAMES.error, code: "STREAM_FAILED", message: "Notification stream failed." })).toEqual({
...baseEvent,
event: REALTIME_EVENT_NAMES.error,
code: "STREAM_FAILED",
message: "Notification stream failed.",
});
});
it("publishes deterministic SSE frames through exported server APIs", () => {
const snapshotFrame = serializeRealtimeEvent({
...baseEvent,
event: REALTIME_EVENT_NAMES.notificationSnapshot,
items: [item],
});
expect(snapshotFrame).toBe(
`id: ${item.cursor}\nevent: notification.snapshot\ndata: {"event":"notification.snapshot","cursor":"${item.cursor}","requestId":"req_boundary_1","tenantId":"tenant_1","userId":"user_1","items":[{"id":"delivery_boundary_1","cursor":"${item.cursor}","tenantId":"tenant_1","userId":"user_1","channel":"IN_APP","category":"billing","messageKey":"invoice.ready","status":"PENDING","attemptsMade":0,"createdAt":"2026-05-08T19:59:00.000Z","updatedAt":"2026-05-08T20:00:00.000Z","sentAt":null,"readAt":null}]}\n\n`,
);
expect(
serializeRealtimeEvent(
{
...baseEvent,
event: REALTIME_EVENT_NAMES.notificationUpdate,
item: { ...item, status: "SENT", attemptsMade: 1, sentAt: "2026-05-08T20:01:00.000Z" },
},
{ retry: 3000 },
),
).toContain("retry: 3000\nid: 2026-05-08T20:00:00.000Z:delivery_boundary_1\nevent: notification.update\n");
expect(serializeRealtimeComment("keepalive req_boundary_1")).toBe(": keepalive req_boundary_1\n\n");
expect(serializeRealtimeEvent({ ...baseEvent, event: REALTIME_EVENT_NAMES.keepalive })).toContain("event: keepalive\n");
expect(serializeRealtimeEvent({ ...baseEvent, event: REALTIME_EVENT_NAMES.error, code: "STREAM_FAILED", message: "Notification stream failed." })).toContain(
'event: error\ndata: {"event":"error"',
);
});
it("rejects secret-bearing public inputs and keeps diagnostics redacted", () => {
expect(isRealtimeEvent({ ...baseEvent, event: REALTIME_EVENT_NAMES.notificationUpdate, item: { ...item, providerPayload: "token=secret" } })).toBe(false);
expect(() =>
serializeRealtimeEvent({
...baseEvent,
event: REALTIME_EVENT_NAMES.error,
code: "STREAM_FAILED",
message: "Notification stream failed.",
authorizationHeader: "Bearer secret",
}),
).toThrow(/secret-like key/);
const frame = serializeRealtimeEvent({
...baseEvent,
event: REALTIME_EVENT_NAMES.error,
code: "STREAM_FAILED",
message: "Notification stream failed.",
});
expect(frame).not.toMatch(/token|authorization|providerPayload|Bearer secret/i);
});
});