|
| 1 | +/* eslint max-nested-callbacks: ["error", 5] */ |
| 2 | +/* eslint-env mocha */ |
| 3 | +'use strict' |
| 4 | + |
| 5 | +const series = require('async/series') |
| 6 | +const loadFixture = require('aegir/fixtures') |
| 7 | + |
| 8 | +const { spawnNodeWithId } = require('../utils/spawn') |
| 9 | +const { getDescribe, getIt, expect } = require('../utils/mocha') |
| 10 | + |
| 11 | +const fixture = Object.freeze({ |
| 12 | + data: loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core') |
| 13 | +}) |
| 14 | + |
| 15 | +module.exports = (createCommon, options) => { |
| 16 | + const describe = getDescribe(options) |
| 17 | + const it = getIt(options) |
| 18 | + const common = createCommon() |
| 19 | + |
| 20 | + describe('.name.pubsub.cancel', function () { |
| 21 | + let ipfs |
| 22 | + let nodeId |
| 23 | + let value |
| 24 | + |
| 25 | + before(function (done) { |
| 26 | + // CI takes longer to instantiate the daemon, so we need to increase the |
| 27 | + // timeout for the before step |
| 28 | + this.timeout(60 * 1000) |
| 29 | + |
| 30 | + common.setup((err, factory) => { |
| 31 | + expect(err).to.not.exist() |
| 32 | + |
| 33 | + spawnNodeWithId(factory, (err, node) => { |
| 34 | + expect(err).to.not.exist() |
| 35 | + |
| 36 | + ipfs = node |
| 37 | + nodeId = node.peerId.id |
| 38 | + |
| 39 | + ipfs.files.add(fixture.data, { pin: false }, (err, res) => { |
| 40 | + expect(err).to.not.exist() |
| 41 | + |
| 42 | + value = res[0].path |
| 43 | + done() |
| 44 | + }) |
| 45 | + }) |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + after((done) => common.teardown(done)) |
| 50 | + |
| 51 | + it('should return false when the name that is intended to cancel is not subscribed', function (done) { |
| 52 | + this.timeout(60 * 1000) |
| 53 | + |
| 54 | + ipfs.name.pubsub.cancel(nodeId, (err, res) => { |
| 55 | + expect(err).to.not.exist() |
| 56 | + expect(res).to.exist() |
| 57 | + expect(res).to.have.property('canceled') |
| 58 | + expect(res.canceled).to.eql(false) |
| 59 | + |
| 60 | + done() |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should cancel a subscription correctly returning true', function (done) { |
| 65 | + this.timeout(300 * 1000) |
| 66 | + const ipnsPath = `/ipns/${nodeId}` |
| 67 | + |
| 68 | + series([ |
| 69 | + (cb) => ipfs.name.pubsub.subs(cb), |
| 70 | + (cb) => ipfs.name.publish(value, { resolve: false }, cb), |
| 71 | + (cb) => ipfs.name.resolve(nodeId, cb), |
| 72 | + (cb) => ipfs.name.pubsub.subs(cb), |
| 73 | + (cb) => ipfs.name.pubsub.cancel(ipnsPath, cb), |
| 74 | + (cb) => ipfs.name.pubsub.subs(cb) |
| 75 | + ], (err, res) => { |
| 76 | + expect(err).to.not.exist() |
| 77 | + expect(res).to.exist() |
| 78 | + expect(res[0]).to.eql([]) // initally empty |
| 79 | + expect(res[4]).to.have.property('canceled') |
| 80 | + expect(res[4].canceled).to.eql(true) |
| 81 | + expect(res[5]).to.be.an('array').that.does.not.include(ipnsPath) |
| 82 | + |
| 83 | + done() |
| 84 | + }) |
| 85 | + }) |
| 86 | + }) |
| 87 | +} |
0 commit comments