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

98 lines
3.9 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn(), ApiError: class ApiError extends Error { status: number; data?: unknown; constructor(message: string, status: number, data?: unknown) { super(message); this.name = "ApiError"; this.status = status; this.data = data; } } }));
vi.mock("@/app/lib/clientApi", () => ({
apiGet: mocks.apiGet,
apiPost: mocks.apiPost,
ApiError: mocks.ApiError,
}));
import { approveRefund, getRefund, listRefunds, retryRefundEmail, rejectRefund } from "./adminRefunds";
beforeEach(() => {
mocks.apiGet.mockReset();
mocks.apiPost.mockReset();
});
describe("listRefunds", () => {
it("GETs the list route with status and pagination", async () => {
const page = { content: [], totalElements: 0, page: 0, size: 20, pendingCount: 0 };
mocks.apiGet.mockResolvedValue(page);
await expect(listRefunds({ status: "P" })).resolves.toBe(page);
expect(mocks.apiGet).toHaveBeenCalledWith("/api/refunds?status=P&page=0&size=20");
});
it("omits empty text filters from the query, but still sets default page/size", async () => {
mocks.apiGet.mockResolvedValue({ content: [], totalElements: 0, page: 0, size: 20, pendingCount: 0 });
await listRefunds({ requestNo: "", invoiceNo: "", member: "" });
expect(mocks.apiGet).toHaveBeenCalledWith("/api/refunds?page=0&size=20");
});
it("forwards all filter params", async () => {
mocks.apiGet.mockResolvedValue({ content: [], totalElements: 0, page: 0, size: 20, pendingCount: 0 });
await listRefunds({
status: "P", requestNo: "RR0001", invoiceNo: "IN0002", member: "MY6217232",
from: "2024-01-01", to: "2024-12-31", page: 1, size: 10,
});
expect(mocks.apiGet).toHaveBeenCalledWith(
"/api/refunds?status=P&requestNo=RR0001&invoiceNo=IN0002&member=MY6217232&from=2024-01-01&to=2024-12-31&page=1&size=10",
);
});
});
describe("getRefund", () => {
it("GETs the detail route", async () => {
const detail = { request: {}, buyerName: null, orderDate: null, grandTotal: 0, shippingCost: 0, tenders: [], stockEffects: [] };
mocks.apiGet.mockResolvedValue(detail);
await expect(getRefund("R-1")).resolves.toBe(detail);
expect(mocks.apiGet).toHaveBeenCalledWith("/api/refunds/R-1");
});
it("passes through a genuine upstream 404 as ApiError", async () => {
mocks.apiGet.mockRejectedValue(new mocks.ApiError("Not found", 404));
await expect(getRefund("NOPE")).rejects.toThrow("Not found");
});
});
describe("approveRefund", () => {
it("POSTs the uuid (a mutation — never a GET)", async () => {
const row = { uuid: "R-1", status: "A" };
mocks.apiPost.mockResolvedValue(row);
await expect(approveRefund("R-1")).resolves.toBe(row);
expect(mocks.apiPost).toHaveBeenCalledWith("/api/refunds/R-1/approve", {});
});
it("drops a doneBy field from the payload", async () => {
const row = { uuid: "R-1", status: "A" };
mocks.apiPost.mockResolvedValue(row);
// Even if note has doneBy, the service strips it
await expect(approveRefund("R-1", { note: "OK" })).resolves.toBe(row);
expect(mocks.apiPost).toHaveBeenCalledWith("/api/refunds/R-1/approve", { note: "OK" });
});
});
describe("rejectRefund", () => {
it("POSTs the reason (a mutation — never a GET)", async () => {
const row = { uuid: "R-1", status: "R" };
mocks.apiPost.mockResolvedValue(row);
await expect(rejectRefund("R-1", "Duplicate")).resolves.toBe(row);
expect(mocks.apiPost).toHaveBeenCalledWith("/api/refunds/R-1/reject", { reason: "Duplicate" });
});
});
describe("retryRefundEmail", () => {
it("POSTs an empty body to retryEmail (a mutation — never a GET)", async () => {
const result = { uuid: "R-1", emailSentAt: "2024-01-01" };
mocks.apiPost.mockResolvedValue(result);
await expect(retryRefundEmail("R-1")).resolves.toBe(result);
expect(mocks.apiPost).toHaveBeenCalledWith("/api/refunds/R-1/retry-email", {});
});
});