'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');
var cid = require('multiformats/cid');

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

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

function testLs(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.ls', 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.clearRemotePins(ipfs);
    });
    it('requires service option', async () => {
      const result = ipfs.pin.remote.ls({});
      await chai_js.expect(all__default["default"](result)).to.eventually.be.rejectedWith(/service name must be passed/);
    });
    it('list no pins', async () => {
      const result = ipfs.pin.remote.ls({ service: SERVICE });
      const pins = await all__default["default"](result);
      chai_js.expect(pins).to.deep.equal([]);
    });
    describe('list pins by status', () => {
      it('list only pinned pins by default', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          one: cid1,
          'pinned-two': cid2,
          'pinning-three': cid3,
          'failed-four': cid4
        });
        const list = await all__default["default"](ipfs.pin.remote.ls({ service: SERVICE }));
        chai_js.expect(list).to.deep.equal([{
            status: 'pinned',
            cid: cid2,
            name: 'pinned-two'
          }]);
      });
      it('should list "queued" pins', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          one: cid1,
          'pinned-two': cid2,
          'pinning-three': cid3,
          'failed-four': cid4
        });
        const list = await all__default["default"](ipfs.pin.remote.ls({
          status: ['queued'],
          service: SERVICE
        }));
        chai_js.expect(list).to.deep.equal([{
            status: 'queued',
            cid: cid1,
            name: 'one'
          }]);
      });
      it('should list "pinning" pins', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          one: cid1,
          'pinned-two': cid2,
          'pinning-three': cid3,
          'failed-four': cid4
        });
        const list = await all__default["default"](ipfs.pin.remote.ls({
          status: ['pinning'],
          service: SERVICE
        }));
        chai_js.expect(list).to.deep.equal([{
            status: 'pinning',
            cid: cid3,
            name: 'pinning-three'
          }]);
      });
      it('should list "failed" pins', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          one: cid1,
          'pinned-two': cid2,
          'pinning-three': cid3,
          'failed-four': cid4
        });
        const list = await all__default["default"](ipfs.pin.remote.ls({
          status: ['failed'],
          service: SERVICE
        }));
        chai_js.expect(list).to.deep.equal([{
            status: 'failed',
            cid: cid4,
            name: 'failed-four'
          }]);
      });
      it('should list queued+pinned pins', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          one: cid1,
          'pinned-two': cid2,
          'pinning-three': cid3,
          'failed-four': cid4
        });
        const list = await all__default["default"](ipfs.pin.remote.ls({
          status: [
            'queued',
            'pinned'
          ],
          service: SERVICE
        }));
        chai_js.expect(list.sort(byCID)).to.deep.equal([
          {
            status: 'queued',
            cid: cid1,
            name: 'one'
          },
          {
            status: 'pinned',
            cid: cid2,
            name: 'pinned-two'
          }
        ].sort(byCID));
      });
      it('should list queued+pinned+pinning pins', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          one: cid1,
          'pinned-two': cid2,
          'pinning-three': cid3,
          'failed-four': cid4
        });
        const list = await all__default["default"](ipfs.pin.remote.ls({
          status: [
            'queued',
            'pinned',
            'pinning'
          ],
          service: SERVICE
        }));
        chai_js.expect(list.sort(byCID)).to.deep.equal([
          {
            status: 'queued',
            cid: cid1,
            name: 'one'
          },
          {
            status: 'pinned',
            cid: cid2,
            name: 'pinned-two'
          },
          {
            status: 'pinning',
            cid: cid3,
            name: 'pinning-three'
          }
        ].sort(byCID));
      });
      it('should list queued+pinned+pinning+failed pins', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          one: cid1,
          'pinned-two': cid2,
          'pinning-three': cid3,
          'failed-four': cid4
        });
        const list = await all__default["default"](ipfs.pin.remote.ls({
          status: [
            'queued',
            'pinned',
            'pinning',
            'failed'
          ],
          service: SERVICE
        }));
        chai_js.expect(list.sort(byCID)).to.deep.equal([
          {
            status: 'queued',
            cid: cid1,
            name: 'one'
          },
          {
            status: 'pinned',
            cid: cid2,
            name: 'pinned-two'
          },
          {
            status: 'pinning',
            cid: cid3,
            name: 'pinning-three'
          },
          {
            status: 'failed',
            cid: cid4,
            name: 'failed-four'
          }
        ].sort(byCID));
      });
    });
    describe('list pins by name', () => {
      it('should list no pins when names do not match', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          a: cid1,
          b: cid2,
          c: cid3
        });
        const list = await all__default["default"](ipfs.pin.remote.ls({
          name: 'd',
          status: [
            'queued',
            'pinning',
            'pinned',
            'failed'
          ],
          service: SERVICE
        }));
        chai_js.expect(list).to.deep.equal([]);
      });
      it('should list only pins with matchin names', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          a: cid1,
          b: cid2
        });
        await utils.addRemotePins(ipfs, SERVICE, {
          a: cid3,
          b: cid4
        });
        const list = await all__default["default"](ipfs.pin.remote.ls({
          name: 'a',
          status: [
            'queued',
            'pinning',
            'pinned',
            'failed'
          ],
          service: SERVICE
        }));
        chai_js.expect(list.sort(byCID)).to.deep.equal([
          {
            status: 'queued',
            name: 'a',
            cid: cid1
          },
          {
            status: 'queued',
            name: 'a',
            cid: cid3
          }
        ].sort(byCID));
      });
      it('should list only pins with matchin names & status', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          a: cid1,
          b: cid2
        });
        await utils.addRemotePins(ipfs, SERVICE, {
          a: cid3,
          b: cid4
        });
        await utils.addRemotePins(ipfs, SERVICE, { 'pinned-a': cid3 });
        const list = await all__default["default"](ipfs.pin.remote.ls({
          name: 'a',
          status: ['pinned'],
          service: SERVICE
        }));
        chai_js.expect(list).to.deep.equal([{
            status: 'pinned',
            name: 'a',
            cid: cid3
          }]);
      });
    });
    describe('list pins by cid', () => {
      it('should list pins with matching cid', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          a: cid1,
          b: cid2,
          c: cid3,
          d: cid4
        });
        const list = await all__default["default"](ipfs.pin.remote.ls({
          cid: [cid1],
          status: [
            'queued',
            'pinned',
            'pinning',
            'failed'
          ],
          service: SERVICE
        }));
        chai_js.expect(list).to.deep.equal([{
            status: 'queued',
            cid: cid1,
            name: 'a'
          }]);
      });
      it('should list pins with any matching cid', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          a: cid1,
          b: cid2,
          c: cid3,
          d: cid4
        });
        const list = await all__default["default"](ipfs.pin.remote.ls({
          cid: [
            cid1,
            cid3
          ],
          status: [
            'queued',
            'pinned',
            'pinning',
            'failed'
          ],
          service: SERVICE
        }));
        chai_js.expect(list.sort(byCID)).to.deep.equal([
          {
            status: 'queued',
            cid: cid1,
            name: 'a'
          },
          {
            status: 'queued',
            cid: cid3,
            name: 'c'
          }
        ].sort(byCID));
      });
      it('should list pins with matching cid+status', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          'pinned-a': cid1,
          'failed-b': cid2,
          'pinned-c': cid3,
          d: cid4
        });
        const list = await all__default["default"](ipfs.pin.remote.ls({
          cid: [
            cid1,
            cid2
          ],
          status: [
            'pinned',
            'failed'
          ],
          service: SERVICE
        }));
        chai_js.expect(list.sort(byCID)).to.deep.equal([
          {
            status: 'pinned',
            cid: cid1,
            name: 'pinned-a'
          },
          {
            status: 'failed',
            cid: cid2,
            name: 'failed-b'
          }
        ].sort(byCID));
      });
      it('should list pins with matching cid+status+name', async () => {
        await utils.addRemotePins(ipfs, SERVICE, {
          'pinned-a': cid1,
          'failed-b': cid2,
          'pinned-c': cid3,
          d: cid4
        });
        const list = await all__default["default"](ipfs.pin.remote.ls({
          cid: [
            cid4,
            cid1,
            cid2
          ],
          name: 'd',
          status: [
            'queued',
            'pinned'
          ],
          service: SERVICE
        }));
        chai_js.expect(list).to.deep.equal([{
            status: 'queued',
            cid: cid4,
            name: 'd'
          }]);
      });
    });
  });
}
const byCID = (a, b) => a.cid.toString() > b.cid.toString() ? 1 : -1;

exports.testLs = testLs;
