import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; const mocks = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn() })); vi.mock("@/app/lib/clientApi", async () => { const actual = await vi.importActual("@/app/lib/clientApi"); return { ApiError: actual.ApiError, apiGet: mocks.apiGet, apiPost: mocks.apiPost }; }); import * as s from "./adminFulfilment"; import { ApiError } from "@/app/lib/clientApi"; const fetchMock = vi.fn(); beforeEach(() => { mocks.apiGet.mockReset().mockResolvedValue({}); mocks.apiPost.mockReset().mockResolvedValue({}); fetchMock.mockReset(); vi.stubGlobal("fetch", fetchMock); }); afterEach(() => vi.unstubAllGlobals()); describe("adminFulfilment client (add-manual-fulfilment-console)", () => { it("creates a batch from ticked rows or from the filter", async () => { await s.createBatch("DELIVER", { uuids: ["a", "b"] }); expect(mocks.apiPost).toHaveBeenLastCalledWith("/api/fulfilment/batches", { type: "DELIVER", uuids: ["a", "b"] }); await s.createBatch("READY", { filter: { warehouseId: "42", deliveryStatus: "SH" } }); expect(mocks.apiPost).toHaveBeenLastCalledWith("/api/fulfilment/batches", { type: "READY", filter: { warehouseId: "42", deliveryStatus: "SH" } }); }); it("creates a tracking batch", async () => { const rows = [{ row: 2, doNumber: "D1", courierCode: "JNT", trackingNumber: "T1" }]; await s.createTrackingBatch(rows, true); expect(mocks.apiPost).toHaveBeenLastCalledWith("/api/fulfilment/batches/tracking", { rows, correctExisting: true }); }); it("reads a batch and its failures", async () => { await s.getBatch(7); await s.getBatchFailures(7); expect(mocks.apiGet.mock.calls.map(c => c[0])).toEqual(["/api/fulfilment/batches/7", "/api/fulfilment/batches/7/failures"]); }); it("exports courier rows for a selection", async () => { mocks.apiPost.mockResolvedValue({ rows: [], skippedDrafts: 1 }); expect(await s.exportForCourier({ uuids: ["a"] })).toEqual({ rows: [], skippedDrafts: 1 }); expect(mocks.apiPost).toHaveBeenLastCalledWith("/api/fulfilment/export", { uuids: ["a"] }); }); it("prints: posts the selection and returns the PDF blob plus the skipped-drafts count", async () => { fetchMock.mockResolvedValue(new Response(new Uint8Array([1, 2]), { status: 200, headers: { "content-type": "application/pdf", "x-skipped-drafts": "3" }, })); const r = await s.printDeliveryOrders({ filter: { warehouseId: "42" } }); const [url, init] = fetchMock.mock.calls[0]; expect(url).toBe("/api/fulfilment/print"); expect(init.method).toBe("POST"); expect(JSON.parse(init.body)).toEqual({ filter: { warehouseId: "42" } }); expect(r.skippedDrafts).toBe(3); expect(r.pdf).toBeInstanceOf(Blob); expect(r.pdf.size).toBe(2); }); it("print throws an ApiError carrying the route's message", async () => { fetchMock.mockResolvedValue(new Response(JSON.stringify({ error: "Print at most 200 delivery orders at a time" }), { status: 400 })); await expect(s.printDeliveryOrders({ uuids: ["a"] })).rejects.toThrow(ApiError); fetchMock.mockResolvedValue(new Response(JSON.stringify({ error: "Print at most 200 delivery orders at a time" }), { status: 400 })); await expect(s.printDeliveryOrders({ uuids: ["a"] })).rejects.toThrow("Print at most 200 delivery orders at a time"); }); });