'use strict';

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

var multiaddr = require('multiaddr');
var PeerId = require('peer-id');
var delay = require('delay');
var env_js = require('ipfs-utils/src/env.js');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');
var ipfsOptionsWebsocketsFilterAll = require('../utils/ipfs-options-websockets-filter-all.js');

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

var PeerId__default = /*#__PURE__*/_interopDefaultLegacy(PeerId);
var delay__default = /*#__PURE__*/_interopDefaultLegacy(delay);

function testPeers(factory, options) {
  const ipfsOptions = ipfsOptionsWebsocketsFilterAll.ipfsOptionsWebsocketsFilterAll();
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.swarm.peers', function () {
    this.timeout(80 * 1000);
    let ipfsA;
    let ipfsB;
    let ipfsBId;
    before(async () => {
      ipfsA = (await factory.spawn({
        type: 'proc',
        ipfsOptions
      })).api;
      ipfsB = (await factory.spawn({ type: env_js.isWebWorker ? 'go' : undefined })).api;
      ipfsBId = await ipfsB.id();
      await ipfsA.swarm.connect(ipfsBId.addresses[0]);
    });
    after(() => factory.clean());
    it('should list peers this node is connected to', async () => {
      const peers = await ipfsA.swarm.peers();
      chai_js.expect(peers).to.have.length.above(0);
      const peer = peers[0];
      chai_js.expect(peer).to.have.a.property('addr');
      chai_js.expect(multiaddr.Multiaddr.isMultiaddr(peer.addr)).to.equal(true);
      chai_js.expect(peer).to.have.a.property('peer').that.is.a('string');
      chai_js.expect(PeerId__default["default"].parse(peer.peer)).to.be.ok();
      chai_js.expect(peer).to.not.have.a.property('latency');
    });
    it('should list peers this node is connected to with verbose option', async () => {
      const peers = await ipfsA.swarm.peers({ verbose: true });
      chai_js.expect(peers).to.have.length.above(0);
      const peer = peers[0];
      chai_js.expect(peer).to.have.a.property('addr');
      chai_js.expect(multiaddr.Multiaddr.isMultiaddr(peer.addr)).to.equal(true);
      chai_js.expect(peer).to.have.a.property('peer');
      chai_js.expect(peer).to.have.a.property('latency');
      chai_js.expect(peer.latency).to.match(/n\/a|[0-9]+[mµ]?s/);
    });
    function getConfig(addrs) {
      addrs = Array.isArray(addrs) ? addrs : [addrs];
      return {
        Addresses: {
          Swarm: addrs,
          API: '/ip4/127.0.0.1/tcp/0',
          Gateway: '/ip4/127.0.0.1/tcp/0'
        },
        Bootstrap: [],
        Discovery: { MDNS: { Enabled: false } }
      };
    }
    it('should list peers only once', async () => {
      const nodeA = (await factory.spawn({
        type: 'proc',
        ipfsOptions
      })).api;
      const nodeB = (await factory.spawn({ type: env_js.isWebWorker ? 'go' : undefined })).api;
      const nodeBId = await nodeB.id();
      await nodeA.swarm.connect(nodeBId.addresses[0]);
      await delay__default["default"](1000);
      const peersA = await nodeA.swarm.peers();
      const peersB = await nodeB.swarm.peers();
      chai_js.expect(peersA).to.have.length(1);
      chai_js.expect(peersB).to.have.length(1);
    });
    it('should list peers only once even if they have multiple addresses', async () => {
      const config = getConfig(env_js.isBrowser && factory.opts.type !== 'go' ? [
        `${ process.env.SIGNALA_SERVER }`,
        `${ process.env.SIGNALB_SERVER }`
      ] : [
        '/ip4/127.0.0.1/tcp/26545/ws',
        '/ip4/127.0.0.1/tcp/26546/ws'
      ]);
      const nodeA = (await factory.spawn({
        type: env_js.isBrowser && factory.opts.type === 'go' || env_js.isWebWorker ? 'js' : 'proc',
        ipfsOptions
      })).api;
      const nodeAId = await nodeA.id();
      const nodeB = (await factory.spawn({
        type: env_js.isWebWorker ? 'go' : undefined,
        ipfsOptions: { config }
      })).api;
      await nodeB.swarm.connect(nodeAId.addresses[0]);
      await delay__default["default"](1000);
      const peersA = await nodeA.swarm.peers();
      const peersB = await nodeB.swarm.peers();
      chai_js.expect(peersA).to.have.length(1);
      chai_js.expect(peersB).to.have.length(1);
    });
  });
}

exports.testPeers = testPeers;
