home-api.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { describe, it, expect, beforeAll, afterAll } from '@jest/globals';
  2. import { DataSource } from 'typeorm';
  3. import { AppDataSource } from '@/server/data-source';
  4. import { HomeService } from '@/server/modules/home/home.service';
  5. import { PolicyNews } from '@/server/modules/silver-users/policy-news.entity';
  6. import { Job } from '@/server/modules/silver-jobs/job.entity';
  7. import { SilverKnowledge } from '@/server/modules/silver-users/silver-knowledge.entity';
  8. import { SilverTimeBank } from '@/server/modules/silver-users/silver-time-bank.entity';
  9. import { UserEntity } from '@/server/modules/users/user.entity';
  10. import { SilverUserProfile } from '@/server/modules/silver-users/silver-user-profile.entity';
  11. describe('HomeService', () => {
  12. let service: HomeService;
  13. let dataSource: DataSource;
  14. beforeAll(async () => {
  15. if (!AppDataSource.isInitialized) {
  16. await AppDataSource.initialize();
  17. }
  18. dataSource = AppDataSource;
  19. service = new HomeService(dataSource);
  20. });
  21. afterAll(async () => {
  22. if (AppDataSource.isInitialized) {
  23. await AppDataSource.destroy();
  24. }
  25. });
  26. describe('getHomeData', () => {
  27. it('should return home data for anonymous users', async () => {
  28. const result = await service.getHomeData();
  29. expect(result).toHaveProperty('banners');
  30. expect(result).toHaveProperty('recommendedJobs');
  31. expect(result).toHaveProperty('hotKnowledge');
  32. expect(result).toHaveProperty('timeBankActivities');
  33. expect(result).toHaveProperty('userStats');
  34. expect(Array.isArray(result.banners)).toBe(true);
  35. expect(Array.isArray(result.recommendedJobs)).toBe(true);
  36. expect(Array.isArray(result.hotKnowledge)).toBe(true);
  37. expect(Array.isArray(result.timeBankActivities)).toBe(true);
  38. expect(typeof result.userStats).toBe('object');
  39. });
  40. it('should return user-specific data when userId provided', async () => {
  41. const mockUserId = 1;
  42. const result = await service.getHomeData(mockUserId);
  43. // 验证用户统计数据存在
  44. expect(result.userStats).toHaveProperty('pointBalance');
  45. expect(result.userStats).toHaveProperty('timeBankHours');
  46. expect(result.userStats).toHaveProperty('publishedCount');
  47. expect(result.userStats).toHaveProperty('favoriteCount');
  48. });
  49. it('should handle empty results gracefully', async () => {
  50. const result = await service.getHomeData();
  51. // 确保返回空数组而不是null
  52. expect(result.banners).toBeInstanceOf(Array);
  53. expect(result.recommendedJobs).toBeInstanceOf(Array);
  54. expect(result.hotKnowledge).toBeInstanceOf(Array);
  55. expect(result.timeBankActivities).toBeInstanceOf(Array);
  56. });
  57. });
  58. describe('search', () => {
  59. it('should return search results for valid keyword', async () => {
  60. const keyword = '老师';
  61. const result = await service.search(keyword, 'all', 5);
  62. expect(result).toHaveProperty('jobs');
  63. expect(result).toHaveProperty('knowledge');
  64. expect(result).toHaveProperty('companies');
  65. });
  66. it('should limit results based on limit parameter', async () => {
  67. const keyword = '培训';
  68. const limit = 3;
  69. const result = await service.search(keyword, 'jobs', limit);
  70. expect(result.jobs.length).toBeLessThanOrEqual(limit);
  71. });
  72. });
  73. describe('getBanners', () => {
  74. it('should return banners with default limit', async () => {
  75. const banners = await service.getBanners();
  76. expect(banners.length).toBeLessThanOrEqual(5);
  77. });
  78. it('should return banners with custom limit', async () => {
  79. const limit = 3;
  80. const banners = await service.getBanners(limit);
  81. expect(banners.length).toBeLessThanOrEqual(limit);
  82. });
  83. });
  84. describe('getRecommendedJobs', () => {
  85. it('should return jobs for anonymous users', async () => {
  86. const jobs = await service.getRecommendedJobs(undefined, 5);
  87. expect(jobs).toBeInstanceOf(Array);
  88. expect(jobs.length).toBeLessThanOrEqual(5);
  89. });
  90. it('should exclude viewed jobs for logged-in users', async () => {
  91. const userId = 1;
  92. const jobs = await service.getRecommendedJobs(userId, 5);
  93. expect(jobs).toBeInstanceOf(Array);
  94. });
  95. });
  96. describe('performance', () => {
  97. it('should complete data retrieval within 3 seconds', async () => {
  98. const startTime = Date.now();
  99. const result = await service.getHomeData();
  100. const endTime = Date.now();
  101. expect(endTime - startTime).toBeLessThan(3000);
  102. });
  103. it('should handle concurrent requests', async () => {
  104. const promises = Array.from({ length: 10 }, () => service.getHomeData());
  105. const results = await Promise.all(promises);
  106. expect(results).toHaveLength(10);
  107. results.forEach(result => {
  108. expect(result).toHaveProperty('banners');
  109. });
  110. });
  111. });
  112. });