var path = '/tmp/test-level-master'
var path2 = '/tmp/test-level-slave'

require('rimraf').sync(path)
require('rimraf').sync(path2)

var levelup = require('levelup')
var SubLevel = require('level-sublevel')

var db   = SubLevel(levelup(path))
var _db  = SubLevel(levelup(path2))

var Master = require('../')
var pull   = require('pull-stream')

var help   = require('./helper')

var test   = require('tape')

var master = Master(db, 'master', 'T1')

slave = Master(_db, 'master', 'T2')

test('setup', function (t) {
  help.populate(db, 100, function (err) {
    t.notOk(err)
    t.end()
  })
})

//master-slave replication must pull from master.
//connect to master, copy current since value.


test('createStream', function (t) {

  slave.clock(function (err, clock) {
    t.deepEqual(clock, {})

    console.log('PULL STREAM SINCE:', clock)
    master.createMasterStream()
      .pipe(pull.through(function (data) {
        t.ok(data.ts > clock, data.ts + ' > ' + clock)
      }))
      .pipe(slave.createSlaveStream(function (err) {
        //*********************************
        //SOME TIMES THIS DOESN"T HAPPEN????
        //*********************************
        t.end()
      }))
  })
})

return
test('updates', function (t) {
  help.populate(db, 100, function (err) {
    t.notOk(err)
    t.end()
  })
})

test('createStream2', function (t) {

  slave.clock(function (err, clock) {

    t.notEqual(since, 0)

    console.log('PULL STREAM SINCE:', since)
    master.createMasterStream({clock: clock})
      .pipe(pull.through(function (data) {
        t.ok(data.ts > since, data.ts + ' > ' + since)
      }))
      .pipe(slave.createSlaveStream(function (err) {
        if(err) throw err
        help.hash(db, function (err, sum) {
          help.hash(_db, function (err, _sum) {
            console.log(sum, _sum)
            t.equal(sum, _sum)
            t.end()
          })
        })
      }))
  })
})


@@ -480,16 +480,121 @@
  'T2')%0A%0A
+process.on('uncaughtException', function (err) %7B%0A  console.error(err.stack %7C%7C err)%0A  process.exit(1)%0A%7D)%0A%0A
 test('se
@@ -1039,21 +1039,37 @@
 ta.ts %3E 
+(
 clock
+%5Bdata.id%5D %7C%7C 0)
 , data.t
@@ -1080,21 +1080,37 @@
 ' %3E ' + 
+(
 clock
+%5Bdata.id%5D %7C%7C 0)
 )%0A      
@@ -1158,32 +1158,53 @@
 unction (err) %7B%0A
+        t.notOk(err)%0A
         //******
@@ -1359,15 +1359,8 @@
 %7D)%0A%0A
-return%0A
 test
@@ -1547,34 +1547,8 @@
  %7B%0A%0A
-    t.notEqual(since, 0)%0A%0A
     
@@ -1581,21 +1581,21 @@
 INCE:', 
-since
+clock
 )%0A    ma
@@ -1700,21 +1700,37 @@
 ta.ts %3E 
-since
+(clock%5Bdata.id%5D %7C%7C 0)
 , data.t
@@ -1741,21 +1741,30 @@
 ' %3E ' + 
-since
+clock%5Bdata.id%5D
 )%0A      

use all tests