From 7964bfbae7c73b569793bc7c4f4bfcab84cbaf81 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Wed, 9 Mar 2022 13:52:45 -0800 Subject: [PATCH] fix(tests): Fixing broken unit tests --- src/adapters/wallet.js | 120 ++++++++++----------- src/use-cases/offer.js | 2 +- test/unit/adapters/wallet.adapter.unit.js | 76 ++++++------- test/unit/mocks/adapters/index.js | 12 +++ test/unit/mocks/adapters/wallet.js | 109 ++++++++++--------- test/unit/use-cases/offer.use-case.unit.js | 10 +- 6 files changed, 176 insertions(+), 153 deletions(-) diff --git a/src/adapters/wallet.js b/src/adapters/wallet.js index b431caf..842bacc 100644 --- a/src/adapters/wallet.js +++ b/src/adapters/wallet.js @@ -10,9 +10,9 @@ const JsonFiles = require('./json-files') const config = require('../../config') const WALLET_FILE = `${__dirname.toString()}/../../wallet.json` -const PROOF_OF_BURN_QTY = 0.01 -const P2WDB_TOKEN_ID = - '38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0' +// const PROOF_OF_BURN_QTY = 0.01 +// const P2WDB_TOKEN_ID = +// '38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0' class WalletAdapter { constructor (localConfig = {}) { @@ -178,63 +178,63 @@ class WalletAdapter { } } - // Burn enough PSF to generate a valide proof-of-burn for writing to the P2WDB. - async burnPsf () { - try { - // TODO: Throw error if this.bchWallet has not been instantiated. - - // console.log('walletData: ', walletData) - // console.log( - // `walletData.utxos.utxoStore.slpUtxos: ${JSON.stringify( - // walletData.utxos.utxoStore.slpUtxos, - // null, - // 2, - // )}`, - // ) - - // Get token UTXOs held by the wallet. - const tokenUtxos = this.bchWallet.utxos.utxoStore.slpUtxos.type1.tokens - // console.log(`tokenUtxos: ${JSON.stringify(tokenUtxos, null, 2)}`) - - // Find a token UTXO that contains PSF with a quantity higher than needed - // to generate a proof-of-burn. - let tokenUtxo = {} - for (let i = 0; i < tokenUtxos.length; i++) { - const thisUtxo = tokenUtxos[i] - - // If token ID matches. - if (thisUtxo.tokenId === P2WDB_TOKEN_ID) { - if (parseFloat(thisUtxo.qtyStr) >= PROOF_OF_BURN_QTY) { - tokenUtxo = thisUtxo - break - } - } - } - - if (tokenUtxo.tokenId !== P2WDB_TOKEN_ID) { - throw new Error( - `Token UTXO of with ID of ${P2WDB_TOKEN_ID} and quantity greater than ${PROOF_OF_BURN_QTY} could not be found in wallet.` - ) - } - // console.log(`tokenUtxo: ${JSON.stringify(tokenUtxo, null, 2)}`) - - const result = await this.bchWallet.burnTokens( - PROOF_OF_BURN_QTY, - P2WDB_TOKEN_ID - ) - // console.log('walletData.burnTokens() result: ', result) - - return result - - // return { - // success: true, - // txid: 'fakeTxid', - // } - } catch (err) { - console.error('Error in burnPsf(): ', err) - throw err - } - } +// Burn enough PSF to generate a valide proof-of-burn for writing to the P2WDB. +// async burnPsf () { +// try { +// // TODO: Throw error if this.bchWallet has not been instantiated. +// +// // console.log('walletData: ', walletData) +// // console.log( +// // `walletData.utxos.utxoStore.slpUtxos: ${JSON.stringify( +// // walletData.utxos.utxoStore.slpUtxos, +// // null, +// // 2, +// // )}`, +// // ) +// +// // Get token UTXOs held by the wallet. +// const tokenUtxos = this.bchWallet.utxos.utxoStore.slpUtxos.type1.tokens +// // console.log(`tokenUtxos: ${JSON.stringify(tokenUtxos, null, 2)}`) +// +// // Find a token UTXO that contains PSF with a quantity higher than needed +// // to generate a proof-of-burn. +// let tokenUtxo = {} +// for (let i = 0; i < tokenUtxos.length; i++) { +// const thisUtxo = tokenUtxos[i] +// +// // If token ID matches. +// if (thisUtxo.tokenId === P2WDB_TOKEN_ID) { +// if (parseFloat(thisUtxo.qtyStr) >= PROOF_OF_BURN_QTY) { +// tokenUtxo = thisUtxo +// break +// } +// } +// } +// +// if (tokenUtxo.tokenId !== P2WDB_TOKEN_ID) { +// throw new Error( +// `Token UTXO of with ID of ${P2WDB_TOKEN_ID} and quantity greater than ${PROOF_OF_BURN_QTY} could not be found in wallet.` +// ) +// } +// // console.log(`tokenUtxo: ${JSON.stringify(tokenUtxo, null, 2)}`) +// +// const result = await this.bchWallet.burnTokens( +// PROOF_OF_BURN_QTY, +// P2WDB_TOKEN_ID +// ) +// // console.log('walletData.burnTokens() result: ', result) +// +// return result +// +// // return { +// // success: true, +// // txid: 'fakeTxid', +// // } +// } catch (err) { +// console.error('Error in burnPsf(): ', err) +// throw err +// } +// } } module.exports = WalletAdapter diff --git a/src/use-cases/offer.js b/src/use-cases/offer.js index 085a9c6..41419a6 100644 --- a/src/use-cases/offer.js +++ b/src/use-cases/offer.js @@ -130,7 +130,7 @@ class OfferLib { // in the offer. let totalTokenBalance = 0 tokenUtxos.map(x => (totalTokenBalance += parseFloat(x.qtyStr))) - // console.log('totalTokenBalance: ', totalTokenBalance) + console.log('totalTokenBalance: ', totalTokenBalance) // If there are fewer tokens in the wallet than what's in the offer, // throw an error. diff --git a/test/unit/adapters/wallet.adapter.unit.js b/test/unit/adapters/wallet.adapter.unit.js index d86507a..0892865 100644 --- a/test/unit/adapters/wallet.adapter.unit.js +++ b/test/unit/adapters/wallet.adapter.unit.js @@ -156,44 +156,44 @@ describe('#wallet', () => { }) }) - describe('#burnPsf', () => { - it('should burn PSF tokens and return the txid', async () => { - // mock instance of minimal-slp-wallet - uut.bchWallet = new MockBchWallet() - - const result = await uut.burnPsf() - // console.log('result: ', result) - - assert.equal(result.success, true) - assert.equal(result.txid, 'txid') - }) - - it('should throw error if no PSF tokens are found', async () => { - try { - // mock instance of minimal-slp-wallet - uut.bchWallet = new MockBchWallet() - - // Remove the PSF token from the mock data. - uut.bchWallet.utxos.utxoStore.slpUtxos.type1.tokens.pop() - - await uut.burnPsf() - - assert.fail('Unexpected code path') - } catch (err) { - assert.include(err.message, 'Token UTXO of with ID of') - } - }) - - it('should catch and throw an error', async () => { - try { - await uut.burnPsf() - - assert.fail('Unexpected code path') - } catch (err) { - assert.include(err.message, 'Cannot read') - } - }) - }) + // describe('#burnPsf', () => { + // it('should burn PSF tokens and return the txid', async () => { + // // mock instance of minimal-slp-wallet + // uut.bchWallet = new MockBchWallet() + // + // const result = await uut.burnPsf() + // // console.log('result: ', result) + // + // assert.equal(result.success, true) + // assert.equal(result.txid, 'txid') + // }) + // + // it('should throw error if no PSF tokens are found', async () => { + // try { + // // mock instance of minimal-slp-wallet + // uut.bchWallet = new MockBchWallet() + // + // // Remove the PSF token from the mock data. + // uut.bchWallet.utxos.utxoStore.slpUtxos.type1.tokens.pop() + // + // await uut.burnPsf() + // + // assert.fail('Unexpected code path') + // } catch (err) { + // assert.include(err.message, 'Token UTXO of with ID of') + // } + // }) + // + // it('should catch and throw an error', async () => { + // try { + // await uut.burnPsf() + // + // assert.fail('Unexpected code path') + // } catch (err) { + // assert.include(err.message, 'Cannot read') + // } + // }) + // }) describe('#incrementNextAddress', () => { it('should increment the nextAddress property', async () => { diff --git a/test/unit/mocks/adapters/index.js b/test/unit/mocks/adapters/index.js index 8b0e5c6..36d2a73 100644 --- a/test/unit/mocks/adapters/index.js +++ b/test/unit/mocks/adapters/index.js @@ -83,6 +83,18 @@ const localdb = { static find () {} static findOne () {} + async save () { + return {} + } + }, + + Offer: class Offer { + constructor (obj) {} + + static findById () {} + static find () {} + static findOne () {} + async save () { return {} } diff --git a/test/unit/mocks/adapters/wallet.js b/test/unit/mocks/adapters/wallet.js index 349ecd2..e7a70ae 100644 --- a/test/unit/mocks/adapters/wallet.js +++ b/test/unit/mocks/adapters/wallet.js @@ -5,11 +5,9 @@ const BCHJS = require('@psf/bch-js') const mockWallet = { - mnemonic: - 'course abstract aerobic deer try switch turtle diet fence affair butter top', + mnemonic: 'course abstract aerobic deer try switch turtle diet fence affair butter top', privateKey: 'L5D2UAam8tvo3uii5kpgaGyjvVMimdrXu8nWGQSQjuuAix6ji1YQ', - publicKey: - '0379433ffc401483ade310469953c1cba77c71af904f07c15bde330d7198b4d6dc', + publicKey: '0379433ffc401483ade310469953c1cba77c71af904f07c15bde330d7198b4d6dc', cashAddress: 'bitcoincash:qzl0d3gcqeypv4cy7gh8rgdszxa9vvm2acv7fqtd00', address: 'bitcoincash:qzl0d3gcqeypv4cy7gh8rgdszxa9vvm2acv7fqtd00', slpAddress: 'simpleledger:qzl0d3gcqeypv4cy7gh8rgdszxa9vvm2acq9zm7d33', @@ -29,7 +27,8 @@ class MockBchWallet { this.sendTokens = async () => { return 'fakeTxid' } - this.getUtxos = async () => {} + this.getUtxos = async () => { + } // Environment variable is used by wallet-balance.unit.js to force an error. if (process.env.NO_UTXO) { @@ -41,8 +40,7 @@ class MockBchWallet { bchUtxos: [ { height: 700685, - tx_hash: - '1fc577caaff5626a8477162581e57bae1b19dc6aa6c10638013c2b1ba14dc654', + tx_hash: '1fc577caaff5626a8477162581e57bae1b19dc6aa6c10638013c2b1ba14dc654', tx_pos: 0, value: 1000, txid: '1fc577caaff5626a8477162581e57bae1b19dc6aa6c10638013c2b1ba14dc654', @@ -51,8 +49,7 @@ class MockBchWallet { }, { height: 700685, - tx_hash: - '1fc577caaff5626a8477162581e57bae1b19dc6aa6c10638013c2b1ba14dc654', + tx_hash: '1fc577caaff5626a8477162581e57bae1b19dc6aa6c10638013c2b1ba14dc654', tx_pos: 2, value: 19406, txid: '1fc577caaff5626a8477162581e57bae1b19dc6aa6c10638013c2b1ba14dc654', @@ -66,47 +63,61 @@ class MockBchWallet { mintBatons: [], tokens: [ { - height: 700522, - tx_hash: - 'bb5691b50930816be78dad76d203a1c97ac94c03f6051b2fa0159c71c43aa3d0', - tx_pos: 1, - value: 546, - txid: 'bb5691b50930816be78dad76d203a1c97ac94c03f6051b2fa0159c71c43aa3d0', - vout: 1, - utxoType: 'token', - transactionType: 'send', - tokenId: - 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2', - tokenTicker: 'TROUT', - tokenName: "Trout's test token", - tokenDocumentUrl: 'troutsblog.com', - tokenDocumentHash: '', - decimals: 2, - tokenType: 1, - isValid: true, - tokenQty: '4.25' + 'height': 717331, + 'tx_hash': '74889580bb1a5f8c026aa2f55118ac9917df3332f7abae72a70343daa1c29621', + 'tx_pos': 1, + 'value': 546, + 'txid': '74889580bb1a5f8c026aa2f55118ac9917df3332f7abae72a70343daa1c29621', + 'vout': 1, + 'isSlp': true, + 'type': 'token', + 'qty': '10', + 'tokenId': '600ee24d0f208aebc2bdd2c4ee1b9acb6d57343561442e8676b5bbea311d5a0f', + 'address': 'bitcoincash:qqraj35x6l2qyqhjm5l7qlt7z2245ez8l5z3dwkeq5', + 'ticker': 'FLIPS', + 'name': 'FLIPS', + 'documentUri': '', + 'documentHash': '', + 'decimals': 1, + 'qtyStr': '1' }, { - height: 0, - tx_hash: - 'c0ac066ce6efa1fa4763bf85a91c738e57c12b8765731bd07f0d8f5a55ce582f', - tx_pos: 1, - value: 546, - txid: 'c0ac066ce6efa1fa4763bf85a91c738e57c12b8765731bd07f0d8f5a55ce582f', - vout: 1, - utxoType: 'token', - transactionType: 'send', - tokenId: - '38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0', - tokenTicker: 'PSF', - tokenName: 'Permissionless Software Foundation', - tokenDocumentUrl: 'psfoundation.cash', - tokenDocumentHash: '', - decimals: 8, - tokenType: 1, - isValid: true, - tokenQty: '1', - qtyStr: '1' + 'height': 730597, + 'tx_hash': '52520faddfafc46b8f8c9548b097f3a3b82a5bf363b5095047b9c5f83247fe36', + 'tx_pos': 1, + 'value': 546, + 'txid': '52520faddfafc46b8f8c9548b097f3a3b82a5bf363b5095047b9c5f83247fe36', + 'vout': 1, + 'isSlp': true, + 'type': 'token', + 'qty': '34999991', + 'tokenId': '38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0', + 'address': 'bitcoincash:qqraj35x6l2qyqhjm5l7qlt7z2245ez8l5z3dwkeq5', + 'ticker': 'PSF', + 'name': 'Permissionless Software Foundation', + 'documentUri': 'psfoundation.cash', + 'documentHash': '', + 'decimals': 8, + 'qtyStr': '0.34999991' + }, + { + 'height': 730597, + 'tx_hash': '5dc7e7c91382aed1666a51212dfb74050261e12c3c4f62b6b1e57f42d6c51ee1', + 'tx_pos': 2, + 'value': 546, + 'txid': '5dc7e7c91382aed1666a51212dfb74050261e12c3c4f62b6b1e57f42d6c51ee1', + 'vout': 2, + 'isSlp': true, + 'type': 'token', + 'qty': '18898', + 'tokenId': 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2', + 'address': 'bitcoincash:qqraj35x6l2qyqhjm5l7qlt7z2245ez8l5z3dwkeq5', + 'ticker': 'TROUT', + 'name': "Trout's test token", + 'documentUri': 'troutsblog.com', + 'documentHash': '', + 'decimals': 2, + 'qtyStr': '188.98' } ] }, @@ -122,4 +133,4 @@ class MockBchWallet { } } -module.exports = { MockBchWallet, mockWallet } +module.exports = { MockBchWallet, mockWallet} diff --git a/test/unit/use-cases/offer.use-case.unit.js b/test/unit/use-cases/offer.use-case.unit.js index 5a66e27..ece4c65 100644 --- a/test/unit/use-cases/offer.use-case.unit.js +++ b/test/unit/use-cases/offer.use-case.unit.js @@ -108,14 +108,14 @@ describe('#offer-use-case', () => { } // Mock dependencies - sandbox.stub(uut.adapters.wallet, 'burnPsf').resolves('fakeTxid') + // sandbox.stub(uut.adapters.wallet, 'burnPsf').resolves('fakeTxid') sandbox.stub(uut.offerEntity, 'validate').returns(entryObj) sandbox.stub(uut, 'ensureFunds').resolves() - sandbox.stub(uut, 'moveTokens').resolves({ txid: 'fakeTxid', vout: 0 }) + sandbox.stub(uut, 'moveTokens').resolves({ txid: 'fakeTxid', vout: 0, hdIndex: 1 }) sandbox.stub(uut.adapters.wallet.bchWallet, 'getUtxos').resolves() - sandbox - .stub(uut.adapters.wallet, 'generateSignature') - .resolves('fakeSignature') + // sandbox + // .stub(uut.adapters.wallet, 'generateSignature') + // .resolves('fakeSignature') sandbox.stub(uut.adapters.p2wdb, 'write').resolves('fakeHash') const result = await uut.createOffer(entryObj)