import assert from "node:assert/strict"; import { test } from "node:test"; import { toBrandDisplayName, toBrandSwitcherOptions } from "../src/lib/brand-switcher.ts"; test("完整成员记录转换为品牌切换选项", () => { assert.deepEqual( toBrandSwitcherOptions([ { id: "membership-1", role: "owner", brand: { id: "brand-1", name: "独立工坊" } }, { id: "membership-2", role: "editor", brand: { id: "brand-2", name: "林境门作" } }, ]), [ { value: "brand-1", label: "独立工坊" }, { value: "brand-2", label: "林境门作" }, ], ); }); test("异常成员条目被跳过而不是让外壳崩溃", () => { assert.deepEqual(toBrandSwitcherOptions([{}, null, { brand: null }, { brand: { id: 1 } }]), []); assert.deepEqual( toBrandSwitcherOptions([ {}, null, { brand: { id: "brand-1", name: "独立工坊" } }, { brand: { id: "" } }, ]), [{ value: "brand-1", label: "独立工坊" }], ); }); test("非数组返回空选项集合", () => { for (const value of [undefined, null, {}, "oops", 42]) { assert.deepEqual(toBrandSwitcherOptions(value), []); } }); test("重复品牌与缺失名称按稳定规则收敛", () => { assert.deepEqual( toBrandSwitcherOptions([ { brand: { id: "brand-1", name: "独立工坊" } }, { brand: { id: "brand-1", name: "重复品牌" } }, { brand: { id: "brand-2", name: 123 } }, ]), [ { value: "brand-1", label: "独立工坊" }, { value: "brand-2", label: "brand-2" }, ], ); }); test("品牌名称只接受非空字符串", () => { assert.equal(toBrandDisplayName("独立工坊", "当前品牌"), "独立工坊"); assert.equal(toBrandDisplayName(" ", "当前品牌"), "当前品牌"); assert.equal(toBrandDisplayName({ name: "x" }, "当前品牌"), "当前品牌"); assert.equal(toBrandDisplayName(null, "当前品牌"), "当前品牌"); assert.equal(toBrandDisplayName(undefined, ""), ""); });