import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ apiGet: vi.fn(), apiPut: vi.fn() })); vi.mock("@/app/lib/clientApi", () => mocks); import { getLead, listLeads, updateLead } from "./adminLeads"; beforeEach(() => { mocks.apiGet.mockReset(); mocks.apiPut.mockReset(); }); describe("adminLeads", () => { it("lists with a 0-based page and q", async () => { mocks.apiGet.mockResolvedValue({ content: [{ leadUuid: "1" }], totalElements: 41 }); const out = await listLeads({ q: "ann", page: 3, pageSize: 20 }); expect(mocks.apiGet).toHaveBeenCalledWith("/api/leads?q=ann&page=2&size=20"); expect(out).toEqual({ items: [{ leadUuid: "1" }], total: 41 }); }); it("omits a blank q and tolerates an empty body", async () => { mocks.apiGet.mockResolvedValue(null); const out = await listLeads({ q: "", page: 1, pageSize: 10 }); expect(mocks.apiGet).toHaveBeenCalledWith("/api/leads?page=0&size=10"); expect(out).toEqual({ items: [], total: 0 }); }); it("gets and updates one lead", async () => { mocks.apiGet.mockResolvedValue({ leadUuid: "l-1" }); mocks.apiPut.mockResolvedValue({ leadUuid: "l-1" }); await getLead("l-1"); const payload = { name: "A", mobile: null, email: "a@x.com", companyName: null, jobTitle: null, notes: null }; await updateLead("l-1", payload); expect(mocks.apiGet).toHaveBeenCalledWith("/api/leads/l-1"); expect(mocks.apiPut).toHaveBeenCalledWith("/api/leads/l-1", payload); }); });