import { describe, it, expect, vi, beforeEach } from "vitest"; const api = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn() })); vi.mock("@/app/lib/clientApi", async (orig) => ({ ...(await orig()), apiGet: api.apiGet, apiPost: api.apiPost, })); import * as svc from "./adminMessages"; // BABYBOO-56 broadcast read count. Page is 1-based in, 0-based on the wire (like listAdminConversations). const parts = (url: string) => { const [path, qs = ""] = url.split("?"); return { path, sp: new URLSearchParams(qs) }; }; beforeEach(() => api.apiGet.mockReset().mockResolvedValue({ items: [], page: 0, size: 20, total: 0 })); describe("broadcast read helpers", () => { it("listAdminBroadcasts({ q }, page, size) GETs the summaries", async () => { await svc.listAdminBroadcasts({ q: "raya" }, 2, 20); const { path, sp } = parts(api.apiGet.mock.calls.at(-1)![0]); expect(path).toBe("/api/admin/member-messages/broadcasts"); expect([sp.get("q"), sp.get("page"), sp.get("size")]).toEqual(["raya", "1", "20"]); }); it("listAdminBroadcasts omits an empty q", async () => { await svc.listAdminBroadcasts({ q: "" }, 1, 20); const { sp } = parts(api.apiGet.mock.calls.at(-1)![0]); expect(sp.has("q")).toBe(false); expect([sp.get("page"), sp.get("size")]).toEqual(["0", "20"]); }); it("listAdminBroadcasts sends sortBy and sortDir when set, omits them otherwise", async () => { await svc.listAdminBroadcasts({ q: "", sortBy: "read", sortDir: "desc" }, 1, 20); let { sp } = parts(api.apiGet.mock.calls.at(-1)![0]); expect([sp.get("sortBy"), sp.get("sortDir")]).toEqual(["read", "desc"]); await svc.listAdminBroadcasts({ q: "" }, 1, 20); ({ sp } = parts(api.apiGet.mock.calls.at(-1)![0])); expect(sp.has("sortBy")).toBe(false); expect(sp.has("sortDir")).toBe(false); }); it("listAdminBroadcastRecipients(uuid, { unreadOnly }, page, size) GETs the recipients", async () => { await svc.listAdminBroadcastRecipients("b 1", { unreadOnly: true }, 3, 20); const { path, sp } = parts(api.apiGet.mock.calls.at(-1)![0]); expect(path).toBe("/api/admin/member-messages/broadcasts/b%201/recipients"); expect([sp.get("unreadOnly"), sp.get("page"), sp.get("size")]).toEqual(["true", "2", "20"]); }); it("listAdminBroadcastRecipients omits unreadOnly when false", async () => { await svc.listAdminBroadcastRecipients("b1", { unreadOnly: false }, 1, 20); expect(parts(api.apiGet.mock.calls.at(-1)![0]).sp.has("unreadOnly")).toBe(false); }); it("getAdminBroadcast(uuid) GETs the single summary with the id encoded", async () => { await svc.getAdminBroadcast("b 1"); const { path } = parts(api.apiGet.mock.calls.at(-1)![0]); expect(path).toBe("/api/admin/member-messages/broadcasts/b%201"); }); });