'use strict';

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

var fromString = require('uint8arrays/from-string');
var dagPB = require('@ipld/dag-pb');
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 testAddLink(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.object.patch.addLink', function () {
    this.timeout(80 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should add a link to an existing node', async () => {
      const obj = {
        Data: fromString.fromString('patch test object'),
        Links: []
      };
      const node2 = {
        Data: fromString.fromString('some other node'),
        Links: []
      };
      await ipfs.object.put(node2);
      const node2Buf = dagPB__namespace.encode(node2);
      const link = {
        Name: 'link-to-node',
        Tsize: node2Buf.length,
        Hash: cid.CID.createV0(await sha2.sha256.digest(node2Buf))
      };
      const node1a = {
        Data: obj.Data,
        Links: obj.Links
      };
      const node1b = {
        Data: node1a.Data,
        Links: [link]
      };
      const node1bCid = await ipfs.object.put(node1b);
      const testNodeCid = await ipfs.object.put(obj);
      const cid$1 = await ipfs.object.patch.addLink(testNodeCid, link);
      chai_js.expect(node1bCid).to.eql(cid$1);
    });
    it('returns error for request without arguments', () => {
      return chai_js.expect(ipfs.object.patch.addLink(null, null, null)).to.eventually.be.rejected.and.be.an.instanceOf(Error);
    });
    it('returns error for request with only one invalid argument', () => {
      return chai_js.expect(ipfs.object.patch.addLink('invalid', null, null)).to.eventually.be.rejected.and.be.an.instanceOf(Error);
    });
  });
}

exports.testAddLink = testAddLink;
