import { describe, expect, it } from "vitest"; import { buildFlockSavedFreightPreset, filterFlockSavedFreight, flockSavedFreightStorageKey, parseFlockSavedFreightList, removeFlockSavedFreight, upsertFlockSavedFreight, type FlockSavedFreightPreset, } from "@/lib/flock/saved-freight-presets"; const baseSource = { description: "家电木托", quantity: "4", packagingType: "pallets_48x40", lengthIn: "48", widthIn: "40", heightIn: "48", totalWeightLb: "1200", freightClass: "70", stackable: true, turnable: false, }; function preset(partial: Partial): FlockSavedFreightPreset { return { id: "id-1", name: "家电", description: "家电木托", quantity: "4", packagingType: "pallets_48x40", lengthIn: "48", widthIn: "40", heightIn: "48", totalWeightLb: "1200", freightClass: "70", stackable: true, turnable: false, updatedAt: "2026-07-15T00:00:00.000Z", ...partial, }; } describe("saved-freight-presets", () => { it("storage key 按客户隔离", () => { expect(flockSavedFreightStorageKey("CUST_001")).toContain("CUST_001"); expect(flockSavedFreightStorageKey(" ")).toContain("anon"); }); it("build 用描述作默认名称", () => { const p = buildFlockSavedFreightPreset(baseSource, ""); expect(p?.name).toBe("家电木托"); expect(p?.description).toBe("家电木托"); }); it("空描述且空名称拒绝", () => { expect( buildFlockSavedFreightPreset({ ...baseSource, description: "" }, " "), ).toBeNull(); }); it("同名覆盖且新条目置顶", () => { const a = preset({ id: "a", name: "木托A" }); const b = preset({ id: "b", name: "木托B" }); const next = upsertFlockSavedFreight( [a, b], preset({ id: "c", name: "木托A", description: "新描述" }), ); expect(next).toHaveLength(2); expect(next[0]?.description).toBe("新描述"); expect(next[0]?.name).toBe("木托A"); }); it("按名称或描述过滤", () => { const list = [ preset({ id: "1", name: "家电托", description: "电视" }), preset({ id: "2", name: "建材", description: "瓷砖" }), ]; expect(filterFlockSavedFreight(list, "瓷")).toHaveLength(1); expect(filterFlockSavedFreight(list, "家电")).toHaveLength(1); }); it("删除与解析容错", () => { const list = [preset({ id: "x" }), preset({ id: "y", name: "Y" })]; expect(removeFlockSavedFreight(list, "x")).toHaveLength(1); expect(parseFlockSavedFreightList("not-json")).toEqual([]); expect(parseFlockSavedFreightList('[{"id":"1","name":"n","description":"d"}]')).toHaveLength( 1, ); }); });