import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() })); vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi })); vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken })); import { POST } from "./route"; function postReq(body: unknown) { return new Request("http://localhost/api/admin/topup/reverse", { method: "POST", body: JSON.stringify(body), }) as unknown as Parameters[0]; } beforeEach(() => { mocks.callMobileApi.mockReset(); mocks.getToken.mockReset(); }); describe("POST /api/admin/topup/reverse", () => { it("requires a numeric uuid and a reason", async () => { mocks.getToken.mockResolvedValue("jwt-abc"); expect((await POST(postReq({ uuid: "555" }))).status).toBe(400); expect((await POST(postReq({ uuid: "x", reason: "r" }))).status).toBe(400); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("forwards uuid + trimmed reason as a POST to mobile-api with the admin JWT", async () => { mocks.getToken.mockResolvedValue("jwt-abc"); mocks.callMobileApi.mockResolvedValue({ ok: true, data: { uuid: "555", status: "R" } }); const res = await POST(postReq({ uuid: "555", reason: " Chargeback " })); expect(res.status).toBe(200); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/topup/reverse", { method: "POST", body: { uuid: "555", reason: "Chargeback" }, token: "jwt-abc", }); }); it("returns 401 without a session", async () => { mocks.getToken.mockResolvedValue(null); const res = await POST(postReq({ uuid: "555", reason: "r" })); expect(res.status).toBe(401); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); });