'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');
var env_js = require('ipfs-utils/src/env.js');
var retry = require('p-retry');

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

var retry__default = /*#__PURE__*/_interopDefaultLegacy(retry);

function testId(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.id', function () {
    this.timeout(60 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should get the node ID', async () => {
      const res = await ipfs.id();
      chai_js.expect(res).to.have.a.property('id').that.is.a('string');
      chai_js.expect(res).to.have.a.property('publicKey');
      chai_js.expect(res).to.have.a.property('agentVersion').that.is.a('string');
      chai_js.expect(res).to.have.a.property('protocolVersion').that.is.a('string');
      chai_js.expect(res).to.have.a.property('addresses').that.is.an('array');
      for (const ma of res.addresses) {
        chai_js.expect(multiaddr.Multiaddr.isMultiaddr(ma)).to.be.true();
      }
    });
    it('should have protocols property', async () => {
      const res = await ipfs.id();
      chai_js.expect(res).to.have.a.property('protocols').that.is.an('array');
      chai_js.expect(res.protocols).to.include.members([
        '/floodsub/1.0.0',
        '/ipfs/bitswap/1.0.0',
        '/ipfs/bitswap/1.1.0',
        '/ipfs/bitswap/1.2.0',
        '/ipfs/id/1.0.0',
        '/ipfs/id/push/1.0.0',
        '/ipfs/lan/kad/1.0.0',
        '/ipfs/ping/1.0.0',
        '/libp2p/circuit/relay/0.1.0',
        '/meshsub/1.0.0',
        '/meshsub/1.1.0'
      ]);
    });
    it('should return swarm ports opened after startup', async function () {
      if (env_js.isWebWorker) {
        return this.skip();
      }
      await chai_js.expect(ipfs.id()).to.eventually.have.property('addresses').that.is.not.empty();
    });
    it('should get the id of another node in the swarm', async function () {
      if (env_js.isWebWorker) {
        return this.skip();
      }
      const ipfsB = (await factory.spawn()).api;
      const ipfsBId = await ipfsB.id();
      await ipfs.swarm.connect(ipfsBId.addresses[0]);
      await retry__default["default"](async () => {
        const result = await ipfs.id({ peerId: ipfsBId.id });
        chai_js.expect(result).to.deep.equal(ipfsBId);
      }, { retries: 5 });
    });
    it('should get our own id when passed as an option', async function () {
      const res = await ipfs.id();
      const result = await ipfs.id({ peerId: res.id });
      chai_js.expect(result).to.deep.equal(res);
    });
  });
}

exports.testId = testId;
