添加后端集成测试套件
- 实现 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,387 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,341 @@
|
||||
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/channels', () => {
|
||||
it('应成功创建邮件类型的推送渠道', async () => {
|
||||
const payload = {
|
||||
name: '邮件通知',
|
||||
type: 'email',
|
||||
config: {
|
||||
to: 'test@example.com',
|
||||
subject: '资讯周报',
|
||||
},
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/channels',
|
||||
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('email');
|
||||
});
|
||||
|
||||
it('应成功创建 Slack 类型的推送渠道', async () => {
|
||||
const payload = {
|
||||
name: 'Slack 通知',
|
||||
type: 'slack',
|
||||
config: {
|
||||
webhookUrl: 'https://hooks.slack.com/services/xxx',
|
||||
},
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/channels',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.type).toBe('slack');
|
||||
});
|
||||
|
||||
it('应成功创建钉钉类型的推送渠道', async () => {
|
||||
const payload = {
|
||||
name: '钉钉通知',
|
||||
type: 'dingtalk',
|
||||
config: {
|
||||
webhookUrl: 'https://oapi.dingtalk.com/robot/send',
|
||||
secret: 'SEC123',
|
||||
},
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/channels',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.type).toBe('dingtalk');
|
||||
});
|
||||
|
||||
it('应成功创建企业微信类型的推送渠道', async () => {
|
||||
const payload = {
|
||||
name: '企业微信通知',
|
||||
type: 'wechat',
|
||||
config: {
|
||||
webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send',
|
||||
},
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/channels',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.type).toBe('wechat');
|
||||
});
|
||||
|
||||
it('缺少必填字段时应返回 400 错误', async () => {
|
||||
const payload = {
|
||||
name: '不完整渠道',
|
||||
type: 'email',
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/channels',
|
||||
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: 'email',
|
||||
config: { to: 'test@example.com' },
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/channels',
|
||||
payload,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(201);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.enabled).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/channels', () => {
|
||||
it('应返回推送渠道列表', async () => {
|
||||
await prisma.channel.createMany({
|
||||
data: [
|
||||
{
|
||||
name: '邮件渠道',
|
||||
type: 'email',
|
||||
config: { to: 'test@example.com' },
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'Slack 渠道',
|
||||
type: 'slack',
|
||||
config: { webhookUrl: 'https://hooks.slack.com/xxx' },
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/channels',
|
||||
});
|
||||
|
||||
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.channel.createMany({
|
||||
data: [
|
||||
{
|
||||
name: '邮件渠道 1',
|
||||
type: 'email',
|
||||
config: { to: 'test1@example.com' },
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'Slack 渠道 1',
|
||||
type: 'slack',
|
||||
config: { webhookUrl: 'https://hooks.slack.com/xxx' },
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/channels?type=email',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data).toHaveLength(1);
|
||||
expect(body.data[0].type).toBe('email');
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/channels/:id', () => {
|
||||
it('应返回单个推送渠道及其关联的主题', async () => {
|
||||
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: '测试主题',
|
||||
processMode: 'summary',
|
||||
scheduleConfig: { cron: '0 0 * * *' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.topicChannel.create({
|
||||
data: {
|
||||
topicId: topic.id,
|
||||
channelId: channel.id,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: `/api/v1/channels/${channel.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.id).toBe(channel.id);
|
||||
expect(body.data.topicChannels).toHaveLength(1);
|
||||
expect(body.data.topicChannels[0].topic.name).toBe('测试主题');
|
||||
});
|
||||
|
||||
it('推送渠道不存在时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/channels/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/channels/:id', () => {
|
||||
it('应成功更新推送渠道', async () => {
|
||||
const channel = await prisma.channel.create({
|
||||
data: {
|
||||
name: '原始渠道',
|
||||
type: 'email',
|
||||
config: { to: 'old@example.com' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const updateData = {
|
||||
name: '更新后渠道',
|
||||
config: { to: 'new@example.com' },
|
||||
enabled: false,
|
||||
};
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'PUT',
|
||||
url: `/api/v1/channels/${channel.id}`,
|
||||
payload: updateData,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data.name).toBe('更新后渠道');
|
||||
expect(body.data.config.to).toBe('new@example.com');
|
||||
expect(body.data.enabled).toBe(false);
|
||||
expect(body.message).toBe('推送渠道更新成功');
|
||||
});
|
||||
|
||||
it('更新不存在的推送渠道时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/v1/channels/99999',
|
||||
payload: {
|
||||
name: '更新',
|
||||
type: 'email',
|
||||
config: { to: 'test@example.com' },
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DELETE /api/v1/channels/:id', () => {
|
||||
it('应成功删除推送渠道', async () => {
|
||||
const channel = await prisma.channel.create({
|
||||
data: {
|
||||
name: '待删除渠道',
|
||||
type: 'email',
|
||||
config: { to: 'test@example.com' },
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: `/api/v1/channels/${channel.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.message).toBe('推送渠道删除成功');
|
||||
|
||||
const deleted = await prisma.channel.findUnique({
|
||||
where: { id: channel.id },
|
||||
});
|
||||
expect(deleted).toBeNull();
|
||||
});
|
||||
|
||||
it('删除不存在的推送渠道时应返回 404', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: '/api/v1/channels/99999',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(404);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,363 @@
|
||||
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/collection-logs', () => {
|
||||
it('应返回采集日志列表', async () => {
|
||||
const source = await prisma.source.create({
|
||||
data: {
|
||||
name: '测试源',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.collectionLog.createMany({
|
||||
data: [
|
||||
{
|
||||
sourceId: source.id,
|
||||
status: 'success',
|
||||
articlesCount: 5,
|
||||
startedAt: new Date('2024-01-01T00:00:00Z'),
|
||||
finishedAt: new Date('2024-01-01T00:01:00Z'),
|
||||
},
|
||||
{
|
||||
sourceId: source.id,
|
||||
status: 'failed',
|
||||
errorMessage: '连接超时',
|
||||
startedAt: new Date('2024-01-02T00:00:00Z'),
|
||||
finishedAt: new Date('2024-01-02T00:01:00Z'),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/collection-logs',
|
||||
});
|
||||
|
||||
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.collectionLog.create({
|
||||
data: {
|
||||
sourceId: source1.id,
|
||||
status: 'success',
|
||||
articlesCount: 10,
|
||||
startedAt: new Date('2024-01-01T00:00:00Z'),
|
||||
finishedAt: new Date('2024-01-01T00:01:00Z'),
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.collectionLog.create({
|
||||
data: {
|
||||
sourceId: source2.id,
|
||||
status: 'success',
|
||||
articlesCount: 5,
|
||||
startedAt: new Date('2024-01-01T00:00:00Z'),
|
||||
finishedAt: new Date('2024-01-01T00:01:00Z'),
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: `/api/v1/collection-logs?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);
|
||||
expect(body.data[0].articlesCount).toBe(10);
|
||||
});
|
||||
|
||||
it('应支持按状态筛选', async () => {
|
||||
const source = await prisma.source.create({
|
||||
data: {
|
||||
name: '测试源',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.collectionLog.createMany({
|
||||
data: [
|
||||
{
|
||||
sourceId: source.id,
|
||||
status: 'success',
|
||||
articlesCount: 10,
|
||||
startedAt: new Date('2024-01-01T00:00:00Z'),
|
||||
finishedAt: new Date('2024-01-01T00:01:00Z'),
|
||||
},
|
||||
{
|
||||
sourceId: source.id,
|
||||
status: 'failed',
|
||||
errorMessage: '错误',
|
||||
startedAt: new Date('2024-01-02T00:00:00Z'),
|
||||
finishedAt: new Date('2024-01-02T00:01:00Z'),
|
||||
},
|
||||
{
|
||||
sourceId: source.id,
|
||||
status: 'running',
|
||||
startedAt: new Date('2024-01-03T00:00:00Z'),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/collection-logs?status=success',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data).toHaveLength(1);
|
||||
expect(body.data[0].status).toBe('success');
|
||||
});
|
||||
|
||||
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 }, () => ({
|
||||
sourceId: source.id,
|
||||
status: 'success' as const,
|
||||
articlesCount: 5,
|
||||
startedAt: new Date('2024-01-01T00:00:00Z'),
|
||||
finishedAt: new Date('2024-01-01T00:01:00Z'),
|
||||
}));
|
||||
await prisma.collectionLog.createMany({ data });
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/collection-logs?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);
|
||||
});
|
||||
|
||||
it('应按创建时间倒序排列', async () => {
|
||||
const source = await prisma.source.create({
|
||||
data: {
|
||||
name: '测试源',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
const log1 = await prisma.collectionLog.create({
|
||||
data: {
|
||||
sourceId: source.id,
|
||||
status: 'success',
|
||||
articlesCount: 5,
|
||||
startedAt: new Date('2024-01-01T00:00:00Z'),
|
||||
finishedAt: new Date('2024-01-01T00:01:00Z'),
|
||||
},
|
||||
});
|
||||
|
||||
// 等待确保时间差
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
|
||||
const log2 = await prisma.collectionLog.create({
|
||||
data: {
|
||||
sourceId: source.id,
|
||||
status: 'success',
|
||||
articlesCount: 3,
|
||||
startedAt: new Date('2024-01-02T00:00:00Z'),
|
||||
finishedAt: new Date('2024-01-02T00:01:00Z'),
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/collection-logs',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data[0].id).toBe(log2.id);
|
||||
expect(body.data[1].id).toBe(log1.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/collection-logs/stats', () => {
|
||||
it('应返回所有数据源的统计信息', async () => {
|
||||
const source1 = await prisma.source.create({
|
||||
data: {
|
||||
name: 'RSS 源 1',
|
||||
type: 'rss',
|
||||
config: { url: 'https://example.com/rss1' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: true,
|
||||
lastCollectedAt: new Date('2024-01-15T10:00:00Z'),
|
||||
},
|
||||
});
|
||||
|
||||
const source2 = await prisma.source.create({
|
||||
data: {
|
||||
name: 'API 源 1',
|
||||
type: 'api',
|
||||
config: { url: 'https://api.example.com' },
|
||||
cronExpression: '0 * * * *',
|
||||
enabled: false,
|
||||
},
|
||||
});
|
||||
|
||||
// 为源 1 创建文章
|
||||
await prisma.article.createMany({
|
||||
data: [
|
||||
{
|
||||
sourceId: source1.id,
|
||||
title: '文章 1',
|
||||
url: 'https://example.com/1',
|
||||
content: '内容',
|
||||
collectedAt: new Date('2024-01-01T00:00:00Z'),
|
||||
},
|
||||
{
|
||||
sourceId: source1.id,
|
||||
title: '文章 2',
|
||||
url: 'https://example.com/2',
|
||||
content: '内容',
|
||||
collectedAt: new Date('2024-01-01T00:00:00Z'),
|
||||
},
|
||||
{
|
||||
sourceId: source1.id,
|
||||
title: '文章 3',
|
||||
url: 'https://example.com/3',
|
||||
content: '内容',
|
||||
collectedAt: new Date('2024-01-01T00:00:00Z'),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// 为源 1 创建采集日志
|
||||
await prisma.collectionLog.createMany({
|
||||
data: [
|
||||
{
|
||||
sourceId: source1.id,
|
||||
status: 'success',
|
||||
articlesCount: 3,
|
||||
startedAt: new Date('2024-01-15T10:00:00Z'),
|
||||
finishedAt: new Date('2024-01-15T10:01:00Z'),
|
||||
},
|
||||
{
|
||||
sourceId: source1.id,
|
||||
status: 'failed',
|
||||
errorMessage: '超时',
|
||||
startedAt: new Date('2024-01-14T10:00:00Z'),
|
||||
finishedAt: new Date('2024-01-14T10:01:00Z'),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// 为源 2 创建文章
|
||||
await prisma.article.create({
|
||||
data: {
|
||||
sourceId: source2.id,
|
||||
title: '文章 4',
|
||||
url: 'https://example.com/4',
|
||||
content: '内容',
|
||||
collectedAt: new Date('2024-01-01T00:00:00Z'),
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/collection-logs/stats',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data).toHaveLength(2);
|
||||
|
||||
const stats1 = body.data.find((s: any) => s.id === source1.id);
|
||||
expect(stats1).toBeDefined();
|
||||
expect(stats1.name).toBe('RSS 源 1');
|
||||
expect(stats1.type).toBe('rss');
|
||||
expect(stats1.enabled).toBe(true);
|
||||
expect(stats1.totalArticles).toBe(3);
|
||||
expect(stats1.totalCollections).toBe(2);
|
||||
expect(stats1.lastCollectedAt).toBeDefined();
|
||||
expect(stats1.lastLog).toBeDefined();
|
||||
expect(stats1.lastLog.status).toBe('success');
|
||||
|
||||
const stats2 = body.data.find((s: any) => s.id === source2.id);
|
||||
expect(stats2).toBeDefined();
|
||||
expect(stats2.name).toBe('API 源 1');
|
||||
expect(stats2.enabled).toBe(false);
|
||||
expect(stats2.totalArticles).toBe(1);
|
||||
expect(stats2.totalCollections).toBe(0);
|
||||
expect(stats2.lastCollectedAt).toBeNull();
|
||||
expect(stats2.lastLog).toBeNull();
|
||||
});
|
||||
|
||||
it('应返回空数组当没有数据源时', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/collection-logs/stats',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.data).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,50 @@
|
||||
import { beforeAll, afterAll, 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();
|
||||
await cleanupDatabase();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
await cleanupDatabase();
|
||||
});
|
||||
|
||||
describe('GET /api/v1/health', () => {
|
||||
it('应返回健康状态', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/health',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.success).toBe(true);
|
||||
expect(body.data.status).toBe('ok');
|
||||
expect(body.data.timestamp).toBeDefined();
|
||||
expect(body.data.version).toBe('1.0.0');
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/health/detailed', () => {
|
||||
it('应返回详细的健康状态包括数据库检查', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/health/detailed',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = JSON.parse(response.body);
|
||||
expect(body.success).toBe(true);
|
||||
expect(body.data.status).toBe('ok');
|
||||
expect(body.data.checks).toBeDefined();
|
||||
expect(body.data.checks.server).toBe('ok');
|
||||
expect(body.data.checks.database).toBe('ok');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,73 @@
|
||||
import Fastify from 'fastify';
|
||||
import cors from '@fastify/cors';
|
||||
import { healthRoutes } from '../../src/web/routes/health';
|
||||
import { llmConfigRoutes } from '../../src/web/routes/llm-configs';
|
||||
import { sourceRoutes } from '../../src/web/routes/sources';
|
||||
import { channelRoutes } from '../../src/web/routes/channels';
|
||||
import { topicRoutes } from '../../src/web/routes/topics';
|
||||
import { reportRoutes } from '../../src/web/routes/reports';
|
||||
import { articleRoutes } from '../../src/web/routes/articles';
|
||||
import { collectionLogRoutes } from '../../src/web/routes/collection-logs';
|
||||
import { AppError } from '../../src/utils/errors';
|
||||
|
||||
/**
|
||||
* 创建用于测试的 Fastify 应用实例
|
||||
*/
|
||||
export async function createTestApp() {
|
||||
const app = Fastify({
|
||||
logger: false,
|
||||
});
|
||||
|
||||
// 注册 CORS
|
||||
await app.register(cors, {
|
||||
origin: true,
|
||||
credentials: true,
|
||||
});
|
||||
|
||||
// 简单的错误处理器(复制自 error-handler.ts)
|
||||
app.setErrorHandler(async (error, request, reply) => {
|
||||
if (error instanceof AppError) {
|
||||
return reply.status(error.statusCode).send({
|
||||
success: false,
|
||||
error: error.message,
|
||||
...(error instanceof Error && process.env.NODE_ENV === 'development' && {
|
||||
stack: error.stack,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
// 处理验证错误
|
||||
if (error.validation) {
|
||||
return reply.status(400).send({
|
||||
success: false,
|
||||
error: '请求参数验证失败',
|
||||
details: error.validation,
|
||||
});
|
||||
}
|
||||
|
||||
// 未知错误
|
||||
reply.status(500).send({
|
||||
success: false,
|
||||
error: '服务器内部错误',
|
||||
});
|
||||
});
|
||||
|
||||
// 注册路由
|
||||
await app.register(healthRoutes, { prefix: '/api/v1' });
|
||||
await app.register(llmConfigRoutes, { prefix: '/api/v1/llm-configs' });
|
||||
await app.register(sourceRoutes, { prefix: '/api/v1/sources' });
|
||||
await app.register(channelRoutes, { prefix: '/api/v1/channels' });
|
||||
await app.register(topicRoutes, { prefix: '/api/v1/topics' });
|
||||
await app.register(reportRoutes, { prefix: '/api/v1/reports' });
|
||||
await app.register(articleRoutes, { prefix: '/api/v1/articles' });
|
||||
await app.register(collectionLogRoutes, { prefix: '/api/v1/collection-logs' });
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待一段时间(用于异步操作)
|
||||
*/
|
||||
export function wait(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
// 使用单独的测试数据库 URL(如果配置了)或默认数据库
|
||||
const testDatabaseUrl = process.env.TEST_DATABASE_URL || process.env.DATABASE_URL;
|
||||
|
||||
const prisma = new PrismaClient({
|
||||
datasources: {
|
||||
db: {
|
||||
url: testDatabaseUrl,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export async function setup() {
|
||||
await prisma.$connect();
|
||||
console.log('测试数据库已连接');
|
||||
}
|
||||
|
||||
export async function teardown() {
|
||||
await prisma.$disconnect();
|
||||
console.log('测试数据库已断开');
|
||||
}
|
||||
|
||||
// 清理数据库(按依赖关系顺序删除表以避免外键约束)
|
||||
export async function cleanupDatabase() {
|
||||
// 按依赖关系顺序删除(从最底层的依赖开始)
|
||||
// 删除所有关联和日志表
|
||||
await prisma.pushLog.deleteMany({});
|
||||
await prisma.conversation.deleteMany({});
|
||||
await prisma.article.deleteMany({});
|
||||
await prisma.report.deleteMany({});
|
||||
await prisma.collectionLog.deleteMany({});
|
||||
|
||||
// 删除关联表(在删除主表之前)
|
||||
await prisma.topicSource.deleteMany({});
|
||||
await prisma.topicChannel.deleteMany({});
|
||||
|
||||
// 删除主表
|
||||
await prisma.topic.deleteMany({});
|
||||
await prisma.channel.deleteMany({});
|
||||
await prisma.source.deleteMany({});
|
||||
await prisma.llmConfig.deleteMany({});
|
||||
}
|
||||
|
||||
export { prisma };
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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