activities.integration.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. import { describe, it, expect, beforeEach } from 'vitest';
  2. import { testClient } from 'hono/testing';
  3. import {
  4. IntegrationTestDatabase,
  5. setupIntegrationDatabaseHooks,
  6. TestDataFactory
  7. } from '~/utils/server/integration-test-db';
  8. import { IntegrationTestAssertions } from '~/utils/server/integration-test-utils';
  9. import { adminActivitiesRoutesExport } from '@d8d/server/api';
  10. import { AuthService } from '@d8d/server/modules/auth/auth.service';
  11. import { ActivityType } from '@d8d/server/modules/activities/activity.entity';
  12. import { UserService } from '@d8d/server/modules/users/user.service';
  13. // 设置集成测试钩子
  14. setupIntegrationDatabaseHooks()
  15. describe('活动管理API集成测试', () => {
  16. let client: ReturnType<typeof testClient<typeof adminActivitiesRoutesExport>>['api']['v1']['admin'];
  17. let testToken: string;
  18. beforeEach(async () => {
  19. // 创建测试客户端
  20. client = testClient(adminActivitiesRoutesExport).api.v1.admin;
  21. // 创建测试用户并生成token
  22. const dataSource = await IntegrationTestDatabase.getDataSource();
  23. const userService = new UserService(dataSource);
  24. const authService = new AuthService(userService);
  25. // 确保admin用户存在
  26. const user = await authService.ensureAdminExists();
  27. // 生成admin用户的token
  28. testToken = authService.generateToken(user);
  29. });
  30. describe('活动创建测试', () => {
  31. it('应该成功创建去程活动', async () => {
  32. const dataSource = await IntegrationTestDatabase.getDataSource();
  33. const testLocation = await TestDataFactory.createTestLocation(dataSource);
  34. const activityData = {
  35. name: '测试活动',
  36. description: '这是一个测试活动',
  37. startDate: '2025-10-17T08:00:00.000Z',
  38. endDate: '2025-10-17T18:00:00.000Z',
  39. venueLocationId: testLocation.id
  40. };
  41. const response = await client.activities.$post({
  42. json: activityData,
  43. },
  44. {
  45. headers: {
  46. 'Authorization': `Bearer ${testToken}`
  47. }
  48. });
  49. // 断言响应
  50. expect(response.status).toBe(201);
  51. if (response.status === 201) {
  52. const responseData = await response.json();
  53. expect(responseData).toHaveProperty('id');
  54. expect(responseData.name).toBe(activityData.name);
  55. expect(responseData.type).toBe(activityData.type);
  56. expect(responseData.isDisabled).toBe(0); // 默认启用
  57. // 断言数据库中存在活动
  58. await IntegrationTestAssertions.expectActivityToExist(responseData.id);
  59. }
  60. });
  61. it('应该成功创建返程活动', async () => {
  62. const dataSource = await IntegrationTestDatabase.getDataSource();
  63. const testLocation = await TestDataFactory.createTestLocation(dataSource);
  64. const activityData = {
  65. name: '测试返程活动',
  66. description: '这是一个测试返程活动',
  67. startDate: '2025-10-17T16:00:00.000Z',
  68. endDate: '2025-10-17T20:00:00.000Z',
  69. venueLocationId: testLocation.id
  70. };
  71. const response = await client.activities.$post({
  72. json: activityData,
  73. },
  74. {
  75. headers: {
  76. 'Authorization': `Bearer ${testToken}`
  77. }
  78. });
  79. expect(response.status).toBe(201);
  80. if (response.status === 201) {
  81. const responseData = await response.json();
  82. expect(responseData.type).toBe('return');
  83. }
  84. });
  85. it('应该拒绝创建无效活动类型的活动', async () => {
  86. const dataSource = await IntegrationTestDatabase.getDataSource();
  87. const testLocation = await TestDataFactory.createTestLocation(dataSource);
  88. const activityData = {
  89. name: '测试无效活动',
  90. description: '这是一个测试活动',
  91. type: 'invalid_type' as any, // 无效类型
  92. startDate: '2025-10-17T08:00:00.000Z',
  93. endDate: '2025-10-17T18:00:00.000Z',
  94. venueLocationId: testLocation.id
  95. };
  96. const response = await client.activities.$post({
  97. json: activityData,
  98. },
  99. {
  100. headers: {
  101. 'Authorization': `Bearer ${testToken}`
  102. }
  103. });
  104. // 应该返回验证错误
  105. expect([400, 500]).toContain(response.status);
  106. });
  107. });
  108. describe('活动读取测试', () => {
  109. it('应该成功获取活动列表', async () => {
  110. const dataSource = await IntegrationTestDatabase.getDataSource();
  111. if (!dataSource) throw new Error('Database not initialized');
  112. // 创建几个测试活动
  113. await TestDataFactory.createTestActivity(dataSource, { name: '活动1' });
  114. await TestDataFactory.createTestActivity(dataSource, { name: '活动2' });
  115. const response = await client.activities.$get({
  116. query: {}
  117. },
  118. {
  119. headers: {
  120. 'Authorization': `Bearer ${testToken}`
  121. }
  122. });
  123. if (response.status !== 200) {
  124. const errorData = await response.json();
  125. console.debug('获取活动列表失败:', errorData);
  126. }
  127. expect(response.status).toBe(200);
  128. if (response.status === 200) {
  129. const responseData = await response.json();
  130. expect(Array.isArray(responseData.data)).toBe(true);
  131. expect(responseData.data.length).toBeGreaterThanOrEqual(2);
  132. }
  133. });
  134. it('应该成功获取单个活动详情', async () => {
  135. const dataSource = await IntegrationTestDatabase.getDataSource();
  136. if (!dataSource) throw new Error('Database not initialized');
  137. const testActivity = await TestDataFactory.createTestActivity(dataSource, {
  138. name: '测试活动详情'
  139. });
  140. const response = await client.activities[':id'].$get({
  141. param: { id: testActivity.id }
  142. },
  143. {
  144. headers: {
  145. 'Authorization': `Bearer ${testToken}`
  146. }
  147. });
  148. expect(response.status).toBe(200);
  149. if (response.status === 200) {
  150. const responseData = await response.json();
  151. expect(responseData.id).toBe(testActivity.id);
  152. expect(responseData.name).toBe(testActivity.name);
  153. expect(responseData.type).toBe(testActivity.type);
  154. }
  155. });
  156. it('应该返回404当活动不存在时', async () => {
  157. const response = await client.activities[':id'].$get({
  158. param: { id: 999999 }
  159. },
  160. {
  161. headers: {
  162. 'Authorization': `Bearer ${testToken}`
  163. }
  164. });
  165. expect(response.status).toBe(404);
  166. if (response.status === 404) {
  167. const responseData = await response.json();
  168. expect(responseData.message).toContain('资源不存在');
  169. }
  170. });
  171. });
  172. describe('活动更新测试', () => {
  173. it('应该成功更新活动信息', async () => {
  174. const dataSource = await IntegrationTestDatabase.getDataSource();
  175. if (!dataSource) throw new Error('Database not initialized');
  176. const testActivity = await TestDataFactory.createTestActivity(dataSource, {
  177. name: '测试活动更新'
  178. });
  179. const updateData = {
  180. name: '更新后的活动名称',
  181. description: '更新后的活动描述'
  182. };
  183. const response = await client.activities[':id'].$put({
  184. param: { id: testActivity.id },
  185. json: updateData
  186. },
  187. {
  188. headers: {
  189. 'Authorization': `Bearer ${testToken}`
  190. }
  191. });
  192. expect(response.status).toBe(200);
  193. if (response.status === 200) {
  194. const responseData = await response.json();
  195. expect(responseData.name).toBe(updateData.name);
  196. expect(responseData.description).toBe(updateData.description);
  197. }
  198. // 验证数据库中的更新
  199. const getResponse = await client.activities[':id'].$get({
  200. param: { id: testActivity.id }
  201. },
  202. {
  203. headers: {
  204. 'Authorization': `Bearer ${testToken}`
  205. }
  206. });
  207. if (getResponse.status === 200) {
  208. expect(getResponse.status).toBe(200);
  209. const getResponseData = await getResponse.json();
  210. expect(getResponseData.name).toBe(updateData.name);
  211. }
  212. });
  213. it('应该成功启用/禁用活动', async () => {
  214. const dataSource = await IntegrationTestDatabase.getDataSource();
  215. if (!dataSource) throw new Error('Database not initialized');
  216. const testActivity = await TestDataFactory.createTestActivity(dataSource, {
  217. name: '测试状态切换',
  218. isDisabled: 0 // 启用状态
  219. });
  220. // 禁用活动
  221. const disableResponse = await client.activities[':id'].$put({
  222. param: { id: testActivity.id },
  223. json: { isDisabled: 1 } // 禁用
  224. },
  225. {
  226. headers: {
  227. 'Authorization': `Bearer ${testToken}`
  228. }
  229. });
  230. expect(disableResponse.status).toBe(200);
  231. if (disableResponse.status === 200) {
  232. const disableData = await disableResponse.json();
  233. expect(disableData.isDisabled).toBe(1);
  234. }
  235. // 重新启用活动
  236. const enableResponse = await client.activities[':id'].$put({
  237. param: { id: testActivity.id },
  238. json: { isDisabled: 0 } // 启用
  239. },
  240. {
  241. headers: {
  242. 'Authorization': `Bearer ${testToken}`
  243. }
  244. });
  245. expect(enableResponse.status).toBe(200);
  246. if (enableResponse.status === 200) {
  247. const enableData = await enableResponse.json();
  248. expect(enableData.isDisabled).toBe(0);
  249. }
  250. });
  251. it('应该返回404当更新不存在的活动时', async () => {
  252. const updateData = {
  253. name: '更新后的名称'
  254. };
  255. const response = await client.activities[':id'].$put({
  256. param: { id: 999999 },
  257. json: updateData
  258. },
  259. {
  260. headers: {
  261. 'Authorization': `Bearer ${testToken}`
  262. }
  263. });
  264. expect(response.status).toBe(404);
  265. if (response.status === 404) {
  266. const responseData = await response.json();
  267. expect(responseData.message).toContain('资源不存在');
  268. }
  269. });
  270. });
  271. describe('活动删除测试', () => {
  272. it('应该成功删除活动', async () => {
  273. const dataSource = await IntegrationTestDatabase.getDataSource();
  274. if (!dataSource) throw new Error('Database not initialized');
  275. const testActivity = await TestDataFactory.createTestActivity(dataSource, {
  276. name: '测试活动删除'
  277. });
  278. const response = await client.activities[':id'].$delete({
  279. param: { id: testActivity.id }
  280. },
  281. {
  282. headers: {
  283. 'Authorization': `Bearer ${testToken}`
  284. }
  285. });
  286. IntegrationTestAssertions.expectStatus(response, 204);
  287. // 验证活动已从数据库中删除
  288. await IntegrationTestAssertions.expectActivityNotToExist(testActivity.id);
  289. // 验证再次获取活动返回404
  290. const getResponse = await client.activities[':id'].$get({
  291. param: { id: testActivity.id }
  292. },
  293. {
  294. headers: {
  295. 'Authorization': `Bearer ${testToken}`
  296. }
  297. });
  298. IntegrationTestAssertions.expectStatus(getResponse, 404);
  299. });
  300. it('应该返回404当删除不存在的活动时', async () => {
  301. const response = await client.activities[':id'].$delete({
  302. param: { id: 999999 }
  303. },
  304. {
  305. headers: {
  306. 'Authorization': `Bearer ${testToken}`
  307. }
  308. });
  309. IntegrationTestAssertions.expectStatus(response, 404);
  310. if (response.status === 404) {
  311. const responseData = await response.json();
  312. expect(responseData.message).toContain('资源不存在');
  313. }
  314. });
  315. });
  316. describe('活动搜索测试', () => {
  317. it('应该能够按活动名称搜索活动', async () => {
  318. const dataSource = await IntegrationTestDatabase.getDataSource();
  319. if (!dataSource) throw new Error('Database not initialized');
  320. await TestDataFactory.createTestActivity(dataSource, { name: '搜索活动1', description: '测试描述1' });
  321. await TestDataFactory.createTestActivity(dataSource, { name: '搜索活动2', description: '测试描述2' });
  322. await TestDataFactory.createTestActivity(dataSource, { name: '其他活动', description: '其他描述' });
  323. const response = await client.activities.$get({
  324. query: { keyword: '搜索活动' }
  325. },
  326. {
  327. headers: {
  328. 'Authorization': `Bearer ${testToken}`
  329. }
  330. });
  331. IntegrationTestAssertions.expectStatus(response, 200);
  332. if (response.status === 200) {
  333. const responseData = await response.json();
  334. expect(Array.isArray(responseData.data)).toBe(true);
  335. expect(responseData.data.length).toBe(2);
  336. // 验证搜索结果包含正确的活动
  337. const names = responseData.data.map((activity) => activity.name);
  338. expect(names).toContain('搜索活动1');
  339. expect(names).toContain('搜索活动2');
  340. expect(names).not.toContain('其他活动');
  341. }
  342. });
  343. it('应该能够按活动描述搜索活动', async () => {
  344. const dataSource = await IntegrationTestDatabase.getDataSource();
  345. if (!dataSource) throw new Error('Database not initialized');
  346. await TestDataFactory.createTestActivity(dataSource, { name: '活动1', description: '测试描述搜索1' });
  347. await TestDataFactory.createTestActivity(dataSource, { name: '活动2', description: '测试描述搜索2' });
  348. const response = await client.activities.$get({
  349. query: { keyword: '测试描述' }
  350. },
  351. {
  352. headers: {
  353. 'Authorization': `Bearer ${testToken}`
  354. }
  355. });
  356. IntegrationTestAssertions.expectStatus(response, 200);
  357. if (response.status === 200) {
  358. const responseData = await response.json();
  359. expect(responseData.data.length).toBe(2);
  360. const descriptions = responseData.data.map((activity) => activity.description);
  361. expect(descriptions).toContain('测试描述搜索1');
  362. expect(descriptions).toContain('测试描述搜索2');
  363. }
  364. });
  365. it('应该能够按活动类型筛选活动', async () => {
  366. const dataSource = await IntegrationTestDatabase.getDataSource();
  367. if (!dataSource) throw new Error('Database not initialized');
  368. await TestDataFactory.createTestActivity(dataSource, { name: '活动1' });
  369. await TestDataFactory.createTestActivity(dataSource, { name: '活动2' });
  370. await TestDataFactory.createTestActivity(dataSource, { name: '活动3' });
  371. const response = await client.activities.$get({
  372. query: { filters: JSON.stringify({ isDisabled: 0 }) }
  373. },
  374. {
  375. headers: {
  376. 'Authorization': `Bearer ${testToken}`
  377. }
  378. });
  379. IntegrationTestAssertions.expectStatus(response, 200);
  380. if (response.status === 200) {
  381. const responseData = await response.json();
  382. expect(responseData.data.length).toBe(2);
  383. const types = responseData.data.map((activity) => activity.type);
  384. expect(types.every((type: string) => type === 'departure')).toBe(true);
  385. }
  386. });
  387. });
  388. describe('性能测试', () => {
  389. it('活动列表查询响应时间应小于200ms', async () => {
  390. const dataSource = await IntegrationTestDatabase.getDataSource();
  391. if (!dataSource) throw new Error('Database not initialized');
  392. // 创建一些测试数据
  393. for (let i = 0; i < 10; i++) {
  394. await TestDataFactory.createTestActivity(dataSource, {
  395. name: `性能测试活动_${i}`,
  396. description: `性能测试描述_${i}`
  397. });
  398. }
  399. const startTime = Date.now();
  400. const response = await client.activities.$get({
  401. query: {}
  402. },
  403. {
  404. headers: {
  405. 'Authorization': `Bearer ${testToken}`
  406. }
  407. });
  408. const endTime = Date.now();
  409. const responseTime = endTime - startTime;
  410. IntegrationTestAssertions.expectStatus(response, 200);
  411. expect(responseTime).toBeLessThan(200); // 响应时间应小于200ms
  412. });
  413. });
  414. });