var test = require('tape');
var BSFS = require('../lib/bsfs');

indexedDB.deleteDatabase('bsfs-tests');

function randomId () { return Math.random().toString(36).substr(2) }


test('bsfs exists', function (t) {
  t.plan(1);

  var bsfs = new BSFS('bsfs-tests');
  if (!bsfs._ready) return bsfs.once('ready', ready);
  else process.nextTick(ready);

  function ready () {
    t.equals(typeof bsfs, 'object');
  }
});

test('bsfs has file router', function (t) {
  t.plan(1);

  var bsfs = new BSFS('bsfs-tests');
  if (!bsfs._ready) return bsfs.once('ready', ready);
  else process.nextTick(ready);

  function ready () {
    t.equal(typeof bsfs._fileRouter, 'object');
  }
});

test('write without path throws', function (t) {
  t.plan(1);

  var bsfs = new BSFS('bsfs-tests');
  if (!bsfs._ready) return bsfs.once('ready', ready);
  else process.nextTick(ready);

  function ready () {
    t.throws(function() {
      bsfs.createWriteStream(null, function() {})
    });
  }
});

test('write file', function (t) {
  t.plan(1);

  var bsfs = new BSFS('bsfs-tests');
  if (!bsfs._ready) return bsfs.once('ready', ready);
  else process.nextTick(ready);

  function ready () {
    var path = '/tmp/test-' + randomId();
    var content = 'Hello cruel world ' + randomId();

    var w = bsfs.createWriteStream(path, function(err, meta) {
      t.equal(err, null);
    });

    w.end(content);
  }
});

test('write then read file by key', function (t) { 
  t.plan(1);

  var path = '/tmp/test-' + randomId();
  var content = 'Hello cruel world ' + randomId();

  var bsfs = new BSFS('bsfs-tests');
  if (!bsfs._ready) return bsfs.once('ready', ready);
  else process.nextTick(ready);

  function readBack (key) {
    var r = bsfs.createReadStreamFromKey(key);
    var readContent = ''; 
    r.on('data', function (chunk) {
      readContent += chunk;
    });
    r.on('end', function () {
      t.equal(content, readContent);
    });
  }

  function ready () {
    var w = bsfs.createWriteStream(path, function(err, meta) {
      readBack(meta.key);
    });
    w.end(content);
  }
});

test('write then read file by name', function (t) { 
  t.plan(1);

  var content = 'Hello cruel world ' + randomId();
  var path = '/tmp/test-' + randomId();

  var bsfs = new BSFS('bsfs-tests');
  if (!bsfs._ready) return bsfs.once('ready', ready);
  else process.nextTick(ready);

  function readBack (path) {
    var r = bsfs.createReadStream(path);
    var readContent = ''; 
    r.on('data', function (chunk) {
      readContent += chunk;
    });
    r.on('end', function () {
      t.equal(content, readContent);
    });
  }

  function ready () {
    var w = bsfs.createWriteStream(path, function(err, meta) {
      readBack(path);
    });
    w.end(content);
  }
});

test('access', function (t) {

  t.test('is silent (for now)', function (t) {
    t.plan(3);

    var bsfs = new BSFS('bsfs-tests');

    if (!bsfs.ready) return bsfs.once('ready', ready)
    else process.nextTick(ready);

    function ready () {
      bsfs.access(null, function (err) {
        t.ifError(err);
      });
      bsfs.access('/tmp', function (err) {
        t.ifError(err);
      }); 
      bsfs.access('/tmp', 2, function (err) {
        t.ifError(err);
      }); 
    }
  });

  t.test('throws on invalid callback argument', function (t) {
    t.plan(1);

    var bsfs = new BSFS('bsfs-tests');

    if (!bsfs.ready) return bsfs.once('ready', ready)
    else process.nextTick(ready);

    function ready () {
      t.throws(function () {
        bsfs.access('/tmp/', 0, 'potatoe');
      })
    }
  });
});

test('accessSync', function (t) {

  t.test('is silent (for now)', function (t) {
    t.plan(2);

    var bsfs = new BSFS('bsfs-tests');

    if (!bsfs.ready) return bsfs.once('ready', ready)
    else process.nextTick(ready);

    function ready () {
      t.ifError(bsfs.accessSync(randomId()));
      t.ifError(bsfs.accessSync());
    }
  })
});

test('exists', function (t) {

  t.test('is true for all paths (for now)', function (t) {
    t.plan(1);

    var bsfs = new BSFS('bsfs-tests');

    if (!bsfs.ready) return bsfs.once('ready', ready);
    else proccess.nextTick(ready);

    function ready () {
      bsfs.exists(randomId(), function (exists) {
        t.ok(exists);
      });
    }
  });
});

test('existsSync', function (t) {

  t.test('throws on null path', function (t) {
    t.plan(1);

    var bsfs = new BSFS('bsfs-tests');
    
    if (!bsfs.ready) return bsfs.once('ready', ready);
    else proccess.nextTick(ready);

    function ready () {
      t.throws(bsfs.existsSync());
    }
  });

  t.test('is true for all paths (for now)', function (t) {
    t.plan(2);

    var bsfs = new BSFS('bsfs-tests');

    if (!bsfs.ready) return bsfs.once('ready', ready);
    else proccess.nextTick(ready);

    function ready () {
      t.ok(bsfs.existsSync(randomId()));
      t.ok(bsfs.existsSync());
    }
  });
});

test('appendFile without path throws', function (t) {
  t.plan(1);

  var bsfs = new BSFS('bsfs-tests');

  if (!bsfs._ready) return bsfs.once('ready', ready);
  else process.nextTick(ready);

  function ready () {
    t.throws(function () {
      bsfs.appendFile(null, function () {});
    });
  }
});

