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

189 lines
7.4 KiB
TypeScript

// @vitest-environment jsdom
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
const mocks = vi.hoisted(() => ({ apiGet: vi.fn() }));
vi.mock("@/app/lib/clientApi", () => ({
apiGet: mocks.apiGet,
ApiError: class ApiError extends Error {
status: number;
constructor(message: string, status: number) {
super(message);
this.status = status;
}
},
}));
import {
listWebOrders,
getWebOrder,
downloadInvoice,
orderTypeToFilter,
orderTypeLabel,
formatMoney,
ORDER_STATUS_LABEL,
ORDER_STATUS_TONE,
} from "./adminWebOrders";
import { ApiError } from "@/app/lib/clientApi";
import { tEn } from "@/test/i18nCatalog";
beforeEach(() => {
mocks.apiGet.mockReset();
});
describe("listWebOrders", () => {
it("builds the query string and calls apiGet", async () => {
mocks.apiGet.mockResolvedValue({ content: [], number: 0, size: 20, totalElements: 0, totalPages: 0 });
await listWebOrders({ dateFrom: "2026-01-01", dateTo: "2026-12-31", page: 0, size: 20 });
expect(mocks.apiGet).toHaveBeenCalledWith(
"/api/web-orders?page=0&size=20&dateFrom=2026-01-01&dateTo=2026-12-31",
);
});
it("forwards channel/status/search filters", async () => {
mocks.apiGet.mockResolvedValue({ content: [], number: 0, size: 20, totalElements: 0, totalPages: 0 });
await listWebOrders({
page: 0, size: 20,
saleChannel: "NORMAL", sourcePlatform: "CP", status: "Y",
documentNo: "IN0001", buyerCode: "MY10001",
});
expect(mocks.apiGet).toHaveBeenCalledWith(
"/api/web-orders?page=0&size=20&saleChannel=NORMAL&sourcePlatform=CP&status=Y&documentNo=IN0001&buyerCode=MY10001",
);
});
it("calls apiGet with no query string when no params are given", async () => {
mocks.apiGet.mockResolvedValue({ content: [], number: 0, size: 20, totalElements: 0, totalPages: 0 });
await listWebOrders();
expect(mocks.apiGet).toHaveBeenCalledWith("/api/web-orders");
});
});
describe("getWebOrder", () => {
it("GETs one order's detail by uuid", async () => {
const detail = { uuid: "O-1", documentNo: "IN0001", lines: [], payments: [] };
mocks.apiGet.mockResolvedValue(detail);
await expect(getWebOrder("O-1")).resolves.toBe(detail);
expect(mocks.apiGet).toHaveBeenCalledWith("/api/web-orders/O-1");
});
});
describe("orderTypeToFilter", () => {
it("maps Web to NORMAL + CP, Counter to NORMAL + BO", () => {
expect(orderTypeToFilter("WEB")).toEqual({ saleChannel: "NORMAL", sourcePlatform: "CP" });
expect(orderTypeToFilter("COUNTER")).toEqual({ saleChannel: "NORMAL", sourcePlatform: "BO" });
});
it("maps Event and Outlet to their sale channel only", () => {
expect(orderTypeToFilter("EVENT")).toEqual({ saleChannel: "EVENT" });
expect(orderTypeToFilter("OUTLET")).toEqual({ saleChannel: "OUTLET" });
});
});
describe("orderType mapping round trip", () => {
it.each(["WEB", "COUNTER", "EVENT", "OUTLET"] as const)("orderTypeOf inverts orderTypeToFilter for %s", async t => {
const { orderTypeOf } = await import("./adminWebOrders");
const f = orderTypeToFilter(t);
expect(orderTypeOf(f.saleChannel, f.sourcePlatform ?? "BO")).toBe(t);
});
it("classifies only NORMAL+CP/BO, case-insensitively", async () => {
const { orderTypeOf } = await import("./adminWebOrders");
expect(orderTypeOf("normal", "cp")).toBe("WEB");
expect(orderTypeOf("normal", "bo")).toBe("COUNTER");
expect(orderTypeOf("NORMAL", "AD")).toBeNull();
expect(orderTypeOf("NORMAL", "APP")).toBeNull();
expect(orderTypeOf("NORMAL", null)).toBeNull();
expect(orderTypeOf("NORMAL", undefined)).toBeNull();
});
});
describe("orderTypeLabel", () => {
it("derives the human label from saleChannel + sourcePlatform", () => {
expect(orderTypeLabel("NORMAL", "CP", tEn)).toBe("Web");
expect(orderTypeLabel("NORMAL", "BO", tEn)).toBe("Counter");
expect(orderTypeLabel("EVENT", "BO", tEn)).toBe("Event");
expect(orderTypeLabel("OUTLET", null, tEn)).toBe("Outlet");
});
it("falls back to the raw channel or a dash", () => {
expect(orderTypeLabel("WEIRD", "XX", tEn)).toBe("WEIRD");
expect(orderTypeLabel(null, null, tEn)).toBe("—");
});
});
describe("downloadInvoice", () => {
const originalFetch = global.fetch;
let clickSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
(URL as unknown as { createObjectURL: () => string }).createObjectURL = vi.fn(() => "blob:mock");
(URL as unknown as { revokeObjectURL: () => void }).revokeObjectURL = vi.fn();
clickSpy = vi.spyOn(HTMLAnchorElement.prototype, "click").mockImplementation(() => {});
});
afterEach(() => {
global.fetch = originalFetch;
clickSpy.mockRestore();
});
it("GETs the invoice route and triggers a browser download with the server filename", async () => {
const res = {
ok: true,
status: 200,
headers: {
get: (k: string) =>
k.toLowerCase() === "content-disposition"
? 'attachment; filename="invoice-IN00000021.pdf"'
: null,
},
blob: async () => new Blob(["%PDF"], { type: "application/pdf" }),
};
const fetchSpy = vi.fn().mockResolvedValue(res);
global.fetch = fetchSpy as unknown as typeof fetch;
await downloadInvoice("O-1");
expect(fetchSpy).toHaveBeenCalledWith("/api/web-orders/O-1/invoice", { cache: "no-store" });
expect(clickSpy).toHaveBeenCalledTimes(1);
expect((URL as unknown as { createObjectURL: ReturnType<typeof vi.fn> }).createObjectURL).toHaveBeenCalled();
});
it("throws ApiError(404) with a not-found message when the order/invoice is absent", async () => {
global.fetch = vi.fn().mockResolvedValue(new Response("", { status: 404 })) as unknown as typeof fetch;
await expect(downloadInvoice("NOPE")).rejects.toMatchObject({ status: 404 });
await expect(downloadInvoice("NOPE")).rejects.toBeInstanceOf(ApiError);
expect(clickSpy).not.toHaveBeenCalled();
});
});
describe("formatMoney / ORDER_STATUS_TONE", () => {
it("groups thousands and keeps the currency symbol", () => {
expect(formatMoney("RM", "1500.5")).toBe("RM 1,500.50");
expect(formatMoney(null, "3")).toBe("3.00");
expect(formatMoney("RM", "")).toBe("—");
});
it("tones every labelled status", () => {
expect(Object.keys(ORDER_STATUS_TONE).sort()).toEqual(Object.keys(ORDER_STATUS_LABEL).sort());
expect(ORDER_STATUS_TONE.Y).toBe("positive");
});
});
describe("order type and fulfilment tones", () => {
it("derives the type from saleChannel and sourcePlatform, and gives each type its own tone", async () => {
const { orderTypeOf, orderTypeTone } = await import("./adminWebOrders");
expect(orderTypeOf("NORMAL", "CP")).toBe("WEB");
expect(orderTypeOf("NORMAL", "BO")).toBe("COUNTER");
expect(orderTypeOf("EVENT", null)).toBe("EVENT");
expect(orderTypeOf("OUTLET", "BO")).toBe("OUTLET");
expect(orderTypeOf("MYSTERY", null)).toBeNull();
expect(orderTypeTone("NORMAL", "CP")).toBe("contrast");
expect(orderTypeTone("EVENT", null)).toBe("violet");
expect(orderTypeTone("OUTLET", null)).toBe("teal");
expect(orderTypeTone("NORMAL", "BO")).toBe("neutral");
expect(orderTypeTone("MYSTERY", null)).toBe("neutral");
});
it("tones fulfilment like the matching order type", async () => {
const { FULFILMENT_TONE } = await import("./adminWebOrders");
expect(FULFILMENT_TONE).toEqual({ DELIVERY: "contrast", PICKUP: "teal", TAKE_NOW: "neutral" });
});
});