'use strict';

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

var concat = require('uint8arrays/concat');
var drain = require('it-drain');
var all = require('it-all');
var index = require('../utils/index.js');
var chai_js = require('aegir/utils/chai.js');
var mocha = require('../utils/mocha.js');
var createShardedDirectory = require('../utils/create-sharded-directory.js');
var isoRandomStream = require('iso-random-stream');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var drain__default = /*#__PURE__*/_interopDefaultLegacy(drain);
var all__default = /*#__PURE__*/_interopDefaultLegacy(all);

function testRead(factory, options) {
  const describe = mocha.getDescribe(options);
  const it = mocha.getIt(options);
  const smallFile = isoRandomStream.randomBytes(13);
  describe('.files.read', function () {
    this.timeout(120 * 1000);
    let ipfs;
    before(async () => {
      ipfs = (await factory.spawn()).api;
    });
    after(() => factory.clean());
    it('reads a small file', async () => {
      const filePath = '/small-file.txt';
      await ipfs.files.write(filePath, smallFile, { create: true });
      const bytes = concat.concat(await all__default["default"](ipfs.files.read(filePath)));
      chai_js.expect(bytes).to.deep.equal(smallFile);
    });
    it('reads a file with an offset', async () => {
      const path = `/some-file-${ Math.random() }.txt`;
      const data = isoRandomStream.randomBytes(100);
      const offset = 10;
      await ipfs.files.write(path, data, { create: true });
      const bytes = concat.concat(await all__default["default"](ipfs.files.read(path, { offset })));
      chai_js.expect(bytes).to.deep.equal(data.slice(offset));
    });
    it('reads a file with a length', async () => {
      const path = `/some-file-${ Math.random() }.txt`;
      const data = isoRandomStream.randomBytes(100);
      const length = 10;
      await ipfs.files.write(path, data, { create: true });
      const bytes = concat.concat(await all__default["default"](ipfs.files.read(path, { length })));
      chai_js.expect(bytes).to.deep.equal(data.slice(0, length));
    });
    it('reads a file with an offset and a length', async () => {
      const path = `/some-file-${ Math.random() }.txt`;
      const data = isoRandomStream.randomBytes(100);
      const offset = 10;
      const length = 10;
      await ipfs.files.write(path, data, { create: true });
      const buffer = concat.concat(await all__default["default"](ipfs.files.read(path, {
        offset,
        length
      })));
      chai_js.expect(buffer).to.deep.equal(data.slice(offset, offset + length));
    });
    it('refuses to read a directory', async () => {
      const path = '/';
      await chai_js.expect(drain__default["default"](ipfs.files.read(path))).to.eventually.be.rejectedWith(/not a file/);
    });
    it('refuses to read a non-existent file', async () => {
      const path = `/file-${ Math.random() }.txt`;
      await chai_js.expect(drain__default["default"](ipfs.files.read(path))).to.eventually.be.rejectedWith(/does not exist/);
    });
    it('should read from outside of mfs', async () => {
      const {cid} = await ipfs.add(index.fixtures.smallFile.data);
      const testFileData = concat.concat(await all__default["default"](ipfs.files.read(`/ipfs/${ cid }`)));
      chai_js.expect(testFileData).to.eql(index.fixtures.smallFile.data);
    });
    describe('with sharding', () => {
      let ipfs;
      before(async function () {
        const ipfsd = await factory.spawn({
          ipfsOptions: {
            EXPERIMENTAL: { sharding: true },
            config: { Internal: { UnixFSShardingSizeThreshold: '1B' } }
          }
        });
        ipfs = ipfsd.api;
      });
      it('reads file from inside a sharded directory', async () => {
        const shardedDirPath = await createShardedDirectory.createShardedDirectory(ipfs);
        const filePath = `${ shardedDirPath }/file-${ Math.random() }.txt`;
        const content = Uint8Array.from([
          0,
          1,
          2,
          3,
          4
        ]);
        await ipfs.files.write(filePath, content, { create: true });
        const bytes = concat.concat(await all__default["default"](ipfs.files.read(filePath)));
        chai_js.expect(bytes).to.deep.equal(content);
      });
    });
  });
}

exports.testRead = testRead;
