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

119 lines
4.7 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", async () => ({
...(await vi.importActual<typeof import("@/app/lib/clientApi")>("@/app/lib/clientApi")),
apiGet: mocks.apiGet,
apiPost: mocks.apiPost,
}));
import { ApiError } from "@/app/lib/clientApi";
import { adjustWallet, getAllWalletLedger, getMemberWalletBalances, getMemberWalletLedger } from "./wallet";
const input = {
memberCode: "MY10001",
walletCode: "ACORN" as const,
direction: "OUT" as const,
amount: 10,
reason: "Correction",
refNo: "ref-9",
};
beforeEach(() => {
mocks.apiGet.mockReset();
mocks.apiPost.mockReset();
});
function emptyPage() {
return { content: [], number: 0, size: 10, totalElements: 0, totalPages: 0 };
}
describe("getAllWalletLedger", () => {
it("forwards page/size and each filter to the BFF route, expanding the category to a code set", async () => {
mocks.apiGet.mockResolvedValue(emptyPage());
await getAllWalletLedger(2, 25, {
category: "SALES",
from: "2026-08-01",
to: "2026-08-31",
memberCode: "MY10001",
});
expect(mocks.apiGet).toHaveBeenCalledWith(
"/api/admin/wallet/ledger?page=1&size=25&types=SLI%2CSLO&from=2026-08-01&to=2026-08-31&memberCode=MY10001",
);
});
it("sends the reference filter as refNo, trimmed (BABYBOO-54)", async () => {
mocks.apiGet.mockResolvedValue(emptyPage());
await getAllWalletLedger(1, 10, { refNo: " 3299434405991206912 " } as never);
expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/wallet/ledger?page=0&size=10&refNo=3299434405991206912");
});
it("omits the type filter for an unknown/absent category", async () => {
mocks.apiGet.mockResolvedValue(emptyPage());
await getAllWalletLedger(1, 10, { category: "all" });
expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/wallet/ledger?page=0&size=10");
});
it("omits empty filters and keeps 1-based paging", async () => {
mocks.apiGet.mockResolvedValue(emptyPage());
await getAllWalletLedger(1, 10, {});
expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/wallet/ledger?page=0&size=10");
});
it("trims whitespace around the exact member code", async () => {
mocks.apiGet.mockResolvedValue(emptyPage());
await getAllWalletLedger(1, 10, { memberCode: " MY10001 " });
expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/wallet/ledger?page=0&size=10&memberCode=MY10001");
});
it("maps the wire displayWalletName to the portal walletName", async () => {
mocks.apiGet.mockResolvedValue({
content: [
{ refNo: "R1", trxnType: "TPU", displayWalletName: "Ewallet", memberCode: "MY10001" },
{ refNo: "R2", trxnType: "SLO" },
],
number: 0,
size: 10,
totalElements: 2,
totalPages: 1,
});
const result = await getAllWalletLedger(1, 10, {});
expect(result.totalElements).toBe(2);
expect(result.content[0]).toMatchObject({ refNo: "R1", walletName: "Ewallet", memberCode: "MY10001" });
expect(result.content[1]).toMatchObject({ refNo: "R2", walletName: undefined });
});
});
describe("getMemberWalletBalances / getMemberWalletLedger (existing reads unchanged)", () => {
it("getMemberWalletBalances hits the member balances route", async () => {
mocks.apiGet.mockResolvedValue([]);
await getMemberWalletBalances("MY10001");
expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/wallet/MY10001/balances");
});
it("getMemberWalletLedger hits the member ledger route with 1-based paging", async () => {
mocks.apiGet.mockResolvedValue(emptyPage());
await getMemberWalletLedger("MY10001", 3, 10);
expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/wallet/MY10001/ledger?page=2&size=10");
});
});
describe("adjustWallet (BABYBOO-54)", () => {
it("POSTs the body to the BFF and returns the result", async () => {
const res = { refNo: "ref-9", trxnType: "AJO", amount: 10, balance: 90 };
mocks.apiPost.mockResolvedValue(res);
await expect(adjustWallet(input)).resolves.toEqual(res);
expect(mocks.apiPost).toHaveBeenCalledWith("/api/admin/wallet/adjust", input);
});
it("surfaces a 409 INSUFFICIENT_BALANCE as the WalletAdjInsufficient error", async () => {
mocks.apiPost.mockRejectedValue(new ApiError("Insufficient balance", 409, { code: "INSUFFICIENT_BALANCE" }));
await expect(adjustWallet(input)).rejects.toMatchObject({ message: "WalletAdjInsufficient" });
});
it("does not disguise other failures as insufficient", async () => {
mocks.apiPost.mockRejectedValue(new ApiError("reason is required", 400));
await expect(adjustWallet(input)).rejects.toMatchObject({ message: "reason is required" });
});
});