'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 env_js = require('ipfs-utils/src/env.js');
var testTimeout = require('../utils/test-timeout.js');
var cid = require('multiformats/cid');
var delay = require('delay');
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 delay__default = /*#__PURE__*/_interopDefaultLegacy(delay);

function testWantlist(factory, options) {
  const ipfsOptions = ipfsOptionsWebsocketsFilterAll.ipfsOptionsWebsocketsFilterAll();
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.bitswap.wantlist', function () {
    this.timeout(60 * 1000);
    let ipfsA;
    let ipfsB;
    const key = 'QmUBdnXXPyoDFXj3Hj39dNJ5VkN3QFRskXxcGaYFBB8CNR';
    before(async () => {
      ipfsA = (await factory.spawn({
        type: 'proc',
        ipfsOptions
      })).api;
      ipfsB = (await factory.spawn({ type: env_js.isWebWorker ? 'go' : undefined })).api;
      ipfsB.block.get(cid.CID.parse(key)).catch(() => {
      });
      const ipfsBId = await ipfsB.id();
      await ipfsA.swarm.connect(ipfsBId.addresses[0]);
    });
    after(() => factory.clean());
    it('should respect timeout option when getting bitswap wantlist', () => {
      return testTimeout(() => ipfsA.bitswap.wantlist({ timeout: 1 }));
    });
    it('should get the wantlist', function () {
      return utils.waitForWantlistKey(ipfsB, key);
    });
    it('should not get the wantlist when offline', async () => {
      const node = await factory.spawn();
      await node.stop();
      return chai_js.expect(node.api.bitswap.stat()).to.eventually.be.rejected();
    });
    it('should remove blocks from the wantlist when requests are cancelled', async () => {
      const controller = new AbortController();
      const cid$1 = cid.CID.parse('QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KaGa');
      const getPromise = ipfsA.dag.get(cid$1, { signal: controller.signal });
      await utils.waitForWantlistKey(ipfsA, cid$1.toString());
      controller.abort();
      await chai_js.expect(getPromise).to.eventually.be.rejectedWith(/aborted/);
      await utils.waitForWantlistKeyToBeRemoved(ipfsA, cid$1.toString());
    });
    it('should keep blocks in the wantlist when only one request is cancelled', async () => {
      const controller = new AbortController();
      const otherController = new AbortController();
      const cid$1 = cid.CID.parse('QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1Kaaa');
      const getPromise = ipfsA.dag.get(cid$1, { signal: controller.signal });
      const otherGetPromise = ipfsA.dag.get(cid$1, { signal: otherController.signal });
      await utils.waitForWantlistKey(ipfsA, cid$1.toString());
      controller.abort();
      await chai_js.expect(getPromise).to.eventually.be.rejectedWith(/aborted/);
      await delay__default["default"](1000);
      await utils.waitForWantlistKey(ipfsA, cid$1.toString());
      otherController.abort();
      await chai_js.expect(otherGetPromise).to.eventually.be.rejectedWith(/aborted/);
      await utils.waitForWantlistKeyToBeRemoved(ipfsA, cid$1.toString());
    });
  });
}

exports.testWantlist = testWantlist;
