import { describe, expect, it } from "vitest"; import { addressLooksResidential, buildLoggedInAddressSearchQuery, buildMsCheckoutDetailsDefaults, formatLoggedInReadyGridCell, formatLoggedInReadyGridCellWithYear, isMsCheckoutPaymentButtonLabel, isMsCheckoutPaymentPageText, MOTHERSHIP_CREATE_SHIPMENT_URL, parseLoggedInRateCardText, pickLoggedInRateCardIndex, } from "@/workers/rpa/mothership-logged-in-quote"; import { filterQuotesForMotherShipUiDisplay } from "@/lib/constants/mothership-ui-tiers"; import { validateQuoteSchema } from "@/workers/rpa/quote-capture/quote-schema-validator"; import type { QuoteRequest } from "@/modules/providers/quote-provider"; describe("MOTHERSHIP_CREATE_SHIPMENT_URL", () => { it("登录态一级表单直达 /ship(避免菜单文案超时)", () => { expect(MOTHERSHIP_CREATE_SHIPMENT_URL).toBe( "https://dashboard.mothership.com/ship", ); }); }); describe("parseLoggedInRateCardText", () => { it("解析 ABF Direct 卡片文本", () => { const item = parseLoggedInRateCardText( "Selected ABF Direct $835.61 Estimated 5 business days", ); expect(item?.carrier).toBe("ABF Direct"); expect(item?.rawTotal).toBe(835.61); expect(item?.transitDays).toBe("5"); expect(item?.serviceLevel).toBe("standard"); }); it("解析 XPO Direct", () => { const item = parseLoggedInRateCardText( "XPO Direct $1,015.28 Estimated 4 business days", ); expect(item?.carrier).toBe("XPO Direct"); expect(item?.rawTotal).toBe(1015.28); }); it("解析区间时效 5-7 business days", () => { const item = parseLoggedInRateCardText( "SAIA Direct $900.00 5-7 business days", ); expect(item?.transitDays).toBe("5-7"); }); it("无时效文案时用待确认,且通过 schema 校验(避免 standard/bestValue 时效为空)", () => { const item = parseLoggedInRateCardText("Daylight Direct $733.98"); expect(item?.transitDays).toBe("待确认"); expect(item?.rateOption).toBe("bestValue"); expect(validateQuoteSchema([item!])).toHaveLength(1); }); }); describe("formatLoggedInReadyGridCell", () => { it("ISO 日期转为 gridcell 名", () => { expect(formatLoggedInReadyGridCell("2026-07-16")).toBe("Thu Jul 16"); }); it("带年份日格匹配零填充日", () => { const re = formatLoggedInReadyGridCellWithYear("2026-08-05"); expect(re.test("Wed Aug 05 2026")).toBe(true); expect(re.test("Wed Aug 5 2026")).toBe(true); expect(re.test("Wed Jul 05 2026")).toBe(false); }); }); describe("buildLoggedInAddressSearchQuery", () => { it("优先用 street/city/state/zip,避开 Places description 的 floor/suite", () => { const q = buildLoggedInAddressSearchQuery({ street: "2020 West Washington Boulevard", city: "Los Angeles", state: "CA", zip: "", placeId: "ChIJ-test", formattedAddress: "2020 West Washington Boulevard floor 5 rm 508, Los Angeles, CA, USA", selectedFromSuggestions: true, mothershipOptionId: "ChIJ-test", mothershipDisplayLabel: "2020 West Washington Boulevard floor 5 rm 508, Los Angeles, CA, USA", selectedFromMothership: true, }); expect(q).toBe("2020 West Washington Boulevard, Los Angeles, CA"); expect(q).not.toMatch(/floor|rm 508/i); }); it("ste/suite 也会从 street 剥离", () => { const q = buildLoggedInAddressSearchQuery({ street: "555 Market St ste 1001", city: "San Francisco", state: "CA", zip: "94105", placeId: "ChIJ-sf", formattedAddress: "555 Market St ste 1001, San Francisco, CA, USA", selectedFromSuggestions: true, mothershipOptionId: "ChIJ-sf", mothershipDisplayLabel: "555 Market St ste 1001, San Francisco, CA, USA", selectedFromMothership: true, }); expect(q).toBe("555 Market St, San Francisco, CA, 94105"); expect(q).not.toMatch(/ste|1001/i); }); }); describe("filterQuotesForMotherShipUiDisplay 多承运商", () => { it("同档不同承运商均保留", () => { const ui = filterQuotesForMotherShipUiDisplay([ { service_level: "standard", rate_option: "bestValue", carrier: "ABF Direct", raw: 835, }, { service_level: "standard", rate_option: "bestValue", carrier: "XPO Direct", raw: 1015, }, ]); expect(ui).toHaveLength(2); }); }); describe("checkout helpers", () => { it("支付按钮黑名单", () => { expect(isMsCheckoutPaymentButtonLabel("Pay now")).toBe(true); expect(isMsCheckoutPaymentButtonLabel("Place order")).toBe(true); expect(isMsCheckoutPaymentButtonLabel("Complete payment")).toBe(true); expect(isMsCheckoutPaymentButtonLabel("Add payment method")).toBe(true); expect(isMsCheckoutPaymentButtonLabel("Agree")).toBe(false); expect(isMsCheckoutPaymentButtonLabel("Proceed to checkout")).toBe(false); }); it("支付页正文识别", () => { expect(isMsCheckoutPaymentPageText("Add payment method\nCard number")).toBe( true, ); expect( isMsCheckoutPaymentPageText("Review your shipment\nAgree to terms"), ).toBe(false); }); it("选价:hint 优先,否则最低价", () => { const items = [ { carrier: "ABF Direct", rawTotal: 888.67 }, { carrier: "SAIA Direct", rawTotal: 1051.01 }, { carrier: "Old Dominion", rawTotal: 1803.19 }, ]; expect(pickLoggedInRateCardIndex(items)).toBe(0); expect(pickLoggedInRateCardIndex(items, "SAIA")).toBe(1); expect(pickLoggedInRateCardIndex(items, "no-match")).toBe(0); expect(pickLoggedInRateCardIndex([])).toBe(-1); }); it("Details 默认值含双侧姓名与分邮箱", () => { const req = { cargoHash: "t", pickup: { street: "a", city: "b", state: "CA", zip: "90001", placeId: "p", formattedAddress: "a", selectedFromSuggestions: true, }, delivery: { street: "c", city: "d", state: "TX", zip: "75201", placeId: "d", formattedAddress: "c", selectedFromSuggestions: true, }, weightLb: 100, dimsIn: { l: 48, w: 40, h: 48 }, palletCount: 2, cargoType: "general_freight", } satisfies QuoteRequest; const d = buildMsCheckoutDetailsDefaults(req); expect(d.pickupFirst).toBe("Ops"); expect(d.deliveryLast).toBe("Receiver"); expect(d.pickupEmail).not.toBe(d.deliveryEmail); expect(d.pieceCountQty).toBe("2"); expect(d.cargoDescription).toBe("General freight pallets"); expect(d.pickupReference).toBe("PO-PICKUP-001"); expect(d.deliveryNotes).toContain("Receiver"); }); }); describe("addressLooksResidential", () => { const base = { city: "Columbus", state: "OH", zip: "43217", placeId: "x", formattedAddress: "", selectedFromSuggestions: true, }; it("商业 ste/suite 地址不算住宅(与官网一致,可直接出价)", () => { expect( addressLooksResidential({ ...base, street: "6600 Don Eisele Rd ste 2", formattedAddress: "6600 Don Eisele Rd ste 2, Columbus, OH 43217, USA", mothershipDisplayLabel: "6600 Don Eisele Rd ste 2, Columbus, OH 43217, USA", }), ).toBe(false); expect( addressLooksResidential({ ...base, street: "555 Market St Suite 1001", city: "San Francisco", state: "CA", zip: "94105", }), ).toBe(false); }); it("明确 apt/apartment 才视为住宅", () => { expect( addressLooksResidential({ ...base, street: "100 Main St Apt 4B", }), ).toBe(true); expect( addressLooksResidential({ ...base, street: "100 Main St Apartment 2", }), ).toBe(true); }); });