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"; function getReq(search = "") { return new Request(`http://localhost/api/admin/dashboard/event${search}`) as unknown as Parameters[0]; } beforeEach(() => { mocks.callMobileApi.mockReset(); mocks.getToken.mockReset(); }); describe("GET /api/admin/dashboard/event", () => { it("forwards only the event id and the admin JWT (never an operator)", async () => { mocks.getToken.mockResolvedValue("jwt-abc"); mocks.callMobileApi.mockResolvedValue({ ok: true, data: { pendingOrders: 1 } }); const res = await GET(getReq("?eventId=7&operator=Ben")); expect(res.status).toBe(200); expect(await res.json()).toEqual({ pendingOrders: 1 }); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/dashboard/event?eventId=7", { token: "jwt-abc" }); }); it("is 401 without a session and never calls mobile-api", async () => { mocks.getToken.mockResolvedValue(null); const res = await GET(getReq("?eventId=7")); expect(res.status).toBe(401); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("is 400 for a missing or non-numeric event id", async () => { mocks.getToken.mockResolvedValue("jwt-abc"); expect((await GET(getReq())).status).toBe(400); expect((await GET(getReq("?eventId=7;drop"))).status).toBe(400); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("passes a downstream 403 through", async () => { mocks.getToken.mockResolvedValue("jwt-abc"); mocks.callMobileApi.mockResolvedValue({ ok: false, status: 403, error: "The BO_DASHBOARD_EVENT module is required." }); const res = await GET(getReq("?eventId=7")); expect(res.status).toBe(403); }); });