|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const chai = require('chai') |
| 4 | +const dirtyChai = require('dirty-chai') |
| 5 | +const expect = chai.expect |
| 6 | +const statsTests = require('./utils/stats') |
| 7 | +chai.use(dirtyChai) |
| 8 | +const CID = require('cids') |
| 9 | + |
| 10 | +module.exports = (common) => { |
| 11 | + describe('.bitswap online', () => { |
| 12 | + let ipfs |
| 13 | + const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR' |
| 14 | + |
| 15 | + before(function (done) { |
| 16 | + // CI takes longer to instantiate the daemon, so we need to increase the |
| 17 | + // timeout for the before step |
| 18 | + this.timeout(60 * 1000) |
| 19 | + |
| 20 | + common.setup((err, factory) => { |
| 21 | + expect(err).to.not.exist() |
| 22 | + factory.spawnNode((err, node) => { |
| 23 | + expect(err).to.not.exist() |
| 24 | + ipfs = node |
| 25 | + ipfs.block.get(new CID(key)) |
| 26 | + .then(() => {}) |
| 27 | + .catch(() => {}) |
| 28 | + setTimeout(done, 250) |
| 29 | + }) |
| 30 | + }) |
| 31 | + }) |
| 32 | + |
| 33 | + after((done) => common.teardown(done)) |
| 34 | + |
| 35 | + it('.stat', (done) => { |
| 36 | + |
| 37 | + ipfs.bitswap.stat((err, stats) => { |
| 38 | + statsTests.expectIsBitswap(err, stats) |
| 39 | + done() |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + it('.wantlist', (done) => { |
| 44 | + ipfs.bitswap.wantlist((err, list) => { |
| 45 | + expect(err).to.not.exist() |
| 46 | + expect(list[0].cid.toBaseEncodedString()).to.equal(key) |
| 47 | + done() |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + it('.unwant', (done) => { |
| 52 | + ipfs.bitswap.unwant(new CID(key), (err) => { |
| 53 | + ipfs.bitswap.wantlist((err, list) => { |
| 54 | + expect(list).to.be.empty() |
| 55 | + done() |
| 56 | + }) |
| 57 | + }) |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + describe('.bitswap offline', () => { |
| 62 | + let ipfs |
| 63 | + |
| 64 | + before(function (done) { |
| 65 | + // CI takes longer to instantiate the daemon, so we need to increase the |
| 66 | + // timeout for the before step |
| 67 | + this.timeout(60 * 1000) |
| 68 | + |
| 69 | + common.setup((err, factory) => { |
| 70 | + expect(err).to.not.exist() |
| 71 | + factory.spawnNode((err, node) => { |
| 72 | + expect(err).to.not.exist() |
| 73 | + ipfs = node |
| 74 | + ipfs.id((err, id) => { |
| 75 | + expect(err).to.not.exist() |
| 76 | + ipfs.stop((err) => { |
| 77 | + // TODO: go-ipfs returns an error, https://github.com/ipfs/go-ipfs/issues/4078 |
| 78 | + if (!id.agentVersion.startsWith('go-ipfs')) { |
| 79 | + expect(err).to.not.exist() |
| 80 | + } |
| 81 | + done() |
| 82 | + }) |
| 83 | + }) |
| 84 | + }) |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + it('.stat gives error while offline', () => { |
| 89 | + ipfs.bitswap.stat((err, stats) => { |
| 90 | + expect(err).to.exist() |
| 91 | + expect(stats).to.not.exist() |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + it('.wantlist gives error if offline', () => { |
| 96 | + ipfs.bitswap.wantlist((err, list) => { |
| 97 | + expect(err).to.exist() |
| 98 | + expect(list).to.not.exist() |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + it('.unwant gives error if offline', () => { |
| 103 | + expect(() => ipfs.bitswap.unwant('my key'), (err) => { |
| 104 | + expect(err).to.exist() |
| 105 | + }) |
| 106 | + }) |
| 107 | + }) |
| 108 | +} |
0 commit comments