mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-21 16:52:04 -07:00
Improved tests for electrumx lib
This commit is contained in:
+1
-1
@@ -16,7 +16,7 @@
|
|||||||
"dev": "nodemon ./dist/app.js",
|
"dev": "nodemon ./dist/app.js",
|
||||||
"test": "npm run lint && npm run test-v3",
|
"test": "npm run lint && npm run test-v3",
|
||||||
"lint": "standard --env mocha --fix",
|
"lint": "standard --env mocha --fix",
|
||||||
"test-v3": "export NETWORK=mainnet && nyc --reporter=text mocha --timeout 60000 test/v3/electrumx.js",
|
"test-v3": "export NETWORK=mainnet && nyc --reporter=text mocha --timeout 60000 test/v3/",
|
||||||
"test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/blockchain.js",
|
"test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/blockchain.js",
|
||||||
"test:integration": "mocha test/v3/integration",
|
"test:integration": "mocha test/v3/integration",
|
||||||
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ class Electrum {
|
|||||||
|
|
||||||
// Returns a promise that resolves to UTXO data for an address. Expects input
|
// Returns a promise that resolves to UTXO data for an address. Expects input
|
||||||
// to be a cash address, and input validation to have already been done by
|
// to be a cash address, and input validation to have already been done by
|
||||||
// calling function.
|
// parent, calling function.
|
||||||
async _utxosFromElectrumx (address) {
|
async _utxosFromElectrumx (address) {
|
||||||
try {
|
try {
|
||||||
// Convert the address to a scripthash.
|
// Convert the address to a scripthash.
|
||||||
@@ -143,8 +143,10 @@ class Electrum {
|
|||||||
|
|
||||||
return electrumResponse
|
return electrumResponse
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
// console.log('err: ', err)
|
||||||
|
|
||||||
// Write out error to error log.
|
// Write out error to error log.
|
||||||
wlogger.error('Error in elecrumx.js/_utxosFromElectrumx().')
|
wlogger.error('Error in elecrumx.js/_utxosFromElectrumx(): ', err)
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -191,7 +193,7 @@ class Electrum {
|
|||||||
|
|
||||||
// Get data from ElectrumX server.
|
// Get data from ElectrumX server.
|
||||||
const electrumResponse = await _this._utxosFromElectrumx(cashAddr)
|
const electrumResponse = await _this._utxosFromElectrumx(cashAddr)
|
||||||
console.log(`electrumResponse: ${JSON.stringify(electrumResponse, null, 2)}`)
|
// console.log(`electrumResponse: ${JSON.stringify(electrumResponse, null, 2)}`)
|
||||||
|
|
||||||
// Pass the error message if ElectrumX reports an error.
|
// Pass the error message if ElectrumX reports an error.
|
||||||
if (Object.prototype.hasOwnProperty.call(electrumResponse, 'code')) {
|
if (Object.prototype.hasOwnProperty.call(electrumResponse, 'code')) {
|
||||||
|
|||||||
+80
-10
@@ -163,22 +163,31 @@ describe('#ElectrumX Router', () => {
|
|||||||
assert.property(result, 'success')
|
assert.property(result, 'success')
|
||||||
assert.equal(result.success, false)
|
assert.equal(result.success, false)
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
describe('#_utxosFromElectrumx', () => {
|
it('should pass errors from ElectrumX to user', async () => {
|
||||||
// Unit test only.
|
// Address has invalid checksum.
|
||||||
if (process.env.TEST === 'unit') {
|
req.params.address =
|
||||||
it('should pass errors from ElectrumX to user', async () => {
|
'bitcoincash:qr69kyzha07dcecrsvjwsj4s6slnlq4r8c30lxnur2'
|
||||||
req.params.address =
|
|
||||||
'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
|
||||||
|
|
||||||
|
// Mock unit tests to prevent live network calls.
|
||||||
|
if (process.env.TEST === 'unit') {
|
||||||
electrumxRoute.isReady = true // Force flag.
|
electrumxRoute.isReady = true // Force flag.
|
||||||
|
|
||||||
sandbox
|
sandbox
|
||||||
.stub(electrumxRoute.electrumx, 'request')
|
.stub(electrumxRoute.electrumx, 'request')
|
||||||
.resolves(mockData.utxos)
|
.resolves(mockData.utxos)
|
||||||
})
|
}
|
||||||
}
|
|
||||||
|
// Call the details API.
|
||||||
|
const result = await electrumxRoute.getUtxos(req, res)
|
||||||
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
|
||||||
|
assert.property(result, 'success')
|
||||||
|
assert.equal(result.success, false)
|
||||||
|
|
||||||
|
assert.property(result, 'error')
|
||||||
|
assert.include(result.error, 'Unsupported address format')
|
||||||
|
})
|
||||||
|
|
||||||
it('should get balance for a single address', async () => {
|
it('should get balance for a single address', async () => {
|
||||||
req.params.address =
|
req.params.address =
|
||||||
@@ -195,7 +204,7 @@ describe('#ElectrumX Router', () => {
|
|||||||
|
|
||||||
// Call the details API.
|
// Call the details API.
|
||||||
const result = await electrumxRoute.getUtxos(req, res)
|
const result = await electrumxRoute.getUtxos(req, res)
|
||||||
console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
|
||||||
assert.property(result, 'success')
|
assert.property(result, 'success')
|
||||||
assert.equal(result.success, true)
|
assert.equal(result.success, true)
|
||||||
@@ -209,4 +218,65 @@ describe('#ElectrumX Router', () => {
|
|||||||
assert.property(result.utxos[0], 'value')
|
assert.property(result.utxos[0], 'value')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('#_utxosFromElectrumx', () => {
|
||||||
|
it('should throw error for invalid address', async () => {
|
||||||
|
try {
|
||||||
|
// Address has invalid checksum.
|
||||||
|
const address = 'bitcoincash:qr69kyzha07dcecrsvjwsj4s6slnlq4r8c30lxnur2'
|
||||||
|
|
||||||
|
// Call the details API.
|
||||||
|
await electrumxRoute._utxosFromElectrumx(address)
|
||||||
|
|
||||||
|
assert.equal(true, false, 'Unexpected code path')
|
||||||
|
} catch (err) {
|
||||||
|
assert.include(err.message, 'Invalid checksum')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return empty array for address with no utxos', async () => {
|
||||||
|
// Address has invalid checksum.
|
||||||
|
const address =
|
||||||
|
'bchtest:qqtmlpspjakqlvywae226esrcdrj9auynuwadh55uf'
|
||||||
|
|
||||||
|
// Mock unit tests to prevent live network calls.
|
||||||
|
if (process.env.TEST === 'unit') {
|
||||||
|
electrumxRoute.isReady = true // Force flag.
|
||||||
|
|
||||||
|
sandbox
|
||||||
|
.stub(electrumxRoute.electrumx, 'request')
|
||||||
|
.resolves([])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the details API.
|
||||||
|
const result = await electrumxRoute._utxosFromElectrumx(address)
|
||||||
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
|
||||||
|
assert.isArray(result)
|
||||||
|
assert.equal(result.length, 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should get balance for a single address', async () => {
|
||||||
|
const address = 'bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf'
|
||||||
|
|
||||||
|
// Mock unit tests to prevent live network calls.
|
||||||
|
if (process.env.TEST === 'unit') {
|
||||||
|
electrumxRoute.isReady = true // Force flag.
|
||||||
|
|
||||||
|
sandbox
|
||||||
|
.stub(electrumxRoute.electrumx, 'request')
|
||||||
|
.resolves(mockData.utxos)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the details API.
|
||||||
|
const result = await electrumxRoute._utxosFromElectrumx(address)
|
||||||
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
|
||||||
|
assert.isArray(result)
|
||||||
|
assert.property(result[0], 'height')
|
||||||
|
assert.property(result[0], 'tx_hash')
|
||||||
|
assert.property(result[0], 'tx_pos')
|
||||||
|
assert.property(result[0], 'value')
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user