activities.integration.test.ts 16 KB

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