|
| 1 | +import { createMock } from '@golevelup/ts-jest'; |
| 2 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 3 | +import { CACHE_MANAGER } from '@nestjs/cache-manager'; |
| 4 | +import { Cache } from 'cache-manager'; |
| 5 | +import { Logger } from '@nestjs/common'; |
| 6 | +import { StatisticsClient } from '@human-protocol/sdk'; |
| 7 | +import { EnvironmentConfigService } from '../../common/config/env-config.service'; |
| 8 | +import { ChainId, NETWORKS } from '@human-protocol/sdk'; |
| 9 | +import { MainnetsId } from '../../common/utils/constants'; |
| 10 | +import { HttpService } from '@nestjs/axios'; |
| 11 | +import { NetworkConfigService } from './network-config.service'; |
| 12 | +import { ConfigService } from '@nestjs/config'; |
| 13 | + |
| 14 | +jest.mock('@human-protocol/sdk', () => ({ |
| 15 | + ...jest.requireActual('@human-protocol/sdk'), |
| 16 | + StatisticsClient: jest.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +describe('NetworkConfigService', () => { |
| 20 | + let networkConfigService: NetworkConfigService; |
| 21 | + let cacheManager: Cache; |
| 22 | + |
| 23 | + beforeEach(async () => { |
| 24 | + const module: TestingModule = await Test.createTestingModule({ |
| 25 | + providers: [ |
| 26 | + NetworkConfigService, |
| 27 | + { provide: HttpService, useValue: createMock<HttpService>() }, |
| 28 | + { |
| 29 | + provide: CACHE_MANAGER, |
| 30 | + useValue: { |
| 31 | + get: jest.fn(), |
| 32 | + set: jest.fn(), |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + provide: EnvironmentConfigService, |
| 37 | + useValue: { |
| 38 | + networkUsageFilterMonths: 3, |
| 39 | + networkAvailableCacheTtl: 1000, |
| 40 | + }, |
| 41 | + }, |
| 42 | + ConfigService, |
| 43 | + Logger, |
| 44 | + ], |
| 45 | + }).compile(); |
| 46 | + |
| 47 | + networkConfigService = |
| 48 | + module.get<NetworkConfigService>(NetworkConfigService); |
| 49 | + cacheManager = module.get<Cache>(CACHE_MANAGER); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should regenerate network list when cache TTL expires', async () => { |
| 53 | + const mockNetworkList = [ |
| 54 | + ChainId.MAINNET, |
| 55 | + ChainId.BSC_MAINNET, |
| 56 | + ChainId.POLYGON, |
| 57 | + ChainId.XLAYER, |
| 58 | + ChainId.MOONBEAM, |
| 59 | + ChainId.CELO, |
| 60 | + ChainId.AVALANCHE, |
| 61 | + ]; |
| 62 | + |
| 63 | + // Step 1: Initial request - populate cache |
| 64 | + jest.spyOn(cacheManager, 'get').mockResolvedValue(null); |
| 65 | + jest.spyOn(cacheManager, 'set').mockResolvedValue(undefined); |
| 66 | + |
| 67 | + const mockStatisticsClient = { |
| 68 | + getHMTDailyData: jest |
| 69 | + .fn() |
| 70 | + .mockResolvedValue([{ totalTransactionCount: 7 }]), |
| 71 | + getEscrowStatistics: jest.fn().mockResolvedValue({ totalEscrows: 1 }), |
| 72 | + }; |
| 73 | + (StatisticsClient as jest.Mock).mockImplementation( |
| 74 | + () => mockStatisticsClient, |
| 75 | + ); |
| 76 | + |
| 77 | + // First call should populate cache |
| 78 | + const firstCallResult = await networkConfigService.getAvailableNetworks(); |
| 79 | + |
| 80 | + expect(firstCallResult).toEqual(mockNetworkList); |
| 81 | + expect(cacheManager.set).toHaveBeenCalledWith( |
| 82 | + 'available-networks', |
| 83 | + mockNetworkList, |
| 84 | + 1000, |
| 85 | + ); |
| 86 | + |
| 87 | + // Step 2: Simulate TTL expiration by returning null from cache |
| 88 | + jest.spyOn(cacheManager, 'get').mockResolvedValueOnce(null); |
| 89 | + |
| 90 | + // Second call after TTL should re-generate the network list |
| 91 | + const secondCallResult = await networkConfigService.getAvailableNetworks(); |
| 92 | + expect(secondCallResult).toEqual(mockNetworkList); |
| 93 | + |
| 94 | + // Ensure the cache is set again with the regenerated network list |
| 95 | + expect(cacheManager.set).toHaveBeenCalledWith( |
| 96 | + 'available-networks', |
| 97 | + mockNetworkList, |
| 98 | + 1000, |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should return cached networks if available', async () => { |
| 103 | + const cachedNetworks = [ChainId.MAINNET, ChainId.POLYGON]; |
| 104 | + jest.spyOn(cacheManager, 'get').mockResolvedValue(cachedNetworks); |
| 105 | + |
| 106 | + const result = await networkConfigService.getAvailableNetworks(); |
| 107 | + expect(result).toEqual(cachedNetworks); |
| 108 | + expect(cacheManager.get).toHaveBeenCalledWith('available-networks'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should fetch and filter available networks correctly', async () => { |
| 112 | + jest.spyOn(cacheManager, 'get').mockResolvedValue(null); |
| 113 | + const mockStatisticsClient = { |
| 114 | + getHMTDailyData: jest |
| 115 | + .fn() |
| 116 | + .mockResolvedValue([ |
| 117 | + { totalTransactionCount: 4 }, |
| 118 | + { totalTransactionCount: 3 }, |
| 119 | + ]), |
| 120 | + getEscrowStatistics: jest.fn().mockResolvedValue({ totalEscrows: 1 }), |
| 121 | + }; |
| 122 | + (StatisticsClient as jest.Mock).mockImplementation( |
| 123 | + () => mockStatisticsClient, |
| 124 | + ); |
| 125 | + |
| 126 | + const result = await networkConfigService.getAvailableNetworks(); |
| 127 | + expect(result).toEqual( |
| 128 | + expect.arrayContaining([ChainId.MAINNET, ChainId.POLYGON]), |
| 129 | + ); |
| 130 | + |
| 131 | + expect(cacheManager.set).toHaveBeenCalledWith( |
| 132 | + 'available-networks', |
| 133 | + result, |
| 134 | + 1000, |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should exclude networks without sufficient HMT transfers', async () => { |
| 139 | + jest.spyOn(cacheManager, 'get').mockResolvedValue(null); |
| 140 | + const mockStatisticsClient = { |
| 141 | + getHMTDailyData: jest |
| 142 | + .fn() |
| 143 | + .mockResolvedValue([{ totalTransactionCount: 2 }]), |
| 144 | + getEscrowStatistics: jest.fn().mockResolvedValue({ totalEscrows: 1 }), |
| 145 | + }; |
| 146 | + (StatisticsClient as jest.Mock).mockImplementation( |
| 147 | + () => mockStatisticsClient, |
| 148 | + ); |
| 149 | + |
| 150 | + const result = await networkConfigService.getAvailableNetworks(); |
| 151 | + expect(result).toEqual([]); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should handle missing network configuration gracefully', async () => { |
| 155 | + jest.spyOn(cacheManager, 'get').mockResolvedValue(null); |
| 156 | + |
| 157 | + const originalNetworkConfig = NETWORKS[MainnetsId.MAINNET]; |
| 158 | + NETWORKS[MainnetsId.MAINNET] = undefined; |
| 159 | + |
| 160 | + const mockStatisticsClient = { |
| 161 | + getHMTDailyData: jest |
| 162 | + .fn() |
| 163 | + .mockResolvedValue([ |
| 164 | + { totalTransactionCount: 3 }, |
| 165 | + { totalTransactionCount: 3 }, |
| 166 | + ]), |
| 167 | + getEscrowStatistics: jest.fn().mockResolvedValue({ totalEscrows: 1 }), |
| 168 | + }; |
| 169 | + (StatisticsClient as jest.Mock).mockImplementation( |
| 170 | + () => mockStatisticsClient, |
| 171 | + ); |
| 172 | + |
| 173 | + const result = await networkConfigService.getAvailableNetworks(); |
| 174 | + |
| 175 | + expect(result).not.toContain(MainnetsId.MAINNET); |
| 176 | + expect(result).toEqual(expect.arrayContaining([])); |
| 177 | + |
| 178 | + NETWORKS[MainnetsId.MAINNET] = originalNetworkConfig; |
| 179 | + }); |
| 180 | + |
| 181 | + it('should handle errors in getHMTDailyData gracefully', async () => { |
| 182 | + jest.spyOn(cacheManager, 'get').mockResolvedValue(null); |
| 183 | + const mockStatisticsClient = { |
| 184 | + getHMTDailyData: jest |
| 185 | + .fn() |
| 186 | + .mockRejectedValue(new Error('Failed to fetch HMT data')), |
| 187 | + getEscrowStatistics: jest.fn().mockResolvedValue({ totalEscrows: 1 }), |
| 188 | + }; |
| 189 | + (StatisticsClient as jest.Mock).mockImplementation( |
| 190 | + () => mockStatisticsClient, |
| 191 | + ); |
| 192 | + |
| 193 | + const result = await networkConfigService.getAvailableNetworks(); |
| 194 | + expect(result).toEqual([]); |
| 195 | + }); |
| 196 | + |
| 197 | + it('should handle errors in getEscrowStatistics gracefully', async () => { |
| 198 | + jest.spyOn(cacheManager, 'get').mockResolvedValue(null); |
| 199 | + const mockStatisticsClient = { |
| 200 | + getHMTDailyData: jest |
| 201 | + .fn() |
| 202 | + .mockResolvedValue([ |
| 203 | + { totalTransactionCount: 3 }, |
| 204 | + { totalTransactionCount: 2 }, |
| 205 | + ]), |
| 206 | + getEscrowStatistics: jest |
| 207 | + .fn() |
| 208 | + .mockRejectedValue(new Error('Failed to fetch escrow stats')), |
| 209 | + }; |
| 210 | + (StatisticsClient as jest.Mock).mockImplementation( |
| 211 | + () => mockStatisticsClient, |
| 212 | + ); |
| 213 | + |
| 214 | + const result = await networkConfigService.getAvailableNetworks(); |
| 215 | + expect(result).toEqual([]); |
| 216 | + }); |
| 217 | +}); |
0 commit comments