'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var PeerId = require('peer-id');
var all = require('it-all');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var PeerId__default = /*#__PURE__*/_interopDefaultLegacy(PeerId);
var all__default = /*#__PURE__*/_interopDefaultLegacy(all);

function testCancel(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.name.pubsub.cancel', () => {
    let ipfs;
    let nodeId;
    before(async () => {
      ipfs = (await factory.spawn()).api;
      const peerInfo = await ipfs.id();
      nodeId = peerInfo.id;
    });
    after(() => factory.clean());
    it('should return false when the name that is intended to cancel is not subscribed', async function () {
      this.timeout(60 * 1000);
      const res = await ipfs.name.pubsub.cancel(nodeId);
      chai_js.expect(res).to.exist();
      chai_js.expect(res).to.have.property('canceled');
      chai_js.expect(res.canceled).to.be.false();
    });
    it('should cancel a subscription correctly returning true', async function () {
      this.timeout(300 * 1000);
      const peerId = await PeerId__default["default"].create({ bits: 512 });
      const id = peerId.toB58String();
      const ipnsPath = `/ipns/${ id }`;
      const subs = await ipfs.name.pubsub.subs();
      chai_js.expect(subs).to.be.an('array').that.does.not.include(ipnsPath);
      await chai_js.expect(all__default["default"](ipfs.name.resolve(id))).to.eventually.be.rejected();
      const subs1 = await ipfs.name.pubsub.subs();
      const cancel = await ipfs.name.pubsub.cancel(ipnsPath);
      const subs2 = await ipfs.name.pubsub.subs();
      chai_js.expect(subs1).to.be.an('array').that.includes(ipnsPath);
      chai_js.expect(cancel).to.have.property('canceled');
      chai_js.expect(cancel.canceled).to.be.true();
      chai_js.expect(subs2).to.be.an('array').that.does.not.include(ipnsPath);
    });
  });
}

exports.testCancel = testCancel;
