适配后端统一模型 ID、协议字段、UUID 自动生成和结构化错误响应: - 类型定义:Provider 新增 protocol 字段,Model 新增 unifiedId,CreateModelInput 移除 id - API 客户端:提取结构化错误响应中的错误码 - 供应商管理:添加协议选择下拉框和表格列 - 模型管理:移除 ID 输入,显示统一模型 ID(只读) - Hooks:错误码映射为友好中文消息 - 测试:所有组件测试通过,mock 数据适配新字段 - 文档:更新 README 说明协议字段和统一模型 ID
72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import { render, screen } from '@testing-library/react';
|
|
import { describe, it, expect, vi } from 'vitest';
|
|
import { UsageChart } from '@/pages/Stats/UsageChart';
|
|
import type { UsageStats } from '@/types';
|
|
|
|
// Mock Recharts components
|
|
vi.mock('recharts', () => ({
|
|
ResponsiveContainer: vi.fn(({ children }) => <div data-testid="mock-chart-container">{children}</div>),
|
|
LineChart: vi.fn(() => <div data-testid="mock-line-chart" />),
|
|
Line: vi.fn(() => null),
|
|
XAxis: vi.fn(() => null),
|
|
YAxis: vi.fn(() => null),
|
|
CartesianGrid: vi.fn(() => null),
|
|
Tooltip: vi.fn(() => null),
|
|
}));
|
|
|
|
const mockStats: UsageStats[] = [
|
|
{
|
|
id: 1,
|
|
providerId: 'openai',
|
|
modelName: 'gpt-4o',
|
|
requestCount: 100,
|
|
date: '2024-01-01',
|
|
},
|
|
{
|
|
id: 2,
|
|
providerId: 'openai',
|
|
modelName: 'gpt-3.5-turbo',
|
|
requestCount: 200,
|
|
date: '2024-01-01',
|
|
},
|
|
{
|
|
id: 3,
|
|
providerId: 'anthropic',
|
|
modelName: 'claude-3',
|
|
requestCount: 150,
|
|
date: '2024-01-02',
|
|
},
|
|
];
|
|
|
|
describe('UsageChart', () => {
|
|
it('renders chart title', () => {
|
|
render(<UsageChart stats={mockStats} />);
|
|
|
|
expect(screen.getByText('请求趋势')).toBeInTheDocument();
|
|
});
|
|
|
|
it('renders with data', () => {
|
|
const { container } = render(<UsageChart stats={mockStats} />);
|
|
|
|
// TDesign Card component
|
|
expect(container.querySelector('.t-card')).toBeInTheDocument();
|
|
// Mocked chart container
|
|
expect(screen.getByTestId('mock-chart-container')).toBeInTheDocument();
|
|
});
|
|
|
|
it('renders empty state when no data', () => {
|
|
render(<UsageChart stats={[]} />);
|
|
|
|
expect(screen.getByText('暂无数据')).toBeInTheDocument();
|
|
});
|
|
|
|
it('aggregates data by date correctly', () => {
|
|
const { container } = render(<UsageChart stats={mockStats} />);
|
|
|
|
// TDesign Card component
|
|
expect(container.querySelector('.t-card')).toBeInTheDocument();
|
|
// Mocked chart should render
|
|
expect(screen.getByTestId('mock-chart-container')).toBeInTheDocument();
|
|
});
|
|
});
|