import { describe, it, expect, beforeAll, afterAll } from '@jest/globals'; import { DataSource } from 'typeorm'; import { AppDataSource } from '@/server/data-source'; import { HomeService } from '@/server/modules/home/home.service'; import { PolicyNews } from '@/server/modules/silver-users/policy-news.entity'; import { Job } from '@/server/modules/silver-jobs/job.entity'; import { SilverKnowledge } from '@/server/modules/silver-users/silver-knowledge.entity'; import { SilverTimeBank } from '@/server/modules/silver-users/silver-time-bank.entity'; import { UserEntity } from '@/server/modules/users/user.entity'; import { SilverUserProfile } from '@/server/modules/silver-users/silver-user-profile.entity'; describe('HomeService', () => { let service: HomeService; let dataSource: DataSource; beforeAll(async () => { if (!AppDataSource.isInitialized) { await AppDataSource.initialize(); } dataSource = AppDataSource; service = new HomeService(dataSource); }); afterAll(async () => { if (AppDataSource.isInitialized) { await AppDataSource.destroy(); } }); describe('getHomeData', () => { it('should return home data for anonymous users', async () => { const result = await service.getHomeData(); expect(result).toHaveProperty('banners'); expect(result).toHaveProperty('recommendedJobs'); expect(result).toHaveProperty('hotKnowledge'); expect(result).toHaveProperty('timeBankActivities'); expect(result).toHaveProperty('userStats'); expect(Array.isArray(result.banners)).toBe(true); expect(Array.isArray(result.recommendedJobs)).toBe(true); expect(Array.isArray(result.hotKnowledge)).toBe(true); expect(Array.isArray(result.timeBankActivities)).toBe(true); expect(typeof result.userStats).toBe('object'); }); it('should return user-specific data when userId provided', async () => { const mockUserId = 1; const result = await service.getHomeData(mockUserId); // 验证用户统计数据存在 expect(result.userStats).toHaveProperty('pointBalance'); expect(result.userStats).toHaveProperty('timeBankHours'); expect(result.userStats).toHaveProperty('publishedCount'); expect(result.userStats).toHaveProperty('favoriteCount'); }); it('should handle empty results gracefully', async () => { const result = await service.getHomeData(); // 确保返回空数组而不是null expect(result.banners).toBeInstanceOf(Array); expect(result.recommendedJobs).toBeInstanceOf(Array); expect(result.hotKnowledge).toBeInstanceOf(Array); expect(result.timeBankActivities).toBeInstanceOf(Array); }); }); describe('search', () => { it('should return search results for valid keyword', async () => { const keyword = '老师'; const result = await service.search(keyword, 'all', 5); expect(result).toHaveProperty('jobs'); expect(result).toHaveProperty('knowledge'); expect(result).toHaveProperty('companies'); }); it('should limit results based on limit parameter', async () => { const keyword = '培训'; const limit = 3; const result = await service.search(keyword, 'jobs', limit); expect(result.jobs.length).toBeLessThanOrEqual(limit); }); }); describe('getBanners', () => { it('should return banners with default limit', async () => { const banners = await service.getBanners(); expect(banners.length).toBeLessThanOrEqual(5); }); it('should return banners with custom limit', async () => { const limit = 3; const banners = await service.getBanners(limit); expect(banners.length).toBeLessThanOrEqual(limit); }); }); describe('getRecommendedJobs', () => { it('should return jobs for anonymous users', async () => { const jobs = await service.getRecommendedJobs(undefined, 5); expect(jobs).toBeInstanceOf(Array); expect(jobs.length).toBeLessThanOrEqual(5); }); it('should exclude viewed jobs for logged-in users', async () => { const userId = 1; const jobs = await service.getRecommendedJobs(userId, 5); expect(jobs).toBeInstanceOf(Array); }); }); describe('performance', () => { it('should complete data retrieval within 3 seconds', async () => { const startTime = Date.now(); const result = await service.getHomeData(); const endTime = Date.now(); expect(endTime - startTime).toBeLessThan(3000); }); it('should handle concurrent requests', async () => { const promises = Array.from({ length: 10 }, () => service.getHomeData()); const results = await Promise.all(promises); expect(results).toHaveLength(10); results.forEach(result => { expect(result).toHaveProperty('banners'); }); }); }); });