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
192 lines
6.9 KiB
TypeScript
192 lines
6.9 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
apiGet: vi.fn(),
|
|
apiPost: vi.fn(),
|
|
apiPut: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/clientApi", () => ({
|
|
apiGet: mocks.apiGet,
|
|
apiPost: mocks.apiPost,
|
|
apiPut: mocks.apiPut,
|
|
}));
|
|
|
|
import {
|
|
buildMessagesQuery,
|
|
normalizeMessagePage,
|
|
listMessages,
|
|
getMessage,
|
|
saveMessage,
|
|
createMessage,
|
|
updateMessage,
|
|
exportMessages,
|
|
importMessages,
|
|
AREA_OPTIONS,
|
|
} from "./messages";
|
|
|
|
beforeEach(() => {
|
|
mocks.apiGet.mockReset();
|
|
mocks.apiPost.mockReset();
|
|
mocks.apiPut.mockReset();
|
|
});
|
|
|
|
describe("buildMessagesQuery", () => {
|
|
it("includes missingLocale when present", () => {
|
|
expect(buildMessagesQuery({ missingLocale: "zh_CN", page: 1, pageSize: 20 })).toContain(
|
|
"missingLocale=zh_CN",
|
|
);
|
|
});
|
|
|
|
it("builds a 0-based page/size query with no filters", () => {
|
|
expect(buildMessagesQuery({ page: 1, pageSize: 20 })).toBe("page=0&size=20");
|
|
});
|
|
|
|
it("includes searchKey when present", () => {
|
|
expect(buildMessagesQuery({ searchKey: "Welcome", page: 1, pageSize: 20 })).toBe(
|
|
"searchKey=Welcome&page=0&size=20",
|
|
);
|
|
});
|
|
|
|
it("includes area when present", () => {
|
|
expect(buildMessagesQuery({ area: "AP", page: 1, pageSize: 20 })).toBe("area=AP&page=0&size=20");
|
|
});
|
|
|
|
it("includes both searchKey and area together, and converts a later page to 0-based", () => {
|
|
expect(buildMessagesQuery({ searchKey: "Hi", area: "A", page: 3, pageSize: 10 })).toBe(
|
|
"searchKey=Hi&area=A&page=2&size=10",
|
|
);
|
|
});
|
|
|
|
it("omits an empty/undefined searchKey and area", () => {
|
|
expect(buildMessagesQuery({ searchKey: "", area: undefined, page: 1, pageSize: 20 })).toBe(
|
|
"page=0&size=20",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("normalizeMessagePage", () => {
|
|
it("reads Spring Data Page's content/totalElements shape", () => {
|
|
const page = {
|
|
content: [{ id: 1, code: "Welcome", content: "{}", area: "A" }],
|
|
totalElements: 42,
|
|
};
|
|
expect(normalizeMessagePage(page)).toEqual({ items: page.content, total: 42 });
|
|
});
|
|
|
|
it("falls back to items/total for a simpler shape", () => {
|
|
const page = { items: [{ id: 1, code: "Welcome", content: "{}", area: "A" }], total: 7 };
|
|
expect(normalizeMessagePage(page)).toEqual({ items: page.items, total: 7 });
|
|
});
|
|
|
|
it("returns an empty page for null/non-object input", () => {
|
|
expect(normalizeMessagePage(null)).toEqual({ items: [], total: 0 });
|
|
expect(normalizeMessagePage(undefined)).toEqual({ items: [], total: 0 });
|
|
expect(normalizeMessagePage("oops")).toEqual({ items: [], total: 0 });
|
|
});
|
|
|
|
it("falls back total to the items length when no total field is present", () => {
|
|
const page = { content: [{ id: 1, code: "A", content: "{}", area: "A" }] };
|
|
expect(normalizeMessagePage(page)).toEqual({ items: page.content, total: 1 });
|
|
});
|
|
});
|
|
|
|
describe("listMessages", () => {
|
|
it("calls GET /api/messages with the built query and normalizes the result", async () => {
|
|
mocks.apiGet.mockResolvedValue({ content: [], totalElements: 0 });
|
|
const result = await listMessages({ searchKey: "Hi", area: "A", page: 2, pageSize: 20 });
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/messages?searchKey=Hi&area=A&page=1&size=20");
|
|
expect(result).toEqual({ items: [], total: 0 });
|
|
});
|
|
});
|
|
|
|
describe("getMessage", () => {
|
|
it("calls GET /api/messages/{id}", async () => {
|
|
const message = { id: 5, code: "Welcome", content: "{}", area: "A" };
|
|
mocks.apiGet.mockResolvedValue(message);
|
|
expect(await getMessage(5)).toEqual(message);
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/messages/5");
|
|
});
|
|
});
|
|
|
|
describe("saveMessage", () => {
|
|
it("calls POST /api/messages with the message payload", async () => {
|
|
const payload = { code: "Welcome", content: "{}", area: "A" as const };
|
|
mocks.apiPost.mockResolvedValue({ ...payload, id: 9 });
|
|
const result = await saveMessage(payload);
|
|
expect(mocks.apiPost).toHaveBeenCalledWith("/api/messages", payload);
|
|
expect(result).toEqual({ ...payload, id: 9 });
|
|
});
|
|
});
|
|
|
|
describe("createMessage", () => {
|
|
it("always POSTs to /api/messages without an id", async () => {
|
|
const payload = { code: "New", content: "{}", area: "A" as const };
|
|
mocks.apiPost.mockResolvedValue({ ...payload, id: 10 });
|
|
const result = await createMessage(payload);
|
|
expect(mocks.apiPost).toHaveBeenCalledWith("/api/messages", payload);
|
|
expect(result).toEqual({ ...payload, id: 10 });
|
|
});
|
|
});
|
|
|
|
describe("updateMessage", () => {
|
|
it("PUTs to the record's own URL with the id in the path, not the body", async () => {
|
|
const payload = { code: "Welcome", content: '{"en_US":"Hi"}', area: "A" as const };
|
|
mocks.apiPut.mockResolvedValue({ ...payload, id: 5 });
|
|
const result = await updateMessage(5, payload);
|
|
expect(mocks.apiPut).toHaveBeenCalledWith("/api/messages/5", payload);
|
|
expect(result).toEqual({ ...payload, id: 5 });
|
|
});
|
|
});
|
|
|
|
describe("exportMessages", () => {
|
|
it("GETs the export route for the requested area", async () => {
|
|
const rows = [{ id: 1, code: "Welcome", content: "{}", area: "A" as const }];
|
|
mocks.apiGet.mockResolvedValue(rows);
|
|
const result = await exportMessages("A");
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/messages/export?area=A");
|
|
expect(result).toEqual(rows);
|
|
});
|
|
|
|
it("without an area exports both A and AP, concatenated", async () => {
|
|
mocks.apiGet.mockImplementation(async (url: string) =>
|
|
url.endsWith("area=A") ? [{ code: "X", area: "A" }] : [{ code: "Y", area: "AP" }],
|
|
);
|
|
const result = await exportMessages();
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/messages/export?area=A");
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/messages/export?area=AP");
|
|
expect(result.map((r) => r.code)).toEqual(["X", "Y"]);
|
|
});
|
|
});
|
|
|
|
describe("importMessages", () => {
|
|
it("POSTs the rows to /api/messages/import and returns the summary", async () => {
|
|
const rows = [{ code: "New", content: "{}", area: "A" as const }];
|
|
const summary = { created: 1, updated: 0, errors: [] };
|
|
mocks.apiPost.mockResolvedValue(summary);
|
|
const result = await importMessages(rows);
|
|
expect(mocks.apiPost).toHaveBeenCalledWith("/api/messages/import", rows);
|
|
expect(result).toEqual(summary);
|
|
});
|
|
});
|
|
|
|
describe("AREA_OPTIONS", () => {
|
|
it("offers only the Admin and Customer portal areas", () => {
|
|
expect(AREA_OPTIONS).toEqual([
|
|
{ value: "A", label: "Admin" },
|
|
{ value: "AP", label: "MsgsAreaCustomerPortal" },
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("areaLabel", () => {
|
|
it("maps area codes to labels and passes unknown codes through", async () => {
|
|
const { areaLabel } = await import("./messages");
|
|
const { resolveMessage } = await import("@/app/lib/i18n/t");
|
|
// localise-admin-shell: areaLabel takes t; an empty catalog renders the English defaults.
|
|
const en = (code: string) => resolveMessage({}, code);
|
|
expect(areaLabel("AP", en)).toBe("Customer portal");
|
|
expect(areaLabel("M", en)).toBe("M");
|
|
expect(areaLabel("ZZ", en)).toBe("ZZ");
|
|
});
|
|
});
|