You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.0 KiB

import { beforeEach, describe, expect, it, vi } from "vitest";
import {
mapShipmentRows,
normalizeFbaCodeForCc,
saveContainer,
} from "@/services/cc/save-container";
import { fillShipmentFbaCodes } from "@/services/parse/normalize-fba-code";
import {
resetMockCcState,
setMockConflictContainers,
} from "@/services/cc/mock-state";
import { checkContainerConflict } from "@/services/cc/conflict";
vi.mock("@/services/cc/settings-config", async (importOriginal) => {
const actual =
await importOriginal<typeof import("@/services/cc/settings-config")>();
return {
...actual,
isCcMock: async () => true,
};
});
describe("cc mock", () => {
beforeEach(() => {
process.env.CC_MOCK = "true";
process.env.DATABASE_URL =
process.env.DATABASE_URL ||
"mysql://app:app@localhost:3306/email_forecast";
resetMockCcState();
});
it("SaveContainer returns ok with external id", async () => {
const res = await saveContainer({
entity: {
F_TransMode: 0,
F_OperationType: 0,
F_ContainerNo: "MATU2745683",
F_Classis: 0,
},
shipments: [
{
row_index: 1,
row_status: "VALID",
F_FBACode: "ABQ2",
F_Transporter: "TRUCK",
F_CTNS: 1,
},
{
row_index: 2,
row_status: "VALID",
F_FBACode: "FTW1",
F_Transporter: "TRUCK",
F_CTNS: 4,
},
],
});
expect(res.ok).toBe(true);
if (res.ok) expect(res.externalId).toBeTruthy();
});
it("uses courier name as FBACode when UPS/FEDEX/DHL/USPS have no warehouse", () => {
expect(normalizeFbaCodeForCc("", "UPS")).toBe("UPS");
expect(normalizeFbaCodeForCc("", "FEDEX")).toBe("FEDEX");
expect(normalizeFbaCodeForCc("", "DHL")).toBe("DHL");
expect(normalizeFbaCodeForCc("", "USPS")).toBe("USPS");
expect(normalizeFbaCodeForCc("ups", "UPS")).toBe("UPS");
expect(normalizeFbaCodeForCc("", "TRUCK")).toBe("");
expect(normalizeFbaCodeForCc("", "留仓")).toBe("HOLD");
expect(normalizeFbaCodeForCc("LAS1", "UPS")).toBe("LAS1");
const rows = mapShipmentRows([
{
row_index: 60,
row_status: "VALID",
F_FBACode: "",
F_Transporter: "UPS",
F_CTNS: 2,
},
]);
expect(rows[0].F_FBACode).toBe("UPS");
expect(rows[0].F_Transporter).toBe("UPS");
const displayed = fillShipmentFbaCodes([
{ row_index: 60, F_FBACode: "", F_Transporter: "UPS" },
{ row_index: 61, F_FBACode: "", F_Transporter: "FEDEX" },
{ row_index: 62, F_FBACode: "LAS1", F_Transporter: "TRUCK" },
]) as Array<{ F_FBACode: string }>;
expect(displayed.map((r) => r.F_FBACode)).toEqual(["UPS", "FEDEX", "LAS1"]);
});
it("conflict when container in mock list", async () => {
setMockConflictContainers(["MATU2745683"]);
const r = await checkContainerConflict({
containerNo: "MATU2745683",
mailId: 1n,
});
expect(r.ok).toBe(false);
if (!r.ok) expect(r.code).toBe("CONFLICT_CONTAINER");
});
});