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
70 lines
3.4 KiB
TypeScript
70 lines
3.4 KiB
TypeScript
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<typeof import("@/app/lib/clientApi")>("@/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");
|
|
});
|
|
});
|