- 实现 82 个集成测试用例覆盖所有 API 端点 - 使用真实数据库连接,无 mock 逻辑 - 修复 PUT 请求只更新提供字段的逻辑 - 配置 Vitest 串行执行避免测试竞态 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
268 lines
7.4 KiB
TypeScript
268 lines
7.4 KiB
TypeScript
import { beforeAll, afterAll, beforeEach, describe, it, expect } from 'vitest';
|
|
import { createTestApp } from './helpers/app';
|
|
import { cleanupDatabase, prisma } from './setup';
|
|
|
|
describe('LLM 配置 API', () => {
|
|
let app: Awaited<ReturnType<typeof createTestApp>>;
|
|
|
|
beforeAll(async () => {
|
|
app = await createTestApp();
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
await cleanupDatabase();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
await cleanupDatabase();
|
|
});
|
|
|
|
describe('POST /api/v1/llm-configs', () => {
|
|
it('应成功创建 LLM 配置', async () => {
|
|
const payload = {
|
|
name: 'OpenAI 测试配置',
|
|
baseUrl: 'https://api.openai.com/v1',
|
|
model: 'gpt-4',
|
|
apiKey: 'sk-test123',
|
|
enabled: true,
|
|
};
|
|
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/api/v1/llm-configs',
|
|
payload,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(201);
|
|
const body = JSON.parse(response.body);
|
|
expect(body.success).toBe(true);
|
|
expect(body.message).toBe('LLM 配置创建成功');
|
|
expect(body.data.name).toBe(payload.name);
|
|
expect(body.data.baseUrl).toBe(payload.baseUrl);
|
|
expect(body.data.model).toBe(payload.model);
|
|
expect(body.data.apiKey).toBe(payload.apiKey);
|
|
expect(body.data.enabled).toBe(true);
|
|
expect(body.data.id).toBeDefined();
|
|
});
|
|
|
|
it('缺少必填字段时应返回 400 错误', async () => {
|
|
const payload = {
|
|
name: '不完整配置',
|
|
baseUrl: 'https://api.openai.com/v1',
|
|
};
|
|
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/api/v1/llm-configs',
|
|
payload,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
const body = JSON.parse(response.body);
|
|
expect(body.success).toBe(false);
|
|
expect(body.error).toContain('缺少必填字段');
|
|
});
|
|
|
|
it('enabled 默认值应为 true', async () => {
|
|
const payload = {
|
|
name: 'OpenAI 测试配置',
|
|
baseUrl: 'https://api.openai.com/v1',
|
|
model: 'gpt-4',
|
|
apiKey: 'sk-test123',
|
|
};
|
|
|
|
const response = await app.inject({
|
|
method: 'POST',
|
|
url: '/api/v1/llm-configs',
|
|
payload,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(201);
|
|
const body = JSON.parse(response.body);
|
|
expect(body.data.enabled).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('GET /api/v1/llm-configs', () => {
|
|
it('应返回配置列表', async () => {
|
|
// 创建测试数据
|
|
await prisma.llmConfig.createMany({
|
|
data: [
|
|
{
|
|
name: '配置 1',
|
|
baseUrl: 'https://api.openai.com/v1',
|
|
model: 'gpt-4',
|
|
apiKey: 'sk-key1',
|
|
enabled: true,
|
|
},
|
|
{
|
|
name: '配置 2',
|
|
baseUrl: 'https://api.anthropic.com/v1',
|
|
model: 'claude-3',
|
|
apiKey: 'sk-key2',
|
|
enabled: false,
|
|
},
|
|
],
|
|
});
|
|
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: '/api/v1/llm-configs',
|
|
});
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
const body = JSON.parse(response.body);
|
|
expect(body.data).toHaveLength(2);
|
|
expect(body.pagination.total).toBe(2);
|
|
expect(body.pagination.page).toBe(1);
|
|
expect(body.pagination.limit).toBe(20);
|
|
});
|
|
|
|
it('应支持分页', async () => {
|
|
// 创建 25 条数据
|
|
const data = Array.from({ length: 25 }, (_, i) => ({
|
|
name: `配置 ${i + 1}`,
|
|
baseUrl: 'https://api.openai.com/v1',
|
|
model: 'gpt-4',
|
|
apiKey: `sk-key${i}`,
|
|
enabled: true,
|
|
}));
|
|
await prisma.llmConfig.createMany({ data });
|
|
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: '/api/v1/llm-configs?page=2&limit=10',
|
|
});
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
const body = JSON.parse(response.body);
|
|
expect(body.data).toHaveLength(10);
|
|
expect(body.pagination.total).toBe(25);
|
|
expect(body.pagination.page).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe('GET /api/v1/llm-configs/:id', () => {
|
|
it('应返回单个配置', async () => {
|
|
const config = await prisma.llmConfig.create({
|
|
data: {
|
|
name: '测试配置',
|
|
baseUrl: 'https://api.openai.com/v1',
|
|
model: 'gpt-4',
|
|
apiKey: 'sk-test',
|
|
enabled: true,
|
|
},
|
|
});
|
|
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: `/api/v1/llm-configs/${config.id}`,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
const body = JSON.parse(response.body);
|
|
expect(body.data.id).toBe(config.id);
|
|
expect(body.data.name).toBe('测试配置');
|
|
});
|
|
|
|
it('配置不存在时应返回 404', async () => {
|
|
const response = await app.inject({
|
|
method: 'GET',
|
|
url: '/api/v1/llm-configs/99999',
|
|
});
|
|
|
|
expect(response.statusCode).toBe(404);
|
|
const body = JSON.parse(response.body);
|
|
expect(body.success).toBe(false);
|
|
expect(body.error).toContain('LLM 配置不存在');
|
|
});
|
|
});
|
|
|
|
describe('PUT /api/v1/llm-configs/:id', () => {
|
|
it('应成功更新配置', async () => {
|
|
const config = await prisma.llmConfig.create({
|
|
data: {
|
|
name: '原始配置',
|
|
baseUrl: 'https://api.openai.com/v1',
|
|
model: 'gpt-4',
|
|
apiKey: 'sk-test',
|
|
enabled: true,
|
|
},
|
|
});
|
|
|
|
const updateData = {
|
|
name: '更新后配置',
|
|
model: 'gpt-4-turbo',
|
|
enabled: false,
|
|
};
|
|
|
|
const response = await app.inject({
|
|
method: 'PUT',
|
|
url: `/api/v1/llm-configs/${config.id}`,
|
|
payload: updateData,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
const body = JSON.parse(response.body);
|
|
expect(body.data.name).toBe('更新后配置');
|
|
expect(body.data.model).toBe('gpt-4-turbo');
|
|
expect(body.data.enabled).toBe(false);
|
|
expect(body.message).toBe('LLM 配置更新成功');
|
|
});
|
|
|
|
it('更新不存在的配置时应返回 404', async () => {
|
|
const response = await app.inject({
|
|
method: 'PUT',
|
|
url: '/api/v1/llm-configs/99999',
|
|
payload: {
|
|
name: '更新',
|
|
baseUrl: 'https://api.openai.com/v1',
|
|
model: 'gpt-4',
|
|
apiKey: 'sk-test',
|
|
},
|
|
});
|
|
|
|
expect(response.statusCode).toBe(404);
|
|
});
|
|
});
|
|
|
|
describe('DELETE /api/v1/llm-configs/:id', () => {
|
|
it('应成功删除配置', async () => {
|
|
const config = await prisma.llmConfig.create({
|
|
data: {
|
|
name: '待删除配置',
|
|
baseUrl: 'https://api.openai.com/v1',
|
|
model: 'gpt-4',
|
|
apiKey: 'sk-test',
|
|
enabled: true,
|
|
},
|
|
});
|
|
|
|
const response = await app.inject({
|
|
method: 'DELETE',
|
|
url: `/api/v1/llm-configs/${config.id}`,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
const body = JSON.parse(response.body);
|
|
expect(body.message).toBe('LLM 配置删除成功');
|
|
|
|
// 验证已被删除
|
|
const deleted = await prisma.llmConfig.findUnique({
|
|
where: { id: config.id },
|
|
});
|
|
expect(deleted).toBeNull();
|
|
});
|
|
|
|
it('删除不存在的配置时应返回 404', async () => {
|
|
const response = await app.inject({
|
|
method: 'DELETE',
|
|
url: '/api/v1/llm-configs/99999',
|
|
});
|
|
|
|
expect(response.statusCode).toBe(404);
|
|
});
|
|
});
|
|
});
|