|
| 1 | +import { RPC_URLS } from '@filoz/synapse-sdk' |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | +import { getRpcUrl } from '../../common/get-rpc-url.js' |
| 4 | +import type { CLIAuthOptions } from '../../utils/cli-auth.js' |
| 5 | + |
| 6 | +/** |
| 7 | + * In production Commander already maps env vars (NETWORK/RPC_URL) into CLI options. |
| 8 | + * These tests cover the behavior of getRpcUrl given the options object Commander |
| 9 | + * would pass, without interacting with process.env directly. |
| 10 | + */ |
| 11 | + |
| 12 | +describe('getRpcUrl', () => { |
| 13 | + it('returns explicit rpcUrl even when network is provided', () => { |
| 14 | + const options: CLIAuthOptions = { |
| 15 | + rpcUrl: 'wss://custom.rpc.url/ws', |
| 16 | + network: 'mainnet', |
| 17 | + } |
| 18 | + |
| 19 | + expect(getRpcUrl(options)).toBe('wss://custom.rpc.url/ws') |
| 20 | + }) |
| 21 | + |
| 22 | + it.each([ |
| 23 | + ['mainnet', RPC_URLS.mainnet.websocket], |
| 24 | + ['calibration', RPC_URLS.calibration.websocket], |
| 25 | + ])('returns RPC URL for %s network', (network, expected) => { |
| 26 | + expect(getRpcUrl({ network } satisfies CLIAuthOptions)).toBe(expected) |
| 27 | + }) |
| 28 | + |
| 29 | + it('normalizes network casing and whitespace', () => { |
| 30 | + expect( |
| 31 | + getRpcUrl({ |
| 32 | + network: ' MAINNET ', |
| 33 | + }) |
| 34 | + ).toBe(RPC_URLS.mainnet.websocket) |
| 35 | + |
| 36 | + expect( |
| 37 | + getRpcUrl({ |
| 38 | + network: '\tCaLiBrAtIoN\n', |
| 39 | + }) |
| 40 | + ).toBe(RPC_URLS.calibration.websocket) |
| 41 | + }) |
| 42 | + |
| 43 | + it('defaults to calibration when network is missing or blank', () => { |
| 44 | + expect(getRpcUrl({})).toBe(RPC_URLS.calibration.websocket) |
| 45 | + expect(getRpcUrl({ network: '' })).toBe(RPC_URLS.calibration.websocket) |
| 46 | + expect(getRpcUrl({ network: ' ' })).toBe(RPC_URLS.calibration.websocket) |
| 47 | + }) |
| 48 | + |
| 49 | + it('treats empty rpcUrl as falsy and falls back to defaults', () => { |
| 50 | + expect(getRpcUrl({ rpcUrl: '' })).toBe(RPC_URLS.calibration.websocket) |
| 51 | + }) |
| 52 | + |
| 53 | + it('throws for unsupported networks', () => { |
| 54 | + expect(() => getRpcUrl({ network: 'invalid' })).toThrow( |
| 55 | + 'Invalid network: "invalid". Must be "mainnet" or "calibration"' |
| 56 | + ) |
| 57 | + }) |
| 58 | +}) |
0 commit comments