|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import { NetworkUtil } from '@reown/appkit-common' |
| 4 | +import { ChainController } from '@reown/appkit-controllers' |
| 5 | + |
| 6 | +// Mock the controllers |
| 7 | +vi.mock('@reown/appkit-controllers', () => ({ |
| 8 | + ChainController: { |
| 9 | + getCaipNetworks: vi.fn(), |
| 10 | + getActiveCaipNetwork: vi.fn(), |
| 11 | + state: { |
| 12 | + activeChain: 'eip155' |
| 13 | + } |
| 14 | + }, |
| 15 | + ConnectorController: { |
| 16 | + getConnectorId: vi.fn() |
| 17 | + }, |
| 18 | + AlertController: { |
| 19 | + open: vi.fn() |
| 20 | + }, |
| 21 | + SIWXUtil: { |
| 22 | + authConnectorAuthenticate: vi.fn() |
| 23 | + }, |
| 24 | + getActiveCaipNetwork: vi.fn(), |
| 25 | + getPreferredAccountType: vi.fn() |
| 26 | +})) |
| 27 | + |
| 28 | +// Mock the auth provider |
| 29 | +vi.mock('@reown/appkit/auth-provider', () => ({ |
| 30 | + W3mFrameProviderSingleton: { |
| 31 | + getInstance: vi.fn() |
| 32 | + } |
| 33 | +})) |
| 34 | + |
| 35 | +// Mock the utils |
| 36 | +vi.mock('@reown/appkit-utils', () => ({ |
| 37 | + ErrorUtil: { |
| 38 | + ALERT_ERRORS: { |
| 39 | + IFRAME_LOAD_FAILED: 'IFRAME_LOAD_FAILED', |
| 40 | + IFRAME_REQUEST_TIMEOUT: 'IFRAME_REQUEST_TIMEOUT', |
| 41 | + UNVERIFIED_DOMAIN: 'UNVERIFIED_DOMAIN' |
| 42 | + }, |
| 43 | + EmbeddedWalletAbortController: new AbortController() |
| 44 | + } |
| 45 | +})) |
| 46 | + |
| 47 | +// Helper function to create mock CaipNetwork |
| 48 | +const createMockCaipNetwork = (id: number, name: string) => ({ |
| 49 | + id, |
| 50 | + name, |
| 51 | + chainNamespace: 'eip155' as const, |
| 52 | + caipNetworkId: `eip155:${id}` as const, |
| 53 | + nativeCurrency: { |
| 54 | + name: name, |
| 55 | + symbol: name === 'Polygon' ? 'MATIC' : 'ETH', |
| 56 | + decimals: 18 |
| 57 | + }, |
| 58 | + rpcUrls: { |
| 59 | + default: { |
| 60 | + http: [`https://rpc.example.com/${id}`] |
| 61 | + } |
| 62 | + } |
| 63 | +}) |
| 64 | + |
| 65 | +describe('AuthConnector - parseChainId behavior fix', () => { |
| 66 | + beforeEach(() => { |
| 67 | + vi.clearAllMocks() |
| 68 | + }) |
| 69 | + |
| 70 | + afterEach(() => { |
| 71 | + vi.restoreAllMocks() |
| 72 | + }) |
| 73 | + |
| 74 | + it('should return current chain ID when request comes while active namespace is different than eip155', async () => { |
| 75 | + // Mock ChainController to simulate different active namespace |
| 76 | + vi.spyOn(ChainController, 'getCaipNetworks').mockReturnValue([ |
| 77 | + createMockCaipNetwork(137, 'Polygon'), |
| 78 | + createMockCaipNetwork(1, 'Ethereum') |
| 79 | + ]) |
| 80 | + |
| 81 | + // Mock active network to be Polygon (chainId 137) |
| 82 | + vi.spyOn(ChainController, 'getActiveCaipNetwork').mockReturnValue( |
| 83 | + createMockCaipNetwork(137, 'Polygon') |
| 84 | + ) |
| 85 | + |
| 86 | + // Test the parseChainId function behavior by testing the logic directly |
| 87 | + // This simulates the fix where parseChainId should return the current active chain |
| 88 | + // instead of defaulting to mainnet when the chainId is not found in networks |
| 89 | + const networks = ChainController.getCaipNetworks('eip155') |
| 90 | + const chainId = 999 // Unknown chainId from provider |
| 91 | + const currentChainId = ChainController.getActiveCaipNetwork('eip155')?.id |
| 92 | + |
| 93 | + // This is the logic from the parseChainId function |
| 94 | + let result: number |
| 95 | + if (!networks.some(network => network.id === chainId)) { |
| 96 | + if (currentChainId) { |
| 97 | + result = currentChainId as number |
| 98 | + } else { |
| 99 | + result = (networks[0]?.id as number) || 1 |
| 100 | + } |
| 101 | + } else { |
| 102 | + result = NetworkUtil.parseEvmChainId(chainId) || 1 |
| 103 | + } |
| 104 | + |
| 105 | + // Should return the current active chain (137) instead of defaulting to mainnet (1) |
| 106 | + expect(result).toBe(137) |
| 107 | + }) |
| 108 | + |
| 109 | + it('should fallback to first available network when no active network is set', async () => { |
| 110 | + // Mock ChainController with no active network |
| 111 | + vi.spyOn(ChainController, 'getCaipNetworks').mockReturnValue([ |
| 112 | + createMockCaipNetwork(137, 'Polygon'), |
| 113 | + createMockCaipNetwork(1, 'Ethereum') |
| 114 | + ]) |
| 115 | + |
| 116 | + // Mock no active network |
| 117 | + vi.spyOn(ChainController, 'getActiveCaipNetwork').mockReturnValue(undefined) |
| 118 | + |
| 119 | + // Test the parseChainId function behavior |
| 120 | + const networks = ChainController.getCaipNetworks('eip155') |
| 121 | + const chainId = 999 // Unknown chainId |
| 122 | + const currentChainId = ChainController.getActiveCaipNetwork('eip155')?.id |
| 123 | + |
| 124 | + // This is the logic from the parseChainId function |
| 125 | + let result: number |
| 126 | + if (!networks.some(network => network.id === chainId)) { |
| 127 | + if (currentChainId) { |
| 128 | + result = currentChainId as number |
| 129 | + } else { |
| 130 | + result = (networks[0]?.id as number) || 1 |
| 131 | + } |
| 132 | + } else { |
| 133 | + result = NetworkUtil.parseEvmChainId(chainId) || 1 |
| 134 | + } |
| 135 | + |
| 136 | + // Should fallback to first network in the list (137) |
| 137 | + expect(result).toBe(137) |
| 138 | + }) |
| 139 | + |
| 140 | + it('should return the provided chainId when it exists in available networks', async () => { |
| 141 | + // Mock ChainController with multiple networks |
| 142 | + vi.spyOn(ChainController, 'getCaipNetworks').mockReturnValue([ |
| 143 | + createMockCaipNetwork(137, 'Polygon'), |
| 144 | + createMockCaipNetwork(1, 'Ethereum') |
| 145 | + ]) |
| 146 | + |
| 147 | + // Test the parseChainId function behavior |
| 148 | + const networks = ChainController.getCaipNetworks('eip155') |
| 149 | + const chainId = 1 // Valid chainId |
| 150 | + const currentChainId = ChainController.getActiveCaipNetwork('eip155')?.id |
| 151 | + |
| 152 | + // This is the logic from the parseChainId function |
| 153 | + let result: number |
| 154 | + if (!networks.some(network => network.id === chainId)) { |
| 155 | + if (currentChainId) { |
| 156 | + result = currentChainId as number |
| 157 | + } else { |
| 158 | + result = (networks[0]?.id as number) || 1 |
| 159 | + } |
| 160 | + } else { |
| 161 | + result = NetworkUtil.parseEvmChainId(chainId) || 1 |
| 162 | + } |
| 163 | + |
| 164 | + // Should return the provided chainId (1) since it exists in networks |
| 165 | + expect(result).toBe(1) |
| 166 | + }) |
| 167 | + |
| 168 | + it('should handle string chainId correctly', async () => { |
| 169 | + vi.spyOn(ChainController, 'getCaipNetworks').mockReturnValue([ |
| 170 | + createMockCaipNetwork(137, 'Polygon') |
| 171 | + ]) |
| 172 | + |
| 173 | + vi.spyOn(ChainController, 'getActiveCaipNetwork').mockReturnValue( |
| 174 | + createMockCaipNetwork(137, 'Polygon') |
| 175 | + ) |
| 176 | + |
| 177 | + // Test the parseChainId function behavior |
| 178 | + const networks = ChainController.getCaipNetworks('eip155') |
| 179 | + const chainId = '137' // String chainId |
| 180 | + const currentChainId = ChainController.getActiveCaipNetwork('eip155')?.id |
| 181 | + |
| 182 | + // This is the logic from the parseChainId function |
| 183 | + let result: number |
| 184 | + if (!networks.some(network => network.id === chainId)) { |
| 185 | + if (currentChainId) { |
| 186 | + result = currentChainId as number |
| 187 | + } else { |
| 188 | + result = (networks[0]?.id as number) || 1 |
| 189 | + } |
| 190 | + } else { |
| 191 | + result = NetworkUtil.parseEvmChainId(chainId) || 1 |
| 192 | + } |
| 193 | + |
| 194 | + expect(result).toBe(137) |
| 195 | + }) |
| 196 | + |
| 197 | + it('should use current network when request comes from different namespace', async () => { |
| 198 | + // Mock EVM networks |
| 199 | + vi.spyOn(ChainController, 'getCaipNetworks').mockReturnValue([ |
| 200 | + createMockCaipNetwork(137, 'Polygon'), |
| 201 | + createMockCaipNetwork(1, 'Ethereum') |
| 202 | + ]) |
| 203 | + |
| 204 | + // Mock active EVM network |
| 205 | + vi.spyOn(ChainController, 'getActiveCaipNetwork').mockReturnValue( |
| 206 | + createMockCaipNetwork(137, 'Polygon') |
| 207 | + ) |
| 208 | + |
| 209 | + // Test the parseChainId function behavior |
| 210 | + const networks = ChainController.getCaipNetworks('eip155') |
| 211 | + const chainId = 999 // Unknown chainId from provider |
| 212 | + const currentChainId = ChainController.getActiveCaipNetwork('eip155')?.id |
| 213 | + |
| 214 | + // This is the logic from the parseChainId function |
| 215 | + let result: number |
| 216 | + if (!networks.some(network => network.id === chainId)) { |
| 217 | + if (currentChainId) { |
| 218 | + result = currentChainId as number |
| 219 | + } else { |
| 220 | + result = (networks[0]?.id as number) || 1 |
| 221 | + } |
| 222 | + } else { |
| 223 | + result = NetworkUtil.parseEvmChainId(chainId) || 1 |
| 224 | + } |
| 225 | + |
| 226 | + // Should return the current active EVM network (137) instead of defaulting to mainnet |
| 227 | + expect(result).toBe(137) |
| 228 | + }) |
| 229 | + |
| 230 | + it('should handle empty networks array gracefully', async () => { |
| 231 | + vi.spyOn(ChainController, 'getCaipNetworks').mockReturnValue([]) |
| 232 | + vi.spyOn(ChainController, 'getActiveCaipNetwork').mockReturnValue(undefined) |
| 233 | + |
| 234 | + // Test the parseChainId function behavior |
| 235 | + const networks = ChainController.getCaipNetworks('eip155') |
| 236 | + const chainId = 999 |
| 237 | + const currentChainId = ChainController.getActiveCaipNetwork('eip155')?.id |
| 238 | + |
| 239 | + // This is the logic from the parseChainId function |
| 240 | + let result: number |
| 241 | + if (!networks.some(network => network.id === chainId)) { |
| 242 | + if (currentChainId) { |
| 243 | + result = currentChainId as number |
| 244 | + } else { |
| 245 | + result = (networks[0]?.id as number) || 1 |
| 246 | + } |
| 247 | + } else { |
| 248 | + result = NetworkUtil.parseEvmChainId(chainId) || 1 |
| 249 | + } |
| 250 | + |
| 251 | + // Should fallback to default chainId (1) when no networks are available |
| 252 | + expect(result).toBe(1) |
| 253 | + }) |
| 254 | +}) |
0 commit comments