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 { GET } from "./route"; beforeEach(() => { mocks.callMobileApi.mockReset(); mocks.getToken.mockReset(); }); describe("GET /api/voucher", () => { it("401s when not authenticated", async () => { mocks.getToken.mockResolvedValue(undefined); const res = await GET(); expect(res.status).toBe(401); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("forwards the token and returns the voucher list", async () => { mocks.getToken.mockResolvedValue("jwt"); mocks.callMobileApi.mockResolvedValue({ ok: true, data: [ { uuid: 1, name: "New First-Time User", amount: 10, redeemable: true, reason: null }, ], }); const res = await GET(); expect(res.status).toBe(200); expect(await res.json()).toEqual([ { uuid: 1, name: "New First-Time User", amount: 10, redeemable: true, reason: null }, ]); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/voucher/list", { token: "jwt" }); }); it("maps an upstream failure to a 400 error response", async () => { mocks.getToken.mockResolvedValue("jwt"); mocks.callMobileApi.mockResolvedValue({ ok: false, error: "Upstream failure" }); const res = await GET(); expect(res.status).toBe(400); expect((await res.json()).error).toBe("Upstream failure"); }); });