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, "owner@pr.pt", { org: "pr", role: "OWNER" }); const { user: editor } = await seedUser(ctx.db, "editor@pr.pt", { org: "pr", role: "EDITOR" }); const { user: photographer } = await seedUser(ctx.db, "foto@pr.pt", { 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); }); });