var test = require('./helpers')
var concat = require('concat-stream')

test('two versions', function(t, db) {
  db.put('foo', 'bar', function(err) {
    t.notOk(err, 'no err')
    db.put('foo', 'baz', {version:1}, function(err) {
      t.notOk(err, 'no err')
      db.get('foo', function(err, val, version) {
        t.notOk(err, 'no err')
        t.same(val, 'baz')
        t.same(version, 2)
        t.end()
      })
    })
  })
})

test('conflict', function(t, db) {
  db.put('foo', 'bar', function(err) {
    t.notOk(err, 'no err')
    db.put('foo', 'baz', function(err) {
      t.ok(err, 'conflict err')
      t.ok(err.conflict, 'conflict flag set')
      t.same(err.key, 'foo', 'key set')
      t.end()
    })
  })
})

test('no conflict on force', function(t, db) {
  db.put('foo', 'bar', function(err) {
    t.notOk(err, 'no err')
    db.put('foo', 'baz', {force:true}, function(err) {
      t.notOk(err, 'no err')
      db.get('foo', function(err, val, version) {
        t.notOk(err, 'no err')
        t.same(val, 'baz')
        t.same(version, 2)
        t.end()
      })
    })
  })
})

test('version stream', function(t, db) {
  db.put('foo', 'bar', function(err) {
    t.notOk(err, 'no err')
    db.put('foo', 'baz', {version:1}, function(err) {
      t.notOk(err, 'no err')
      db.createVersionStream('foo').pipe(concat(function(versions) {
        t.same(versions.length, 2, '2 versions')
        t.same(versions[0], {key:'foo', value:'bar', version:1})
        t.same(versions[1], {key:'foo', value:'baz', version:2})
        t.end()
      }))
    })
  })
})


@@ -1555,24 +1555,433 @@
     %7D))%0A    %7D)%0A  %7D)%0A%7D)%0A%0A
+test('no conflict on force stream', function(t, db) %7B%0A  var ws = db.createWriteStream(%7Bforce:true%7D)%0A%0A  ws.write(%7B%0A    key: 'foo',%0A    value: '0'%0A  %7D)%0A%0A  ws.write(%7B%0A    key: 'foo',%0A    value: '1'%0A  %7D)%0A%0A  ws.write(%7B%0A    key: 'foo',%0A    value: '2'%0A  %7D)%0A%0A  ws.end(function() %7B%0A    db.createReadStream().on('data', function(data) %7B%0A      t.same(data, %7Bkey:'foo', value:'2', version:3%7D)%0A      t.end()%0A    %7D)%0A  %7D)%0A%7D)

add test for maxogden/dat#154