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/countries", () => { it("returns 401 when there is no session", async () => { mocks.getToken.mockResolvedValue(null); const res = await GET(); expect(res.status).toBe(401); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("proxies /api/public/countries with the JWT and returns the list unmodified", async () => { mocks.getToken.mockResolvedValue("jwt-abc"); const list = [{ code: "MY", name: "Malaysia", currency: "MYR" }, { code: "SG", name: "Singapore", currency: "SGD" }]; mocks.callMobileApi.mockResolvedValue({ ok: true, data: list }); const res = await GET(); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/public/countries", { token: "jwt-abc" }); expect(res.status).toBe(200); expect(await res.json()).toEqual(list); }); it("maps an upstream failure to an error response", async () => { mocks.getToken.mockResolvedValue("jwt-abc"); mocks.callMobileApi.mockResolvedValue({ ok: false, error: "boom" }); const res = await GET(); expect(res.status).toBe(400); expect((await res.json()).error).toBe("boom"); }); });