activities.integration.test.ts 15 KB

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