'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 testLs(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.pin.ls', function () {
    this.timeout(50 * 1000);
    let ipfs;
    before(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.pin.add(utils.fixtures.directory.cid, { recursive: true });
      await ipfs.add(utils.fixtures.files[0].data, {
        pin: false,
        cidVersion: 0
      });
      await ipfs.pin.add(utils.fixtures.files[0].cid, { recursive: true });
      await ipfs.add(utils.fixtures.files[1].data, {
        pin: false,
        cidVersion: 0
      });
      await ipfs.pin.add(utils.fixtures.files[1].cid, { recursive: false });
    });
    after(() => factory.clean());
    it('should list all recursive pins', async () => {
      const pinset = await all__default["default"](ipfs.pin.ls({ type: 'recursive' }));
      chai_js.expect(pinset).to.deep.include({
        type: 'recursive',
        cid: utils.fixtures.files[0].cid
      });
      chai_js.expect(pinset).to.deep.include({
        type: 'recursive',
        cid: utils.fixtures.directory.cid
      });
    });
    it('should list all indirect pins', async () => {
      const pinset = await all__default["default"](ipfs.pin.ls({ type: 'indirect' }));
      chai_js.expect(pinset).to.not.deep.include({
        type: 'recursive',
        cid: utils.fixtures.files[0].cid
      });
      chai_js.expect(pinset).to.not.deep.include({
        type: 'direct',
        cid: utils.fixtures.files[1].cid
      });
      chai_js.expect(pinset).to.not.deep.include({
        type: 'recursive',
        cid: utils.fixtures.directory.cid
      });
      chai_js.expect(pinset).to.deep.include({
        type: 'indirect',
        cid: utils.fixtures.directory.files[0].cid
      });
      chai_js.expect(pinset).to.deep.include({
        type: 'indirect',
        cid: utils.fixtures.directory.files[1].cid
      });
    });
    it('should list all types of pins', async () => {
      const pinset = await all__default["default"](ipfs.pin.ls());
      chai_js.expect(pinset).to.not.be.empty();
      chai_js.expect(pinset).to.deep.include({
        type: 'recursive',
        cid: utils.fixtures.directory.cid
      });
      chai_js.expect(pinset).to.deep.include({
        type: 'recursive',
        cid: utils.fixtures.files[0].cid
      });
      chai_js.expect(pinset).to.deep.include({
        type: 'direct',
        cid: utils.fixtures.files[1].cid
      });
      chai_js.expect(pinset).to.deep.include({
        type: 'indirect',
        cid: utils.fixtures.directory.files[0].cid
      });
      chai_js.expect(pinset).to.deep.include({
        type: 'indirect',
        cid: utils.fixtures.directory.files[1].cid
      });
    });
    it('should list all direct pins', async () => {
      const pinset = await all__default["default"](ipfs.pin.ls({ type: 'direct' }));
      chai_js.expect(pinset).to.have.lengthOf(1);
      chai_js.expect(pinset).to.deep.include({
        type: 'direct',
        cid: utils.fixtures.files[1].cid
      });
    });
    it('should list pins for a specific hash', async () => {
      const pinset = await all__default["default"](ipfs.pin.ls({ paths: utils.fixtures.files[0].cid }));
      chai_js.expect(pinset).to.have.lengthOf(1);
      chai_js.expect(pinset).to.have.deep.members([{
          type: 'recursive',
          cid: utils.fixtures.files[0].cid
        }]);
    });
    it('should throw an error on missing direct pins for existing path', () => {
      return chai_js.expect(all__default["default"](ipfs.pin.ls({
        paths: `/ipfs/${ utils.fixtures.directory.cid }/files/ipfs.txt`,
        type: 'direct'
      }))).to.eventually.be.rejected.and.be.an.instanceOf(Error).and.to.have.property('message', `path '/ipfs/${ utils.fixtures.directory.cid }/files/ipfs.txt' is not pinned`);
    });
    it('should throw an error on missing link for a specific path', () => {
      return chai_js.expect(all__default["default"](ipfs.pin.ls({
        paths: `/ipfs/${ utils.fixtures.directory.cid }/I-DONT-EXIST.txt`,
        type: 'direct'
      }))).to.eventually.be.rejected.and.be.an.instanceOf(Error).and.to.have.property('message', `no link named "I-DONT-EXIST.txt" under ${ utils.fixtures.directory.cid }`);
    });
    it('should list indirect pins for a specific path', async () => {
      const pinset = await all__default["default"](ipfs.pin.ls({
        paths: `/ipfs/${ utils.fixtures.directory.cid }/files/ipfs.txt`,
        type: 'indirect'
      }));
      chai_js.expect(pinset).to.have.lengthOf(1);
      chai_js.expect(pinset).to.deep.include({
        type: `indirect through ${ utils.fixtures.directory.cid }`,
        cid: utils.fixtures.directory.files[1].cid
      });
    });
    it('should list recursive pins for a specific hash', async () => {
      const pinset = await all__default["default"](ipfs.pin.ls({
        paths: utils.fixtures.files[0].cid,
        type: 'recursive'
      }));
      chai_js.expect(pinset).to.have.lengthOf(1);
      chai_js.expect(pinset).to.deep.include({
        type: 'recursive',
        cid: utils.fixtures.files[0].cid
      });
    });
    it('should list pins for multiple CIDs', async () => {
      const pinset = await all__default["default"](ipfs.pin.ls({
        paths: [
          utils.fixtures.files[0].cid,
          utils.fixtures.files[1].cid
        ]
      }));
      const cids = pinset.map(p => p.cid.toString());
      chai_js.expect(cids).to.include(utils.fixtures.files[0].cid.toString());
      chai_js.expect(cids).to.include(utils.fixtures.files[1].cid.toString());
    });
    it('should throw error for invalid non-string pin type option', () => {
      return chai_js.expect(all__default["default"](ipfs.pin.ls({ type: 6 }))).to.eventually.be.rejected();
    });
    it('should throw error for invalid string pin type option', () => {
      return chai_js.expect(all__default["default"](ipfs.pin.ls({ type: '__proto__' }))).to.eventually.be.rejected();
    });
    it('should list pins with metadata', async () => {
      const {cid} = await ipfs.add(`data-${ Math.random() }`, { pin: false });
      const metadata = {
        key: 'value',
        one: 2,
        array: [{ thing: 'subthing' }],
        obj: {
          foo: 'bar',
          baz: ['qux']
        }
      };
      await ipfs.pin.add(cid, {
        recursive: false,
        metadata
      });
      const pinset = await all__default["default"](ipfs.pin.ls({ paths: cid }));
      chai_js.expect(pinset).to.have.deep.members([{
          type: 'direct',
          cid: cid,
          metadata
        }]);
    });
  });
}

exports.testLs = testLs;
