import { describe, it, expect, jest, beforeAll, afterEach } from '@jest/globals'; import React from 'react'; import { render, screen, waitFor } from '@testing-library/react'; import '@testing-library/jest-dom'; import SecurityDashboard from './page'; jest.mock('lucide-react', () => ({ Shield: () => , AlertTriangle: () => , Activity: () => , Lock: () => , RefreshCw: () => , TrendingUp: () => , TrendingDown: () => , })); jest.mock('@/components/ui/button', () => ({ Button: ({ children, disabled, ...props }: any) => ( ), })); jest.mock('@/components/ui/card', () => ({ Card: ({ children }: any) =>
{children}
, CardHeader: ({ children }: any) =>
{children}
, CardTitle: ({ children }: any) =>

{children}

, CardContent: ({ children }: any) =>
{children}
, })); global.fetch = jest.fn(() => Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true, logs: [ { id: '1', timestamp: Date.now(), type: 'captcha', severity: 'high', message: '验证码验证失败', ip: '192.168.1.1', }, ], stats: { totalRequests: 100, blockedRequests: 5, captchaAttempts: 10, rateLimitHits: 3, maliciousContentDetected: 2, successRate: 95, }, }), } as Response) ); describe('SecurityDashboard', () => { beforeAll(() => { jest.clearAllMocks(); }); afterEach(() => { jest.clearAllMocks(); }); describe('Rendering', () => { it('should render security dashboard', () => { render(); expect(screen.getByText('安全监控仪表板')).toBeInTheDocument(); expect(screen.getByText('实时监控网站安全状态和威胁检测')).toBeInTheDocument(); }); it('should render all stat cards', () => { render(); expect(screen.getByText('总请求数')).toBeInTheDocument(); expect(screen.getByText('已拦截请求')).toBeInTheDocument(); expect(screen.getByText('验证码尝试')).toBeInTheDocument(); expect(screen.getByText('频率限制命中')).toBeInTheDocument(); expect(screen.getByText('恶意内容检测')).toBeInTheDocument(); expect(screen.getByText('成功率')).toBeInTheDocument(); }); it('should display stats values', async () => { render(); await waitFor(() => { expect(screen.getByText('100')).toBeInTheDocument(); expect(screen.getByText('5')).toBeInTheDocument(); expect(screen.getByText('10')).toBeInTheDocument(); expect(screen.getByText('3')).toBeInTheDocument(); expect(screen.getByText('2')).toBeInTheDocument(); expect(screen.getByText('95%')).toBeInTheDocument(); }); }); }); describe('Security Logs', () => { it('should render security logs section', async () => { render(); await waitFor(() => { expect(screen.getByText('安全日志')).toBeInTheDocument(); }); }); it('should display log entries', async () => { render(); await waitFor(() => { expect(screen.getByText('验证码验证失败')).toBeInTheDocument(); expect(screen.getByText('IP: 192.168.1.1')).toBeInTheDocument(); }); }); it('should have filter buttons', () => { render(); expect(screen.getByText('全部')).toBeInTheDocument(); expect(screen.getByText('高危')).toBeInTheDocument(); expect(screen.getByText('中危')).toBeInTheDocument(); expect(screen.getByText('低危')).toBeInTheDocument(); }); }); describe('Refresh Functionality', () => { it('should have refresh button', async () => { render(); await waitFor(() => { expect(screen.getByTestId('refresh-cw-icon')).toBeInTheDocument(); }); }); it('should call fetch when refresh is clicked', async () => { render(); await waitFor(() => { const refreshButton = screen.getAllByRole('button')[0]; expect(refreshButton).not.toBeDisabled(); refreshButton.click(); expect(global.fetch).toHaveBeenCalled(); }); }); }); });