import { describe, it, expect, vi, beforeEach } from "vitest"; // add-event-summary: the two Event Summary BFF routes are read-only pass-throughs to mobile-api. 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 as getEvents } from "./events/route"; import { GET as getSummary } from "./[eventId]/route"; const req = (path: string) => new Request(`http://localhost${path}`) as unknown as Parameters[0]; const ctx = (eventId: string) => ({ params: Promise.resolve({ eventId }) }); beforeEach(() => { mocks.callMobileApi.mockReset(); mocks.getToken.mockReset().mockResolvedValue("jwt-abc"); }); describe("GET /api/admin/event-summary/events", () => { it("forwards to mobile-api with the admin JWT", async () => { mocks.callMobileApi.mockResolvedValue({ ok: true, data: [{ id: "5" }] }); const res = await getEvents(req("/api/admin/event-summary/events")); expect(res.status).toBe(200); expect(await res.json()).toEqual([{ id: "5" }]); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/event-summary/events", { token: "jwt-abc" }); }); it("is 401 without a session", async () => { mocks.getToken.mockResolvedValue(null); expect((await getEvents(req("/api/admin/event-summary/events"))).status).toBe(401); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); }); describe("GET /api/admin/event-summary/[eventId]", () => { it("forwards the event id and the day", async () => { mocks.callMobileApi.mockResolvedValue({ ok: true, data: { orders: 3 } }); const res = await getSummary(req("/api/admin/event-summary/5?date=2026-10-06&x=1"), ctx("5")); expect(res.status).toBe(200); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/event-summary/5?date=2026-10-06", { token: "jwt-abc" }); }); it("forwards without a day when none is given", async () => { mocks.callMobileApi.mockResolvedValue({ ok: true, data: {} }); await getSummary(req("/api/admin/event-summary/5"), ctx("5")); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/event-summary/5", { token: "jwt-abc" }); }); it("is 400 for a non-numeric id or a malformed day, without calling mobile-api", async () => { expect((await getSummary(req("/api/admin/event-summary/5;x"), ctx("5;x"))).status).toBe(400); expect((await getSummary(req("/api/admin/event-summary/5?date=06-10-2026"), ctx("5"))).status).toBe(400); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("forwards a Date From / Date To range", async () => { // BABYBOO-39 mocks.callMobileApi.mockResolvedValue({ ok: true, data: {} }); await getSummary(req("/api/admin/event-summary/5?from=2026-10-14&to=2026-10-16&x=1"), ctx("5")); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/event-summary/5?from=2026-10-14&to=2026-10-16", { token: "jwt-abc" }); }); it("forwards from, to and the older date together", async () => { mocks.callMobileApi.mockResolvedValue({ ok: true, data: {} }); await getSummary(req("/api/admin/event-summary/5?to=2026-10-16&date=2026-10-14&from=2026-10-14"), ctx("5")); expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/event-summary/5?date=2026-10-14&from=2026-10-14&to=2026-10-16", { token: "jwt-abc" }); }); it("is 400 for a malformed from or to, without calling mobile-api", async () => { expect((await getSummary(req("/api/admin/event-summary/5?from=14-10-2026"), ctx("5"))).status).toBe(400); expect((await getSummary(req("/api/admin/event-summary/5?from=2026-10-14&to=2026-10"), ctx("5"))).status).toBe(400); expect(mocks.callMobileApi).not.toHaveBeenCalled(); }); it("passes a downstream 403 through", async () => { mocks.callMobileApi.mockResolvedValue({ ok: false, status: 403, error: "The BO_EVENT_SUMMARY module is required." }); expect((await getSummary(req("/api/admin/event-summary/5"), ctx("5"))).status).toBe(403); }); });