feat(design-center): admin 入口按钮 +新建功能 / ⚡AI建功能
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
47
src/pages/DesignCenterPage.entrances.test.tsx
Normal file
47
src/pages/DesignCenterPage.entrances.test.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
import '@testing-library/jest-dom';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { vi, describe, it, expect, beforeEach } from 'vitest';
|
||||
import { DesignCenterPage } from './DesignCenterPage';
|
||||
|
||||
// menu 走真实 http → mock 掉,避免网络
|
||||
vi.mock('../api/menu', () => ({
|
||||
fetchMenu: vi.fn().mockResolvedValue({ roots: [] }),
|
||||
collectLeaves: vi.fn().mockReturnValue([]),
|
||||
}));
|
||||
|
||||
// 受控 profile:默认 admin
|
||||
const profileMock = vi.fn();
|
||||
vi.mock('../auth/token', () => ({
|
||||
getProfile: () => profileMock(),
|
||||
}));
|
||||
|
||||
function renderPage() {
|
||||
return render(
|
||||
<MemoryRouter>
|
||||
<DesignCenterPage />
|
||||
</MemoryRouter>,
|
||||
);
|
||||
}
|
||||
|
||||
describe('DesignCenterPage 入口按钮', () => {
|
||||
beforeEach(() => {
|
||||
profileMock.mockReset();
|
||||
});
|
||||
|
||||
it('admin 看到 [+ 新建功能] 与 [⚡ AI 建功能]', async () => {
|
||||
profileMock.mockReturnValue({ userId: 'ADMIN', account: 'admin', name: 'x', roles: [] });
|
||||
renderPage();
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('dc-new-feature')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('dc-ai-feature')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('非 admin 不渲染入口(整页降级)', () => {
|
||||
profileMock.mockReturnValue({ userId: 'U1', account: 'zhangsan', name: 'z', roles: [] });
|
||||
renderPage();
|
||||
expect(screen.queryByTestId('dc-new-feature')).not.toBeInTheDocument();
|
||||
expect(screen.queryByTestId('dc-ai-feature')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@
|
||||
// 列出全部功能页(按菜单分组、可搜索),每页一个「设计」入口打开 editor.html。
|
||||
// 仅管理员可见(路由在 AppShell 下,入口在 Topbar admin-only 按钮)。
|
||||
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';
|
||||
|
||||
@@ -19,6 +20,7 @@ export function DesignCenterPage() {
|
||||
const [error, setError] = useState('');
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [q, setQ] = useState('');
|
||||
const navigate = useNavigate();
|
||||
|
||||
const profile = getProfile();
|
||||
const isAdmin = profile?.account === 'admin' || profile?.userId === 'ADMIN';
|
||||
@@ -71,6 +73,22 @@ export function DesignCenterPage() {
|
||||
</div>
|
||||
</div>
|
||||
<div style={{ flex: 1 }} />
|
||||
<button
|
||||
className="btn btn-sm"
|
||||
data-testid="dc-new-feature"
|
||||
onClick={() => navigate('/new-feature')}
|
||||
title="人走五环,从零搭建一个新功能"
|
||||
>
|
||||
+ 新建功能
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-sm"
|
||||
data-testid="dc-ai-feature"
|
||||
onClick={() => navigate('/ai-feature')}
|
||||
title="AI 加速器:外部 agent 起草,人审后进设计器"
|
||||
>
|
||||
⚡ AI 建功能
|
||||
</button>
|
||||
<div className="gsearch" style={{ width: 280 }}>
|
||||
<input
|
||||
value={q}
|
||||
|
||||
Reference in New Issue
Block a user