Files
co-lite/solvway-admin-portal/app/api/admin/event-summary/route.test.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

78 lines
4.0 KiB
TypeScript

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<typeof getEvents>[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);
});
});