Changeset View
Changeset View
Standalone View
Standalone View
apps/marlin-wallet/web/test/transaction-manager.test.ts
| Show First 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | beforeEach(async function () { | ||||
| await wallet.sync(); | await wallet.sync(); | ||||
| // Mock onBalanceChange callback | // Mock onBalanceChange callback | ||||
| onBalanceChange = sinon.stub().resolves(); | onBalanceChange = sinon.stub().resolves(); | ||||
| transactionManager = new TransactionManager({ | transactionManager = new TransactionManager({ | ||||
| ecashWallet: wallet, | ecashWallet: wallet, | ||||
| chronik: mockChronik as unknown as ChronikClient, | chronik: mockChronik as unknown as ChronikClient, | ||||
| tokenId: null, | |||||
| onBalanceChange, | onBalanceChange, | ||||
| }); | }); | ||||
| }); | }); | ||||
| afterEach(function () { | afterEach(function () { | ||||
| sinon.restore(); | sinon.restore(); | ||||
| }); | }); | ||||
| Show All 37 Lines | describe('sync', function () { | ||||
| }, | }, | ||||
| ]); | ]); | ||||
| await newWallet.sync(); | await newWallet.sync(); | ||||
| const manager = new TransactionManager({ | const manager = new TransactionManager({ | ||||
| ecashWallet: newWallet, | ecashWallet: newWallet, | ||||
| chronik: mockChronik as unknown as ChronikClient, | chronik: mockChronik as unknown as ChronikClient, | ||||
| tokenId: null, | |||||
| onBalanceChange, | onBalanceChange, | ||||
| }); | }); | ||||
| expect(manager.getAvailableBalanceSats()).to.equal(500000); | expect(manager.getAvailableBalanceSats()).to.equal(500000); | ||||
| expect(manager.getTransitionalBalanceSats()).to.equal(300000); | expect(manager.getTransitionalBalanceSats()).to.equal(300000); | ||||
| expect(manager.isPendingTransaction('bb'.repeat(32))).to.equal( | expect(manager.isPendingTransaction('bb'.repeat(32))).to.equal( | ||||
| true, | true, | ||||
| ); | ); | ||||
| expect(manager.isPendingTransaction('aa'.repeat(32))).to.equal( | expect(manager.isPendingTransaction('aa'.repeat(32))).to.equal( | ||||
| false, | false, | ||||
| ); | ); | ||||
| }); | }); | ||||
| it('Should handle null wallet gracefully', function () { | it('Should handle null wallet gracefully', function () { | ||||
| const manager = new TransactionManager({ | const manager = new TransactionManager({ | ||||
| ecashWallet: null, | ecashWallet: null, | ||||
| chronik: mockChronik as unknown as ChronikClient, | chronik: mockChronik as unknown as ChronikClient, | ||||
| tokenId: null, | |||||
| onBalanceChange, | onBalanceChange, | ||||
| }); | }); | ||||
| expect(manager.getAvailableBalanceSats()).to.equal(0); | expect(manager.getAvailableBalanceSats()).to.equal(0); | ||||
| expect(manager.getTransitionalBalanceSats()).to.equal(0); | expect(manager.getTransitionalBalanceSats()).to.equal(0); | ||||
| }); | }); | ||||
| }); | }); | ||||
| describe('addNonFinalTransaction', function () { | describe('addNonFinalTransaction', function () { | ||||
| it('Should add a non-final transaction and update transitional balance', async function () { | it('Should add a non-final transaction and update transitional balance', async function () { | ||||
| const txid = '11'.repeat(32); | const txid = '11'.repeat(32); | ||||
| const amountSats = 50000; // 500 XEC | const amountSats = 50000; // 500 XEC | ||||
| mockChronik.setTx(txid, createIncomingTx(txid, amountSats, wallet)); | mockChronik.setTx(txid, createIncomingTx(txid, amountSats, wallet)); | ||||
| const result = | const result = | ||||
| await transactionManager.addNonFinalTransaction(txid); | await transactionManager.addNonFinalTransaction(txid); | ||||
| expect(result).to.not.equal(false); | expect(result).to.not.equal(false); | ||||
| if (result !== false) { | if (result !== false) { | ||||
| expect(result.amountSats).to.equal(amountSats); | expect(result.amountAtoms).to.equal(amountSats); | ||||
| expect(result.state).to.equal('pending_finalization'); | expect(result.state).to.equal('pending_finalization'); | ||||
| } | } | ||||
| expect(transactionManager.isPendingTransaction(txid)).to.equal( | expect(transactionManager.isPendingTransaction(txid)).to.equal( | ||||
| true, | true, | ||||
| ); | ); | ||||
| expect(transactionManager.getTransitionalBalanceSats()).to.equal( | expect(transactionManager.getTransitionalBalanceSats()).to.equal( | ||||
| amountSats, | amountSats, | ||||
| ▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | describe('addNonFinalTransaction', function () { | ||||
| expect(result).to.equal(false); | expect(result).to.equal(false); | ||||
| expect(onBalanceChange.calledOnce).to.equal(true); // Only called once | expect(onBalanceChange.calledOnce).to.equal(true); // Only called once | ||||
| }); | }); | ||||
| it('Should return false if wallet is not loaded', async function () { | it('Should return false if wallet is not loaded', async function () { | ||||
| const manager = new TransactionManager({ | const manager = new TransactionManager({ | ||||
| ecashWallet: null, | ecashWallet: null, | ||||
| chronik: mockChronik as unknown as ChronikClient, | chronik: mockChronik as unknown as ChronikClient, | ||||
| tokenId: null, | |||||
| onBalanceChange, | onBalanceChange, | ||||
| }); | }); | ||||
| const txid = '44'.repeat(32); | const txid = '44'.repeat(32); | ||||
| const result = await manager.addNonFinalTransaction(txid); | const result = await manager.addNonFinalTransaction(txid); | ||||
| expect(result).to.equal(false); | expect(result).to.equal(false); | ||||
| }); | }); | ||||
| ▲ Show 20 Lines • Show All 261 Lines • Show Last 20 Lines | |||||