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.
chajia/__tests__/lib/flock/flock-checkout-selection.te...

193 lines
5.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { describe, expect, it } from "vitest";
import {
pickLowestRateOptionIndex,
pickFlexibilityOptionIndex,
pickCarrierOptionIndex,
parseFlockFlexibilityKey,
parseFlockRateUsd,
enrichFlexibilityOption,
enrichCarrierOption,
validateFlockCheckoutSelection,
} from "@/lib/flock/flock-checkout-selection";
import {
buildFlockCheckoutDefaults,
isFlockCheckoutPaymentButtonLabel,
isFlockCheckoutPaymentPageText,
} from "@/workers/rpa/flock/checkout";
describe("validateFlockCheckoutSelection", () => {
it("要求 flock_direct 或 standard,且必须选灵活性或承运商", () => {
expect(
validateFlockCheckoutSelection({
preferredTier: "flock_direct",
preferredFlexibility: "2_day",
}),
).toBeNull();
expect(
validateFlockCheckoutSelection({
preferredTier: "standard",
preferredFlexibility: "none",
}),
).toBeNull();
expect(
validateFlockCheckoutSelection({
preferredTier: "standard",
preferredCarrier: "Forward Air",
}),
).toBeNull();
expect(
validateFlockCheckoutSelection({
preferredTier: "other" as "flock_direct",
preferredFlexibility: "2_day",
}),
).toMatch(/请选择/);
expect(
validateFlockCheckoutSelection({ preferredTier: "flock_direct" }),
).toMatch(/灵活性|承运商/);
expect(
validateFlockCheckoutSelection({
preferredTier: "standard",
preferredFlexibility: "2_day",
preferredCarrier: "Forward Air",
}),
).toMatch(/不能同时/);
});
it("接受 preferred_flexibility", () => {
expect(
validateFlockCheckoutSelection({
preferredTier: "flock_direct",
preferredFlexibility: "2_day",
}),
).toBeNull();
});
});
describe("pickLowestRateOptionIndex", () => {
it("选最低价;同价取首项;空列表 -1", () => {
expect(pickLowestRateOptionIndex([])).toBe(-1);
expect(
pickLowestRateOptionIndex([
{ key: "2_day", label: "a", rateUsd: 2068 },
{ key: "1_day", label: "b", rateUsd: 2034 },
{ key: "none", label: "c", rateUsd: 2166 },
]),
).toBe(1);
expect(
pickLowestRateOptionIndex([
{ key: "2_day", label: "a", rateUsd: 100 },
{ key: "1_day", label: "b", rateUsd: 100 },
]),
).toBe(0);
});
});
describe("pickFlexibilityOptionIndex", () => {
it("按 key 选档,找不到则回落最低价", () => {
const opts = [
{ key: "2_day" as const, label: "2-day", rateUsd: 1200 },
{ key: "1_day" as const, label: "1-day", rateUsd: 1250 },
{ key: "none" as const, label: "No", rateUsd: 1300 },
];
expect(pickFlexibilityOptionIndex(opts, "1_day")).toBe(1);
expect(pickFlexibilityOptionIndex(opts, null)).toBe(0);
});
});
describe("parseFlockFlexibilityKey / enrich", () => {
it("从文案解析 key", () => {
expect(parseFlockFlexibilityKey("2-day flexibility Pickup Jul 30")).toBe(
"2_day",
);
expect(parseFlockFlexibilityKey("No flexibility")).toBe("none");
expect(
enrichFlexibilityOption({
label: "1-day flexibility Pickup Jul 30 Deliver by Aug 3 $1,250.00",
rateUsd: 1250,
})?.key,
).toBe("1_day");
});
});
describe("parseFlockRateUsd", () => {
it("解析美元金额", () => {
expect(parseFlockRateUsd("$2,034.00")).toBe(2034);
expect(parseFlockRateUsd("Prices from $1,271")).toBe(1271);
expect(parseFlockRateUsd("no money")).toBeNull();
});
});
describe("flock checkout payment guards", () => {
it("黑名单按钮与支付页文案", () => {
expect(isFlockCheckoutPaymentButtonLabel("Complete your order")).toBe(
true,
);
expect(isFlockCheckoutPaymentButtonLabel("Yes, I want this rate!")).toBe(
false,
);
expect(
isFlockCheckoutPaymentPageText("Complete your order — Pay with card"),
).toBe(true);
});
});
describe("enrichCarrierOption / pickCarrierOptionIndex", () => {
it("从承运商行文案解析字段", () => {
const opt = enrichCarrierOption({
label:
"Forward Air 1 business days Jul 28 – Jul 29 $555.14 Select",
rateUsd: 555.14,
});
expect(opt?.carrierName).toMatch(/Forward Air/i);
expect(opt?.transitDays).toMatch(/1 business/i);
expect(opt?.rateUsd).toBe(555.14);
});
it("按承运商名选档,找不到回落最低价", () => {
const opts = [
{
carrierName: "Forward Air",
transitDays: "1 business days",
rateUsd: 555,
label: "a",
},
{
carrierName: "Roadrunner",
transitDays: "2 business days",
rateUsd: 500,
label: "b",
},
];
expect(pickCarrierOptionIndex(opts, "Roadrunner")).toBe(1);
expect(pickCarrierOptionIndex(opts, null)).toBe(1);
});
});
describe("buildFlockCheckoutDefaults", () => {
it("合并 flock_details 覆盖", () => {
const d = buildFlockCheckoutDefaults({
pickupZip: "90001",
deliveryZip: "75201",
tag: "ab12",
details: {
pickup: { company_name: "Acme Pickup", contact_email: "a@x.com" },
delivery: { company_name: "Acme Del" },
po_number: "PO-99",
declaration_statement: "call dock",
documentation_required: true,
use_billing_for_delivery: true,
},
});
expect(d.pickupCompany).toBe("Acme Pickup");
expect(d.pickupEmail).toBe("a@x.com");
expect(d.deliveryPo).toBe("PO-99");
expect(d.declarationStatement).toBe("call dock");
expect(d.pickupZip).toBe("90001");
expect(d.nmfc).toBeTruthy();
expect(d.documentationRequired).toBe(true);
expect(d.useBillingForPickup).toBe(true);
expect(d.useBillingForDelivery).toBe(true);
expect(d.deliveryPhone).not.toMatch(/555/);
});
});