Files
hughhuangandClaude Sonnet 4.5 697430feed 添加后端集成测试套件
- 实现 82 个集成测试用例覆盖所有 API 端点
- 使用真实数据库连接,无 mock 逻辑
- 修复 PUT 请求只更新提供字段的逻辑
- 配置 Vitest 串行执行避免测试竞态

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-05 15:23:37 +08:00

388 lines
11 KiB
TypeScript

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/articles', () => {
it('应返回资讯列表', async () => {
const source = await prisma.source.create({
data: {
name: '测试源',
type: 'rss',
config: { url: 'https://example.com/rss' },
cronExpression: '0 * * * *',
enabled: true,
},
});
await prisma.article.createMany({
data: [
{
sourceId: source.id,
collectedAt: new Date('2024-01-01T00:00:00Z'),
title: '文章 1',
url: 'https://example.com/1',
content: '内容 1',
publishedAt: new Date('2024-01-01'),
collectedAt: new Date('2024-01-01T00:00:00Z'),
},
{
sourceId: source.id,
collectedAt: new Date('2024-01-01T00:00:00Z'),
title: '文章 2',
url: 'https://example.com/2',
content: '内容 2',
publishedAt: new Date('2024-01-02'),
collectedAt: new Date('2024-01-02T00:00:00Z'),
},
],
});
const response = await app.inject({
method: 'GET',
url: '/api/v1/articles',
});
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].source).toBeDefined();
});
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,
},
});
await prisma.article.create({
data: {
sourceId: source1.id,
title: '来自源 1',
url: 'https://example.com/1',
content: '内容',
collectedAt: new Date('2024-01-01T00:00:00Z'),
},
});
await prisma.article.create({
data: {
sourceId: source2.id,
title: '来自源 2',
url: 'https://example.com/2',
content: '内容',
collectedAt: new Date('2024-01-01T00:00:00Z'),
},
});
const response = await app.inject({
method: 'GET',
url: `/api/v1/articles?sourceId=${source1.id}`,
});
expect(response.statusCode).toBe(200);
const body = JSON.parse(response.body);
expect(body.data).toHaveLength(1);
expect(body.data[0].sourceId).toBe(source1.id);
});
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 source3 = await prisma.source.create({
data: {
name: '源 3',
type: 'rss',
config: { url: 'https://example.com/rss3' },
cronExpression: '0 * * * *',
enabled: true,
},
});
const topic = await prisma.topic.create({
data: {
name: 'AI 资讯',
processMode: 'summary',
scheduleConfig: { cron: '0 0 * * 0' },
enabled: true,
},
});
// 关联源 1 和源 2 到主题
await prisma.topicSource.createMany({
data: [
{ topicId: topic.id, sourceId: source1.id },
{ topicId: topic.id, sourceId: source2.id },
],
});
// 创建文章
await prisma.article.create({
data: {
sourceId: source1.id,
title: '文章 1',
url: 'https://example.com/1',
content: '内容',
collectedAt: new Date('2024-01-01T00:00:00Z'),
},
});
await prisma.article.create({
data: {
sourceId: source2.id,
title: '文章 2',
url: 'https://example.com/2',
content: '内容',
collectedAt: new Date('2024-01-01T00:00:00Z'),
},
});
await prisma.article.create({
data: {
sourceId: source3.id,
title: '文章 3',
url: 'https://example.com/3',
content: '内容',
collectedAt: new Date('2024-01-01T00:00:00Z'),
},
});
const response = await app.inject({
method: 'GET',
url: `/api/v1/articles?topicId=${topic.id}`,
});
expect(response.statusCode).toBe(200);
const body = JSON.parse(response.body);
// 只返回源 1 和源 2 的文章
expect(body.data).toHaveLength(2);
const sourceIds = body.data.map((item: any) => item.sourceId);
expect(sourceIds).toContain(source1.id);
expect(sourceIds).toContain(source2.id);
expect(sourceIds).not.toContain(source3.id);
});
it('应支持按日期范围筛选', async () => {
const source = await prisma.source.create({
data: {
name: '测试源',
type: 'rss',
config: { url: 'https://example.com/rss' },
cronExpression: '0 * * * *',
enabled: true,
},
});
await prisma.article.create({
data: {
sourceId: source.id,
collectedAt: new Date('2024-01-01T00:00:00Z'),
title: '旧文章',
url: 'https://example.com/old',
content: '内容',
publishedAt: new Date('2024-01-01'),
},
});
await prisma.article.create({
data: {
sourceId: source.id,
collectedAt: new Date('2024-01-01T00:00:00Z'),
title: '新文章',
url: 'https://example.com/new',
content: '内容',
publishedAt: new Date('2024-01-15'),
},
});
const response = await app.inject({
method: 'GET',
url: '/api/v1/articles?startDate=2024-01-10&endDate=2024-01-20',
});
expect(response.statusCode).toBe(200);
const body = JSON.parse(response.body);
expect(body.data).toHaveLength(1);
expect(body.data[0].title).toBe('新文章');
});
it('应支持分页', async () => {
const source = await prisma.source.create({
data: {
name: '测试源',
type: 'rss',
config: { url: 'https://example.com/rss' },
cronExpression: '0 * * * *',
enabled: true,
},
});
const data = Array.from({ length: 25 }, (_, i) => ({
sourceId: source.id,
collectedAt: new Date('2024-01-01T00:00:00Z'),
title: `文章 ${i + 1}`,
url: `https://example.com/${i}`,
content: '内容',
}));
await prisma.article.createMany({ data });
const response = await app.inject({
method: 'GET',
url: '/api/v1/articles?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/articles/:id', () => {
it('应返回单篇资讯', async () => {
const source = await prisma.source.create({
data: {
name: '测试源',
type: 'rss',
config: { url: 'https://example.com/rss' },
cronExpression: '0 * * * *',
enabled: true,
},
});
const article = await prisma.article.create({
data: {
sourceId: source.id,
collectedAt: new Date('2024-01-01T00:00:00Z'),
title: '测试文章',
url: 'https://example.com/test',
content: '这是文章内容',
summary: '摘要',
publishedAt: new Date('2024-01-01'),
},
});
const response = await app.inject({
method: 'GET',
url: `/api/v1/articles/${article.id}`,
});
expect(response.statusCode).toBe(200);
const body = JSON.parse(response.body);
expect(body.data.id).toBe(article.id);
expect(body.data.title).toBe('测试文章');
expect(body.data.source).toBeDefined();
expect(body.data.source.name).toBe('测试源');
});
it('资讯不存在时应返回 404', async () => {
const response = await app.inject({
method: 'GET',
url: '/api/v1/articles/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/articles/:id', () => {
it('应成功删除资讯', async () => {
const source = await prisma.source.create({
data: {
name: '测试源',
type: 'rss',
config: { url: 'https://example.com/rss' },
cronExpression: '0 * * * *',
enabled: true,
},
});
const article = await prisma.article.create({
data: {
sourceId: source.id,
collectedAt: new Date('2024-01-01T00:00:00Z'),
title: '待删除文章',
url: 'https://example.com/delete',
content: '内容',
},
});
const response = await app.inject({
method: 'DELETE',
url: `/api/v1/articles/${article.id}`,
});
expect(response.statusCode).toBe(200);
const body = JSON.parse(response.body);
expect(body.message).toBe('资讯删除成功');
const deleted = await prisma.article.findUnique({
where: { id: article.id },
});
expect(deleted).toBeNull();
});
it('删除不存在的资讯时应返回 404', async () => {
const response = await app.inject({
method: 'DELETE',
url: '/api/v1/articles/99999',
});
expect(response.statusCode).toBe(404);
});
});
});