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 { getNetworkTree, getMemberUpline, getPlacementHistory, changeSponsor, directDownline, totalDownline, } from "./network"; import type { AdminTreeNode } from "@/app/lib/types"; const root: AdminTreeNode = { mbid: "BB0100", member: { memberCode: "BB0100", name: "AINA TAN", rank: "OTTER", status: "Y" }, children: [ { mbid: "BB0101", member: { memberCode: "BB0101", name: "WEI MING LEE", rank: "SQUIRREL", status: "Y" }, children: [{ mbid: "BB0110", member: { memberCode: "BB0110", name: "NURUL" } }], }, { mbid: "BB0102", member: { memberCode: "BB0102", name: "SITI AMINAH", rank: "M", status: "S" } }, ], }; beforeEach(() => { mocks.apiGet.mockReset(); mocks.apiPost.mockReset(); }); describe("network tree reads", () => { it("fetches the tree rooted at the member code", async () => { mocks.apiGet.mockResolvedValue(root); const tree = await getNetworkTree("BB0100"); expect(tree.mbid).toBe("BB0100"); expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/network/BB0100?level=99"); }); it("fetches the upline chain", async () => { mocks.apiGet.mockResolvedValue([]); await getMemberUpline("BB0100"); expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/network/BB0100/upline"); }); it("fetches the placement history", async () => { mocks.apiGet.mockResolvedValue({ content: [], totalElements: 0 }); await getPlacementHistory("BB0100"); expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/members/BB0100/placement-history"); }); }); describe("changeSponsor", () => { it("posts the new sponsor code and the reason, and never an actor", async () => { mocks.apiPost.mockResolvedValue({ success: true }); await changeSponsor("BB0100", "BB0001", "Wrong referral code"); expect(mocks.apiPost).toHaveBeenCalledWith("/api/admin/members/BB0100/change-sponsor", { newSponsorCode: "BB0001", reason: "Wrong referral code", }); }); }); describe("downline counts", () => { it("direct = children; total = everyone beneath at any depth", () => { expect(directDownline(root)).toBe(2); expect(totalDownline(root)).toBe(3); expect(directDownline(root.children![0])).toBe(1); expect(totalDownline(root.children![0])).toBe(1); }); });