|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { parseWalletPaste, formatWalletCopyText, SUPPORTED_PAYOUT_COINS } from '@/lib/wallet-import'; |
| 3 | + |
| 4 | +describe('lib/wallet-import', () => { |
| 5 | + describe('SUPPORTED_PAYOUT_COINS', () => { |
| 6 | + it('includes all major cryptocurrencies', () => { |
| 7 | + expect(SUPPORTED_PAYOUT_COINS).toContain('BTC'); |
| 8 | + expect(SUPPORTED_PAYOUT_COINS).toContain('ETH'); |
| 9 | + expect(SUPPORTED_PAYOUT_COINS).toContain('SOL'); |
| 10 | + expect(SUPPORTED_PAYOUT_COINS).toContain('USDC'); |
| 11 | + expect(SUPPORTED_PAYOUT_COINS).toContain('USDT'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('includes CoinPay-specific variants', () => { |
| 15 | + expect(SUPPORTED_PAYOUT_COINS).toContain('USDC_SOL'); |
| 16 | + expect(SUPPORTED_PAYOUT_COINS).toContain('USDC_ETH'); |
| 17 | + expect(SUPPORTED_PAYOUT_COINS).toContain('USDC_POL'); |
| 18 | + expect(SUPPORTED_PAYOUT_COINS).toContain('USDT_SOL'); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('parseWalletPaste', () => { |
| 23 | + it('parses standard CoinPay format', () => { |
| 24 | + const input = `BTC: bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh |
| 25 | +USDC_SOL: FX8QhU1TPUHGs2X8PibbHikd4YvdQMPfVuFd6mqk9qJw |
| 26 | +ETH: 0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb`; |
| 27 | + |
| 28 | + const result = parseWalletPaste(input); |
| 29 | + expect(result.wallets).toHaveLength(3); |
| 30 | + expect(result.wallets[0]).toEqual({ |
| 31 | + coin: 'BTC', |
| 32 | + address: 'bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh', |
| 33 | + label: undefined, |
| 34 | + rawLine: 'BTC: bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh', |
| 35 | + }); |
| 36 | + expect(result.invalidLines).toHaveLength(0); |
| 37 | + expect(result.unsupportedCoins).toHaveLength(0); |
| 38 | + }); |
| 39 | + |
| 40 | + it('parses format with labels', () => { |
| 41 | + const input = `USDC_POL (Treasury): 0xEf993488b444b75585A5CCe171e65F4dD9D99add |
| 42 | +BTC (Cold Storage): bc1qexample`; |
| 43 | + |
| 44 | + const result = parseWalletPaste(input); |
| 45 | + expect(result.wallets).toHaveLength(2); |
| 46 | + expect(result.wallets[0].label).toBe('Treasury'); |
| 47 | + expect(result.wallets[1].label).toBe('Cold Storage'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('ignores empty lines and comments', () => { |
| 51 | + const input = `# My wallets |
| 52 | +BTC: bc1qexample |
| 53 | +
|
| 54 | +ETH: 0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb |
| 55 | +`; |
| 56 | + const result = parseWalletPaste(input); |
| 57 | + expect(result.wallets).toHaveLength(2); |
| 58 | + expect(result.invalidLines).toHaveLength(0); |
| 59 | + }); |
| 60 | + |
| 61 | + it('detects invalid lines', () => { |
| 62 | + const input = `BTC: bc1qexample |
| 63 | +INVALID_LINE_NO_COLON |
| 64 | +: missing_coin |
| 65 | +XRP: `; |
| 66 | + const result = parseWalletPaste(input); |
| 67 | + expect(result.wallets).toHaveLength(1); |
| 68 | + expect(result.invalidLines.length).toBeGreaterThanOrEqual(2); |
| 69 | + }); |
| 70 | + |
| 71 | + it('detects unsupported coins', () => { |
| 72 | + const input = `FAKECOIN: address123 |
| 73 | +BTC: bc1qexample`; |
| 74 | + const result = parseWalletPaste(input); |
| 75 | + expect(result.wallets).toHaveLength(1); |
| 76 | + expect(result.unsupportedCoins).toContain('FAKECOIN'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('deduplicates by coin (last wins)', () => { |
| 80 | + const input = `BTC: bc1qfirst12345 |
| 81 | +BTC: bc1qsecond67890`; |
| 82 | + const result = parseWalletPaste(input); |
| 83 | + expect(result.wallets).toHaveLength(1); |
| 84 | + expect(result.wallets[0].address).toBe('bc1qsecond67890'); |
| 85 | + expect(result.duplicateCoins).toContain('BTC'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('returns empty result for empty input', () => { |
| 89 | + const result = parseWalletPaste(''); |
| 90 | + expect(result.wallets).toHaveLength(0); |
| 91 | + }); |
| 92 | + |
| 93 | + it('handles single wallet line', () => { |
| 94 | + const result = parseWalletPaste('SOL: 7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU'); |
| 95 | + expect(result.wallets).toHaveLength(1); |
| 96 | + expect(result.wallets[0].coin).toBe('SOL'); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('formatWalletCopyText', () => { |
| 101 | + it('formats wallets for clipboard copy', () => { |
| 102 | + const wallets = [ |
| 103 | + { coin: 'BTC', address: 'bc1qexample' }, |
| 104 | + { coin: 'USDC_SOL', address: 'FX8QhU1', label: 'Main' }, |
| 105 | + ]; |
| 106 | + const result = formatWalletCopyText(wallets); |
| 107 | + expect(result).toBe(`BTC: bc1qexample |
| 108 | +USDC_SOL (Main): FX8QhU1`); |
| 109 | + }); |
| 110 | + |
| 111 | + it('returns empty string for empty array', () => { |
| 112 | + expect(formatWalletCopyText([])).toBe(''); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments