diff --git a/src/bitswap/stat.js b/src/bitswap/stat.js index 02461247..569fc414 100644 --- a/src/bitswap/stat.js +++ b/src/bitswap/stat.js @@ -3,7 +3,7 @@ const waterfall = require('async/waterfall') const { getDescribe, getIt, expect } = require('../utils/mocha') -const { expectIsBitswap } = require('../stats/utils') +const { expectIsBitswap, expectIsBitswapHumanReadable } = require('../stats/utils') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -43,6 +43,12 @@ module.exports = (createCommon, options) => { }) }) + it('should get human readable bitswap stats', async () => { + const stats = await ipfs.bitswap.stat({ human: true }) + + expectIsBitswapHumanReadable(null, stats) + }) + it('should not get bitswap stats when offline', function (done) { this.timeout(60 * 1000) diff --git a/src/stats/bitswap.js b/src/stats/bitswap.js index b0e57f38..d3a4a262 100644 --- a/src/stats/bitswap.js +++ b/src/stats/bitswap.js @@ -2,7 +2,7 @@ 'use strict' const { getDescribe, getIt, expect } = require('../utils/mocha') -const { expectIsBitswap } = require('./utils') +const { expectIsBitswap, expectIsBitswapHumanReadable } = require('./utils') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -41,5 +41,11 @@ module.exports = (createCommon, options) => { expectIsBitswap(null, res) }) }) + + it('should get human readable bitswap stats', async () => { + const stats = await ipfs.stats.bitswap({ human: true }) + + expectIsBitswapHumanReadable(null, stats) + }) }) } diff --git a/src/stats/utils.js b/src/stats/utils.js index fd7a1d62..dbbbcb18 100644 --- a/src/stats/utils.js +++ b/src/stats/utils.js @@ -19,9 +19,9 @@ exports.expectIsBitswap = (err, stats) => { expect(stats).to.have.a.property('dupBlksReceived') expect(stats).to.have.a.property('dupDataReceived') - expect(stats.provideBufLen).to.a('number') expect(stats.wantlist).to.be.an('array') expect(stats.peers).to.be.an('array') + expect(isBigInt(stats.provideBufLen)).to.eql(true) expect(isBigInt(stats.blocksReceived)).to.eql(true) expect(isBigInt(stats.dataReceived)).to.eql(true) expect(isBigInt(stats.blocksSent)).to.eql(true) @@ -30,6 +30,34 @@ exports.expectIsBitswap = (err, stats) => { expect(isBigInt(stats.dupDataReceived)).to.eql(true) } +exports.expectIsBitswapHumanReadable = (err, stats) => { + expect(err).to.not.exist() + expect(stats).to.exist() + expect(stats).to.have.a.property('provideBufLen') + .and.to.be.a('number') + expect(stats).to.have.a.property('blocksReceived') + .and.to.be.a('number') + expect(stats).to.have.a.property('wantlist') + .and.to.be.a('string') + .and.to.match(/\[\d+\skeys\]$/gm) + expect(stats).to.have.a.property('peers') + .and.to.be.a('string') + .and.to.match(/\[\d+\]$/gm) + expect(stats).to.have.a.property('dupBlksReceived') + .and.to.be.a('number') + expect(stats).to.have.a.property('dupDataReceived') + .and.to.be.a('string') + .and.to.match(/[\d.]+\s[PTGMK]?B$/gm) + expect(stats).to.have.a.property('dataReceived') + .and.to.be.a('string') + .and.to.match(/[\d.]+\s[PTGMK]?B$/gm) + expect(stats).to.have.a.property('blocksSent') + .and.to.be.a('number') + expect(stats).to.have.a.property('dataSent') + .and.to.be.a('string') + .and.to.match(/[\d.]+\s[PTGMK]?B$/gm) +} + exports.expectIsBandwidth = (err, stats) => { expect(err).to.not.exist() expect(stats).to.exist()