import { describe, expect, it } from "vitest";
import {
  filterQuotesForMotherShipUiDisplay,
  getMotherShipRateOptionLabel,
  getMotherShipServiceLevelLabel,
  listMotherShipUiTabsFromQuotes,
  listVisibleRateOptionsForTab,
  normalizeMotherShipServiceLevel,
} from "@/lib/constants/mothership-ui-tiers";

const SAMPLE = [
  { service_level: "standard", rate_option: "lowest", raw: 235 },
  { service_level: "standard", rate_option: "bestValue", raw: 301 },
  { service_level: "standard", rate_option: "fastest", raw: 689 },
  { service_level: "guaranteed", rate_option: "lowest", raw: 408 },
  { service_level: "guaranteed", rate_option: "fastest", raw: 830 },
  { service_level: "dedicated", rate_option: "bestValue", raw: 4989 },
] as const;

describe("mothership-ui-tiers", () => {
  it("normalizeMotherShipServiceLevel 识别专属卡车别名", () => {
    expect(normalizeMotherShipServiceLevel("专属卡车")).toBe("dedicated");
    expect(normalizeMotherShipServiceLevel("FTL")).toBe("dedicated");
  });

  it("Standard 档 lowest 与 bestValue 并存时均保留（与官网三档一致）", () => {
    const ui = filterQuotesForMotherShipUiDisplay([...SAMPLE]);
    expect(ui.some((q) => q.service_level === "standard" && q.rate_option === "lowest")).toBe(true);
    expect(ui.some((q) => q.service_level === "standard" && q.rate_option === "bestValue")).toBe(true);
    expect(ui.find((q) => q.service_level === "standard" && q.rate_option === "lowest")?.raw).toBe(235);
  });

  it("listMotherShipUiTabsFromQuotes 含 dedicated 第三 Tab", () => {
    const ui = filterQuotesForMotherShipUiDisplay([...SAMPLE]);
    expect(listMotherShipUiTabsFromQuotes(ui)).toEqual([
      "standard",
      "guaranteed",
      "dedicated",
    ]);
  });

  it("dedicated Tab 展示 API 实际返回的子档（不白名单裁剪）", () => {
    const ui = filterQuotesForMotherShipUiDisplay([...SAMPLE]);
    expect(listVisibleRateOptionsForTab(ui, "dedicated")).toEqual(["bestValue"]);
    const withExtra = filterQuotesForMotherShipUiDisplay([
      ...SAMPLE,
      { service_level: "dedicated", rate_option: "lowest", raw: 4000 },
    ]);
    expect(listVisibleRateOptionsForTab(withExtra, "dedicated")).toEqual([
      "bestValue",
      "lowest",
    ]);
  });

  it("bestValue 显示为最优性价比", () => {
    expect(getMotherShipRateOptionLabel("standard", "bestValue")).toBe("最优性价比");
    expect(getMotherShipRateOptionLabel("standard", "lowest")).toBe("最低价格");
  });
});
