'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 cid = require('multiformats/cid');
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);
  const ENDPOINT = new URL(process.env.PINNING_SERVICE_ENDPOINT || '');
  const KEY = `${ process.env.PINNING_SERVICE_KEY }`;
  const SERVICE = 'pinbot';
  const cid1 = cid.CID.parse('QmbKtKBrmeRHjNCwR4zAfCJdMVu6dgmwk9M9AE9pUM9RgG');
  const cid2 = cid.CID.parse('QmdFyxZXsFiP4csgfM5uPu99AvFiKH62CSPDw5TP92nr7w');
  const cid3 = cid.CID.parse('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP');
  const cid4 = cid.CID.parse('QmY9cxiHqTFoWamkQVkpmmqzBrY3hCBEL2XNu3NtX74Fuu');
  describe('.pin.remote.rm', function () {
    this.timeout(50 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
      await ipfs.pin.remote.service.add(SERVICE, {
        endpoint: ENDPOINT,
        key: KEY
      });
    });
    after(async () => {
      await utils.clearServices(ipfs);
      await factory.clean();
    });
    beforeEach(async () => {
      await utils.addRemotePins(ipfs, SERVICE, {
        'queued-a': cid1,
        'pinning-b': cid2,
        'pinned-c': cid3,
        'failed-d': cid4
      });
    });
    afterEach(async () => {
      await utils.clearRemotePins(ipfs);
    });
    it('.rm requires service option', async () => {
      const result = ipfs.pin.remote.rm({});
      await chai_js.expect(result).to.eventually.be.rejectedWith(/service name must be passed/);
    });
    it('.rmAll requires service option', async () => {
      const result = ipfs.pin.remote.rmAll({});
      await chai_js.expect(result).to.eventually.be.rejectedWith(/service name must be passed/);
    });
    it('noop if there is no match', async () => {
      await ipfs.pin.remote.rm({
        cid: [cid1],
        status: [
          'pinned',
          'failed'
        ],
        service: SERVICE
      });
      const list = await all__default["default"](ipfs.pin.remote.ls({
        status: [
          'queued',
          'pinning',
          'pinned',
          'failed'
        ],
        service: SERVICE
      }));
      chai_js.expect(list.sort(byCID)).to.deep.equal([
        {
          cid: cid1,
          status: 'queued',
          name: 'queued-a'
        },
        {
          cid: cid2,
          status: 'pinning',
          name: 'pinning-b'
        },
        {
          cid: cid3,
          status: 'pinned',
          name: 'pinned-c'
        },
        {
          cid: cid4,
          status: 'failed',
          name: 'failed-d'
        }
      ].sort(byCID));
    });
    it('removes matching pin', async () => {
      await ipfs.pin.remote.rm({
        cid: [cid1],
        status: [
          'queued',
          'pinning',
          'pinned',
          'failed'
        ],
        service: SERVICE
      });
      const list = await all__default["default"](ipfs.pin.remote.ls({
        status: [
          'queued',
          'pinning',
          'pinned',
          'failed'
        ],
        service: SERVICE
      }));
      chai_js.expect(list.sort(byCID)).to.deep.equal([
        {
          cid: cid2,
          status: 'pinning',
          name: 'pinning-b'
        },
        {
          cid: cid3,
          status: 'pinned',
          name: 'pinned-c'
        },
        {
          cid: cid4,
          status: 'failed',
          name: 'failed-d'
        }
      ].sort(byCID));
    });
    it('fails on multiple matches', async () => {
      const result = ipfs.pin.remote.rm({
        cid: [
          cid1,
          cid2
        ],
        status: [
          'queued',
          'pinning',
          'pinned',
          'failed'
        ],
        service: SERVICE
      });
      await chai_js.expect(result).to.eventually.be.rejectedWith(/multiple remote pins are matching this query/);
      const list = await all__default["default"](ipfs.pin.remote.ls({
        status: [
          'queued',
          'pinning',
          'pinned',
          'failed'
        ],
        service: SERVICE
      }));
      chai_js.expect(list.sort(byCID)).to.deep.equal([
        {
          cid: cid1,
          status: 'queued',
          name: 'queued-a'
        },
        {
          cid: cid2,
          status: 'pinning',
          name: 'pinning-b'
        },
        {
          cid: cid3,
          status: 'pinned',
          name: 'pinned-c'
        },
        {
          cid: cid4,
          status: 'failed',
          name: 'failed-d'
        }
      ].sort(byCID));
    });
  });
}
const byCID = (a, b) => a.cid.toString() > b.cid.toString() ? 1 : -1;

exports.testRm = testRm;
