|
| 1 | +import { describe, expect, test } from '@jest/globals'; |
| 2 | +import type { Chain } from 'viem'; |
| 3 | +import { sei, seiTestnet, seiDevnet } from 'viem/chains'; |
| 4 | +import { |
| 5 | + DEFAULT_NETWORK, |
| 6 | + DEFAULT_RPC_URL, |
| 7 | + DEFAULT_CHAIN_ID, |
| 8 | + chainMap, |
| 9 | + networkNameMap, |
| 10 | + rpcUrlMap, |
| 11 | + resolveChainId, |
| 12 | + getChain, |
| 13 | + getRpcUrl, |
| 14 | + getSupportedNetworks |
| 15 | +} from '../../core/chains.js'; |
| 16 | + |
| 17 | +describe('chains module', () => { |
| 18 | + // Test constants |
| 19 | + describe('constants', () => { |
| 20 | + test('DEFAULT_NETWORK is set correctly', () => { |
| 21 | + expect(DEFAULT_NETWORK).toBe('sei'); |
| 22 | + }); |
| 23 | + |
| 24 | + test('DEFAULT_RPC_URL is set correctly', () => { |
| 25 | + expect(DEFAULT_RPC_URL).toBe('https://evm-rpc.sei-apis.com'); |
| 26 | + }); |
| 27 | + |
| 28 | + test('DEFAULT_CHAIN_ID is set correctly', () => { |
| 29 | + expect(DEFAULT_CHAIN_ID).toBe(1329); |
| 30 | + }); |
| 31 | + |
| 32 | + test('chainMap contains expected chains', () => { |
| 33 | + expect(chainMap[1329]).toBe(sei); |
| 34 | + expect(chainMap[1328]).toBe(seiTestnet); |
| 35 | + expect(chainMap[713715]).toBe(seiDevnet); |
| 36 | + }); |
| 37 | + |
| 38 | + test('networkNameMap contains expected mappings', () => { |
| 39 | + expect(networkNameMap.sei).toBe(1329); |
| 40 | + expect(networkNameMap['sei-testnet']).toBe(1328); |
| 41 | + expect(networkNameMap['sei-devnet']).toBe(713715); |
| 42 | + }); |
| 43 | + |
| 44 | + test('rpcUrlMap contains expected URLs', () => { |
| 45 | + expect(rpcUrlMap[1329]).toBe('https://evm-rpc.sei-apis.com'); |
| 46 | + expect(rpcUrlMap[1328]).toBe('https://evm-rpc-testnet.sei-apis.com'); |
| 47 | + expect(rpcUrlMap[713715]).toBe('https://evm-rpc-arctic-1.sei-apis.com'); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + // Test resolveChainId function |
| 52 | + describe('resolveChainId', () => { |
| 53 | + test('resolves number chain IDs directly', () => { |
| 54 | + expect(resolveChainId(1329)).toBe(1329); |
| 55 | + expect(resolveChainId(1328)).toBe(1328); |
| 56 | + expect(resolveChainId(713715)).toBe(713715); |
| 57 | + }); |
| 58 | + |
| 59 | + test('resolves network names to chain IDs', () => { |
| 60 | + expect(resolveChainId('sei')).toBe(1329); |
| 61 | + expect(resolveChainId('sei-testnet')).toBe(1328); |
| 62 | + expect(resolveChainId('sei-devnet')).toBe(713715); |
| 63 | + }); |
| 64 | + |
| 65 | + test('resolves case-insensitive network names', () => { |
| 66 | + expect(resolveChainId('SEI')).toBe(1329); |
| 67 | + expect(resolveChainId('Sei-Testnet')).toBe(1328); |
| 68 | + expect(resolveChainId('SEI-DEVNET')).toBe(713715); |
| 69 | + }); |
| 70 | + |
| 71 | + test('resolves string numbers to chain IDs', () => { |
| 72 | + expect(resolveChainId('1329')).toBe(1329); |
| 73 | + expect(resolveChainId('1328')).toBe(1328); |
| 74 | + expect(resolveChainId('713715')).toBe(713715); |
| 75 | + }); |
| 76 | + |
| 77 | + test('defaults to DEFAULT_CHAIN_ID for unknown network names', () => { |
| 78 | + expect(resolveChainId('unknown-network')).toBe(DEFAULT_CHAIN_ID); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + // Test getChain function |
| 83 | + describe('getChain', () => { |
| 84 | + test('returns chain for numeric chain ID', () => { |
| 85 | + expect(getChain(1329)).toBe(sei); |
| 86 | + expect(getChain(1328)).toBe(seiTestnet); |
| 87 | + expect(getChain(713715)).toBe(seiDevnet); |
| 88 | + }); |
| 89 | + |
| 90 | + test('returns chain for network name', () => { |
| 91 | + expect(getChain('sei')).toBe(sei); |
| 92 | + expect(getChain('sei-testnet')).toBe(seiTestnet); |
| 93 | + expect(getChain('sei-devnet')).toBe(seiDevnet); |
| 94 | + }); |
| 95 | + |
| 96 | + test('returns sei chain when network name exists but chain mapping is missing', () => { |
| 97 | + // Create a temporary entry in networkNameMap for a non-existent chain ID |
| 98 | + const originalNetworkNameMap = { ...networkNameMap }; |
| 99 | + // @ts-ignore - Intentionally modifying for test |
| 100 | + networkNameMap['test-network'] = 9999; |
| 101 | + |
| 102 | + try { |
| 103 | + // This should return sei as fallback since chainMap[9999] doesn't exist |
| 104 | + expect(getChain('test-network')).toBe(sei); |
| 105 | + } finally { |
| 106 | + // Restore the original map |
| 107 | + // @ts-ignore - Restoring original state |
| 108 | + for (const key of Object.keys(networkNameMap)) { |
| 109 | + if (key !== 'sei' && key !== 'sei-testnet' && key !== 'sei-devnet') { |
| 110 | + // @ts-ignore - Cleanup |
| 111 | + delete networkNameMap[key]; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + test('returns chain for case-insensitive network name', () => { |
| 118 | + expect(getChain('SEI')).toBe(sei); |
| 119 | + expect(getChain('Sei-Testnet')).toBe(seiTestnet); |
| 120 | + expect(getChain('SEI-DEVNET')).toBe(seiDevnet); |
| 121 | + }); |
| 122 | + |
| 123 | + test('returns default chain when no parameter is provided', () => { |
| 124 | + expect(getChain()).toBe(sei); |
| 125 | + }); |
| 126 | + |
| 127 | + test('returns sei chain for unknown numeric chain ID', () => { |
| 128 | + expect(getChain(9999)).toBe(sei); |
| 129 | + }); |
| 130 | + |
| 131 | + test('throws error for numeric string that is not in networkNameMap', () => { |
| 132 | + // This should throw an error just like other unknown network names |
| 133 | + expect(() => getChain('9999')).toThrow('Unsupported network: 9999'); |
| 134 | + }); |
| 135 | + |
| 136 | + test('throws error for unknown network name', () => { |
| 137 | + expect(() => getChain('unknown-network')).toThrow('Unsupported network: unknown-network'); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + // Test getRpcUrl function |
| 142 | + describe('getRpcUrl', () => { |
| 143 | + test('returns correct RPC URL for numeric chain ID', () => { |
| 144 | + expect(getRpcUrl(1329)).toBe('https://evm-rpc.sei-apis.com'); |
| 145 | + expect(getRpcUrl(1328)).toBe('https://evm-rpc-testnet.sei-apis.com'); |
| 146 | + expect(getRpcUrl(713715)).toBe('https://evm-rpc-arctic-1.sei-apis.com'); |
| 147 | + }); |
| 148 | + |
| 149 | + test('returns correct RPC URL for network name', () => { |
| 150 | + expect(getRpcUrl('sei')).toBe('https://evm-rpc.sei-apis.com'); |
| 151 | + expect(getRpcUrl('sei-testnet')).toBe('https://evm-rpc-testnet.sei-apis.com'); |
| 152 | + expect(getRpcUrl('sei-devnet')).toBe('https://evm-rpc-arctic-1.sei-apis.com'); |
| 153 | + }); |
| 154 | + |
| 155 | + test('returns default RPC URL for unknown chain ID', () => { |
| 156 | + expect(getRpcUrl(9999)).toBe(DEFAULT_RPC_URL); |
| 157 | + }); |
| 158 | + |
| 159 | + test('returns default RPC URL when no parameter is provided', () => { |
| 160 | + expect(getRpcUrl()).toBe(DEFAULT_RPC_URL); |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + // Test getSupportedNetworks function |
| 165 | + describe('getSupportedNetworks', () => { |
| 166 | + test('returns sorted list of supported networks', () => { |
| 167 | + const networks = getSupportedNetworks(); |
| 168 | + |
| 169 | + // Check that all expected networks are included |
| 170 | + expect(networks).toContain('sei'); |
| 171 | + expect(networks).toContain('sei-testnet'); |
| 172 | + expect(networks).toContain('sei-devnet'); |
| 173 | + |
| 174 | + // Check that the list is sorted |
| 175 | + expect(networks).toEqual([...networks].sort()); |
| 176 | + |
| 177 | + // Check that the length matches the expected number of networks |
| 178 | + expect(networks.length).toBe(Object.keys(networkNameMap).length); |
| 179 | + }); |
| 180 | + }); |
| 181 | +}); |
0 commit comments