Files
photoreport/apps/api/test/notifications.test.ts
spark 98a799eeb9
ci / rust (push) Successful in 6m58s
ci / typescript (push) Failing after 8m12s
Notifications: the bell for sales, paid orders, comments to approve, payouts and failed deliveries
Per-person notices written where things happen: a sale of your photos and what it earns you, paid orders (admins), client comments waiting for approval (editors), payouts made to you, deliveries that gave up (admins). Unread notices with the same key fold into one with a count; read ones are dropped after 90 days. The app shows a bell with the unread count, polls every minute and on focus, and links each notice to its page.

Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
2026-10-13 04:22:36 +01:00

42 lines
2.0 KiB
TypeScript

import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
import { Notifications } from "../src/notifications/service.js";
import { createTestApp, resetDb, seedUser, type TestContext } from "./helpers.js";
describe("notifications", () => {
let ctx: TestContext;
beforeAll(async () => {
ctx = await createTestApp();
});
afterAll(async () => {
await ctx.app.close();
await ctx.db.$disconnect();
});
beforeEach(async () => {
await resetDb(ctx.db);
});
it("reaches the right roles and folds unread notices with the same key", async () => {
const { org, user: owner } = await seedUser(ctx.db, "[email protected]", { org: "pr", role: "OWNER" });
const { user: editor } = await seedUser(ctx.db, "[email protected]", { org: "pr", role: "EDITOR" });
const { user: photographer } = await seedUser(ctx.db, "[email protected]", { org: "pr", role: "PHOTOGRAPHER" });
const n = new Notifications(ctx.db);
const notice = { type: "comment_pending" as const, key: "gallery:1", payload: { gallery: "Final" } };
await n.notifyRole(org!.id, "EDITOR", notice);
await n.notifyRole(org!.id, "EDITOR", notice);
const mine = await n.list(editor.id, { limit: 10, unread: false });
expect(mine).toMatchObject({ unreadCount: 1, notifications: [{ type: "comment_pending", count: 2, read: false, payload: { gallery: "Final" } }] });
expect((await n.list(owner.id, { limit: 10, unread: false })).unreadCount).toBe(1);
expect((await n.list(photographer.id, { limit: 10, unread: false })).unreadCount).toBe(0);
// Once read, the next one starts a new notice.
await n.markRead(editor.id, "all");
await n.notifyRole(org!.id, "EDITOR", notice);
expect((await n.list(editor.id, { limit: 10, unread: false })).notifications.map((x) => [x.count, x.read])).toEqual([
[1, false],
[2, true],
]);
// Someone else's notice can't be marked read.
const [first] = (await n.list(owner.id, { limit: 1, unread: true })).notifications;
expect(await n.markRead(editor.id, first!.id)).toBe(0);
});
});