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; 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 }, }); }); });