Skip to content

Commit

Permalink
Added unit tests for detectFunding
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Nov 8, 2023
1 parent ff7dd26 commit 741ff8a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
1 change: 0 additions & 1 deletion typescript/src/services/deposits/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export class Deposit {
* @return Specific UTXOs targeting this deposit. Empty array in case

Check failure on line 80 in typescript/src/services/deposits/deposit.ts

View workflow job for this annotation

GitHub Actions / typescript-format

Use @returns instead
* there are no UTXOs referring this deposit.
*/
// TODO: Cover with unit tests.
async detectFunding(): Promise<BitcoinUtxo[]> {
const utxos = await this.bitcoinClient.findAllUnspentTransactionOutputs(
await this.getBitcoinAddress()
Expand Down
82 changes: 82 additions & 0 deletions typescript/test/services/deposits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,88 @@ describe("Deposits", () => {
describe("Deposit", () => {
// TODO: Implement unit tests for other functions.

describe("detectFunding", () => {
let bitcoinClient: MockBitcoinClient
let tbtcContracts: MockTBTCContracts
let depositService: Deposit
let utxos: BitcoinUtxo[]

beforeEach(async () => {
bitcoinClient = new MockBitcoinClient()
tbtcContracts = new MockTBTCContracts()

depositService = await Deposit.fromReceipt(
deposit,
tbtcContracts,
bitcoinClient
)
})

context("when there are no UTXOs from funding transactions", () => {
context("when Bitcoin client returns undefined", () => {
beforeEach(async () => {
// Do not set any value for the address stored in the deposit
// service so that undefined is returned.
utxos = await depositService.detectFunding()
})

it("should return an empty UTXO array", async () => {
expect(utxos).to.be.empty
})
})

context("when Bitcoin client returns an empty array", () => {
beforeEach(async () => {
const unspentTransactionOutputs = new Map<string, BitcoinUtxo[]>()
// Set an empty array for the address stored in the deposit service.
unspentTransactionOutputs.set(
await depositService.getBitcoinAddress(),
[]
)
bitcoinClient.unspentTransactionOutputs = unspentTransactionOutputs
utxos = await depositService.detectFunding()
})

it("should return an empty UTXO array", async () => {
expect(utxos).to.be.empty
})
})
})

context("when there are UTXOs from funding transactions", () => {
const fundingUtxos: BitcoinUtxo[] = [
{
transactionHash: BitcoinTxHash.from(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
),
outputIndex: 0,
value: BigNumber.from(1111),
},
{
transactionHash: BitcoinTxHash.from(
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
),
outputIndex: 1,
value: BigNumber.from(2222),
},
]

beforeEach(async () => {
const unspentTransactionOutputs = new Map<string, BitcoinUtxo[]>()
unspentTransactionOutputs.set(
await depositService.getBitcoinAddress(),
fundingUtxos
)
bitcoinClient.unspentTransactionOutputs = unspentTransactionOutputs
utxos = await depositService.detectFunding()
})

it("should return funding UTXOs stored in the blockchain", async () => {
expect(utxos).to.be.equal(fundingUtxos)
})
})
})

describe("initiateMinting", () => {
describe("auto funding outpoint detection mode", () => {
// TODO: Unit test for this case.
Expand Down

0 comments on commit 741ff8a

Please sign in to comment.