Files
co-lite/solvway-admin-portal/app/api/admin/member-messages/routes.broadcasts.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

107 lines
5.0 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import type { NextRequest } from "next/server";
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 listBroadcasts } from "./broadcasts/route";
import { GET as summary } from "./broadcasts/[broadcastUuid]/route";
import { GET as recipients } from "./broadcasts/[broadcastUuid]/recipients/route";
// BABYBOO-56 broadcast read count: read-only BFF relays.
const B = "http://localhost/api/admin/member-messages";
const req = (url: string) => new Request(url, { method: "GET" }) as unknown as NextRequest;
const ctx = (broadcastUuid: string) => ({ params: Promise.resolve({ broadcastUuid }) });
const lastCall = () => {
const [url, opts] = mocks.callMobileApi.mock.calls.at(-1)!;
const [path, qs = ""] = String(url).split("?");
return { path, sp: new URLSearchParams(qs), opts };
};
beforeEach(() => {
mocks.callMobileApi.mockReset().mockResolvedValue({ ok: true, data: { fine: 1 } });
mocks.getToken.mockReset().mockResolvedValue("jwt");
process.env.MOBILE_API_BASE_URL = "http://up";
process.env.MOBILE_API_KEY = "k";
});
describe("GET broadcasts summaries relay", () => {
it("forwards q, page and size to mobile-api with the session token", async () => {
const res = await listBroadcasts(req(`${B}/broadcasts?q=raya&page=1&size=20`));
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ fine: 1 });
const { path, sp, opts } = lastCall();
expect(path).toBe("/api/admin/member-messages/broadcasts");
expect([sp.get("q"), sp.get("page"), sp.get("size")]).toEqual(["raya", "1", "20"]);
expect(opts).toEqual(expect.objectContaining({ method: "GET", token: "jwt" }));
});
it("returns 401 with no session and never calls upstream", async () => {
mocks.getToken.mockResolvedValue(null);
expect((await listBroadcasts(req(`${B}/broadcasts?page=0&size=20`))).status).toBe(401);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("maps an upstream failure to an error response", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "Something odd", status: 500 });
expect((await listBroadcasts(req(`${B}/broadcasts?page=0&size=20`))).status).toBeGreaterThanOrEqual(400);
});
});
describe("GET broadcast recipients relay", () => {
it("forwards the uuid, unreadOnly, page and size", async () => {
const res = await recipients(req(`${B}/broadcasts/b1/recipients?unreadOnly=true&page=2&size=20`), ctx("b1"));
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ fine: 1 });
const { path, sp, opts } = lastCall();
expect(path).toBe("/api/admin/member-messages/broadcasts/b1/recipients");
expect([sp.get("unreadOnly"), sp.get("page"), sp.get("size")]).toEqual(["true", "2", "20"]);
expect(opts).toEqual(expect.objectContaining({ method: "GET", token: "jwt" }));
});
it("returns 401 with no session and never calls upstream", async () => {
mocks.getToken.mockResolvedValue(null);
expect((await recipients(req(`${B}/broadcasts/b1/recipients`), ctx("b1"))).status).toBe(401);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("rejects a path-like broadcast id before any upstream call", async () => {
const res = await recipients(req(`${B}/broadcasts/x/recipients`), ctx("../admin"));
expect(res.status).toBe(400);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("passes an upstream 404 through", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "Broadcast not found", status: 404 });
expect((await recipients(req(`${B}/broadcasts/b1/recipients`), ctx("b1"))).status).toBe(404);
});
});
describe("GET single broadcast summary relay", () => {
it("forwards the uuid to mobile-api with the session token", async () => {
const res = await summary(req(`${B}/broadcasts/b1`), ctx("b1"));
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ fine: 1 });
const { path, opts } = lastCall();
expect(path).toBe("/api/admin/member-messages/broadcasts/b1");
expect(opts).toEqual(expect.objectContaining({ method: "GET", token: "jwt" }));
});
it("returns 401 with no session and never calls upstream", async () => {
mocks.getToken.mockResolvedValue(null);
expect((await summary(req(`${B}/broadcasts/b1`), ctx("b1"))).status).toBe(401);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("rejects a path-like broadcast id with 400 before any upstream call", async () => {
expect((await summary(req(`${B}/broadcasts/x`), ctx("../admin"))).status).toBe(400);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("passes an upstream 404 through", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "Broadcast not found", status: 404 });
expect((await summary(req(`${B}/broadcasts/b1`), ctx("b1"))).status).toBe(404);
});
});