'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');
var first = require('it-first');
var drain = require('it-drain');

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 dagPB__namespace = /*#__PURE__*/_interopNamespace(dagPB);
var first__default = /*#__PURE__*/_interopDefaultLegacy(first);
var drain__default = /*#__PURE__*/_interopDefaultLegacy(drain);

function testPut(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.object.put', function () {
    this.timeout(80 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should put an object', async () => {
      const obj = {
        Data: fromString.fromString(nanoid.nanoid()),
        Links: []
      };
      const cid = await ipfs.object.put(obj);
      const node = await ipfs.object.get(cid);
      chai_js.expect(node).to.deep.equal(obj);
    });
    it('should pin an object when putting', async () => {
      const obj = {
        Data: fromString.fromString(nanoid.nanoid()),
        Links: []
      };
      const cid = await ipfs.object.put(obj, { pin: true });
      const pin = await first__default["default"](ipfs.pin.ls({ paths: cid }));
      chai_js.expect(pin).to.have.deep.property('cid', cid);
      chai_js.expect(pin).to.have.property('type', 'recursive');
    });
    it('should not pin an object by default', async () => {
      const obj = {
        Data: fromString.fromString(nanoid.nanoid()),
        Links: []
      };
      const cid = await ipfs.object.put(obj);
      return chai_js.expect(drain__default["default"](ipfs.pin.ls({ paths: cid }))).to.eventually.be.rejectedWith(/not pinned/);
    });
    it('should put a Protobuf DAGNode', async () => {
      const dNode = {
        Data: fromString.fromString(nanoid.nanoid()),
        Links: []
      };
      const cid = await ipfs.object.put(dNode);
      const node = await ipfs.object.get(cid);
      chai_js.expect(dNode).to.deep.equal(node);
    });
    it('should fail if a string is passed', () => {
      return chai_js.expect(ipfs.object.put(nanoid.nanoid())).to.eventually.be.rejected();
    });
    it('should put a Protobuf DAGNode with a link', 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 cid$1 = await ipfs.object.put(node1b);
      const node = await ipfs.object.get(cid$1);
      chai_js.expect(node1b.Data).to.deep.equal(node.Data);
      chai_js.expect(node1b.Links).to.deep.equal(node.Links);
    });
  });
}

exports.testPut = testPut;
