'use strict';

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

var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');
var utils = require('./utils.js');
var all = require('it-all');
var env_js = require('ipfs-utils/src/env.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 all__default = /*#__PURE__*/_interopDefaultLegacy(all);

function testPing(factory, options) {
  const ipfsOptions = ipfsOptionsWebsocketsFilterAll.ipfsOptionsWebsocketsFilterAll();
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.ping', function () {
    this.timeout(60 * 1000);
    let ipfsA;
    let ipfsB;
    let nodeBId;
    before(async () => {
      ipfsA = (await factory.spawn({
        type: 'proc',
        ipfsOptions
      })).api;
      ipfsB = (await factory.spawn({ type: env_js.isWebWorker ? 'go' : undefined })).api;
      nodeBId = await ipfsB.id();
      await ipfsA.swarm.connect(nodeBId.addresses[0]);
    });
    after(() => factory.clean());
    it('should send the specified number of packets', async () => {
      const count = 3;
      const responses = await all__default["default"](ipfsA.ping(nodeBId.id, { count }));
      responses.forEach(utils.expectIsPingResponse);
      const pongs = responses.filter(utils.isPong);
      chai_js.expect(pongs.length).to.equal(count);
    });
    it('should fail when pinging a peer that is not available', () => {
      const notAvailablePeerId = 'QmUmaEnH1uMmvckMZbh3yShaasvELPW4ZLPWnB4entMTEn';
      const count = 2;
      return chai_js.expect(all__default["default"](ipfsA.ping(notAvailablePeerId, { count }))).to.eventually.be.rejected();
    });
    it('should fail when pinging an invalid peer Id', () => {
      const invalidPeerId = 'not a peer ID';
      const count = 2;
      return chai_js.expect(all__default["default"](ipfsA.ping(invalidPeerId, { count }))).to.eventually.be.rejected();
    });
    it('can ping without options', async () => {
      const res = await all__default["default"](ipfsA.ping(nodeBId.id));
      chai_js.expect(res.length).to.be.ok();
      chai_js.expect(res[0].success).to.be.true();
    });
  });
}

exports.testPing = testPing;
