diff --git a/apps/admin/src/app/Dashboard.tsx b/apps/admin/src/app/Dashboard.tsx index 514ecc1..bc56b09 100644 --- a/apps/admin/src/app/Dashboard.tsx +++ b/apps/admin/src/app/Dashboard.tsx @@ -42,6 +42,7 @@ import { AdminOverviewRedirect } from "./AdminRouter.js"; import { createBrandRouteElement } from "./route-access.js"; import { Suspense } from "react"; import { AdminErrorBoundary } from "../components/AdminErrorBoundary.js"; +import { toBrandDisplayName, toBrandSwitcherOptions } from "../lib/brand-switcher.js"; export function Dashboard({ onLogout, @@ -72,6 +73,8 @@ export function Dashboard({ } }; const page = location.pathname.split("/")[2] || "overview"; + const brandName = toBrandDisplayName(brand.data?.name, "当前品牌"); + const brandOptions = toBrandSwitcherOptions(memberships.data); const items = [ { type: "group" as const, @@ -108,7 +111,7 @@ export function Dashboard({
- + 品牌工作区
- {brand.data?.name || "当前品牌"} + {brandName} 品牌工作区 { if (value) void switchBrand(value); }} - options={(memberships.data ?? []).map((item) => ({ - value: item.brand.id, - label: item.brand.name, - }))} + options={brandOptions} />
diff --git a/apps/admin/src/lib/brand-switcher.ts b/apps/admin/src/lib/brand-switcher.ts new file mode 100644 index 0000000..c946249 --- /dev/null +++ b/apps/admin/src/lib/brand-switcher.ts @@ -0,0 +1,32 @@ +export interface BrandSwitcherOption { + value: string; + label: string; +} + +/** + * 品牌切换器只接受形状完整的成员记录。 + * 接口返回异常条目时跳过该条,避免整个后台外壳渲染失败。 + */ +export function toBrandSwitcherOptions(value: unknown): BrandSwitcherOption[] { + if (!Array.isArray(value)) return []; + const options: BrandSwitcherOption[] = []; + const seen = new Set(); + + for (const item of value) { + const brand = (item as { brand?: { id?: unknown; name?: unknown } } | null | undefined)?.brand; + if (!brand || typeof brand.id !== "string" || brand.id.length === 0) continue; + if (seen.has(brand.id)) continue; + seen.add(brand.id); + options.push({ + value: brand.id, + label: typeof brand.name === "string" && brand.name.length > 0 ? brand.name : brand.id, + }); + } + + return options; +} + +/** 品牌名称必须是字符串,异常数据回退到给定文案。 */ +export function toBrandDisplayName(value: unknown, fallback: string): string { + return typeof value === "string" && value.trim().length > 0 ? value : fallback; +} diff --git a/apps/admin/test/brand-switcher.test.mjs b/apps/admin/test/brand-switcher.test.mjs new file mode 100644 index 0000000..44f0032 --- /dev/null +++ b/apps/admin/test/brand-switcher.test.mjs @@ -0,0 +1,57 @@ +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, ""), ""); +}); diff --git a/apps/taro/test/browser/admin-error-boundary.spec.ts b/apps/taro/test/browser/admin-error-boundary.spec.ts index dcae219..2ac95cb 100644 --- a/apps/taro/test/browser/admin-error-boundary.spec.ts +++ b/apps/taro/test/browser/admin-error-boundary.spec.ts @@ -17,6 +17,8 @@ test.afterEach(({ page }) => { interface MockOptions { /** 品牌成员列表的异常返回,用于触发整页渲染错误 */ memberships?: unknown; + /** 品牌名称的异常返回 */ + brandName?: unknown; /** 只在首次请求时返回的异常信任内容,重新加载后恢复 */ trustContentOnce?: unknown; } @@ -44,7 +46,7 @@ async function mockAdminApi(page: Page, options: MockOptions = {}) { return route.fulfill({ json: { id: "brand-current", - name: "独立工坊", + name: options.brandName ?? "独立工坊", slug: "atelier-studio", tagline: "把耐心做进作品", templateKey: "personal", @@ -92,19 +94,19 @@ async function mockAdminApi(page: Page, options: MockOptions = {}) { }); } -test("品牌成员接口返回异常结构时显示整页错误而不是白屏", async ({ page }) => { +test("品牌成员与品牌名称异常时后台外壳仍可正常使用", async ({ page }) => { await page.setViewportSize({ width: 1440, height: 1000 }); - await mockAdminApi(page, { memberships: [{}] }); + await mockAdminApi(page, { + memberships: [{}, null, { brand: { id: "brand-current" } }], + brandName: { unexpected: true }, + }); await page.goto(`${adminOrigin}/admin/publication`); - await expect(page.getByText("页面渲染出错")).toBeVisible(); - await expect(page.getByText(/错误编号:admin-/)).toBeVisible(); - await expect(page.getByRole("button", { name: "重新加载页面" })).toBeVisible(); - await expect(page.getByRole("link", { name: "返回概览" })).toBeVisible(); - - const bodyText = await page.locator("body").innerText(); - expect(bodyText.trim().length).toBeGreaterThan(0); + await expect(page.getByRole("heading", { name: "内容与发布" })).toBeVisible(); + await expect(page.getByText("当前品牌", { exact: true }).first()).toBeVisible(); + await expect(page.getByText("页面渲染出错")).toBeHidden(); + await expect(page.getByRole("button", { name: "退出登录" })).toBeVisible(); }); test("内容区渲染出错时保留导航,重新加载后恢复", async ({ page }) => {