'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 isoRandomStream = require('iso-random-stream');
var concat = require('it-concat');
var nanoid = require('nanoid');
var fromString = require('uint8arrays/from-string');
var pmap = require('p-map');
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 concat__default = /*#__PURE__*/_interopDefaultLegacy(concat);
var pmap__default = /*#__PURE__*/_interopDefaultLegacy(pmap);

function testTransfer(factory, options) {
  const ipfsOptions = ipfsOptionsWebsocketsFilterAll.ipfsOptionsWebsocketsFilterAll();
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('transfer blocks', function () {
    this.timeout(60 * 1000);
    afterEach(() => factory.clean());
    describe('transfer a block between', () => {
      it('2 peers', async function () {
        const remote = (await factory.spawn({ type: env_js.isWebWorker ? 'go' : undefined })).api;
        const remoteId = await remote.id();
        const local = (await factory.spawn({
          type: 'proc',
          ipfsOptions
        })).api;
        await local.swarm.connect(remoteId.addresses[0]);
        const data = fromString.fromString(`IPFS is awesome ${ nanoid.nanoid() }`);
        const cid = await local.block.put(data);
        const b = await remote.block.get(cid);
        chai_js.expect(b).to.equalBytes(data);
      });
      it('3 peers', async () => {
        const blocks = Array(6).fill(0).map(() => fromString.fromString(`IPFS is awesome ${ nanoid.nanoid() }`));
        const remote1 = (await factory.spawn({ type: env_js.isWebWorker ? 'go' : undefined })).api;
        const remote1Id = await remote1.id();
        const remote2 = (await factory.spawn({ type: env_js.isWebWorker ? 'go' : undefined })).api;
        const remote2Id = await remote2.id();
        const local = (await factory.spawn({
          type: 'proc',
          ipfsOptions
        })).api;
        await local.swarm.connect(remote1Id.addresses[0]);
        await local.swarm.connect(remote2Id.addresses[0]);
        await remote1.swarm.connect(remote2Id.addresses[0]);
        const cids = [];
        cids.push(await remote1.block.put(blocks[0]));
        cids.push(await remote1.block.put(blocks[1]));
        cids.push(await remote2.block.put(blocks[2]));
        cids.push(await remote2.block.put(blocks[3]));
        cids.push(await local.block.put(blocks[4]));
        cids.push(await local.block.put(blocks[5]));
        await pmap__default["default"](blocks, async (block, i) => {
          chai_js.expect(await remote1.block.get(cids[i])).to.eql(block);
          chai_js.expect(await remote2.block.get(cids[i])).to.eql(block);
          chai_js.expect(await local.block.get(cids[i])).to.eql(block);
        }, { concurrency: 3 });
      });
    });
    describe('transfer a file between', () => {
      it('2 peers', async () => {
        const content = isoRandomStream.randomBytes(1024);
        const remote = (await factory.spawn({ type: env_js.isWebWorker ? 'go' : undefined })).api;
        const remoteId = await remote.id();
        const local = (await factory.spawn({
          type: 'proc',
          ipfsOptions
        })).api;
        local.swarm.connect(remoteId.addresses[0]);
        const file = await remote.add({
          path: 'awesome.txt',
          content
        });
        const data = await concat__default["default"](local.cat(file.cid));
        chai_js.expect(data.slice()).to.eql(content);
      });
    });
  });
}

exports.testTransfer = testTransfer;
