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

133 lines
5.2 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", () => mocks);
import {
listDeliveryOrders,
getDeliveryOrder,
markDeliveryOrderShipped,
markDeliveryOrderDelivered,
listToIssue,
getIssuePlan,
issueOrder,
} from "./adminDeliveryOrders";
beforeEach(() => {
mocks.apiGet.mockReset();
mocks.apiPost.mockReset();
});
describe("listDeliveryOrders", () => {
it("builds the query string and calls apiGet", async () => {
mocks.apiGet.mockResolvedValue({ content: [], number: 0, size: 20, totalElements: 0, totalPages: 0 });
await listDeliveryOrders({ wmsStatus: "P", dateFrom: "2026-01-01", dateTo: "2026-12-31", page: 0, size: 20 });
expect(mocks.apiGet).toHaveBeenCalledWith(
"/api/delivery-orders?wmsStatus=P&dateFrom=2026-01-01&dateTo=2026-12-31&page=0&size=20",
);
});
it("sends the DO number, order number and shipment status", async () => {
mocks.apiGet.mockResolvedValue({ content: [], number: 0, size: 20, totalElements: 0, totalPages: 0 });
await listDeliveryOrders({ deliveryOrderDocumentNumber: "ODO1", orderInvoiceNumber: "IN1", deliveryStatus: "SH" });
expect(mocks.apiGet).toHaveBeenCalledWith(
"/api/delivery-orders?deliveryStatus=SH&deliveryOrderDocumentNumber=ODO1&orderInvoiceNumber=IN1",
);
});
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 listDeliveryOrders();
expect(mocks.apiGet).toHaveBeenCalledWith("/api/delivery-orders");
});
});
describe("getDeliveryOrder", () => {
it("calls apiGet with the encoded uuid", async () => {
mocks.apiGet.mockResolvedValue({ uuid: "DO-1" });
await getDeliveryOrder("DO-1");
expect(mocks.apiGet).toHaveBeenCalledWith("/api/delivery-orders/DO-1");
});
});
describe("markDeliveryOrderShipped", () => {
it("posts the tracking number, courier and notify flag", async () => {
mocks.apiPost.mockResolvedValue({ uuid: "DO-1", deliveryStatus: "SH" });
await markDeliveryOrderShipped("DO-1", { consignmentNumber: "CN-123", courierCode: "JNT", notifyCustomer: false });
expect(mocks.apiPost).toHaveBeenCalledWith("/api/delivery-orders/DO-1/mark-shipped", {
consignmentNumber: "CN-123",
courierCode: "JNT",
notifyCustomer: false,
});
});
it("posts undefined tracking and courier when none is given", async () => {
mocks.apiPost.mockResolvedValue({ uuid: "DO-1", deliveryStatus: "SH" });
await markDeliveryOrderShipped("DO-1", { notifyCustomer: true });
expect(mocks.apiPost).toHaveBeenCalledWith("/api/delivery-orders/DO-1/mark-shipped", {
consignmentNumber: undefined,
courierCode: undefined,
notifyCustomer: true,
});
});
});
describe("markDeliveryOrderDelivered", () => {
it("posts to the mark-delivered route with no body", async () => {
mocks.apiPost.mockResolvedValue({ uuid: "DO-1", deliveryStatus: "DE" });
await markDeliveryOrderDelivered("DO-1");
expect(mocks.apiPost).toHaveBeenCalledWith("/api/delivery-orders/DO-1/mark-delivered");
});
});
describe("listToIssue", () => {
it("posts filters as property and the page as pageableRest", async () => {
mocks.apiPost.mockResolvedValue({ content: [], totalElements: 0 });
await listToIssue({ warehouseId: "WH-1", pickupOption: "true" }, 2, 20);
expect(mocks.apiPost).toHaveBeenCalledWith("/api/delivery-orders/to-issue", {
property: { warehouseId: "WH-1", pickupOption: "true" },
pageableRest: { number: 2, size: 20 },
});
});
});
describe("listToIssue sorting (BABYBOO-54 B3)", () => {
it("adds top-level sortBy and sortDir to the body when set", async () => {
mocks.apiPost.mockResolvedValue({ content: [], totalElements: 0 });
await listToIssue({}, 0, 20, { sortBy: "member", sortDir: "desc" });
expect(mocks.apiPost).toHaveBeenCalledWith("/api/delivery-orders/to-issue", {
property: {},
pageableRest: { number: 0, size: 20 },
sortBy: "member",
sortDir: "desc",
});
});
it("omits sortBy and sortDir when unsorted", async () => {
mocks.apiPost.mockResolvedValue({ content: [], totalElements: 0 });
await listToIssue({}, 0, 20);
const body = mocks.apiPost.mock.calls[0][1] as Record<string, unknown>;
expect("sortBy" in body).toBe(false);
expect("sortDir" in body).toBe(false);
});
});
describe("getIssuePlan", () => {
it("gets the encoded plan route", async () => {
mocks.apiGet.mockResolvedValue([]);
await getIssuePlan("O 1");
expect(mocks.apiGet).toHaveBeenCalledWith("/api/delivery-orders/to-issue/O%201/plan");
});
});
describe("issueOrder", () => {
it("sends quantities keyed by order line index with the request id", async () => {
mocks.apiPost.mockResolvedValue({ deliveryOrderUuids: [], deliveryOrderNumbers: [], issuedUnits: 3, outstandingUnits: 0 });
await issueOrder("O-1", "REQ-1", { 0: 2, 3: 1 });
expect(mocks.apiPost).toHaveBeenCalledWith("/api/delivery-orders/to-issue/O-1/issue", {
issueRequestId: "REQ-1",
quantities: { 0: 2, 3: 1 },
});
});
});