import { beforeEach, describe, expect, it, vi } from 'vitest'; import { PoolErrorException } from '../../error/pool.error'; import { getUtxosFromTxInputs } from '../fee'; const testAncestorsResult = { totalAncestorsSize: 100, totalAncestorsFee: 200n, }; vi.mock('@saturnbtcio/psbt', () => ({ getTotalSizeAndFeesFromAncestors: vi.fn((txStatuses: any[]) => { return txStatuses.length === 0 ? { totalAncestorsSize: 0, totalAncestorsFee: 0n } : testAncestorsResult; }), })); interface TestUtxo { txid: string; vout: number; value: bigint; collectionStatuses?: any[]; hasInscription?: boolean; status: { confirmed: boolean; }; } const createTestUtxo = ( txid: string, vout: number, value: bigint, confirmed = true, ): TestUtxo => ({ txid, vout, value, collectionStatuses: [], hasInscription: false, status: { confirmed, }, }); class TestTransaction { inputsLength: number; id: string; private inputs: any[]; constructor(inputs: any[], id: string) { this.inputs = inputs; this.inputsLength = inputs.length; this.id = id; } getInput(i: number) { return this.inputs[i]; } } const createTestInput = (txidStr: string, index: number) => ({ txid: Buffer.from(txidStr), index, }); describe('getUtxosFromTxInputs', () => { let testTx: TestTransaction; let walletUtxos: TestUtxo[]; beforeEach(() => { walletUtxos = [ createTestUtxo('747841', 0, 1000n, true), // 747841 encoded txA createTestUtxo('747842', 1, 2000n, true), // 747842 encoded txB ]; }); it('Returns empty array for a transaction with no inputs', () => { testTx = new TestTransaction([], 'dummyTxId'); // tx with no inputs const result = getUtxosFromTxInputs(testTx as any, walletUtxos as any); expect(result).toStrictEqual([]); }); it('Processes each input and returns correct totals when inputs are valid', () => { const input0 = createTestInput('txA', 0); const input1 = createTestInput('txB', 1); testTx = new TestTransaction([input0, input1], 'dummyTxId'); const result = getUtxosFromTxInputs(testTx as any, walletUtxos as any); expect(result[0].txid).toBe('747841'); expect(result[1].txid).toBe('747842'); }); it('Throws an error if an input has txid but no matching wallet utxo', () => { const input0 = createTestInput('txC', 0); testTx = new TestTransaction([input0], 'dummyTxId'); expect(() => { getUtxosFromTxInputs(testTx as any, walletUtxos as any); }).toThrow(PoolErrorException); }); });