'use strict';

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

var all = require('it-all');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');
var car = require('@ipld/car');
var fromString = require('uint8arrays/from-string');
var dagPB = require('@ipld/dag-pb');
var dagCBOR = require('@ipld/dag-cbor');
var loadFixture = require('aegir/utils/fixtures.js');
var toBuffer = require('it-to-buffer');

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

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n["default"] = e;
  return Object.freeze(n);
}

var all__default = /*#__PURE__*/_interopDefaultLegacy(all);
var dagPB__namespace = /*#__PURE__*/_interopNamespace(dagPB);
var dagCBOR__namespace = /*#__PURE__*/_interopNamespace(dagCBOR);
var loadFixture__default = /*#__PURE__*/_interopDefaultLegacy(loadFixture);
var toBuffer__default = /*#__PURE__*/_interopDefaultLegacy(toBuffer);

function testExport(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.dag.export', () => {
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should export a car file', async () => {
      const child = dagPB__namespace.encode({
        Data: fromString.fromString('block-' + Math.random()),
        Links: []
      });
      const childCid = await ipfs.block.put(child, {
        format: 'dag-pb',
        version: 0
      });
      const parent = dagPB__namespace.encode({
        Links: [{
            Hash: childCid,
            Tsize: child.length,
            Name: ''
          }]
      });
      const parentCid = await ipfs.block.put(parent, {
        format: 'dag-pb',
        version: 0
      });
      const grandParent = dagCBOR__namespace.encode({ parent: parentCid });
      const grandParentCid = await await ipfs.block.put(grandParent, {
        format: 'dag-cbor',
        version: 1
      });
      const expectedCids = [
        grandParentCid,
        parentCid,
        childCid
      ];
      const reader = await car.CarReader.fromIterable(ipfs.dag.export(grandParentCid));
      const cids = await all__default["default"](reader.cids());
      chai_js.expect(cids).to.deep.equal(expectedCids);
    });
    it('export of shuffled devnet export identical to canonical original', async function () {
      this.timeout(360000);
      const input = loadFixture__default["default"]('test/fixtures/car/lotus_devnet_genesis.car', 'interface-ipfs-core');
      const result = await all__default["default"](ipfs.dag.import(async function* () {
        yield input;
      }()));
      const exported = await toBuffer__default["default"](ipfs.dag.export(result[0].root.cid));
      chai_js.expect(exported).to.equalBytes(input);
    });
    it('export of shuffled testnet export identical to canonical original', async function () {
      this.timeout(360000);
      const input = loadFixture__default["default"]('test/fixtures/car/lotus_testnet_export_128.car', 'interface-ipfs-core');
      const result = await all__default["default"](ipfs.dag.import(async function* () {
        yield input;
      }()));
      const exported = await toBuffer__default["default"](ipfs.dag.export(result[0].root.cid));
      chai_js.expect(exported).to.equalBytes(input);
    });
  });
}

exports.testExport = testExport;
