'use strict';

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

var fromString = require('uint8arrays/from-string');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../../utils/mocha.js');

function testSetData(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.object.patch.setData', function () {
    this.timeout(80 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should set data for an existing node', async () => {
      const obj = {
        Data: fromString.fromString('patch test object'),
        Links: []
      };
      const patchData = fromString.fromString('set');
      const nodeCid = await ipfs.object.put(obj);
      const patchedNodeCid = await ipfs.object.patch.setData(nodeCid, patchData);
      const patchedNode = await ipfs.object.get(patchedNodeCid);
      chai_js.expect(nodeCid).to.not.deep.equal(patchedNodeCid);
      chai_js.expect(patchedNode.Data).to.eql(patchData);
    });
    it('returns error for request without key & data', () => {
      return chai_js.expect(ipfs.object.patch.setData(null, null)).to.eventually.be.rejected.and.be.an.instanceOf(Error);
    });
    it('returns error for request without data', () => {
      const filePath = 'test/fixtures/test-data/badnode.json';
      return chai_js.expect(ipfs.object.patch.setData(null, filePath)).to.eventually.be.rejected.and.be.an.instanceOf(Error);
    });
  });
}

exports.testSetData = testSetData;
