'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 testSet(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.config.set', function () {
    this.timeout(30 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should set a new key', async () => {
      await ipfs.config.set('Fruit', 'banana');
      const fruit = await ipfs.config.get('Fruit');
      chai_js.expect(fruit).to.equal('banana');
    });
    it('should set an already existing key', async () => {
      await ipfs.config.set('Fruit', 'morango');
      const fruit = await ipfs.config.get('Fruit');
      chai_js.expect(fruit).to.equal('morango');
    });
    it('should set a number', async () => {
      const key = 'Discovery.MDNS.Interval';
      const val = 11;
      await ipfs.config.set(key, val);
      const result = await ipfs.config.get(key);
      chai_js.expect(result).to.equal(val);
    });
    it('should set a boolean', async () => {
      const value = true;
      const key = 'Discovery.MDNS.Enabled';
      await ipfs.config.set(key, value);
      chai_js.expect(await ipfs.config.get(key)).to.equal(value);
    });
    it('should set the other boolean', async () => {
      const value = false;
      const key = 'Discovery.MDNS.Enabled';
      await ipfs.config.set(key, value);
      chai_js.expect(await ipfs.config.get(key)).to.equal(value);
    });
    it('should set a JSON object', async () => {
      const key = 'API.HTTPHeaders.Access-Control-Allow-Origin';
      const val = ['http://example.io'];
      await ipfs.config.set(key, val);
      const result = await ipfs.config.get(key);
      chai_js.expect(result).to.deep.equal(val);
    });
    it('should fail on non valid key', () => {
      return chai_js.expect(ipfs.config.set(fromString.fromString('heeey'), '')).to.eventually.be.rejected();
    });
    it('should fail on non valid value', () => {
      const val = {};
      val.val = val;
      return chai_js.expect(ipfs.config.set('Fruit', val)).to.eventually.be.rejected();
    });
  });
}

exports.testSet = testSet;
