import { describe, expect, it } from "vitest"; import { parseCargoValueUsd, sanitizeCargoValueUsdInput, validateMsCheckoutSelection, } from "@/lib/mothership/ms-checkout-selection"; import { pickLoggedInRateCardIndex } from "@/workers/rpa/mothership-logged-in-quote"; describe("ms checkout selection", () => { it("指定承运商优先", () => { const items = [ { carrier: "TForce Direct", rawTotal: 500 }, { carrier: "ABF Direct", rawTotal: 600 }, { carrier: "Roadrunner Interline", rawTotal: 416.89 }, ]; expect(pickLoggedInRateCardIndex(items, "Roadrunner")).toBe(2); expect(pickLoggedInRateCardIndex(items, "ABF")).toBe(1); expect(pickLoggedInRateCardIndex(items)).toBe(2); // 最低价 }); it("basic 无需货值", () => { expect( validateMsCheckoutSelection({ preferredCarrier: "ABF Direct", coverage: "basic", }), ).toBeNull(); }); it("freight_protect 缺货值 / 非法货值", () => { expect( validateMsCheckoutSelection({ preferredCarrier: "ABF", coverage: "freight_protect", }), ).toMatch(/货物价值/); expect( validateMsCheckoutSelection({ preferredCarrier: "ABF", coverage: "freight_protect", cargoValueUsd: 0, }), ).toMatch(/货物价值/); }); it("freight_protect 合法货值通过", () => { expect( validateMsCheckoutSelection({ preferredCarrier: "Roadrunner Interline", coverage: "freight_protect", cargoValueUsd: 5000, }), ).toBeNull(); }); it("parseCargoValueUsd 兼容 $ 与逗号,并保留小数", () => { expect(parseCargoValueUsd("$5,000")).toBe(5000); expect(parseCargoValueUsd("300.55")).toBe(300.55); expect(parseCargoValueUsd("300.556")).toBe(300.56); expect(parseCargoValueUsd(" ")).toBeNull(); expect(parseCargoValueUsd("-1")).toBeNull(); }); it("sanitizeCargoValueUsdInput 允许输入小数", () => { expect(sanitizeCargoValueUsdInput("300.")).toBe("300."); expect(sanitizeCargoValueUsdInput("300.5")).toBe("300.5"); expect(sanitizeCargoValueUsdInput("300.555")).toBe("300.55"); expect(sanitizeCargoValueUsdInput("$1,200.3")).toBe("1200.3"); }); it("freight_protect 小数货值通过", () => { expect( validateMsCheckoutSelection({ preferredCarrier: "ABF", coverage: "freight_protect", cargoValueUsd: 300.55, }), ).toBeNull(); }); it("未选承运商", () => { expect( validateMsCheckoutSelection({ preferredCarrier: " ", coverage: "basic", }), ).toMatch(/承运商/); }); it("承运商过长 / 货值超上限", () => { expect( validateMsCheckoutSelection({ preferredCarrier: "x".repeat(129), coverage: "basic", }), ).toMatch(/过长/); expect( validateMsCheckoutSelection({ preferredCarrier: "ABF", coverage: "freight_protect", cargoValueUsd: 1_000_001, }), ).toMatch(/超出允许范围/); }); });