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${search}`) as unknown as Parameters[0]; } beforeEach(() => { mocks.callMobileApi.mockReset(); mocks.getToken.mockReset(); }); describe("GET /api/admin/dashboard", () => { it("forwards only the documented params and the admin JWT", async () => { mocks.getToken.mockResolvedValue("jwt-abc"); mocks.callMobileApi.mockResolvedValue({ ok: true, data: { ok: 1 } }); const res = await GET(getReq("?from=2026-09-01&to=2026-09-29&evil=1")); expect(res.status).toBe(200); expect(await res.json()).toEqual({ ok: 1 }); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/dashboard?from=2026-09-01&to=2026-09-29", { token: "jwt-abc" }); }); it("is 401 without a session and never calls mobile-api", async () => { mocks.getToken.mockResolvedValue(null); const res = await GET(getReq()); expect(res.status).toBe(401); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("is 400 for a malformed date", async () => { mocks.getToken.mockResolvedValue("jwt-abc"); const res = await GET(getReq("?from=yesterday")); expect(res.status).toBe(400); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("passes an upstream refusal through", async () => { mocks.getToken.mockResolvedValue("jwt-abc"); mocks.callMobileApi.mockResolvedValue({ ok: false, error: "forbidden", status: 403 }); const res = await GET(getReq()); expect(res.status).toBe(403); }); });