添加后端集成测试套件
- 实现 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,385 @@
|
||||
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/sources', () => {
|
||||
it('应成功创建 RSS 类型的数据源', async () => {
|
||||
const payload = {
|
||||
name: '技术博客 RSS',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss' },
|
||||
cronExpression: '0 */6 * * *',
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/sources',
|
||||
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.type).toBe('rss');
|
||||
});
|
||||
|
||||
it('应成功创建 API 类型的数据源', async () => {
|
||||
const payload = {
|
||||
name: '新闻 API',
|
||||
type: 'api',
|
||||
config: {
|
||||
url: 'https://api.example.com/news',
|
||||
headers: { Authorization: 'Bearer token' },
|
||||
},
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/sources',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.type).toBe('api');
|
||||
});
|
||||
|
||||
it('应成功创建 Playwright 类型的数据源', async () => {
|
||||
const payload = {
|
||||
name: '爬虫数据源',
|
||||
type: 'playwright',
|
||||
config: {
|
||||
url: 'https://example.com',
|
||||
selectors: {
|
||||
title: '.title',
|
||||
content: '.content',
|
||||
},
|
||||
},
|
||||
cronExpression: '0 0 * * *',
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/sources',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.type).toBe('playwright');
|
||||
});
|
||||
|
||||
it('缺少必填字段时应返回 400 错误', async () => {
|
||||
const payload = {
|
||||
name: '不完整数据源',
|
||||
type: 'rss',
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/sources',
|
||||
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: '测试数据源',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss' },
|
||||
cronExpression: '0 * * * *',
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/sources',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.enabled).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/sources', () => {
|
||||
it('应返回数据源列表', async () => {
|
||||
await prisma.source.createMany({
|
||||
data: [
|
||||
{
|
||||
name: 'RSS 源 1',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss1' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'API 源 1',
|
||||
type: 'api',
|
||||
config: { url: 'https://api.example.com' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/sources',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data).toHaveLength(2);
|
||||
expect(body.pagination.total).toBe(2);
|
||||
});
|
||||
|
||||
it('应支持按类型筛选', async () => {
|
||||
await prisma.source.createMany({
|
||||
data: [
|
||||
{
|
||||
name: 'RSS 源 1',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss1' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'API 源 1',
|
||||
type: 'api',
|
||||
config: { url: 'https://api.example.com' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/sources?type=rss',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data).toHaveLength(1);
|
||||
expect(body.data[0].type).toBe('rss');
|
||||
});
|
||||
|
||||
it('应支持分页', async () => {
|
||||
const data = Array.from({ length: 25 }, (_, i) => ({
|
||||
name: `源 ${i + 1}`,
|
||||
type: 'rss' as const,
|
||||
config: { url: `https://example.com/rss${i}` },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
}));
|
||||
await prisma.source.createMany({ data });
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/sources?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/sources/:id', () => {
|
||||
it('应返回单个数据源及其关联的主题', async () => {
|
||||
const source = await prisma.source.create({
|
||||
data: {
|
||||
name: '测试源',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const topic = await prisma.topic.create({
|
||||
data: {
|
||||
name: '测试主题',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * *' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.topicSource.create({
|
||||
data: {
|
||||
topicId: topic.id,
|
||||
sourceId: source.id,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: `/api/v1/sources/${source.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.id).toBe(source.id);
|
||||
expect(body.data.topicSources).toHaveLength(1);
|
||||
expect(body.data.topicSources[0].topic.name).toBe('测试主题');
|
||||
});
|
||||
|
||||
it('数据源不存在时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/sources/99999',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.success).toBe(false);
|
||||
expect(body.error).toContain('数据源不存在');
|
||||
});
|
||||
});
|
||||
|
||||
describe('PUT /api/v1/sources/:id', () => {
|
||||
it('应成功更新数据源', async () => {
|
||||
const source = await prisma.source.create({
|
||||
data: {
|
||||
name: '原始源',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const updateData = {
|
||||
name: '更新后源',
|
||||
enabled: false,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'PUT',
|
||||
url: `/api/v1/sources/${source.id}`,
|
||||
payload: updateData,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.name).toBe('更新后源');
|
||||
expect(body.data.enabled).toBe(false);
|
||||
expect(body.message).toBe('数据源更新成功');
|
||||
});
|
||||
|
||||
it('更新不存在的数据源时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/v1/sources/99999',
|
||||
payload: {
|
||||
name: '更新',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com' },
|
||||
cronExpression: '0 * * * *',
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DELETE /api/v1/sources/:id', () => {
|
||||
it('应成功删除数据源', async () => {
|
||||
const source = await prisma.source.create({
|
||||
data: {
|
||||
name: '待删除源',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: `/api/v1/sources/${source.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.message).toBe('数据源删除成功');
|
||||
|
||||
const deleted = await prisma.source.findUnique({
|
||||
where: { id: source.id },
|
||||
});
|
||||
expect(deleted).toBeNull();
|
||||
});
|
||||
|
||||
it('删除不存在的数据源时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: '/api/v1/sources/99999',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/v1/sources/:id/collect', () => {
|
||||
it('应触发采集任务', async () => {
|
||||
const source = await prisma.source.create({
|
||||
data: {
|
||||
name: '测试源',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: `/api/v1/sources/${source.id}/collect`,
|
||||
});
|
||||
|
||||
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/sources/99999/collect',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user