import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn(), })); vi.mock("@/app/lib/clientApi", () => ({ apiGet: mocks.apiGet, apiPost: mocks.apiPost, })); import { buildNotificationsQuery, normalizeNotificationList, listNotifications, getUnreadCount, markRead, markAllRead, broadcast, } from "./notifications"; import type { AdminNotification } from "@/app/lib/types"; beforeEach(() => { mocks.apiGet.mockReset(); mocks.apiPost.mockReset(); }); describe("buildNotificationsQuery", () => { it("converts the 1-based portal page to a 0-based upstream page", () => { expect(buildNotificationsQuery({ page: 1, pageSize: 10 })).toBe("page=0&size=10"); expect(buildNotificationsQuery({ page: 3, pageSize: 20 })).toBe("page=2&size=20"); }); }); describe("normalizeNotificationList", () => { const item: AdminNotification = { id: 1, message: "hi", seen: false }; it("accepts a plain array (the upstream /notification/inbox shape)", () => { expect(normalizeNotificationList([item])).toEqual({ items: [item], total: 1 }); }); it("accepts a paged {content, totalElements} shape defensively", () => { expect(normalizeNotificationList({ content: [item], totalElements: 3 })).toEqual({ items: [item], total: 3, }); }); it("defaults to an empty list for anything else", () => { expect(normalizeNotificationList(null)).toEqual({ items: [], total: 0 }); expect(normalizeNotificationList("nope")).toEqual({ items: [], total: 0 }); }); }); describe("listNotifications", () => { it("GETs the portal route and normalizes the result", async () => { const item: AdminNotification = { id: 2, message: "x", seen: true }; mocks.apiGet.mockResolvedValue([item]); const result = await listNotifications({ page: 1, pageSize: 10 }); expect(mocks.apiGet).toHaveBeenCalledWith("/api/notifications?page=0&size=10"); expect(result).toEqual({ items: [item], total: 1 }); }); }); describe("getUnreadCount", () => { it("GETs unread-count and returns the count", async () => { mocks.apiGet.mockResolvedValue({ count: 4 }); await expect(getUnreadCount()).resolves.toBe(4); expect(mocks.apiGet).toHaveBeenCalledWith("/api/notifications/unread-count"); }); }); describe("markRead", () => { it("POSTs to the item's read route", async () => { mocks.apiPost.mockResolvedValue({ updated: 1 }); await markRead(42); expect(mocks.apiPost).toHaveBeenCalledWith("/api/notifications/42/read"); }); }); describe("markAllRead", () => { it("POSTs to the read-all route", async () => { mocks.apiPost.mockResolvedValue({ marked: 3 }); await markAllRead(); expect(mocks.apiPost).toHaveBeenCalledWith("/api/notifications/read-all"); }); }); describe("broadcast", () => { it("POSTs the broadcast payload to the portal route and returns the result", async () => { mocks.apiPost.mockResolvedValue({ sent: 2 }); const payload = { message: "Hello", type: "promo", target: { kind: "ALL_MEMBERS" } as const }; const result = await broadcast(payload); expect(mocks.apiPost).toHaveBeenCalledWith("/api/notifications", payload); expect(result).toEqual({ sent: 2 }); }); });