Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -2,12 +2,17 @@ import { describe, expect, it, vi } from "vitest";
import {
createNotificationDelivery,
createAdminPlatformNotificationDelivery,
createPlatformNotificationDelivery,
getNotificationDeliveryDiagnostics,
getPlatformNotificationStreamSnapshot,
getNotificationStreamSnapshot,
getUserNotificationPreferences,
markAllInAppNotificationDeliveriesRead,
markInAppNotificationDeliveryRead,
markNotificationDeliveryStatus,
markPlatformInAppNotificationDeliveryRead,
markPlatformNotificationDeliveryStatus,
NOTIFICATION_DELIVERY_LAST_ERROR_MAX_LENGTH,
redactNotificationDiagnosticText,
upsertUserNotificationPreference,
@@ -25,6 +30,7 @@ function createNotificationsClient(): NotificationsClient {
upsert: vi.fn().mockResolvedValue({ id: "delivery_1" }),
update: vi.fn().mockResolvedValue({ id: "delivery_1" }),
updateMany: vi.fn().mockResolvedValue({ count: 1 }),
findFirst: vi.fn().mockResolvedValue(null),
findMany: vi.fn().mockResolvedValue([]),
},
};
@@ -41,12 +47,12 @@ describe("notification preference boundary", () => {
channel: "in-app",
enabled: null,
},
client,
client
);
expect(client.userNotificationPreference.upsert).toHaveBeenCalledWith({
where: {
tenantId_userId_channel_category: {
user_notification_preference_scope_key: {
tenantId: "tenant_1",
userId: "user_1",
channel: "IN_APP",
@@ -74,11 +80,13 @@ describe("notification preference boundary", () => {
userId: "user_1",
channel: "email",
},
client,
),
client
)
).rejects.toThrow("Notification preference tenant and user scope is required.");
await expect(getUserNotificationPreferences({ tenantId: "tenant_1", userId: " " }, client)).rejects.toThrow("Notification preference tenant and user scope is required.");
await expect(getUserNotificationPreferences({ tenantId: "tenant_1", userId: " " }, client)).rejects.toThrow(
"Notification preference tenant and user scope is required."
);
expect(client.userNotificationPreference.upsert).not.toHaveBeenCalled();
expect(client.userNotificationPreference.findMany).not.toHaveBeenCalled();
});
@@ -93,7 +101,7 @@ describe("notification preference boundary", () => {
channel: "email",
category: " Billing ",
},
client,
client
);
expect(client.userNotificationPreference.findMany).toHaveBeenCalledWith({
@@ -124,9 +132,9 @@ describe("notification preference boundary", () => {
describe("notification delivery boundary", () => {
it("redacts sensitive provider diagnostics before storing last errors", () => {
expect(
redactNotificationDiagnosticText("SMTP token=secret-token failed for user@example.com with Authorization: Bearer abc123 and payload-secret"),
).toBe("SMTP token=[redacted] failed for [redacted-email] with Authorization: Bearer [redacted] and payload-[redacted]");
expect(redactNotificationDiagnosticText("SMTP token=secret-token failed for user@example.com with Authorization: Bearer abc123 and payload-secret")).toBe(
"SMTP token=[redacted] failed for [redacted-email] with Authorization: Bearer [redacted] and payload-[redacted]"
);
});
it("creates delivery records with normalized defaults and redacted scalar metadata", async () => {
@@ -146,7 +154,7 @@ describe("notification delivery boundary", () => {
nested: { token: "secret" },
},
},
client,
client
);
expect(client.notificationDelivery.create).toHaveBeenCalledWith({
@@ -171,9 +179,7 @@ describe("notification delivery boundary", () => {
readAt: null,
},
});
expect(JSON.stringify(vi.mocked(client.notificationDelivery.create).mock.calls[0]?.[0])).not.toContain(
"secret-token",
);
expect(JSON.stringify(vi.mocked(client.notificationDelivery.create).mock.calls[0]?.[0])).not.toContain("secret-token");
});
it("upserts delivery records by source key when the producer supplies one", async () => {
@@ -189,12 +195,12 @@ describe("notification delivery boundary", () => {
status: "sent",
attemptsMade: 1,
},
client,
client
);
expect(client.notificationDelivery.upsert).toHaveBeenCalledWith({
where: {
tenantId_userId_channel_sourceKey: {
notification_delivery_source_key: {
tenantId: "tenant_1",
userId: "user_1",
channel: "EMAIL",
@@ -232,7 +238,7 @@ describe("notification delivery boundary", () => {
body: "secret-body",
headers: { authorization: "Bearer secret" },
} as Parameters<typeof createNotificationDelivery>[0] & Record<string, unknown>,
client,
client
);
const createCall = vi.mocked(client.notificationDelivery.create).mock.calls[0]?.[0];
@@ -252,8 +258,8 @@ describe("notification delivery boundary", () => {
channel: "sms",
messageKey: "announcement.created",
},
client,
),
client
)
).rejects.toThrow("Unsupported notification channel: sms");
expect(client.notificationDelivery.create).not.toHaveBeenCalled();
@@ -270,8 +276,8 @@ describe("notification delivery boundary", () => {
channel: "in_app",
messageKey: "announcement.created",
},
client,
),
client
)
).rejects.toThrow("Notification delivery tenant scope is required.");
expect(client.notificationDelivery.create).not.toHaveBeenCalled();
@@ -280,7 +286,7 @@ describe("notification delivery boundary", () => {
it("updates status diagnostics with redacted and truncated last errors", async () => {
const client = createNotificationsClient();
const oversizedError = `SMTP token=secret-token failed for user@example.com with Authorization: Bearer abc123 ${"x".repeat(
NOTIFICATION_DELIVERY_LAST_ERROR_MAX_LENGTH + 50,
NOTIFICATION_DELIVERY_LAST_ERROR_MAX_LENGTH + 50
)}`;
const sentAt = new Date("2026-05-08T12:00:00.000Z");
@@ -294,7 +300,7 @@ describe("notification delivery boundary", () => {
lastError: oversizedError,
sentAt,
},
client,
client
);
const updateCall = vi.mocked(client.notificationDelivery.updateMany).mock.calls[0]?.[0];
@@ -324,7 +330,7 @@ describe("notification delivery boundary", () => {
userId: " user_1 ",
readAt,
},
client,
client
);
expect(client.notificationDelivery.updateMany).toHaveBeenCalledWith({
@@ -350,8 +356,8 @@ describe("notification delivery boundary", () => {
tenantId: " ",
userId: "user_1",
},
client,
),
client
)
).rejects.toThrow("Notification delivery read scope is required.");
expect(client.notificationDelivery.updateMany).not.toHaveBeenCalled();
@@ -367,18 +373,18 @@ describe("notification delivery boundary", () => {
userId: "user_1",
channel: "EMAIL",
} as Parameters<typeof markInAppNotificationDeliveryRead>[0] & { channel: string },
client,
client
);
expect(client.notificationDelivery.updateMany).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({ channel: "IN_APP" }),
}),
})
);
expect(vi.mocked(client.notificationDelivery.updateMany).mock.calls[0]?.[0]).toEqual(
expect.objectContaining({
where: expect.objectContaining({ status: "SENT" }),
}),
})
);
expect(JSON.stringify(vi.mocked(client.notificationDelivery.updateMany).mock.calls[0]?.[0])).not.toContain("EMAIL");
});
@@ -394,11 +400,26 @@ describe("notification delivery boundary", () => {
tenantId: "tenant_1",
userId: "user_1",
},
client,
),
client
)
).rejects.toThrow("Notification delivery read scope did not match exactly one delivery.");
});
it("treats marking an already-read delivery as an idempotent no-op instead of throwing", async () => {
const client = createNotificationsClient();
vi.mocked(client.notificationDelivery.updateMany).mockResolvedValueOnce({ count: 0 });
vi.mocked(client.notificationDelivery.findFirst).mockResolvedValueOnce({ id: "delivery_1" });
await expect(
markInAppNotificationDeliveryRead({ id: "delivery_1", tenantId: "tenant_1", userId: "user_1" }, client)
).resolves.toEqual({ count: 0 });
expect(client.notificationDelivery.findFirst).toHaveBeenCalledWith({
where: { id: "delivery_1", tenantId: "tenant_1", userId: "user_1", channel: "IN_APP", status: "READ" },
select: { id: true },
});
});
it("bulk-clears every unread in-app delivery within the current tenant/user scope", async () => {
const client = createNotificationsClient();
vi.mocked(client.notificationDelivery.updateMany).mockResolvedValueOnce({ count: 3 });
@@ -410,7 +431,7 @@ describe("notification delivery boundary", () => {
userId: " user_1 ",
readAt,
},
client,
client
);
expect(client.notificationDelivery.updateMany).toHaveBeenCalledWith({
@@ -424,17 +445,15 @@ describe("notification delivery boundary", () => {
const client = createNotificationsClient();
vi.mocked(client.notificationDelivery.updateMany).mockResolvedValueOnce({ count: 0 });
await expect(
markAllInAppNotificationDeliveriesRead({ tenantId: "tenant_1", userId: "user_1" }, client),
).resolves.toBe(0);
await expect(markAllInAppNotificationDeliveriesRead({ tenantId: "tenant_1", userId: "user_1" }, client)).resolves.toBe(0);
});
it("rejects a bulk clear without tenant or user scope before writing", async () => {
const client = createNotificationsClient();
await expect(
markAllInAppNotificationDeliveriesRead({ tenantId: " ", userId: "user_1" }, client),
).rejects.toThrow("Notification delivery bulk read scope is required.");
await expect(markAllInAppNotificationDeliveriesRead({ tenantId: " ", userId: "user_1" }, client)).rejects.toThrow(
"Notification delivery bulk read scope is required."
);
expect(client.notificationDelivery.updateMany).not.toHaveBeenCalled();
});
@@ -451,7 +470,7 @@ describe("notification delivery boundary", () => {
category: "Billing",
take: 25,
},
client,
client
);
expect(client.notificationDelivery.findMany).toHaveBeenCalledWith({
@@ -476,8 +495,8 @@ describe("notification delivery boundary", () => {
tenantId: " ",
userId: "user_1",
},
client,
),
client
)
).rejects.toThrow("Notification delivery tenant scope is required.");
expect(client.notificationDelivery.findMany).not.toHaveBeenCalled();
@@ -494,7 +513,7 @@ describe("notification delivery boundary", () => {
channel: "in-app",
category: " Alerts ",
},
client,
client
);
const findManyCall = vi.mocked(client.notificationDelivery.findMany).mock.calls[0]?.[0];
@@ -519,7 +538,7 @@ describe("notification delivery boundary", () => {
channel: "in-app",
take: 50,
},
client,
client
);
expect(client.notificationDelivery.findMany).toHaveBeenCalledWith({
@@ -535,6 +554,207 @@ describe("notification delivery boundary", () => {
});
});
describe("platform notification delivery boundary", () => {
it("creates an owner-bound AdminUser intent without a tenant User foreign key", async () => {
const client = createNotificationsClient();
await createAdminPlatformNotificationDelivery(
{
adminUserId: " admin-1 ",
channel: "IN_APP",
category: "Admin Sales Email",
messageKey: "admin.sales.email.notifications.opened",
sourceKey: " admin-email-open:event-1 ",
payloadMetadata: { emailMessageId: "message-1" },
},
client as never
);
expect(client.notificationDelivery.findFirst).toHaveBeenCalledWith({
where: {
tenantId: null,
userId: null,
adminUserId: "admin-1",
channel: "IN_APP",
sourceKey: "admin-email-open:event-1",
},
});
expect(client.notificationDelivery.create).toHaveBeenCalledWith({
data: expect.objectContaining({
tenantId: null,
userId: null,
adminUserId: "admin-1",
status: "PENDING",
}),
});
});
it("creates a PENDING tenantless intent through a transaction-compatible client", async () => {
const client = createNotificationsClient();
await createPlatformNotificationDelivery(
{
userId: " user_haris ",
channel: "EMAIL",
category: "Leads",
messageKey: "platform.leads.captured",
sourceKey: " platform-lead:lead_1:email ",
payloadMetadata: {
leadId: "lead_1",
sourceId: "hivegrid-website",
deeplink: "/leads/lead_1",
email: "lead@example.test",
message: "private message",
},
},
client
);
expect(client.notificationDelivery.findFirst).toHaveBeenCalledWith({
where: {
tenantId: null,
userId: "user_haris",
channel: "EMAIL",
sourceKey: "platform-lead:lead_1:email",
},
});
expect(client.notificationDelivery.create).toHaveBeenCalledWith({
data: {
tenantId: null,
userId: "user_haris",
channel: "EMAIL",
category: "leads",
messageKey: "platform.leads.captured",
sourceKey: "platform-lead:lead_1:email",
status: "PENDING",
attemptsMade: 0,
providerMessageId: null,
lastError: null,
payloadMetadata: {
leadId: "lead_1",
sourceId: "hivegrid-website",
deeplink: "/leads/lead_1",
},
sentAt: null,
readAt: null,
},
});
expect(JSON.stringify(vi.mocked(client.notificationDelivery.create).mock.calls[0]?.[0])).not.toContain("lead@example.test");
expect(JSON.stringify(vi.mocked(client.notificationDelivery.create).mock.calls[0]?.[0])).not.toContain("private message");
});
it("updates only the tenantless delivery owned by the required platform user", async () => {
const client = createNotificationsClient();
await markPlatformNotificationDeliveryStatus(
{
id: " delivery_1 ",
userId: " user_haris ",
status: "sent",
attemptsMade: 2,
providerMessageId: "provider_1",
sentAt: new Date("2026-07-16T12:00:00.000Z"),
},
client
);
expect(client.notificationDelivery.updateMany).toHaveBeenCalledWith({
where: { id: "delivery_1", tenantId: null, userId: "user_haris" },
data: {
status: "SENT",
attemptsMade: 2,
providerMessageId: "provider_1",
sentAt: new Date("2026-07-16T12:00:00.000Z"),
},
});
});
it("marks only the recipient's tenantless in-app delivery as read", async () => {
const client = createNotificationsClient();
const readAt = new Date("2026-07-16T12:05:00.000Z");
await markPlatformInAppNotificationDeliveryRead({ id: "delivery_1", userId: "user_haris", readAt }, client);
expect(client.notificationDelivery.updateMany).toHaveBeenCalledWith({
where: { id: "delivery_1", tenantId: null, userId: "user_haris", channel: "IN_APP", status: "SENT" },
data: { status: "READ", readAt },
});
});
it("treats an already-read scoped platform delivery as an idempotent success", async () => {
const client = createNotificationsClient();
vi.mocked(client.notificationDelivery.updateMany).mockResolvedValue({ count: 0 });
vi.mocked(client.notificationDelivery.findFirst).mockResolvedValue({ id: "delivery_1" });
await expect(
markPlatformInAppNotificationDeliveryRead({ id: "delivery_1", userId: "user_haris" }, client)
).resolves.toEqual({ count: 0 });
expect(client.notificationDelivery.findFirst).toHaveBeenCalledWith({
where: { id: "delivery_1", tenantId: null, userId: "user_haris", channel: "IN_APP", status: "READ" },
select: { id: true },
});
vi.mocked(client.notificationDelivery.findFirst).mockResolvedValue(null);
await expect(
markPlatformInAppNotificationDeliveryRead({ id: "delivery_1", userId: "other_user" }, client)
).rejects.toThrow("did not match exactly one delivery");
});
it("returns only tenantless in-app rows for the required platform user", async () => {
const client = createNotificationsClient();
vi.mocked(client.notificationDelivery.findMany).mockResolvedValue([
{
id: "platform_delivery_1",
tenantId: null,
userId: "user_haris",
channel: "IN_APP",
category: "leads",
messageKey: "platform.leads.captured",
status: "SENT",
attemptsMade: 1,
createdAt: new Date("2026-07-16T12:00:00.000Z"),
updatedAt: new Date("2026-07-16T12:00:00.000Z"),
sentAt: new Date("2026-07-16T12:00:00.000Z"),
readAt: null,
payloadMetadata: { deeplink: "/leads/lead_1" },
},
{
id: "tenant_delivery_1",
tenantId: "tenant_1",
userId: "user_haris",
channel: "IN_APP",
category: "leads",
messageKey: "platform.leads.captured",
status: "SENT",
attemptsMade: 1,
createdAt: new Date("2026-07-16T12:00:00.000Z"),
updatedAt: new Date("2026-07-16T12:00:00.000Z"),
},
]);
const snapshot = await getPlatformNotificationStreamSnapshot({ userId: "user_haris" }, client);
expect(snapshot.items).toHaveLength(1);
expect(snapshot.items[0]).toMatchObject({ id: "platform_delivery_1", tenantId: null, userId: "user_haris" });
expect(client.notificationDelivery.findMany).toHaveBeenCalledWith({
where: { tenantId: null, userId: "user_haris", channel: "IN_APP", status: { in: ["SENT", "READ"] } },
orderBy: [{ updatedAt: "desc" }, { createdAt: "desc" }, { id: "desc" }],
take: 50,
});
});
it("rejects missing platform user scope before writes or reads", async () => {
const client = createNotificationsClient();
await expect(markPlatformNotificationDeliveryStatus({ id: "delivery_1", userId: " ", status: "sent" }, client)).rejects.toThrow(
"Platform notification delivery status scope is required"
);
await expect(getPlatformNotificationStreamSnapshot({ userId: " " }, client)).rejects.toThrow("Platform notification stream user scope is required");
expect(client.notificationDelivery.updateMany).not.toHaveBeenCalled();
expect(client.notificationDelivery.findMany).not.toHaveBeenCalled();
});
});
describe("notification stream snapshot boundary", () => {
it("queries tenant/user scoped in-app diagnostics with a bounded default and returns newest-first serializable items", async () => {
const client = createNotificationsClient();
@@ -697,14 +917,14 @@ describe("notification stream snapshot boundary", () => {
const snapshot = await getNotificationStreamSnapshot(
{ tenantId: "tenant_1", userId: "user_1", afterCursor: "2026-05-08T12:00:00.000Z:any", take: 500 },
client,
client
);
expect(client.notificationDelivery.findMany).toHaveBeenCalledWith(
expect.objectContaining({
where: { tenantId: "tenant_1", userId: "user_1", channel: "IN_APP", status: { in: ["SENT", "READ"] } },
take: 50,
}),
})
);
expect(snapshot.items).toHaveLength(1);
expect(snapshot.items[0]?.id).toBe("delivery_new");
@@ -853,11 +1073,15 @@ describe("notification package exports", () => {
upsertUserNotificationPreference,
getUserNotificationPreferences,
createNotificationDelivery,
createPlatformNotificationDelivery,
markInAppNotificationDeliveryRead,
markNotificationDeliveryStatus,
markPlatformInAppNotificationDeliveryRead,
markPlatformNotificationDeliveryStatus,
getNotificationDeliveryDiagnostics,
getNotificationStreamSnapshot,
}),
getPlatformNotificationStreamSnapshot,
})
);
});
@@ -869,11 +1093,15 @@ describe("notification package exports", () => {
upsertUserNotificationPreference,
getUserNotificationPreferences,
createNotificationDelivery,
createPlatformNotificationDelivery,
markInAppNotificationDeliveryRead,
markNotificationDeliveryStatus,
markPlatformInAppNotificationDeliveryRead,
markPlatformNotificationDeliveryStatus,
getNotificationDeliveryDiagnostics,
getNotificationStreamSnapshot,
}),
getPlatformNotificationStreamSnapshot,
})
);
});
});