From 768ffa8551e4cb751a3bf4b3fedc1167102c4d23 Mon Sep 17 00:00:00 2001 From: zhengwr Date: Wed, 24 Jun 2026 11:20:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(web):=20isAdmin=20=E6=94=B6=E5=8F=A3?= =?UTF-8?q?=E5=88=B0=20isAdminProfile(B4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 5 处逐字硬编码 profile?.account==='admin'||userId==='ADMIN'(Topbar/ModelerPage/NewFeatureShell/AiFeatureForm/DesignCenterPage)收口到 auth/token.ts 纯函数 isAdminProfile,口径不变。 顺带:5 个页面测试的 vi.mock('../auth/token') 从整模块替换改为 importOriginal 只覆盖 getProfile(不再 mock 纯函数,修复 mock 漏 isAdminProfile 致的 render 崩)。 验证:新增 token.test.ts 4 测试 + tsc -b 干净 + vitest 18 文件/77 测试全绿。见总览 §10 B4。 Co-Authored-By: Claude Opus 4.8 (1M context) --- src/App.routes.test.tsx | 3 ++- src/auth/token.test.ts | 23 +++++++++++++++++++ src/auth/token.ts | 8 +++++++ src/pages/AiFeatureForm.test.tsx | 5 +++- src/pages/AiFeatureForm.tsx | 4 ++-- src/pages/DesignCenterPage.entrances.test.tsx | 3 ++- src/pages/DesignCenterPage.tsx | 4 ++-- src/pages/ModelerPage.test.tsx | 5 +++- src/pages/ModelerPage.tsx | 4 ++-- src/pages/NewFeatureShell.test.tsx | 5 +++- src/pages/NewFeatureShell.tsx | 4 ++-- src/shell/Topbar.tsx | 3 ++- 12 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 src/auth/token.test.ts diff --git a/src/App.routes.test.tsx b/src/App.routes.test.tsx index 31f1b5e..75aea50 100644 --- a/src/App.routes.test.tsx +++ b/src/App.routes.test.tsx @@ -5,7 +5,8 @@ import { vi, describe, it, expect } from 'vitest'; import { NewFeatureShell } from './pages/NewFeatureShell'; import { AiFeatureForm } from './pages/AiFeatureForm'; -vi.mock('./auth/token', () => ({ +vi.mock('./auth/token', async (importOriginal) => ({ + ...(await importOriginal()), getProfile: () => ({ userId: 'ADMIN', account: 'admin', name: 'x', roles: [] }), })); vi.mock('./api/meta', () => ({ listEntities: vi.fn().mockResolvedValue([]), createEntity: vi.fn() })); diff --git a/src/auth/token.test.ts b/src/auth/token.test.ts new file mode 100644 index 0000000..b7d91d7 --- /dev/null +++ b/src/auth/token.test.ts @@ -0,0 +1,23 @@ +import { describe, it, expect } from 'vitest'; +import { isAdminProfile, type UserProfile } from './token'; + +const base: UserProfile = { userId: 'U1', account: 'user', name: '张三', roles: [] }; + +describe('isAdminProfile', () => { + it('account 为 admin 判为管理员', () => { + expect(isAdminProfile({ ...base, account: 'admin' })).toBe(true); + }); + + it('userId 为 ADMIN 判为管理员', () => { + expect(isAdminProfile({ ...base, userId: 'ADMIN' })).toBe(true); + }); + + it('普通用户(account/userId 均非管理员)判否', () => { + expect(isAdminProfile(base)).toBe(false); + }); + + it('null / undefined 判否', () => { + expect(isAdminProfile(null)).toBe(false); + expect(isAdminProfile(undefined)).toBe(false); + }); +}); diff --git a/src/auth/token.ts b/src/auth/token.ts index f48e458..fe19835 100644 --- a/src/auth/token.ts +++ b/src/auth/token.ts @@ -74,3 +74,11 @@ export function clearSession(): void { export function isAuthenticated(): boolean { return !!getToken(); } + +/** + * 管理员判定(收口 Topbar/ModelerPage/NewFeatureShell/AiFeatureForm/DesignCenterPage 5 处逐字硬编码, + * 总览 §10.3)。口径与历史完全一致:account==='admin' 或 userId==='ADMIN'。 + */ +export function isAdminProfile(profile: UserProfile | null | undefined): boolean { + return profile?.account === 'admin' || profile?.userId === 'ADMIN'; +} diff --git a/src/pages/AiFeatureForm.test.tsx b/src/pages/AiFeatureForm.test.tsx index 7df7f8a..0afbcfb 100644 --- a/src/pages/AiFeatureForm.test.tsx +++ b/src/pages/AiFeatureForm.test.tsx @@ -6,7 +6,10 @@ import { vi, describe, it, expect } from 'vitest'; // AiFeatureForm 现以 amis form 渲染()。amis 内部(auto-develop 提交、ARRAYFILTER 取 // 草稿 targetId、审阅按钮)由 Playwright e2e 覆盖;单测仅守 admin 守卫 + 容器/amis 挂载点。 let mockProfile: { userId: string; account: string; name: string; roles: string[] } | null = null; -vi.mock('../auth/token', () => ({ getProfile: () => mockProfile })); +vi.mock('../auth/token', async (importOriginal) => ({ + ...(await importOriginal()), + getProfile: () => mockProfile, +})); vi.mock('../components/AmisRenderer', () => ({ AmisRenderer: () =>
})); import { AiFeatureForm } from './AiFeatureForm'; diff --git a/src/pages/AiFeatureForm.tsx b/src/pages/AiFeatureForm.tsx index e40d6e9..c856bde 100644 --- a/src/pages/AiFeatureForm.tsx +++ b/src/pages/AiFeatureForm.tsx @@ -5,7 +5,7 @@ // 故 api 用 adaptor 包成 {status:0,data:payload},再以 ARRAYFILTER 取 PAGE_SCHEMA 草稿 targetId。 import { useMemo } from 'react'; import { useNavigate } from 'react-router-dom'; -import { getProfile } from '../auth/token'; +import { getProfile, isAdminProfile } from '../auth/token'; import { AmisRenderer } from '../components/AmisRenderer'; const TYPE_OPTIONS = [ @@ -90,7 +90,7 @@ function buildSchema(): Record { export function AiFeatureForm() { const navigate = useNavigate(); const profile = getProfile(); - const isAdmin = profile?.account === 'admin' || profile?.userId === 'ADMIN'; + const isAdmin = isAdminProfile(profile); const schema = useMemo(buildSchema, []); if (!isAdmin) { diff --git a/src/pages/DesignCenterPage.entrances.test.tsx b/src/pages/DesignCenterPage.entrances.test.tsx index 5507c84..fa5600f 100644 --- a/src/pages/DesignCenterPage.entrances.test.tsx +++ b/src/pages/DesignCenterPage.entrances.test.tsx @@ -12,7 +12,8 @@ vi.mock('../api/menu', () => ({ // 受控 profile:默认 admin const profileMock = vi.fn(); -vi.mock('../auth/token', () => ({ +vi.mock('../auth/token', async (importOriginal) => ({ + ...(await importOriginal()), getProfile: () => profileMock(), })); diff --git a/src/pages/DesignCenterPage.tsx b/src/pages/DesignCenterPage.tsx index 9385043..5ff9236 100644 --- a/src/pages/DesignCenterPage.tsx +++ b/src/pages/DesignCenterPage.tsx @@ -4,7 +4,7 @@ import { useEffect, useMemo, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { fetchMenu, collectLeaves, type MenuLeaf } from '../api/menu'; -import { getProfile } from '../auth/token'; +import { getProfile, isAdminProfile } from '../auth/token'; interface Group { root: string; @@ -23,7 +23,7 @@ export function DesignCenterPage() { const navigate = useNavigate(); const profile = getProfile(); - const isAdmin = profile?.account === 'admin' || profile?.userId === 'ADMIN'; + const isAdmin = isAdminProfile(profile); useEffect(() => { let alive = true; diff --git a/src/pages/ModelerPage.test.tsx b/src/pages/ModelerPage.test.tsx index 3e04087..57e963a 100644 --- a/src/pages/ModelerPage.test.tsx +++ b/src/pages/ModelerPage.test.tsx @@ -7,7 +7,10 @@ vi.mock('../api/meta', () => ({ listEntityColumns: vi.fn(), createEntity: vi.fn(), })); -vi.mock('../auth/token', () => ({ getProfile: vi.fn() })); +vi.mock('../auth/token', async (importOriginal) => ({ + ...(await importOriginal()), + getProfile: vi.fn(), +})); import { listEntities, createEntity } from '../api/meta'; import { getProfile } from '../auth/token'; diff --git a/src/pages/ModelerPage.tsx b/src/pages/ModelerPage.tsx index cb55fcb..95c943e 100644 --- a/src/pages/ModelerPage.tsx +++ b/src/pages/ModelerPage.tsx @@ -3,7 +3,7 @@ // v2:可建新实体 + 给已有实体加字段(受控 ALTER 安全通道,只加列/恒可空)。字段表见同文件下方组件。 import { useEffect, useState } from 'react'; import { Dialog } from '../components/ui/Dialog'; -import { getProfile } from '../auth/token'; +import { getProfile, isAdminProfile } from '../auth/token'; import { listEntities, createEntity, @@ -19,7 +19,7 @@ export function validateTableName(t: string): boolean { export function ModelerPage() { const profile = getProfile(); - const isAdmin = profile?.account === 'admin' || profile?.userId === 'ADMIN'; + const isAdmin = isAdminProfile(profile); const [entities, setEntities] = useState([]); const [loading, setLoading] = useState(true); diff --git a/src/pages/NewFeatureShell.test.tsx b/src/pages/NewFeatureShell.test.tsx index 55e7108..05df650 100644 --- a/src/pages/NewFeatureShell.test.tsx +++ b/src/pages/NewFeatureShell.test.tsx @@ -8,7 +8,10 @@ import { vi, describe, it, expect } from 'vitest'; // (e2e/five-ring.spec、master-detail.spec) 覆盖。单测仅守:① admin 守卫;② admin 下先渲染页面类型选择卡; // ③ 选一种类型后渲染 amis 挂载点(wizard 壳)。 let mockProfile: { userId: string; account: string; name: string; roles: string[] } | null = null; -vi.mock('../auth/token', () => ({ getProfile: () => mockProfile })); +vi.mock('../auth/token', async (importOriginal) => ({ + ...(await importOriginal()), + getProfile: () => mockProfile, +})); vi.mock('../components/AmisRenderer', () => ({ AmisRenderer: () =>
})); import { NewFeatureShell } from './NewFeatureShell'; diff --git a/src/pages/NewFeatureShell.tsx b/src/pages/NewFeatureShell.tsx index 94e46e0..3db0e15 100644 --- a/src/pages/NewFeatureShell.tsx +++ b/src/pages/NewFeatureShell.tsx @@ -7,7 +7,7 @@ // 红线:发布恒人审;standard 写入恒走后端 PageSchemaWriter(前端只调 /api/design/recipe/compile)。 import { useMemo, useState } from 'react'; import { useNavigate } from 'react-router-dom'; -import { getProfile } from '../auth/token'; +import { getProfile, isAdminProfile } from '../auth/token'; import { AmisRenderer } from '../components/AmisRenderer'; const TYPE_OPTIONS = [ @@ -279,7 +279,7 @@ function RecipeChooser({ onPick }: { onPick: (recipe: string) => void }) { export function NewFeatureShell() { const navigate = useNavigate(); const profile = getProfile(); - const isAdmin = profile?.account === 'admin' || profile?.userId === 'ADMIN'; + const isAdmin = isAdminProfile(profile); const [recipe, setRecipe] = useState(null); const schema = useMemo(() => (recipe ? buildWizardSchema(recipe) : null), [recipe]); diff --git a/src/shell/Topbar.tsx b/src/shell/Topbar.tsx index a063959..cde36ef 100644 --- a/src/shell/Topbar.tsx +++ b/src/shell/Topbar.tsx @@ -5,6 +5,7 @@ import { Icon } from '../components/ui/Icon'; import { GlobalSearch } from './GlobalSearch'; import type { MenuLeaf } from '../api/menu'; import type { UserProfile } from '../auth/token'; +import { isAdminProfile } from '../auth/token'; interface TopbarProps { leaves: MenuLeaf[]; @@ -30,7 +31,7 @@ export function Topbar({ // 用户名显示:取 name 首字作头像 const avatarChar = profile?.name?.[0] ?? '?'; const navigate = useNavigate(); - const isAdmin = profile?.account === 'admin' || profile?.userId === 'ADMIN'; + const isAdmin = isAdminProfile(profile); return (