import { describe, expect, it } from "vitest"; import { cmToIn, inToCm, kgToLb, lbToKg, mToIn, tToLb, toAxelWholeInches, toAxelWholePounds, weightValueToLb, dimValueToIn, normalizeWeightUnit, normalizeDimUnit, weightUnitAmbiguousMessage } from "@/modules/quote/unit-converter"; describe("unit-converter", () => { it("227kg → 500.45lb(ROUND_HALF_UP,JS 浮点 227×2.20462)", () => { expect(kgToLb(227)).toBe(500.45); }); it("122cm → 48.03in", () => { expect(cmToIn(122)).toBe(48.03); }); it("1.2m → 47.24in", () => { expect(mToIn(1.2)).toBe(47.24); }); it("0.5t → 1102.31lb", () => { expect(tToLb(0.5)).toBe(1102.31); }); it("120cm 转 axel 尺寸向上取整为 48in", () => { expect(toAxelWholeInches(cmToIn(120))).toBe(48); }); it("1.2m 转 axel 尺寸向上取整为 48in", () => { expect(toAxelWholeInches(mToIn(1.2))).toBe(48); }); it("500kg 转 axel 重量向上取整为 1103lb", () => { expect(toAxelWholePounds(kgToLb(500))).toBe(1103); }); it("0.5t 转 axel 重量向上取整为 1103lb", () => { expect(toAxelWholePounds(tToLb(0.5))).toBe(1103); }); it("500lb 不变", () => { expect(kgToLb(500 / 2.20462)).toBeCloseTo(500, 0); }); it("lbToKg / inToCm 互逆", () => { expect(lbToKg(500)).toBe(226.8); expect(inToCm(48)).toBe(121.92); expect(kgToLb(lbToKg(500))).toBeCloseTo(500, 1); expect(cmToIn(inToCm(48))).toBeCloseTo(48, 1); }); it("normalizeWeightUnit 支持别名,拒绝 ton/tons 歧义", () => { expect(normalizeWeightUnit("LBS")).toBe("lb"); expect(normalizeWeightUnit("tonne")).toBe("t"); expect(normalizeWeightUnit("tonnes")).toBe("t"); expect(normalizeWeightUnit("t")).toBe("t"); expect(normalizeWeightUnit("ton")).toBeNull(); expect(normalizeWeightUnit("tons")).toBeNull(); expect(normalizeWeightUnit("stone")).toBeNull(); expect(weightUnitAmbiguousMessage("ton")).toMatch(/歧义/); }); it("normalizeDimUnit 支持别名", () => { expect(normalizeDimUnit("INCH")).toBe("in"); expect(normalizeDimUnit("meter")).toBe("m"); expect(normalizeDimUnit("CM")).toBe("cm"); }); it("weightValueToLb / dimValueToIn", () => { expect(weightValueToLb(500, "kg")).toBe(1102.31); expect(weightValueToLb(0.5, "t")).toBe(1102.31); expect(dimValueToIn(1.2, "m")).toBe(47.24); }); });