'use strict';

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

var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');
var drain = require('it-drain');
var testTimeout = require('../utils/test-timeout.js');
var utils = require('./utils.js');

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

var drain__default = /*#__PURE__*/_interopDefaultLegacy(drain);

function testQuery(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.dht.query', function () {
    this.timeout(80 * 1000);
    let nodeA;
    let nodeB;
    before(async () => {
      nodeA = (await factory.spawn()).api;
      nodeB = (await factory.spawn()).api;
      await utils.ensureReachable(nodeA, nodeB);
    });
    after(() => factory.clean());
    it('should respect timeout option when querying the DHT', async () => {
      const nodeBId = await nodeB.id();
      return testTimeout(() => drain__default["default"](nodeA.dht.query(nodeBId.id, { timeout: 1 })));
    });
    it('should return the other node in the query', async function () {
      const peers = [];
      const nodeBId = await nodeB.id();
      for await (const event of nodeA.dht.query(nodeBId.id)) {
        if (event.name === 'PEER_RESPONSE') {
          peers.push(...event.closer.map(data => data.id));
        }
      }
      chai_js.expect(peers).to.include(nodeBId.id);
    });
  });
}

exports.testQuery = testQuery;
