Compare commits

..

2 Commits

Author SHA1 Message Date
90888f7e06 chore(web): 删 api/schema.ts overlay 死代码(B2)
fetchSchemaLayer/putOverlay/restoreStandard 全仓(含 e2e)零引用,真实 overlay API 在 editor/overlayApi.ts;删重复死份。tsc 干净 + vitest 77/77。见总览 §10 B2。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-24 11:22:47 +08:00
768ffa8551 refactor(web): isAdmin 收口到 isAdminProfile(B4)
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) <noreply@anthropic.com>
2026-06-24 11:20:16 +08:00
13 changed files with 57 additions and 53 deletions

View File

@@ -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<typeof import('./auth/token')>()),
getProfile: () => ({ userId: 'ADMIN', account: 'admin', name: 'x', roles: [] }),
}));
vi.mock('./api/meta', () => ({ listEntities: vi.fn().mockResolvedValue([]), createEntity: vi.fn() }));

View File

@@ -61,42 +61,3 @@ export async function fetchSchema(templateId: string): Promise<Record<string, un
);
return data;
}
// ============================================================================
// F4-L1 overlay API(Node 侧 http 调用,供设计器 e2e 断言 X-Schema-Layer 用)
// ============================================================================
/** 获取 schema 并返回 layer 信息(overlay / standard) */
export async function fetchSchemaLayer(
templateId: string,
): Promise<{ schema: Record<string, unknown>; layer: string }> {
const resp = await http.get<Record<string, unknown>>(
`/api/page/schema/${encodeURIComponent(templateId)}`,
{ headers: { 'Cache-Control': 'no-cache' } },
);
const layer = (resp.headers['x-schema-layer'] ?? 'standard') as string;
return { schema: resp.data, layer };
}
/** 保存 overlay(仅管理员) */
export async function putOverlay(
templateId: string,
schemaJson: string,
): Promise<{ status: number; msg: string }> {
const resp = await http.put(
`/api/page/schema/${encodeURIComponent(templateId)}/overlay`,
schemaJson,
{ headers: { 'Content-Type': 'application/json' } },
);
return resp.data as { status: number; msg: string };
}
/** 还原标准版(删除 overlay,仅管理员) */
export async function restoreStandard(
templateId: string,
): Promise<{ status: number; msg: string }> {
const resp = await http.delete(
`/api/page/schema/${encodeURIComponent(templateId)}/overlay`,
);
return resp.data as { status: number; msg: string };
}

23
src/auth/token.test.ts Normal file
View File

@@ -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);
});
});

View File

@@ -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';
}

View File

@@ -6,7 +6,10 @@ import { vi, describe, it, expect } from 'vitest';
// AiFeatureForm 现以 amis form 渲染(<AmisRenderer>)。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<typeof import('../auth/token')>()),
getProfile: () => mockProfile,
}));
vi.mock('../components/AmisRenderer', () => ({ AmisRenderer: () => <div data-testid="amis-mount" /> }));
import { AiFeatureForm } from './AiFeatureForm';

View File

@@ -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<string, unknown> {
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) {

View File

@@ -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<typeof import('../auth/token')>()),
getProfile: () => profileMock(),
}));

View File

@@ -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;

View File

@@ -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<typeof import('../auth/token')>()),
getProfile: vi.fn(),
}));
import { listEntities, createEntity } from '../api/meta';
import { getProfile } from '../auth/token';

View File

@@ -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<EntitySummary[]>([]);
const [loading, setLoading] = useState(true);

View File

@@ -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<typeof import('../auth/token')>()),
getProfile: () => mockProfile,
}));
vi.mock('../components/AmisRenderer', () => ({ AmisRenderer: () => <div data-testid="amis-mount" /> }));
import { NewFeatureShell } from './NewFeatureShell';

View File

@@ -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<string | null>(null);
const schema = useMemo(() => (recipe ? buildWizardSchema(recipe) : null), [recipe]);

View File

@@ -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 (
<header className="topbar">