添加后端集成测试套件
- 实现 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,440 @@
|
||||
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('POST /api/v1/topics', () => {
|
||||
it('应成功创建主题', async () => {
|
||||
const payload = {
|
||||
name: 'AI 技术资讯',
|
||||
description: '收集 AI 相关的技术文章',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/topics',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.success).toBe(true);
|
||||
expect(body.message).toBe('主题创建成功');
|
||||
expect(body.data.name).toBe(payload.name);
|
||||
expect(body.data.processMode).toBe('summary');
|
||||
});
|
||||
|
||||
it('应成功创建主题并关联数据源和推送渠道', async () => {
|
||||
const source = await prisma.source.create({
|
||||
data: {
|
||||
name: '测试源',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const channel = await prisma.channel.create({
|
||||
data: {
|
||||
name: '测试渠道',
|
||||
type: 'email',
|
||||
config: { to: 'test@example.com' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const payload = {
|
||||
name: '完整配置主题',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
sourceIds: [source.id],
|
||||
channelIds: [channel.id],
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/topics',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
|
||||
// 验证关联关系
|
||||
const topicSources = await prisma.topicSource.findMany({
|
||||
where: { sourceId: source.id },
|
||||
});
|
||||
expect(topicSources).toHaveLength(1);
|
||||
|
||||
const topicChannels = await prisma.topicChannel.findMany({
|
||||
where: { channelId: channel.id },
|
||||
});
|
||||
expect(topicChannels).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('应支持关联 LLM 配置', 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 payload = {
|
||||
name: 'AI 资讯',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
llmConfigId: llmConfig.id,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/topics',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.llmConfigId).toBe(llmConfig.id);
|
||||
});
|
||||
|
||||
it('缺少必填字段时应返回 400 错误', async () => {
|
||||
const payload = {
|
||||
name: '不完整主题',
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/topics',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(400);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.success).toBe(false);
|
||||
expect(body.error).toContain('缺少必填字段');
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/topics', () => {
|
||||
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 response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/topics',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data).toHaveLength(1);
|
||||
expect(body.data[0].llmConfig).toBeDefined();
|
||||
expect(body.data[0].llmConfig.name).toBe('OpenAI');
|
||||
});
|
||||
|
||||
it('应支持分页', async () => {
|
||||
const data = Array.from({ length: 25 }, (_, i) => ({
|
||||
name: `主题 ${i + 1}`,
|
||||
processMode: 'summary' as const,
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
enabled: true,
|
||||
}));
|
||||
await prisma.topic.createMany({ data });
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/topics?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/topics/: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 source = await prisma.source.create({
|
||||
data: {
|
||||
name: '测试源',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const channel = await prisma.channel.create({
|
||||
data: {
|
||||
name: '测试渠道',
|
||||
type: 'email',
|
||||
config: { to: 'test@example.com' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const topic = await prisma.topic.create({
|
||||
data: {
|
||||
name: 'AI 资讯',
|
||||
description: 'AI 技术文章',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
llmConfigId: llmConfig.id,
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.topicSource.create({
|
||||
data: { topicId: topic.id, sourceId: source.id },
|
||||
});
|
||||
|
||||
await prisma.topicChannel.create({
|
||||
data: { topicId: topic.id, channelId: channel.id },
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: `/api/v1/topics/${topic.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.id).toBe(topic.id);
|
||||
expect(body.data.llmConfig).toBeDefined();
|
||||
expect(body.data.topicSources).toHaveLength(1);
|
||||
expect(body.data.topicSources[0].source.name).toBe('测试源');
|
||||
expect(body.data.topicChannels).toHaveLength(1);
|
||||
expect(body.data.topicChannels[0].channel.name).toBe('测试渠道');
|
||||
});
|
||||
|
||||
it('主题不存在时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/topics/99999',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('PUT /api/v1/topics/:id', () => {
|
||||
it('应成功更新主题', async () => {
|
||||
const topic = await prisma.topic.create({
|
||||
data: {
|
||||
name: '原始主题',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const updateData = {
|
||||
name: '更新后主题',
|
||||
description: '新描述',
|
||||
enabled: false,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'PUT',
|
||||
url: `/api/v1/topics/${topic.id}`,
|
||||
payload: updateData,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.name).toBe('更新后主题');
|
||||
expect(body.data.description).toBe('新描述');
|
||||
expect(body.data.enabled).toBe(false);
|
||||
expect(body.message).toBe('主题更新成功');
|
||||
});
|
||||
|
||||
it('应更新数据源和渠道关联', async () => {
|
||||
const source1 = await prisma.source.create({
|
||||
data: {
|
||||
name: '源 1',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss1' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const source2 = await prisma.source.create({
|
||||
data: {
|
||||
name: '源 2',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss2' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const topic = await prisma.topic.create({
|
||||
data: {
|
||||
name: '测试主题',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
// 初始关联
|
||||
await prisma.topicSource.create({
|
||||
data: { topicId: topic.id, sourceId: source1.id },
|
||||
});
|
||||
|
||||
// 更新关联
|
||||
const response = await app.inject({
|
||||
method: 'PUT',
|
||||
url: `/api/v1/topics/${topic.id}`,
|
||||
payload: {
|
||||
name: '测试主题',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
sourceIds: [source2.id],
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
// 验证关联已更新
|
||||
const topicSources = await prisma.topicSource.findMany({
|
||||
where: { topicId: topic.id },
|
||||
});
|
||||
expect(topicSources).toHaveLength(1);
|
||||
expect(topicSources[0].sourceId).toBe(source2.id);
|
||||
});
|
||||
|
||||
it('更新不存在的主题时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/v1/topics/99999',
|
||||
payload: {
|
||||
name: '更新',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DELETE /api/v1/topics/:id', () => {
|
||||
it('应成功删除主题', async () => {
|
||||
const topic = await prisma.topic.create({
|
||||
data: {
|
||||
name: '待删除主题',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: `/api/v1/topics/${topic.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.message).toBe('主题删除成功');
|
||||
|
||||
const deleted = await prisma.topic.findUnique({
|
||||
where: { id: topic.id },
|
||||
});
|
||||
expect(deleted).toBeNull();
|
||||
});
|
||||
|
||||
it('删除不存在的主题时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: '/api/v1/topics/99999',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/v1/topics/:id/generate-report', () => {
|
||||
it('应触发报告生成任务', async () => {
|
||||
const topic = await prisma.topic.create({
|
||||
data: {
|
||||
name: 'AI 资讯',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * 0' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: `/api/v1/topics/${topic.id}/generate-report`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.message).toBe('报告生成任务已触发');
|
||||
});
|
||||
|
||||
it('主题不存在时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/topics/99999/generate-report',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user