'use strict';

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

var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');
var multiaddr = require('multiaddr');

const invalidArg = 'this/Is/So/Invalid/';
const validIp4 = new multiaddr.Multiaddr('/ip4/104.236.176.52/tcp/4001/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z');
function testAdd(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.bootstrap.add', function () {
    this.timeout(100 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should return an error when called with an invalid arg', () => {
      return chai_js.expect(ipfs.bootstrap.add(invalidArg)).to.eventually.be.rejected.and.be.an.instanceOf(Error);
    });
    it('should return a list containing the bootstrap peer when called with a valid arg (ip4)', async () => {
      const res = await ipfs.bootstrap.add(validIp4);
      chai_js.expect(res).to.be.eql({ Peers: [validIp4] });
      const peers = res.Peers;
      chai_js.expect(peers).to.have.property('length').that.is.equal(1);
    });
    it('should prevent duplicate inserts of bootstrap peers', async () => {
      await ipfs.bootstrap.clear();
      const added = await ipfs.bootstrap.add(validIp4);
      chai_js.expect(added).to.have.property('Peers').that.deep.equals([validIp4]);
      const addedAgain = await ipfs.bootstrap.add(validIp4);
      chai_js.expect(addedAgain).to.have.property('Peers').that.deep.equals([validIp4]);
      const list = await ipfs.bootstrap.list();
      chai_js.expect(list).to.have.property('Peers').that.deep.equals([validIp4]);
    });
    it('add a peer to the bootstrap list', async () => {
      const peer = new multiaddr.Multiaddr('/ip4/111.111.111.111/tcp/1001/p2p/QmXFX2P5ammdmXQgfqGkfswtEVFsZUJ5KeHRXQYCTdiTAb');
      const res = await ipfs.bootstrap.add(peer);
      chai_js.expect(res).to.be.eql({ Peers: [peer] });
      const list = await ipfs.bootstrap.list();
      chai_js.expect(list.Peers).to.deep.include(peer);
      chai_js.expect(list.Peers.every(ma => multiaddr.Multiaddr.isMultiaddr(ma))).to.be.true();
    });
  });
}

exports.testAdd = testAdd;
