'use strict';

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

var fromString = require('uint8arrays/from-string');
var cid = require('multiformats/cid');
var all = require('it-all');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');
var utils = require('./utils.js');

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

var all__default = /*#__PURE__*/_interopDefaultLegacy(all);

function testProvide(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.dht.provide', function () {
    this.timeout(80 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
      const nodeB = (await factory.spawn()).api;
      await utils.ensureReachable(ipfs, nodeB);
    });
    after(() => factory.clean());
    it('should provide local CID', async () => {
      const res = await ipfs.add(fromString.fromString('test'));
      await all__default["default"](ipfs.dht.provide(res.cid));
    });
    it('should not provide if block not found locally', () => {
      const cid$1 = cid.CID.parse('Qmd7qZS4T7xXtsNFdRoK1trfMs5zU94EpokQ9WFtxdPxsZ');
      return chai_js.expect(all__default["default"](ipfs.dht.provide(cid$1))).to.eventually.be.rejected.and.be.an.instanceOf(Error).and.have.property('message').that.include('not found locally');
    });
    it('should provide a CIDv1', async () => {
      const res = await ipfs.add(fromString.fromString('test'), { cidVersion: 1 });
      await all__default["default"](ipfs.dht.provide(res.cid));
    });
    it('should error on non CID arg', async () => {
      return chai_js.expect(all__default["default"](ipfs.dht.provide({}))).to.eventually.be.rejected();
    });
  });
}

exports.testProvide = testProvide;
