'use strict';

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

var nanoid = require('nanoid');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');

function testRename(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.key.rename', () => {
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should rename a key', async function () {
      this.timeout(30 * 1000);
      const oldName = nanoid.nanoid();
      const newName = nanoid.nanoid();
      const key = await ipfs.key.gen(oldName, {
        type: 'rsa',
        size: 2048
      });
      const renameRes = await ipfs.key.rename(oldName, newName);
      chai_js.expect(renameRes).to.exist();
      chai_js.expect(renameRes).to.have.property('was', oldName);
      chai_js.expect(renameRes).to.have.property('now', newName);
      chai_js.expect(renameRes).to.have.property('id', key.id);
      const res = await ipfs.key.list();
      chai_js.expect(res.find(k => k.name === newName)).to.exist();
      chai_js.expect(res.find(k => k.name === oldName)).to.not.exist();
    });
  });
}

exports.testRename = testRename;
