import { describe, expect, it } from "vitest";
import {
  convertDimInputValue,
  convertWeightInputValue,
  describeDimEquivalent,
  describeWeightEquivalent,
  displayUnitToCargoUnits,
} from "@/lib/frontend/cargo-form-units";
import {
  buildDimDisplayForUnit,
  inferDimUnitFromFieldHint,
} from "@/workers/rpa/cargo-dimensions";
import { kgToLb } from "@/modules/quote/unit-converter";

describe("cargo-form-units", () => {
  it("500 lb 切 kg 应约为 226.8", () => {
    const kg = convertWeightInputValue(500, "lb", "kg");
    expect(kg).toBeCloseTo(226.8, 1);
    expect(kgToLb(kg)).toBeCloseTo(500, 0);
  });

  it("500 kg 询价提示为 1103 lb", () => {
    expect(describeWeightEquivalent(500, "kg")).toBe(
      "询价将按 1103 lb（500 kg 换算，小数进一）",
    );
  });

  it("48 cm 询价提示为 19 in", () => {
    expect(describeDimEquivalent(48, 40, 48, "cm")).toBe(
      "询价将按 19 × 16 × 19 in（厘米换算，小数进一）",
    );
  });

  it("48 in 切 cm 应约为 121.92", () => {
    const cm = convertDimInputValue(48, "in", "cm");
    expect(cm).toBeCloseTo(121.92, 1);
  });

  it("公制显示同步 kg/cm", () => {
    expect(displayUnitToCargoUnits("metric")).toEqual({
      weight: "kg",
      dim: "cm",
    });
  });
});

describe("cargo-dimensions", () => {
  it("cm 字段填入换算值", () => {
    expect(inferDimUnitFromFieldHint("Length (cm)")).toBe("cm");
    expect(buildDimDisplayForUnit(48, "cm")).toBe("121.92");
  });

  it("in 字段填入整英寸", () => {
    expect(buildDimDisplayForUnit(47.24, "in")).toBe("48");
  });
});
