var fs = require('fs')
var path = require('path')
var test = require('tape')
var rimraf = require('rimraf')

var Dat = require('..')

// os x adds this if you view the fixtures in finder and breaks the file count assertions
try { fs.unlinkSync(path.join(__dirname, 'fixtures', '.DS_Store')) } catch (e) { /* ignore error */ }

var fixtures = path.join(__dirname, 'fixtures')
var dat

test('Share events with default opts', function (t) {
  dat = Dat({dir: fixtures})

  dat.once('ready', function () {
    t.pass('emits ready')
    t.ok(dat.dir === fixtures, 'correct directory')
    fs.stat(path.join(fixtures, '.dat'), function (err, stat) {
      if (err) return console.error(err)
      t.pass('creates .dat dir')
    })

    dat.share(function (err) {
      t.error(err, 'no sharing error')
      endTest(t)
    })
  })

  dat.once('append-ready', function () {
    t.pass('append ready emits')
    t.ok(dat.stats.filesTotal > 0, 'append ready stats')
  })

  dat.once('key', function (key) {
    t.ok(key && key.length === 50, 'emits key')
  })

  dat.once('archive-finalized', function () {
    t.pass('emits archive-finalized')
  })
})

test('Share stats', function (t) {
  dat = Dat({dir: fixtures})
  var stats = {
    filesTotal: 3,
    bytesTotal: 1543
  }

  dat.once('ready', function () {
    dat.share(function (err) {
      t.error(err, 'no share error')
      endTest(t)
    })
  })

  dat.once('append-ready', function () {
    t.same(stats.filesTotal, dat.stats.filesTotal, 'total files')
    t.same(stats.bytesTotal, dat.stats.bytesTotal, 'total bytes')
  })

  dat.once('archive-finalized', function () {
    t.same(dat.stats.filesProgress, stats.filesTotal, 'progress file count')
    t.same(dat.stats.bytesProgress, stats.bytesTotal, 'progress byte count')
  })
})

function endTest (t) {
  dat.db.close(function () {
    rimraf(path.join(dat.dir, '.dat'), function () {
      dat.close()
      t.end()
    })
  })
}

@@ -73,39 +73,8 @@
 pe')
-%0Avar rimraf = require('rimraf')
 %0A%0Ava
@@ -428,16 +428,90 @@
 xtures%7D)
+%0A  var stats = %7B%0A    filesTotal: 3,%0A    bytesTotal: 1543%0A  %7D%0A%0A  t.plan(12)
 %0A%0A  dat.
@@ -832,33 +832,16 @@
 error')%0A
-      endTest(t)%0A
     %7D)%0A 
@@ -1052,10 +1052,10 @@
 === 
-50
+64
 , 'e
@@ -1163,268 +1163,8 @@
   %7D)
-%0A%7D)%0A%0Atest('Share stats', function (t) %7B%0A  dat = Dat(%7Bdir: fixtures%7D)%0A  var stats = %7B%0A    filesTotal: 3,%0A    bytesTotal: 1543%0A  %7D%0A%0A  dat.once('ready', function () %7B%0A    dat.share(function (err) %7B%0A      t.error(err, 'no share error')%0A      endTest(t)%0A    %7D)%0A  %7D)
 %0A%0A  
@@ -1553,99 +1553,22 @@
 %7D)%0A%0A
-function endTest (t) %7B%0A  dat.db.close(function () %7B%0A    rimraf(path.join(dat.dir, '.dat'), 
+test.onFinish(
 func
@@ -1577,20 +1577,16 @@
 on () %7B%0A
-    
   dat.cl
@@ -1595,32 +1595,7 @@
 e()%0A
-      t.end()%0A    %7D)%0A  %7D)%0A
 %7D
+)
 %0A

update test for hyperdrive-import-files