import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn(), apiPut: vi.fn() })); vi.mock("@/app/lib/clientApi", () => ({ apiGet: mocks.apiGet, apiPost: mocks.apiPost, apiPut: mocks.apiPut, })); import { approveWithdrawal, getPendingWithdrawals, getWithdrawalConfig, rejectWithdrawal, updateWithdrawalConfig, } from "./adminWithdrawals"; beforeEach(() => { mocks.apiGet.mockReset(); mocks.apiPost.mockReset(); mocks.apiPut.mockReset(); }); describe("getPendingWithdrawals", () => { it("GETs the queue route with a 0-based upstream page", async () => { const page = { content: [], number: 0, size: 10, totalElements: 0, totalPages: 0 }; mocks.apiGet.mockResolvedValue(page); await expect(getPendingWithdrawals(1, 10)).resolves.toBe(page); expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/withdrawal/list?page=0&size=10"); }); it("converts the 1-based portal page to 0-based", async () => { mocks.apiGet.mockResolvedValue({ content: [], number: 2, size: 10, totalElements: 0, totalPages: 0 }); await getPendingWithdrawals(3, 10); expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/withdrawal/list?page=2&size=10"); }); it("appends the cycle filter when supplied", async () => { mocks.apiGet.mockResolvedValue({ content: [], number: 0, size: 10, totalElements: 0, totalPages: 0 }); await getPendingWithdrawals(1, 10, { cycle: "2026-10-02T17:00:00" }); expect(mocks.apiGet).toHaveBeenCalledWith( "/api/admin/withdrawal/list?page=0&size=10&cycle=2026-10-02T17%3A00%3A00", ); }); it("omits the cycle param entirely when not supplied", async () => { mocks.apiGet.mockResolvedValue({ content: [], number: 0, size: 10, totalElements: 0, totalPages: 0 }); await getPendingWithdrawals(1, 10); expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/withdrawal/list?page=0&size=10"); }); }); describe("getPendingWithdrawals status and member filters", () => { const empty = { content: [], number: 0, size: 10, totalElements: 0, totalPages: 0 }; it("puts status and member on the query, URL-encoded", async () => { mocks.apiGet.mockResolvedValue(empty); await getPendingWithdrawals(1, 10, { status: "ALL", member: "IRENE LIM" }); expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/withdrawal/list?page=0&size=10&status=ALL&member=IRENE+LIM"); }); it("puts cycle, status and member together with sort", async () => { mocks.apiGet.mockResolvedValue(empty); await getPendingWithdrawals(1, 10, { cycle: "NONE", status: "C", member: "MY1647047" }, { sortBy: "net", sortDir: "desc" }); const url = mocks.apiGet.mock.calls[0][0] as string; const q = new URL(url, "http://x").searchParams; expect(Object.fromEntries(q)).toEqual({ page: "0", size: "10", cycle: "NONE", status: "C", member: "MY1647047", sortBy: "net", sortDir: "desc", }); }); it("omits empty status and member", async () => { mocks.apiGet.mockResolvedValue(empty); await getPendingWithdrawals(1, 10, { cycle: undefined, status: "", member: "" }); expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/withdrawal/list?page=0&size=10"); }); }); describe("getWithdrawalConfig", () => { it("GETs the config route", async () => { const config = { minAmount: 50, maxAmount: null, increment: null, feePercent: 2, feeCapAmount: null, weeklyCutoffDay: 5, weeklyCutoffTime: "17:00:00", nextCutoff: "2026-10-02T17:00:00", doneBy: "admin", }; mocks.apiGet.mockResolvedValue(config); await expect(getWithdrawalConfig()).resolves.toBe(config); expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/withdrawal/config"); }); }); describe("updateWithdrawalConfig", () => { it("PUTs the config body (a mutation — never a GET)", async () => { const input = { minAmount: 50, maxAmount: null, increment: null, feePercent: 2, feeCapAmount: null, weeklyCutoffDay: null, weeklyCutoffTime: null, }; const updated = { ...input, nextCutoff: null, doneBy: "admin" }; mocks.apiPut.mockResolvedValue(updated); await expect(updateWithdrawalConfig(input)).resolves.toBe(updated); expect(mocks.apiPut).toHaveBeenCalledWith("/api/admin/withdrawal/config", input); }); }); describe("approveWithdrawal", () => { it("POSTs the uuid (a mutation — never a GET)", async () => { const row = { uuid: "555", status: "C" }; mocks.apiPost.mockResolvedValue(row); await expect(approveWithdrawal("555")).resolves.toBe(row); expect(mocks.apiPost).toHaveBeenCalledWith("/api/admin/withdrawal/approve", { uuid: "555" }); }); }); describe("rejectWithdrawal", () => { it("POSTs the uuid and reason (a mutation — never a GET)", async () => { const row = { uuid: "555", status: "N" }; mocks.apiPost.mockResolvedValue(row); await expect(rejectWithdrawal("555", "Bank details invalid")).resolves.toBe(row); expect(mocks.apiPost).toHaveBeenCalledWith("/api/admin/withdrawal/reject", { uuid: "555", reason: "Bank details invalid", }); }); });