'use strict';

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

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

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

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

function testRm(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.pin.rm', function () {
    this.timeout(50 * 1000);
    let ipfs;
    beforeEach(async () => {
      ipfs = (await factory.spawn()).api;
      const dir = utils.fixtures.directory.files.map(file => ({
        path: file.path,
        content: file.data
      }));
      await all__default["default"](ipfs.addAll(dir, {
        pin: false,
        cidVersion: 0
      }));
      await ipfs.add(utils.fixtures.files[0].data, { pin: false });
      await ipfs.add(utils.fixtures.files[1].data, { pin: false });
    });
    after(() => factory.clean());
    beforeEach(() => {
      return utils.clearPins(ipfs);
    });
    it('should remove a recursive pin', async () => {
      await ipfs.pin.add(utils.fixtures.directory.cid);
      const unpinnedCid = await ipfs.pin.rm(utils.fixtures.directory.cid, { recursive: true });
      chai_js.expect(unpinnedCid).to.deep.equal(utils.fixtures.directory.cid);
      const pinset = await all__default["default"](ipfs.pin.ls({ type: 'recursive' }));
      chai_js.expect(pinset).to.not.deep.include({
        cid: utils.fixtures.directory.cid,
        type: 'recursive'
      });
    });
    it('should remove a direct pin', async () => {
      await ipfs.pin.add(utils.fixtures.directory.cid, { recursive: false });
      const unpinnedCid = await ipfs.pin.rm(utils.fixtures.directory.cid, { recursive: false });
      chai_js.expect(unpinnedCid).to.deep.equal(utils.fixtures.directory.cid);
      const pinset = await all__default["default"](ipfs.pin.ls({ type: 'direct' }));
      chai_js.expect(pinset.map(p => p.cid)).to.not.deep.include(utils.fixtures.directory.cid);
    });
    it('should fail to remove an indirect pin', async () => {
      await ipfs.pin.add(utils.fixtures.directory.cid, { recursive: true });
      await chai_js.expect(ipfs.pin.rm(utils.fixtures.directory.files[0].cid)).to.eventually.be.rejectedWith(/pinned indirectly/);
      await utils.expectPinned(ipfs, utils.fixtures.directory.files[0].cid);
    });
    it('should fail when an item is not pinned', async () => {
      await chai_js.expect(ipfs.pin.rm(utils.fixtures.directory.cid)).to.eventually.be.rejectedWith(/not pinned/);
    });
  });
}

exports.testRm = testRm;
