'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 testFlush(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  describe('.files.flush', function () {
    this.timeout(120 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('should not flush not found file/dir, expect error', async () => {
      const testDir = `/test-${ nanoid.nanoid() }`;
      try {
        await ipfs.files.flush(`${ testDir }/404`);
      } catch (err) {
        chai_js.expect(err).to.exist();
      }
    });
    it('should require a path', () => {
      chai_js.expect(ipfs.files.flush()).to.eventually.be.rejected();
    });
    it('should flush root', async () => {
      const root = await ipfs.files.stat('/');
      const flushed = await ipfs.files.flush('/');
      chai_js.expect(root.cid.toString()).to.equal(flushed.toString());
    });
    it('should flush specific dir', async () => {
      const testDir = `/test-${ nanoid.nanoid() }`;
      await ipfs.files.mkdir(testDir, { parents: true });
      const dirStats = await ipfs.files.stat(testDir);
      const flushed = await ipfs.files.flush(testDir);
      chai_js.expect(dirStats.cid.toString()).to.equal(flushed.toString());
    });
  });
}

exports.testFlush = testFlush;
