Files
co-lite/solvway-admin-portal/app/lib/services/bulletins.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

130 lines
4.5 KiB
TypeScript

import { describe, expect, it, vi, beforeEach } from "vitest";
import {
buildBulletinsQuery,
listBulletins,
getBulletin,
saveBulletin,
uploadBulletinImage,
AREA_OPTIONS,
} from "./bulletins";
describe("AREA_OPTIONS", () => {
it("covers all four bulletin areas", () => {
expect(AREA_OPTIONS.map((o) => o.value)).toEqual(["A", "M", "P", "K"]);
});
// localise-admin-content: labels are message codes, translated with t() where they render.
it("labels each area with a message code", () => {
expect(AREA_OPTIONS.map((o) => o.label)).toEqual(["Admin", "Member", "Public", "BltnAreaBcKp"]);
});
});
describe("buildBulletinsQuery", () => {
it("translates 1-based page to 0-based and includes filters", () => {
const qs = buildBulletinsQuery({ area: "A", status: "Y", page: 2, pageSize: 20 });
expect(qs).toBe("area=A&status=Y&page=1&size=20");
});
it("includes the type filter", () => {
expect(buildBulletinsQuery({ type: "P", page: 1, pageSize: 20 })).toBe("type=P&page=0&size=20");
});
it("omits absent filters", () => {
const qs = buildBulletinsQuery({ page: 1, pageSize: 20 });
expect(qs).toBe("page=0&size=20");
});
});
describe("listBulletins / getBulletin / saveBulletin", () => {
beforeEach(() => {
vi.resetAllMocks();
vi.stubGlobal("fetch", vi.fn());
});
it("listBulletins normalizes the upstream page shape", async () => {
(fetch as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(
new Response(JSON.stringify({ content: [{ id: 1, title: "A" }], totalElements: 1 }), { status: 200 }),
);
const result = await listBulletins({ page: 1, pageSize: 20 });
expect(result.items).toHaveLength(1);
expect(result.total).toBe(1);
});
it("getBulletin fetches by id", async () => {
(fetch as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(
new Response(JSON.stringify({ id: 5, title: "Found" }), { status: 200 }),
);
const result = await getBulletin(5);
expect(result.title).toBe("Found");
expect((fetch as unknown as ReturnType<typeof vi.fn>).mock.calls[0][0]).toBe("/api/bulletins/5");
});
it("saveBulletin posts to /api/bulletins when id is absent", async () => {
(fetch as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(
new Response(JSON.stringify({ id: 1 }), { status: 200 }),
);
await saveBulletin({
title: "New",
content: "",
dateFrom: "2026-01-01T00:00:00",
dateTo: "2026-01-02T00:00:00",
status: "Y",
type: "B",
areaList: ["A"],
countryPrefix: ["60"],
});
const call = (fetch as unknown as ReturnType<typeof vi.fn>).mock.calls[0];
expect(call[0]).toBe("/api/bulletins");
expect(call[1]?.method).toBe("POST");
});
it("saveBulletin puts to /api/bulletins/{id} when id is present", async () => {
(fetch as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(
new Response(JSON.stringify({ id: 9 }), { status: 200 }),
);
await saveBulletin({
id: 9,
title: "Edit",
content: "",
dateFrom: "2026-01-01T00:00:00",
dateTo: "2026-01-02T00:00:00",
status: "Y",
type: "B",
areaList: ["A"],
countryPrefix: ["60"],
});
const call = (fetch as unknown as ReturnType<typeof vi.fn>).mock.calls[0];
expect(call[0]).toBe("/api/bulletins/9");
expect(call[1]?.method).toBe("PUT");
});
it("uploadBulletinImage posts the base64 image to /api/bulletins/image and returns the url", async () => {
(fetch as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(
new Response(JSON.stringify({ url: "https://cdn/pub/bucket/img.png" }), { status: 200 }),
);
const result = await uploadBulletinImage({
fileName: "img.png",
fileType: "image/png",
base: "QUJD",
});
expect(result.url).toBe("https://cdn/pub/bucket/img.png");
const call = (fetch as unknown as ReturnType<typeof vi.fn>).mock.calls[0];
expect(call[0]).toBe("/api/bulletins/image");
expect(call[1]?.method).toBe("POST");
expect(JSON.parse(call[1]?.body as string)).toEqual({
fileName: "img.png",
fileType: "image/png",
base: "QUJD",
});
});
it("uploadBulletinImage throws ApiError on a 4xx response", async () => {
(fetch as unknown as ReturnType<typeof vi.fn>).mockResolvedValue(
new Response(JSON.stringify({ error: "Unsupported image type." }), { status: 400 }),
);
await expect(
uploadBulletinImage({ fileName: "a.txt", fileType: "text/plain", base: "QUJD" }),
).rejects.toThrow("Unsupported image type.");
});
});