'use strict';

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

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

function testDisconnect(factory, options) {
  const ipfsOptions = ipfsOptionsWebsocketsFilterAll.ipfsOptionsWebsocketsFilterAll();
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.swarm.disconnect', 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();
    });
    beforeEach(async () => {
      await ipfsA.swarm.connect(ipfsBId.addresses[0]);
    });
    after(() => factory.clean());
    it('should disconnect from a peer', async () => {
      let peers;
      peers = await ipfsA.swarm.peers();
      chai_js.expect(peers).to.have.length.above(0);
      await ipfsA.swarm.disconnect(ipfsBId.addresses[0]);
      peers = await ipfsA.swarm.peers();
      chai_js.expect(peers).to.have.length(0);
    });
  });
}

exports.testDisconnect = testDisconnect;
