添加后端集成测试套件
- 实现 82 个集成测试用例覆盖所有 API 端点 - 使用真实数据库连接,无 mock 逻辑 - 修复 PUT 请求只更新提供字段的逻辑 - 配置 Vitest 串行执行避免测试竞态 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6df528f187
commit
697430feed
18 files changed
+4333
-8
No files matched your search
@@ -0,0 +1,307 @@
|
||||
import { beforeAll, afterAll, beforeEach, describe, it, expect } from 'vitest';
|
||||
import { createTestApp } from './helpers/app';
|
||||
import { cleanupDatabase, prisma } from './setup';
|
||||
|
||||
describe('报告 API', () => {
|
||||
let app: Awaited<ReturnType<typeof createTestApp>>;
|
||||
|
||||
beforeAll(async () => {
|
||||
app = await createTestApp();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await cleanupDatabase();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
await cleanupDatabase();
|
||||
});
|
||||
|
||||
describe('GET /api/v1/reports', () => {
|
||||
it('应返回报告列表', async () => {
|
||||
const topic = await prisma.topic.create({
|
||||
data: {
|
||||
name: 'AI 资讯',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.report.createMany({
|
||||
data: [
|
||||
{
|
||||
title: '报告 1',
|
||||
topicId: topic.id,
|
||||
status: 'completed',
|
||||
content: '报告内容 1',
|
||||
periodStart: new Date('2024-01-01'),
|
||||
periodEnd: new Date('2024-01-07'),
|
||||
},
|
||||
{
|
||||
title: '报告 2',
|
||||
topicId: topic.id,
|
||||
status: 'pending',
|
||||
content: '',
|
||||
periodStart: new Date('2024-01-08'),
|
||||
periodEnd: new Date('2024-01-14'),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/reports',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data).toHaveLength(2);
|
||||
expect(body.pagination.total).toBe(2);
|
||||
expect(body.data[0].topic).toBeDefined();
|
||||
});
|
||||
|
||||
it('应支持按主题 ID 筛选', async () => {
|
||||
const topic1 = await prisma.topic.create({
|
||||
data: {
|
||||
name: '主题 1',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const topic2 = await prisma.topic.create({
|
||||
data: {
|
||||
name: '主题 2',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.report.create({
|
||||
data: {
|
||||
title: '报告 1',
|
||||
topicId: topic1.id,
|
||||
status: 'completed',
|
||||
content: '报告 1',
|
||||
periodStart: new Date('2024-01-01'),
|
||||
periodEnd: new Date('2024-01-07'),
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.report.create({
|
||||
data: {
|
||||
title: '报告 2',
|
||||
topicId: topic2.id,
|
||||
status: 'completed',
|
||||
content: '报告 2',
|
||||
periodStart: new Date('2024-01-01'),
|
||||
periodEnd: new Date('2024-01-07'),
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: `/api/v1/reports?topicId=${topic1.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data).toHaveLength(1);
|
||||
expect(body.data[0].topicId).toBe(topic1.id);
|
||||
});
|
||||
|
||||
it('应支持按状态筛选', async () => {
|
||||
const topic = await prisma.topic.create({
|
||||
data: {
|
||||
name: 'AI 资讯',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.report.createMany({
|
||||
data: [
|
||||
{
|
||||
title: '完成报告',
|
||||
topicId: topic.id,
|
||||
status: 'completed',
|
||||
content: '完成',
|
||||
periodStart: new Date('2024-01-01'),
|
||||
periodEnd: new Date('2024-01-07'),
|
||||
},
|
||||
{
|
||||
title: '待处理报告',
|
||||
topicId: topic.id,
|
||||
status: 'pending',
|
||||
content: '',
|
||||
periodStart: new Date('2024-01-08'),
|
||||
periodEnd: new Date('2024-01-14'),
|
||||
},
|
||||
{
|
||||
title: '失败报告',
|
||||
topicId: topic.id,
|
||||
status: 'failed',
|
||||
errorMessage: '失败',
|
||||
content: '',
|
||||
periodStart: new Date('2024-01-15'),
|
||||
periodEnd: new Date('2024-01-21'),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/reports?status=completed',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data).toHaveLength(1);
|
||||
expect(body.data[0].status).toBe('completed');
|
||||
});
|
||||
|
||||
it('应支持分页', async () => {
|
||||
const topic = await prisma.topic.create({
|
||||
data: {
|
||||
name: 'AI 资讯',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const data = Array.from({ length: 25 }, (_, i) => ({
|
||||
title: `报告 ${i + 1}`,
|
||||
topicId: topic.id,
|
||||
status: 'completed' as const,
|
||||
content: '报告内容',
|
||||
periodStart: new Date('2024-01-01'),
|
||||
periodEnd: new Date('2024-01-07'),
|
||||
}));
|
||||
await prisma.report.createMany({ data });
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/reports?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/reports/:id', () => {
|
||||
it('应返回单个报告及关联的主题数据', async () => {
|
||||
const llmConfig = await prisma.llmConfig.create({
|
||||
data: {
|
||||
name: 'OpenAI',
|
||||
baseUrl: 'https://api.openai.com/v1',
|
||||
model: 'gpt-4',
|
||||
apiKey: 'sk-test',
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const topic = await prisma.topic.create({
|
||||
data: {
|
||||
name: 'AI 资讯',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
llmConfigId: llmConfig.id,
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const report = await prisma.report.create({
|
||||
data: {
|
||||
title: 'AI 资讯周报',
|
||||
topicId: topic.id,
|
||||
status: 'completed',
|
||||
content: '这是 AI 资讯周报',
|
||||
periodStart: new Date('2024-01-01'),
|
||||
periodEnd: new Date('2024-01-07'),
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: `/api/v1/reports/${report.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.id).toBe(report.id);
|
||||
expect(body.data.content).toBe('这是 AI 资讯周报');
|
||||
expect(body.data.topic).toBeDefined();
|
||||
expect(body.data.topic.llmConfig).toBeDefined();
|
||||
expect(body.data.topic.llmConfig.name).toBe('OpenAI');
|
||||
});
|
||||
|
||||
it('报告不存在时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/reports/99999',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.success).toBe(false);
|
||||
expect(body.error).toContain('报告不存在');
|
||||
});
|
||||
});
|
||||
|
||||
describe('DELETE /api/v1/reports/:id', () => {
|
||||
it('应成功删除报告', async () => {
|
||||
const topic = await prisma.topic.create({
|
||||
data: {
|
||||
name: 'AI 资讯',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const report = await prisma.report.create({
|
||||
data: {
|
||||
title: '待删除报告',
|
||||
topicId: topic.id,
|
||||
status: 'completed',
|
||||
content: '报告内容',
|
||||
periodStart: new Date('2024-01-01'),
|
||||
periodEnd: new Date('2024-01-07'),
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: `/api/v1/reports/${report.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.message).toBe('报告删除成功');
|
||||
|
||||
const deleted = await prisma.report.findUnique({
|
||||
where: { id: report.id },
|
||||
});
|
||||
expect(deleted).toBeNull();
|
||||
});
|
||||
|
||||
it('删除不存在的报告时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: '/api/v1/reports/99999',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user