Files

363 lines
10 KiB
TypeScript
Raw Permalink Normal View History

2026-02-05 15:23:37 +08:00
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([]);
});
});
});