

var createPeer = require('../')
//var options = require('./options')
var test = require('tape')

function create (id) {
  var store = {}

  function append (msg, cb) {
    store[msg.author] = store[msg.author] || []
    if(msg.sequence - 1 != store[msg.author].length)
      cb(new Error('out of order'))
    else {
      store[msg.author].push(msg)
      p.onAppend(msg)
      cb(null, msg)
    }
  }

  var p = createPeer({
    id: id,
    getClock: function (id, cb) {
      cb(null, {})
    },
    setClock: function () {},
    getAt: function (pair, cb) {
      if(!store[pair.id] || !store[pair.id][pair.sequence]) cb(new Error('not found'))
      else cb(null, store[pair.id][pair.sequence])
    },
    append: append
  })
  p.store = store
  p.append = append
  return p
}

test('a<->b', function (t) {

  var alice = create('alice')
  var bob = create('bob')

  alice.request('alice', true)
  alice.request('bob', true)
  bob.request('alice', true)
  bob.request('bob', true)

  var as = alice.createStream('bob')
  var bs = bob.createStream('alice')

  console.log('initial.alice:',alice.progress())
  console.log('initial.bob  :',bob.progress())

  as.pipe(bs).pipe(as)


  alice.append({author: 'alice', sequence: 1, content: 'hello'}, function () {})
  bob.append({author: 'bob', sequence: 1, content: 'hello'}, function () {})

  console.log(bob.store)
  console.log(alice.store)

  console.log('final.alice:',alice.progress())
  console.log('final.bob  :',bob.progress())

  t.deepEqual(alice.store, bob.store)
  t.end()

})

test('a<->b,b', function (t) {

  var alice = create('alice')
  var bob = create('bob')

  alice.request('alice', true)
  alice.request('bob', true)
  bob.request('alice', true)
  bob.request('bob', true)

  var as = alice.createStream('bob')
  var bs = bob.createStream('alice')

  console.log('initial.alice:',alice.progress())
  console.log('initial.bob  :',bob.progress())

  as.pipe(bs).pipe(as)

  alice.append({author: 'alice', sequence: 1, content: 'hello'}, function () {})
  bob.append({author: 'bob', sequence: 1, content: 'hello'}, function () {})

  console.log(bob.store)
  console.log(alice.store)

  console.log('final.alice:',alice.progress())
  console.log('final.bob  :',bob.progress())

  t.deepEqual(alice.store, bob.store)

  var bs2 = bob.createStream('alice')
  var as2 = alice.createStream('bob')

  as2.pipe(bs2).pipe(as2)

  bob.append({author: 'bob', sequence: 2, content: 'hello2'}, function () {})

  t.throws(function () {
    as.write({alice: -1, bob: -2})
  })
  t.equal(as.ended, true)
  t.equal(bs.ended, true)

  t.deepEqual(alice.store, bob.store)

  t.end()

})


@@ -1,10 +1,8 @@
-%0A%0A
 var crea
@@ -29,45 +29,8 @@
 /')%0A
-//var options = require('./options')%0A
 var 
@@ -2271,32 +2271,35 @@
 teStream('alice'
+, 2
 )%0A  var as2 = al
@@ -2316,24 +2316,27 @@
 Stream('bob'
+, 2
 )%0A%0A  as2.pip
@@ -2588,24 +2588,1109 @@
 .store)%0A%0A  t.end()%0A%0A%7D)%0A%0A
+%0Atest('a3%3C-%3Eb3%3C-%3Ec2', function (t) %7B%0A%0A  var alice = create('alice')%0A  var bob = create('bob')%0A  var charles = create('charles')%0A%0A  alice.request('alice', true)%0A  alice.request('bob', true)%0A  bob.request('alice', true)%0A  bob.request('bob', true)%0A  charles.request('alice', true)%0A  charles.request('bob', true)%0A%0A  var as3 = alice.createStream('bob', 3)%0A  var bs3 = bob.createStream('alice', 3)%0A  var bs2 = bob.createStream('charles', 2)%0A  var cs2 = charles.createStream('bob', 2)%0A%0A  console.log('initial.alice:',alice.progress())%0A  console.log('initial.bob  :',bob.progress())%0A  console.log('initial.charles  :',charles.progress())%0A%0A  as3.pipe(bs3).pipe(as3)%0A  cs2.pipe(bs2).pipe(cs2)%0A%0A%0A  alice.append(%7Bauthor: 'alice', sequence: 1, content: 'hello'%7D, function () %7B%7D)%0A  bob.append(%7Bauthor: 'bob', sequence: 1, content: 'hello'%7D, function () %7B%7D)%0A%0A  console.log(bob.store)%0A  console.log(alice.store)%0A%0A  console.log('final.alice:',alice.progress())%0A  console.log('final.bob  :',bob.progress())%0A%0A  t.deepEqual(alice.store, bob.store)%0A  t.deepEqual(alice.store, charles.store)%0A  t.end()%0A%0A%7D)%0A%0A

test a peer using both v2 and v3