返回 AiToEarn
topic.e2e-spec.ts
根目录 / project / aitoearn-electron / server / test / topic / topic.e2e-spec.ts
1 import { Agent } from '../agent';
2 import supertest from 'supertest';
3 import { Model } from 'mongoose';
4 import { getModelToken } from '@nestjs/mongoose';
5 import { platform } from '../mockData';
6 import { Topic } from '../../src/db/schema/topic.schema';
7
8 describe('TopicController (e2e)', () => {
9 let agent: supertest.Agent;
10 let topicModel: Model<Topic>;
11
12 const mockTopics = [
13 {
14 platformId: platform._id,
15 title: 'Test Topic 1',
16 description: 'Test Description 1',
17 category: 'Entertainment',
18 subCategory: 'Movies',
19 url: 'http://example.com/topic1',
20 author: 'Author 1',
21 topics: ['movie', 'entertainment'],
22 rank: 1,
23 createTime: new Date(),
24 updateTime: new Date(),
25 },
26 {
27 platformId: platform._id,
28 title: 'Test Topic 2',
29 description: 'Test Description 2',
30 category: 'Technology',
31 subCategory: 'AI',
32 url: 'http://example.com/topic2',
33 author: 'Author 2',
34 topics: ['ai', 'tech'],
35 rank: 2,
36 createTime: new Date(),
37 updateTime: new Date(),
38 },
39 ];
40
41 beforeAll(async () => {
42 agent = Agent.get();
43 topicModel = Agent.app().get(getModelToken(Topic.name));
44 await topicModel.insertMany(mockTopics);
45 });
46
47 afterAll(async () => {
48 await topicModel.deleteMany({});
49 });
50
51 describe('/topics (GET)', () => {
52 it('should return all topics', () => {
53 return agent
54 .get('/topics')
55 .expect(200)
56 .expect((res) => {
57 expect(res.body.code).toBe(0);
58 expect(res.body.data.items).toHaveLength(2);
59 expect(res.body.data.items[0]).toHaveProperty('title');
60 expect(res.body.data.items[0]).toHaveProperty('category');
61 expect(res.body.data.meta).toMatchObject({
62 currentPage: 1,
63 itemsPerPage: 20,
64 totalItems: 2,
65 totalPages: 1
66 });
67 });
68 });
69
70 it('should filter topics by category', () => {
71 return agent
72 .get('/topics?category=Technology')
73 .expect(200)
74 .expect((res) => {
75 expect(res.body.code).toBe(0);
76 expect(res.body.data.items).toHaveLength(1);
77 expect(res.body.data.items[0].category).toBe('Technology');
78 expect(res.body.data.meta).toMatchObject({
79 currentPage: 1,
80 itemsPerPage: 20,
81 totalItems: 1,
82 totalPages: 1
83 });
84 });
85 });
86 });
87
88 describe('/topics/categories (GET)', () => {
89 it('should return all categories', () => {
90 return agent
91 .get('/topics/categories')
92 .expect(200)
93 .expect((res) => {
94 expect(res.body.code).toBe(0);
95 expect(Array.isArray(res.body.data)).toBe(true);
96 expect(res.body.data).toContain('Entertainment');
97 expect(res.body.data).toContain('Technology');
98 });
99 });
100 });
101
102 describe('/topics/sub-categories (GET)', () => {
103 it('should return all sub-categories for a category', () => {
104 return agent
105 .get('/topics/sub-categories?category=Technology')
106 .expect(200)
107 .expect((res) => {
108 expect(res.body.code).toBe(0);
109 expect(Array.isArray(res.body.data)).toBe(true);
110 expect(res.body.data).toContain('AI');
111 });
112 });
113 });
114
115 describe('/topics/topics (GET)', () => {
116 it('should return all topic tags', () => {
117 return agent
118 .get('/topics/topics')
119 .expect(200)
120 .expect((res) => {
121 expect(res.body.code).toBe(0);
122 expect(Array.isArray(res.body.data)).toBe(true);
123 expect(res.body.data).toContain('movie');
124 expect(res.body.data).toContain('ai');
125 });
126 });
127 });
128 });
129
129 lines TYPESCRIPT