|
1 | | -import { optimismSepolia, sepolia } from 'viem/chains'; |
| 1 | +import { base, baseSepolia, optimismSepolia, sepolia } from 'viem/chains'; |
2 | 2 |
|
3 | 3 | import { ChainClients } from './store.js'; |
4 | 4 | import { createClients } from './utils.js'; |
5 | 5 |
|
6 | 6 | describe('chain-clients/utils', () => { |
| 7 | + beforeEach(() => { |
| 8 | + // Clear the ChainClients state before each test |
| 9 | + ChainClients.setState(() => ({}), true); |
| 10 | + }); |
| 11 | + |
7 | 12 | it('should create clients', () => { |
8 | 13 | createClients([ |
9 | 14 | { |
@@ -47,4 +52,81 @@ describe('chain-clients/utils', () => { |
47 | 52 | expect(ChainClients.getState()[sepolia.id].bundlerClient).toBeDefined(); |
48 | 53 | expect(ChainClients.getState()[optimismSepolia.id].bundlerClient).toBeDefined(); |
49 | 54 | }); |
| 55 | + |
| 56 | + describe('fallback RPC URLs', () => { |
| 57 | + it('should use fallback RPC URL for Base mainnet when wallet does not provide one', () => { |
| 58 | + createClients([ |
| 59 | + { |
| 60 | + id: base.id, // Base mainnet |
| 61 | + // No rpcUrl provided |
| 62 | + }, |
| 63 | + ]); |
| 64 | + |
| 65 | + expect(ChainClients.getState()[base.id]).toBeDefined(); |
| 66 | + expect(ChainClients.getState()[base.id].client).toBeDefined(); |
| 67 | + expect(ChainClients.getState()[base.id].bundlerClient).toBeDefined(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should use fallback RPC URL for Base Sepolia when wallet does not provide one', () => { |
| 71 | + createClients([ |
| 72 | + { |
| 73 | + id: baseSepolia.id, // Base Sepolia |
| 74 | + // No rpcUrl provided |
| 75 | + }, |
| 76 | + ]); |
| 77 | + |
| 78 | + expect(ChainClients.getState()[baseSepolia.id]).toBeDefined(); |
| 79 | + expect(ChainClients.getState()[baseSepolia.id].client).toBeDefined(); |
| 80 | + expect(ChainClients.getState()[baseSepolia.id].bundlerClient).toBeDefined(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should prefer wallet-provided RPC URL over fallback for Base mainnet', () => { |
| 84 | + const customRpcUrl = 'https://custom.base.rpc.url'; |
| 85 | + createClients([ |
| 86 | + { |
| 87 | + id: base.id, // Base mainnet |
| 88 | + rpcUrl: customRpcUrl, |
| 89 | + }, |
| 90 | + ]); |
| 91 | + |
| 92 | + expect(ChainClients.getState()[base.id]).toBeDefined(); |
| 93 | + expect(ChainClients.getState()[base.id].client).toBeDefined(); |
| 94 | + // We can't directly test the RPC URL used, but we can verify the client was created |
| 95 | + }); |
| 96 | + |
| 97 | + it('should not create client for unsupported chain without RPC URL', () => { |
| 98 | + createClients([ |
| 99 | + { |
| 100 | + id: 999999, // Unsupported chain |
| 101 | + // No rpcUrl provided |
| 102 | + }, |
| 103 | + ]); |
| 104 | + |
| 105 | + expect(ChainClients.getState()[999999]).toBeUndefined(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should handle mixed chains with and without RPC URLs', () => { |
| 109 | + createClients([ |
| 110 | + { |
| 111 | + id: base.id, // Base mainnet - will use fallback |
| 112 | + }, |
| 113 | + { |
| 114 | + id: sepolia.id, |
| 115 | + rpcUrl: sepolia.rpcUrls.default.http[0], |
| 116 | + }, |
| 117 | + { |
| 118 | + id: 999999, // Unsupported chain - will be skipped |
| 119 | + }, |
| 120 | + { |
| 121 | + id: baseSepolia.id, // Base Sepolia - will use fallback |
| 122 | + }, |
| 123 | + ]); |
| 124 | + |
| 125 | + expect(Object.keys(ChainClients.getState()).length).toBe(3); |
| 126 | + expect(ChainClients.getState()[base.id]).toBeDefined(); |
| 127 | + expect(ChainClients.getState()[sepolia.id]).toBeDefined(); |
| 128 | + expect(ChainClients.getState()[baseSepolia.id]).toBeDefined(); |
| 129 | + expect(ChainClients.getState()[999999]).toBeUndefined(); |
| 130 | + }); |
| 131 | + }); |
50 | 132 | }); |
0 commit comments