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

84 lines
3.8 KiB
TypeScript

import { describe, it, expect, vi } from "vitest";
import { apiGet, apiPost } from "@/app/lib/clientApi";
import { listCurrency, getCurrencyHistory, createCurrency, buildCurrencyQuery } from "./currency";
vi.mock("@/app/lib/clientApi");
describe("currency service", () => {
it("buildCurrencyQuery requests the full window once (pagination is client-side)", () => {
const qs = buildCurrencyQuery({ type: "TRX", page: 2, pageSize: 20 });
expect(qs).toBe("type=TRX&page=0&size=500");
});
it("buildCurrencyQuery includes status when present", () => {
const qs = buildCurrencyQuery({ type: "TRX", status: "N", page: 1, pageSize: 20 });
expect(qs).toBe("type=TRX&status=N&page=0&size=500");
});
it("buildCurrencyQuery includes searchKey when present", () => {
const qs = buildCurrencyQuery({ type: "BNS", searchKey: "MY", page: 1, pageSize: 20 });
expect(qs).toBe("type=BNS&searchKey=MY&page=0&size=500");
});
it("listCurrency calls /api/currency with the built query", async () => {
vi.mocked(apiGet).mockResolvedValue({ content: [], totalElements: 0 });
await listCurrency({ type: "BNS", page: 1, pageSize: 20 });
expect(apiGet).toHaveBeenCalledWith("/api/currency?type=BNS&page=0&size=500");
});
it("listCurrency normalizes the upstream page into items/total", async () => {
const rows = [
{ id: 1, countryCode: "MY", currencySymbol: "MYR", type: "TRX", rate: 1, effectiveTime: "2026-01-01T00:00:00", status: "Y" },
];
vi.mocked(apiGet).mockResolvedValue({ content: rows, totalElements: 1 });
const result = await listCurrency({ type: "TRX", page: 1, pageSize: 20 });
expect(result).toEqual({ items: rows, total: 1 });
});
it("filters the upstream window by type and paginates the filtered set with no empty pages", async () => {
const trx = (id: number) => ({
id,
countryCode: "MY",
currencySymbol: "MYR",
type: "TRX" as const,
rate: id,
effectiveTime: "2026-01-01T00:00:00",
status: "Y" as const,
});
const bns = (id: number) => ({ ...trx(id), id: id + 100, type: "BNS" as const });
// One upstream window mixing both families (the old server-side pagination
// could strand BNS rows on later pages; the client filter + pagination must
// not).
const mixed = [trx(1), bns(1), trx(2), bns(2), trx(3)];
vi.mocked(apiGet).mockResolvedValue({ content: mixed, totalElements: mixed.length });
const page1 = await listCurrency({ type: "TRX", page: 1, pageSize: 2 });
expect(page1.items.map((r) => r.type)).toEqual(["TRX", "TRX"]);
expect(page1.items.some((r) => r.type === "BNS")).toBe(false);
expect(page1.total).toBe(3); // 3 TRX rows, regardless of the 5-row window
const page2 = await listCurrency({ type: "TRX", page: 2, pageSize: 2 });
expect(page2.items.map((r) => r.type)).toEqual(["TRX"]);
expect(page2.items.some((r) => r.type === "BNS")).toBe(false);
expect(page2.total).toBe(3);
// A page beyond the filtered set is empty, but total caps the pages offered,
// so the pagination never presents a zero-row page in the middle.
const page3 = await listCurrency({ type: "TRX", page: 3, pageSize: 2 });
expect(page3.items).toEqual([]);
expect(page3.total).toBe(3);
});
it("getCurrencyHistory calls the history route", async () => {
vi.mocked(apiGet).mockResolvedValue([]);
await getCurrencyHistory("MY", "TRX");
expect(apiGet).toHaveBeenCalledWith("/api/currency/history/MY/TRX");
});
it("createCurrency posts to /api/currency", async () => {
vi.mocked(apiPost).mockResolvedValue({ id: 1 });
await createCurrency({ countryCode: "MY", currencySymbol: "MYR", type: "TRX", rate: 1, effectiveTime: "2026-01-01T00:00:00", status: "Y" });
expect(apiPost).toHaveBeenCalledWith("/api/currency", expect.objectContaining({ countryCode: "MY" }));
});
});