'use strict';

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

var dagPB = require('@ipld/dag-pb');
var nanoid = require('nanoid');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');
var ipfsUnixfs = require('ipfs-unixfs');
var isoRandomStream = require('iso-random-stream');
var fromString = require('uint8arrays/from-string');
var cid = require('multiformats/cid');
var sha2 = require('multiformats/hashes/sha2');

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 dagPB__namespace = /*#__PURE__*/_interopNamespace(dagPB);

function testGet(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.object.get', function () {
    this.timeout(80 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should get object by multihash', async () => {
      const obj = {
        Data: fromString.fromString(nanoid.nanoid()),
        Links: []
      };
      const node1Cid = await ipfs.object.put(obj);
      const node1 = await ipfs.object.get(node1Cid);
      let node2 = await ipfs.object.get(node1Cid);
      if (typeof node2.Data === 'string') {
        node2 = {
          Data: fromString.fromString(node2.Data),
          Links: node2.Links
        };
      }
      chai_js.expect(node1.Data).to.eql(node2.Data);
      chai_js.expect(node1.Links).to.eql(node2.Links);
    });
    it('should get object with links by multihash string', async () => {
      const node1a = {
        Data: fromString.fromString('Some data 1'),
        Links: []
      };
      const node2 = {
        Data: fromString.fromString('Some data 2'),
        Links: []
      };
      const node2Buf = dagPB__namespace.encode(node2);
      const link = {
        Name: 'some-link',
        Tsize: node2Buf.length,
        Hash: cid.CID.createV0(await sha2.sha256.digest(node2Buf))
      };
      const node1b = {
        Data: node1a.Data,
        Links: [link]
      };
      const node1bCid = await ipfs.object.put(node1b);
      let node1c = await ipfs.object.get(node1bCid);
      if (typeof node1c.Data === 'string') {
        node1c = {
          Data: fromString.fromString(node1c.Data),
          Links: node1c.Links
        };
      }
      chai_js.expect(node1a.Data).to.eql(node1c.Data);
    });
    it('should get object by base58 encoded multihash', async () => {
      const obj = {
        Data: fromString.fromString(nanoid.nanoid()),
        Links: []
      };
      const node1aCid = await ipfs.object.put(obj);
      const node1a = await ipfs.object.get(node1aCid);
      let node1b = await ipfs.object.get(node1aCid, { enc: 'base58' });
      if (typeof node1b.Data === 'string') {
        node1b = {
          Data: fromString.fromString(node1b.Data),
          Links: node1b.Links
        };
      }
      chai_js.expect(node1a.Data).to.eql(node1b.Data);
      chai_js.expect(node1a.Links).to.eql(node1b.Links);
    });
    it('should supply unaltered data', async () => {
      const data = isoRandomStream.randomBytes(1024 * 3000);
      const result = await ipfs.add({
        path: '',
        content: data
      });
      const node = await ipfs.object.get(result.cid);
      if (!node.Data) {
        throw new Error('Node did not have data');
      }
      const meta = ipfsUnixfs.UnixFS.unmarshal(node.Data);
      chai_js.expect(meta.fileSize()).to.equal(data.length);
    });
    it('should error for request without argument', () => {
      return chai_js.expect(ipfs.object.get(null)).to.eventually.be.rejected.and.be.an.instanceOf(Error);
    });
    it('returns error for request with invalid argument', () => {
      return chai_js.expect(ipfs.object.get('invalid', { enc: 'base58' })).to.eventually.be.rejected.and.be.an.instanceOf(Error);
    });
  });
}

exports.testGet = testGet;
