'use strict';

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

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

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 testStat(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.object.stat', function () {
    this.timeout(80 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should get stats by multihash', async () => {
      const testObj = {
        Data: fromString.fromString('get test object'),
        Links: []
      };
      const cid$1 = await ipfs.object.put(testObj);
      const stats = await ipfs.object.stat(cid$1);
      const expected = {
        Hash: cid.CID.parse('QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ').toV1(),
        NumLinks: 0,
        BlockSize: 17,
        LinksSize: 2,
        DataSize: 15,
        CumulativeSize: 17
      };
      chai_js.expect(stats).to.deep.equal(expected);
    });
    it('should get stats for object with links by multihash', async () => {
      const node1a = {
        Data: fromString.fromString(nanoid.nanoid()),
        Links: []
      };
      const node2 = {
        Data: fromString.fromString(nanoid.nanoid()),
        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);
      const stats = await ipfs.object.stat(node1bCid);
      const expected = {
        Hash: node1bCid,
        NumLinks: 1,
        BlockSize: 74,
        LinksSize: 53,
        DataSize: 21,
        CumulativeSize: 97
      };
      chai_js.expect(stats).to.deep.equal(expected);
    });
    it('returns error for request without argument', () => {
      return chai_js.expect(ipfs.object.stat(null)).to.eventually.be.rejected.and.be.an.instanceOf(Error);
    });
    it('returns error for request with invalid argument', () => {
      return chai_js.expect(ipfs.object.stat('invalid', { enc: 'base58' })).to.eventually.be.rejected.and.be.an.instanceOf(Error);
    });
  });
}

exports.testStat = testStat;
