|
| 1 | +/* eslint-disable no-magic-numbers, no-loop-func, @typescript-eslint/no-non-null-assertion, unicorn/consistent-function-scoping, @typescript-eslint/no-explicit-any, @typescript-eslint/no-var-requires, camelcase */ |
| 2 | +import { MaestroBitcoinDataProvider } from '../src/wallet/lib/providers/MaestroBitcoinDataProvider'; |
| 3 | +import { TransactionStatus, FeeEstimationMode } from '../src/wallet/lib/providers/BitcoinDataProvider'; |
| 4 | +import { Network } from '../src/wallet/lib/common'; |
| 5 | + |
| 6 | +describe('MaestroBitcoinDataProvider', () => { |
| 7 | + let provider: MaestroBitcoinDataProvider; |
| 8 | + let mockAxios: any; |
| 9 | + let mockCache: any; |
| 10 | + let mockLogger: any; |
| 11 | + |
| 12 | + const mockTxDetails = { |
| 13 | + vin: [{ txid: 'abc', vout: 0, address: 'addr1', value: '0.0001' }], |
| 14 | + vout: [{ address: 'addr2', value: '0.00009' }], |
| 15 | + confirmations: 5, |
| 16 | + blockheight: 100_000, |
| 17 | + blocktime: 1_696_969_696 |
| 18 | + }; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + mockAxios = { |
| 22 | + get: jest.fn(), |
| 23 | + post: jest.fn() |
| 24 | + }; |
| 25 | + |
| 26 | + mockCache = { |
| 27 | + get: jest.fn(), |
| 28 | + set: jest.fn() |
| 29 | + }; |
| 30 | + |
| 31 | + mockLogger = { |
| 32 | + debug: jest.fn() |
| 33 | + }; |
| 34 | + |
| 35 | + jest.spyOn(require('axios'), 'create').mockReturnValue(mockAxios); |
| 36 | + |
| 37 | + provider = new MaestroBitcoinDataProvider('dummy-token', mockCache, mockLogger, Network.Mainnet); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + jest.restoreAllMocks(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('fetches last known block', async () => { |
| 45 | + mockAxios.get.mockResolvedValue({ data: { last_updated: { block_height: 100, block_hash: 'abc123' } } }); |
| 46 | + |
| 47 | + const result = await provider.getLastKnownBlock(); |
| 48 | + expect(result).toEqual({ height: 100, hash: 'abc123' }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('fetches transaction details', async () => { |
| 52 | + const txHash = 'tx123'; |
| 53 | + const txDetails = { |
| 54 | + vin: [{ txid: 'prevTx', vout: 0, address: 'addr1', value: '0.0001' }], |
| 55 | + vout: [{ address: 'addr2', value: '0.00009' }], |
| 56 | + confirmations: 10, |
| 57 | + blockheight: 500, |
| 58 | + blocktime: 1_234_567 |
| 59 | + }; |
| 60 | + |
| 61 | + mockCache.get.mockResolvedValue(); |
| 62 | + mockAxios.get.mockResolvedValue({ data: { data: txDetails } }); |
| 63 | + |
| 64 | + const result = await provider.getTransaction(txHash); |
| 65 | + expect(result.transactionHash).toBe(txHash); |
| 66 | + expect(result.status).toBe(TransactionStatus.Confirmed); |
| 67 | + expect(result.inputs[0].address).toBe('addr1'); |
| 68 | + expect(result.outputs[0].address).toBe('addr2'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('fetches transaction history with pagination', async () => { |
| 72 | + mockAxios.get |
| 73 | + .mockResolvedValueOnce({ data: { data: [{ tx_hash: 'tx1' }], next_cursor: 'next-cursor' } }) |
| 74 | + .mockResolvedValueOnce({ data: { data: mockTxDetails } }); |
| 75 | + |
| 76 | + mockCache.get.mockResolvedValue(); |
| 77 | + |
| 78 | + const result = await provider.getTransactions('addr1'); |
| 79 | + expect(result.transactions.length).toBe(1); |
| 80 | + expect(result.transactions[0].transactionHash).toBe('tx1'); |
| 81 | + expect(result.nextCursor).toBe('next-cursor'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('fetches mempool transactions', async () => { |
| 85 | + mockAxios.get |
| 86 | + .mockResolvedValueOnce({ data: { data: [{ txid: 'tx-mempool', mempool: true }] } }) |
| 87 | + .mockResolvedValueOnce({ data: { data: mockTxDetails } }); |
| 88 | + |
| 89 | + mockCache.get.mockResolvedValue(); |
| 90 | + |
| 91 | + const result = await provider.getTransactionsInMempool('addr1'); |
| 92 | + expect(result.length).toBe(1); |
| 93 | + expect(result[0].transactionHash).toBe('tx-mempool'); |
| 94 | + expect(result[0].status).toBe(TransactionStatus.Pending); |
| 95 | + }); |
| 96 | + |
| 97 | + it('fetches UTXOs', async () => { |
| 98 | + const utxoData = [{ txid: 'abc', vout: 0, satoshis: '10000', address: 'addr1' }]; |
| 99 | + mockAxios.get.mockResolvedValue({ data: { data: utxoData } }); |
| 100 | + |
| 101 | + const result = await provider.getUTxOs('addr1'); |
| 102 | + expect(result[0].txId).toBe('abc'); |
| 103 | + expect(result[0].address).toBe('addr1'); |
| 104 | + expect(typeof result[0].satoshis).toBe('bigint'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('submits transaction and returns hash', async () => { |
| 108 | + mockAxios.post.mockResolvedValue({ status: 201, data: 'txhash123' }); |
| 109 | + |
| 110 | + const result = await provider.submitTransaction('rawtx'); |
| 111 | + expect(result).toBe('txhash123'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('returns status as Confirmed', async () => { |
| 115 | + mockAxios.get.mockResolvedValue({ data: { confirmations: 3 } }); |
| 116 | + |
| 117 | + const result = await provider.getTransactionStatus('tx1'); |
| 118 | + expect(result).toBe(TransactionStatus.Confirmed); |
| 119 | + }); |
| 120 | + |
| 121 | + it('returns status as Dropped for 404', async () => { |
| 122 | + mockAxios.get.mockRejectedValue({ response: { status: 404 } }); |
| 123 | + |
| 124 | + const result = await provider.getTransactionStatus('tx404'); |
| 125 | + expect(result).toBe(TransactionStatus.Dropped); |
| 126 | + }); |
| 127 | + |
| 128 | + it('estimates fees', async () => { |
| 129 | + mockAxios.get.mockResolvedValue({ status: 200, data: { data: { feerate: 22, blocks: 3 } } }); |
| 130 | + |
| 131 | + const result = await provider.estimateFee(3, FeeEstimationMode.Conservative); |
| 132 | + expect(result.feeRate).toBe(22); |
| 133 | + expect(result.blocks).toBe(3); |
| 134 | + }); |
| 135 | +}); |
0 commit comments