| 1 | import { Agent } from '../agent'; |
| 2 | import { platform, ranking } from '../mockData'; |
| 3 | import { ContentStatus, ContentType } from '../../src/db/schema/content.schema'; |
| 4 | import { ObjectId } from 'mongodb'; |
| 5 | import { getModelToken } from '@nestjs/mongoose'; |
| 6 | import { Content } from '../../src/db/schema/content.schema'; |
| 7 | import { Model } from 'mongoose'; |
| 8 | import supertest from 'supertest'; |
| 9 | |
| 10 | describe('RankingContents (e2e)', () => { |
| 11 | let agent: supertest.Agent; |
| 12 | let contentModel: Model<Content>; |
| 13 | |
| 14 | beforeAll(async () => { |
| 15 | agent = Agent.get(); |
| 16 | contentModel = Agent.app().get(getModelToken(Content.name)); |
| 17 | }); |
| 18 | |
| 19 | beforeEach(async () => { |
| 20 | await contentModel.deleteMany({}); |
| 21 | |
| 22 | // 创建测试内容数据 |
| 23 | await contentModel.create([ |
| 24 | { |
| 25 | title: '内容1', |
| 26 | platformId: platform._id, |
| 27 | rankingId: ranking._id, |
| 28 | originalId: '1', |
| 29 | type: ContentType.ARTICLE, |
| 30 | category: '分类1', |
| 31 | url: 'http://test1.com', |
| 32 | status: ContentStatus.OPEN, |
| 33 | rankingPosition: 1, |
| 34 | }, |
| 35 | { |
| 36 | title: '内容2', |
| 37 | platformId: platform._id, |
| 38 | rankingId: ranking._id, |
| 39 | originalId: '2', |
| 40 | type: ContentType.ARTICLE, |
| 41 | category: '分类1', |
| 42 | url: 'http://test2.com', |
| 43 | status: ContentStatus.OPEN, |
| 44 | rankingPosition: 2, |
| 45 | }, |
| 46 | ]); |
| 47 | }); |
| 48 | |
| 49 | describe('GET /ranking/:id/contents', () => { |
| 50 | it('应该返回榜单内容列表', async () => { |
| 51 | const { body } = await agent |
| 52 | .get(`/ranking/${ranking._id}/contents`) |
| 53 | .expect(200); |
| 54 | |
| 55 | expect(Array.isArray(body.data.items)).toBe(true); |
| 56 | expect(body.data.items.length).toBe(2); |
| 57 | expect(body.data.items[0]).toMatchObject({ |
| 58 | title: '内容1', |
| 59 | originalId: '1', |
| 60 | rankingPosition: 1, |
| 61 | }); |
| 62 | expect(body.data.items[1]).toMatchObject({ |
| 63 | title: '内容2', |
| 64 | originalId: '2', |
| 65 | rankingPosition: 2, |
| 66 | }); |
| 67 | expect(body.data.meta).toMatchObject({ |
| 68 | itemCount: 2, |
| 69 | itemsPerPage: 20, |
| 70 | currentPage: 1, |
| 71 | }); |
| 72 | }); |
| 73 | |
| 74 | it('使用分页参数应该正确返回数据', async () => { |
| 75 | const { body } = await agent |
| 76 | .get(`/ranking/${ranking._id}/contents`) |
| 77 | .query({ page: 1, pageSize: 1 }) |
| 78 | .expect(200); |
| 79 | |
| 80 | expect(Array.isArray(body.data.items)).toBe(true); |
| 81 | expect(body.data.items.length).toBe(1); |
| 82 | expect(body.data.items[0]).toMatchObject({ |
| 83 | title: expect.any(String), |
| 84 | originalId: expect.any(String), |
| 85 | }); |
| 86 | }); |
| 87 | |
| 88 | it('无效的ObjectId应该返回400错误', async () => { |
| 89 | await agent.get('/ranking/invalid-id/contents').expect(400); |
| 90 | }); |
| 91 | |
| 92 | it('不存在的榜单ID应该返回空数组', async () => { |
| 93 | const nonExistentId = new ObjectId().toString(); |
| 94 | const { body } = await agent |
| 95 | .get(`/ranking/${nonExistentId}/contents`) |
| 96 | .expect(200); |
| 97 | |
| 98 | expect(Array.isArray(body.data.items)).toBe(true); |
| 99 | expect(body.data.items.length).toBe(0); |
| 100 | expect(body.data.meta).toMatchObject({ |
| 101 | itemCount: 0, |
| 102 | itemsPerPage: 20, |
| 103 | currentPage: 1, |
| 104 | }); |
| 105 | }); |
| 106 | }); |
| 107 | |
| 108 | describe('GET /ranking/:id/categories', () => { |
| 109 | beforeEach(async () => { |
| 110 | await contentModel.deleteMany({}); |
| 111 | |
| 112 | // 创建测试内容数据 |
| 113 | await contentModel.create([ |
| 114 | { |
| 115 | title: '内容1', |
| 116 | platformId: platform._id, |
| 117 | rankingId: ranking._id, |
| 118 | originalId: '1', |
| 119 | type: ContentType.ARTICLE, |
| 120 | category: '分类1', |
| 121 | url: 'http://test1.com', |
| 122 | status: ContentStatus.OPEN, |
| 123 | }, |
| 124 | { |
| 125 | title: '内容2', |
| 126 | platformId: platform._id, |
| 127 | rankingId: ranking._id, |
| 128 | originalId: '2', |
| 129 | type: ContentType.ARTICLE, |
| 130 | category: '分类2', |
| 131 | url: 'http://test2.com', |
| 132 | status: ContentStatus.OPEN, |
| 133 | }, |
| 134 | { |
| 135 | title: '内容3', |
| 136 | platformId: platform._id, |
| 137 | rankingId: ranking._id, |
| 138 | originalId: '3', |
| 139 | type: ContentType.ARTICLE, |
| 140 | category: '分类1', // 重复的分类 |
| 141 | url: 'http://test3.com', |
| 142 | status: ContentStatus.STOP, // 停用状态 |
| 143 | }, |
| 144 | { |
| 145 | title: '内容4', |
| 146 | platformId: new ObjectId(), // 不同平台 |
| 147 | rankingId: ranking._id, |
| 148 | originalId: '4', |
| 149 | type: ContentType.ARTICLE, |
| 150 | category: '分类3', |
| 151 | url: 'http://test4.com', |
| 152 | status: ContentStatus.OPEN, |
| 153 | }, |
| 154 | ]); |
| 155 | }); |
| 156 | |
| 157 | it('应该返回榜单内容的唯一分类列表', async () => { |
| 158 | const { body } = await agent |
| 159 | .get(`/ranking/${ranking._id}/categories`) |
| 160 | .expect(200); |
| 161 | |
| 162 | expect(Array.isArray(body.data)).toBe(true); |
| 163 | expect(body.data).toHaveLength(2); |
| 164 | expect(body.data).toContain('分类1'); |
| 165 | expect(body.data).toContain('分类2'); |
| 166 | // 不应该包含停用状态的内容分类和其他平台的分类 |
| 167 | expect(body.data).not.toContain('分类3'); |
| 168 | }); |
| 169 | |
| 170 | it('无效的ObjectId应该返回400错误', async () => { |
| 171 | await agent.get('/ranking/invalid-id/categories').expect(400); |
| 172 | }); |
| 173 | |
| 174 | it('不存在的榜单ID应该返回空数组', async () => { |
| 175 | const nonExistentId = new ObjectId().toString(); |
| 176 | const { body } = await agent |
| 177 | .get(`/ranking/${nonExistentId}/categories`) |
| 178 | .expect(200); |
| 179 | |
| 180 | expect(Array.isArray(body.data)).toBe(true); |
| 181 | expect(body.data).toHaveLength(0); |
| 182 | }); |
| 183 | }); |
| 184 | }); |
| 185 |