Files
bch-js/test/unit/ninsight.js
T

256 lines
8.5 KiB
JavaScript
Raw Normal View History

2021-01-02 15:41:12 -08:00
const chai = require('chai')
2020-07-27 21:32:51 -07:00
const assert = chai.assert
2021-01-02 15:41:12 -08:00
const axios = require('axios')
const sinon = require('sinon')
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
const BCHJS = require('../../src/bch-js')
2020-07-27 21:32:51 -07:00
const bchjs = new BCHJS()
2021-01-02 15:41:12 -08:00
const mockData = require('./fixtures/ninsight-mock')
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
describe('#Ninsight', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
2021-01-02 15:41:12 -08:00
describe('#utxo', () => {
it('should throw an error for improper input', async () => {
2020-07-27 21:32:51 -07:00
try {
const addr = 12345
await bchjs.Ninsight.utxo(addr)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result!')
2020-07-27 21:32:51 -07:00
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err: `, err)
assert.include(
err.message,
2021-01-02 15:41:12 -08:00
'Input address must be a string or array of strings.'
)
2020-07-27 21:32:51 -07:00
}
})
2021-01-02 15:41:12 -08:00
it('should GET utxos for a single address', async () => {
2020-07-27 21:32:51 -07:00
// Stub the network call.
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'post').resolves({ data: mockData.utxo })
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
const addr = 'bitcoincash:qqh793x9au6ehvh7r2zflzguanlme760wuzehgzjh9'
2020-07-27 21:32:51 -07:00
const result = await bchjs.Ninsight.utxo(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'utxos')
assert.property(result, 'legacyAddress')
assert.property(result, 'cashAddress')
assert.property(result, 'slpAddress')
assert.property(result, 'scriptPubKey')
assert.property(result, 'asm')
2020-07-27 21:32:51 -07:00
assert.isArray(result.utxos)
2021-01-02 15:41:12 -08:00
assert.property(result.utxos[0], 'txid')
assert.property(result.utxos[0], 'vout')
assert.property(result.utxos[0], 'amount')
assert.property(result.utxos[0], 'satoshis')
assert.property(result.utxos[0], 'height')
assert.property(result.utxos[0], 'confirmations')
2020-07-27 21:32:51 -07:00
})
2021-01-02 15:41:12 -08:00
it('should POST utxo details for an array of addresses', async () => {
// Mock the network call.
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'post').resolves({ data: mockData.utxoPost })
const addr = [
2021-01-02 15:41:12 -08:00
'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7',
'bitcoincash:qz0us0z6ucpqt07jgpad0shgh7xmwxyr3ynlcsq0wr'
]
const result = await bchjs.Ninsight.utxo(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.isArray(result)
assert.isArray(result[0].utxos)
2021-01-02 15:41:12 -08:00
assert.property(result[0], 'utxos')
assert.property(result[0], 'legacyAddress')
assert.property(result[0], 'cashAddress')
assert.property(result[0], 'slpAddress')
assert.property(result[0], 'scriptPubKey')
assert.property(result[0], 'asm')
})
2020-07-27 21:32:51 -07:00
})
2020-11-09 16:12:10 -08:00
2021-01-02 15:41:12 -08:00
describe('#unconfirmed', () => {
it('should throw an error for improper input', async () => {
2020-11-09 17:44:09 +09:00
try {
const addr = 12345
await bchjs.Ninsight.unconfirmed(addr)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result!')
2020-11-09 17:44:09 +09:00
} catch (err) {
assert.include(
err.message,
2021-01-02 15:41:12 -08:00
'Input address must be a string or array of strings.'
2020-11-09 17:44:09 +09:00
)
}
})
2020-11-09 16:12:10 -08:00
2021-01-02 15:41:12 -08:00
it('should POST utxos for a single address', async () => {
2020-11-09 17:44:09 +09:00
// Stub the network call.
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'post').resolves({ data: mockData.unconfirmed })
2020-11-09 17:44:09 +09:00
2021-01-02 15:41:12 -08:00
const addr = 'bitcoincash:qpkkjkhe29mqhqmu3evtq3dsnruuzl3rku6usknlh5'
2020-11-09 17:44:09 +09:00
const result = await bchjs.Ninsight.unconfirmed(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
2021-01-02 15:41:12 -08:00
assert.property(result, 'utxos')
assert.property(result, 'legacyAddress')
assert.property(result, 'cashAddress')
assert.property(result, 'slpAddress')
assert.property(result, 'scriptPubKey')
2020-11-09 17:44:09 +09:00
assert.isArray(result.utxos)
2021-01-02 15:41:12 -08:00
assert.property(result.utxos[0], 'txid')
assert.property(result.utxos[0], 'vout')
assert.property(result.utxos[0], 'amount')
assert.property(result.utxos[0], 'satoshis')
assert.property(result.utxos[0], 'confirmations')
assert.property(result.utxos[0], 'ts')
2020-11-09 17:44:09 +09:00
})
2020-11-09 16:12:10 -08:00
2021-01-02 15:41:12 -08:00
it('should POST utxo details for an array of addresses', async () => {
2020-11-09 17:44:09 +09:00
// Mock the network call.
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'post').resolves({ data: mockData.unconfirmedPost })
2020-11-09 17:44:09 +09:00
const addr = [
2021-01-02 15:41:12 -08:00
'bitcoincash:qpkkjkhe29mqhqmu3evtq3dsnruuzl3rku6usknlh5',
'bitcoincash:qz0us0z6ucpqt07jgpad0shgh7xmwxyr3ynlcsq0wr'
2020-11-09 17:44:09 +09:00
]
const result = await bchjs.Ninsight.unconfirmed(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.isArray(result)
assert.isArray(result[0].utxos)
2021-01-02 15:41:12 -08:00
assert.property(result[0], 'utxos')
assert.property(result[0], 'legacyAddress')
assert.property(result[0], 'cashAddress')
assert.property(result[0], 'slpAddress')
assert.property(result[0], 'scriptPubKey')
2020-11-09 17:44:09 +09:00
})
})
2020-11-09 16:12:10 -08:00
2021-01-02 15:41:12 -08:00
describe('#transactions', () => {
it('should throw an error for improper input', async () => {
2020-11-09 19:49:41 +09:00
try {
const addr = 12345
await bchjs.Ninsight.transactions(addr)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result!')
2020-11-09 19:49:41 +09:00
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err: `, err)
2020-11-09 19:49:41 +09:00
assert.include(
err.message,
2021-01-02 15:41:12 -08:00
'Input address must be a string or array of strings.'
2020-11-09 19:49:41 +09:00
)
}
})
2021-01-02 15:41:12 -08:00
it('should POST transaction history for a single address', async () => {
2020-11-09 19:49:41 +09:00
// Stub the network call.
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'post').resolves({ data: mockData.transactionsPost })
2020-11-09 19:49:41 +09:00
2021-01-02 15:41:12 -08:00
const addr = 'bitcoincash:qqh793x9au6ehvh7r2zflzguanlme760wuzehgzjh9'
2020-11-09 19:49:41 +09:00
const result = await bchjs.Ninsight.transactions(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.isArray(result)
2021-01-02 15:41:12 -08:00
assert.property(result[0], 'cashAddress')
assert.property(result[0], 'legacyAddress')
assert.property(result[0], 'txs')
2020-11-09 19:49:41 +09:00
assert.isArray(result[0].txs)
2021-01-02 15:41:12 -08:00
assert.property(result[0].txs[0], 'txid')
assert.property(result[0].txs[0], 'vin')
assert.property(result[0].txs[0], 'vout')
2020-11-09 19:49:41 +09:00
})
2021-01-02 15:41:12 -08:00
it('should POST transaction history for an array of addresses', async () => {
2020-11-09 19:49:41 +09:00
// Mock the network call.
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'post').resolves({ data: mockData.transactionsPost })
2020-11-09 19:49:41 +09:00
const addr = [
2021-01-02 15:41:12 -08:00
'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7',
'bitcoincash:qz0us0z6ucpqt07jgpad0shgh7xmwxyr3ynlcsq0wr'
2020-11-09 19:49:41 +09:00
]
const result = await bchjs.Ninsight.transactions(addr)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.isArray(result)
2021-01-02 15:41:12 -08:00
assert.property(result[0], 'cashAddress')
assert.property(result[0], 'legacyAddress')
assert.property(result[0], 'txs')
2020-11-09 19:49:41 +09:00
assert.isArray(result[0].txs)
2021-01-02 15:41:12 -08:00
assert.property(result[0].txs[0], 'txid')
2020-11-09 19:49:41 +09:00
})
})
2021-01-02 15:41:12 -08:00
describe('#txDetails', () => {
it('should throw an error for improper input', async () => {
2020-11-10 19:04:16 +09:00
try {
const txid = 12345
await bchjs.Ninsight.txDetails(txid)
2021-01-02 15:41:12 -08:00
assert.equal(true, false, 'Unexpected result!')
2020-11-10 19:04:16 +09:00
} catch (err) {
2021-01-02 15:41:12 -08:00
// console.log(`err: `, err)
2020-11-10 19:04:16 +09:00
assert.include(
err.message,
2021-01-02 15:41:12 -08:00
'Transaction ID must be a string or array of strings.'
2020-11-10 19:04:16 +09:00
)
}
})
2021-01-02 15:41:12 -08:00
it('should POST transaction details for a single TxID', async () => {
2020-11-10 19:04:16 +09:00
// Stub the network call.
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'post').resolves({ data: mockData.detailsPost })
2020-11-10 19:04:16 +09:00
2021-01-02 15:41:12 -08:00
const txid = 'fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33'
2020-11-10 19:04:16 +09:00
const result = await bchjs.Ninsight.txDetails(txid)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.isArray(result)
2021-01-02 15:41:12 -08:00
assert.property(result[0], 'txid')
assert.property(result[0], 'version')
assert.property(result[0], 'locktime')
assert.property(result[0], 'vin')
assert.property(result[0], 'vout')
assert.property(result[0], 'blockhash')
assert.property(result[0], 'blockheight')
assert.property(result[0], 'confirmations')
assert.property(result[0], 'time')
assert.property(result[0], 'blocktime')
assert.property(result[0], 'isCoinBase')
assert.property(result[0], 'valueOut')
assert.property(result[0], 'size')
2020-11-10 19:04:16 +09:00
})
2021-01-02 15:41:12 -08:00
it('should POST transaction details for an array of TxIDs', async () => {
2020-11-10 19:04:16 +09:00
// Stub the network call.
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'post').resolves({ data: mockData.detailsPost })
2020-11-10 19:04:16 +09:00
const txid = [
2021-01-02 15:41:12 -08:00
'fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33',
'fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33'
2020-11-10 19:04:16 +09:00
]
const result = await bchjs.Ninsight.txDetails(txid)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.isArray(result)
2021-01-02 15:41:12 -08:00
assert.property(result[0], 'txid')
assert.property(result[0], 'vin')
assert.property(result[0], 'vout')
2020-11-10 19:04:16 +09:00
})
})
2020-07-27 21:32:51 -07:00
})