var tape   = require('tape')
var CAS    = require('../')
var osenv  = require('osenv')
var path   = require('path')
var fs     = require('fs')
var shasum = require('shasum')

var db = CAS(path.join(osenv.tmpdir(), 'test-cas'), 'sha1')

tape('add a value', function (t) {
  t.plan(2)
  db.add('hello', {encoding: 'utf8'}, function (err, hash) {
    if(err) throw err
    t.equal(hash, 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
    db.get(hash, function (err, data) {
      if(err) throw err
      t.equal(shasum(data), hash)
      t.end()
    })
  })
})

tape('add the empty string', function (t) {
  db.add('', {encoding: 'utf8'}, function (err, hash) {
    if(err) throw err
    t.equal(hash, 'da39a3ee5e6b4b0d3255bfef95601890afd80709')
    db.get(hash, function (err, data) {
      if(err) throw err
      t.equal(shasum(data), hash)
      t.end()
    })
  })
})

tape('add twice & detect cached', function (t) {
  db.add('abc', {encoding: 'utf8'}, function (err, hash, cached) {
    if(err) throw err
    t.equal(hash, 'a9993e364706816aba3e25717850c26c9cd0d89d')
    t.notOk(cached)

    db.add('abc', {encoding: 'utf8'}, function (err, hash, cached) {
      if(err) throw err
      t.equal(hash, 'a9993e364706816aba3e25717850c26c9cd0d89d')
      t.ok(cached)
      t.end()
    })
  })

})

tape('add a stream', function (t) {
  t.plan(2)
  fs.createReadStream(path.join(__dirname,'..', 'README.md'))
  .pipe(db.addStream())
  .on('close', function () {
    var stream = this
    console.log(stream.hash)
    t.ok(stream.hash)
    db.get(stream.hash, function (err, content) {
      console.log('readme:', content.toString().split('\n').shift() + '...')
      t.equal(stream.hash, shasum(content))
      t.end()
    })
  })
})


@@ -170,23 +170,51 @@
 um')
+%0Avar rimraf = require('rimraf')
 %0A%0Avar d
-b
+ir
  = 
-CAS(
 path
@@ -250,17 +250,245 @@
 as')
-, 'sha1')
+%0Arimraf.sync(dir)%0A%0Avar db = CAS(dir, 'sha1')%0A%0Avar helloHash = 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'%0Avar emptyHash = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'%0Avar abcHash   = 'a9993e364706816aba3e25717850c26c9cd0d89d'%0Avar readmeHash
 %0A%0Ata
@@ -637,50 +637,17 @@
 sh, 
-'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'
+helloHash
 )%0A  
@@ -916,50 +916,17 @@
 sh, 
-'da39a3ee5e6b4b0d3255bfef95601890afd80709'
+emptyHash
 )%0A  
@@ -1211,70 +1211,57 @@
 sh, 
-'a9993e364706816aba3e25717850c26c9cd0d89d')%0A    t.notOk(cached
+abcHash)%0A    t.notOk(cached, 'abc was not cached'
 )%0A%0A 
@@ -1376,69 +1376,61 @@
 sh, 
-'a9993e364706816aba3e25717850c26c9cd0d89d')%0A      t.ok(cached
+abcHash)%0A      t.ok(cached , 'abc was already cached'
 )%0A  
@@ -1659,16 +1659,29 @@
 ole.log(
+readmeHash = 
 stream.h
@@ -1888,28 +1888,263 @@
     t.end()%0A    %7D)%0A  %7D)%0A%7D)%0A%0A
+tape('get all keys', function (t) %7B%0A  db.all(function (err, all) %7B%0A    if(err) throw err%0A    console.error(all)%0A    t.deepEqual(all, %5B%0A      helloHash,%0A      abcHash,%0A      readmeHash,%0A      emptyHash%0A    %5D.sort())%0A    t.end()%0A  %7D)%0A%7D)%0A

test to get all keys