Files
co-lite/solvway-admin-portal/app/lib/services/notifications.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
3.2 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({
apiGet: vi.fn(),
apiPost: vi.fn(),
}));
vi.mock("@/app/lib/clientApi", () => ({
apiGet: mocks.apiGet,
apiPost: mocks.apiPost,
}));
import {
buildNotificationsQuery,
normalizeNotificationList,
listNotifications,
getUnreadCount,
markRead,
markAllRead,
broadcast,
} from "./notifications";
import type { AdminNotification } from "@/app/lib/types";
beforeEach(() => {
mocks.apiGet.mockReset();
mocks.apiPost.mockReset();
});
describe("buildNotificationsQuery", () => {
it("converts the 1-based portal page to a 0-based upstream page", () => {
expect(buildNotificationsQuery({ page: 1, pageSize: 10 })).toBe("page=0&size=10");
expect(buildNotificationsQuery({ page: 3, pageSize: 20 })).toBe("page=2&size=20");
});
});
describe("normalizeNotificationList", () => {
const item: AdminNotification = { id: 1, message: "hi", seen: false };
it("accepts a plain array (the upstream /notification/inbox shape)", () => {
expect(normalizeNotificationList([item])).toEqual({ items: [item], total: 1 });
});
it("accepts a paged {content, totalElements} shape defensively", () => {
expect(normalizeNotificationList({ content: [item], totalElements: 3 })).toEqual({
items: [item],
total: 3,
});
});
it("defaults to an empty list for anything else", () => {
expect(normalizeNotificationList(null)).toEqual({ items: [], total: 0 });
expect(normalizeNotificationList("nope")).toEqual({ items: [], total: 0 });
});
});
describe("listNotifications", () => {
it("GETs the portal route and normalizes the result", async () => {
const item: AdminNotification = { id: 2, message: "x", seen: true };
mocks.apiGet.mockResolvedValue([item]);
const result = await listNotifications({ page: 1, pageSize: 10 });
expect(mocks.apiGet).toHaveBeenCalledWith("/api/notifications?page=0&size=10");
expect(result).toEqual({ items: [item], total: 1 });
});
});
describe("getUnreadCount", () => {
it("GETs unread-count and returns the count", async () => {
mocks.apiGet.mockResolvedValue({ count: 4 });
await expect(getUnreadCount()).resolves.toBe(4);
expect(mocks.apiGet).toHaveBeenCalledWith("/api/notifications/unread-count");
});
});
describe("markRead", () => {
it("POSTs to the item's read route", async () => {
mocks.apiPost.mockResolvedValue({ updated: 1 });
await markRead(42);
expect(mocks.apiPost).toHaveBeenCalledWith("/api/notifications/42/read");
});
});
describe("markAllRead", () => {
it("POSTs to the read-all route", async () => {
mocks.apiPost.mockResolvedValue({ marked: 3 });
await markAllRead();
expect(mocks.apiPost).toHaveBeenCalledWith("/api/notifications/read-all");
});
});
describe("broadcast", () => {
it("POSTs the broadcast payload to the portal route and returns the result", async () => {
mocks.apiPost.mockResolvedValue({ sent: 2 });
const payload = { message: "Hello", type: "promo", target: { kind: "ALL_MEMBERS" } as const };
const result = await broadcast(payload);
expect(mocks.apiPost).toHaveBeenCalledWith("/api/notifications", payload);
expect(result).toEqual({ sent: 2 });
});
});