
var test = require('tape')

var events = require('../events')


test('if connects to multiple peers, should replicate a feed from only one', function (t) {

  var state = events.initialize('alice', Date.now())
  state.follows = {
    alice: true, bob: true, charles: true
  }
  state = events.clock(state, {alice: 3, bob: 2, charles: 5})
  
  state = events.connect(state, {id: 'bob'})
  state = events.peerClock(state, {id: 'bob', value: {alice: 2, bob: 2, charles: 5}})
  state = events.connect(state, {id: 'charles', value: {alice: 2, bob: 2, charles: 5}})
  state = events.peerClock(state, {id: 'charles', value: {alice: 2, bob: 2, charles: 5}})

  console.log(state.peers)

  state = events.notes(state, {id: 'bob', value: {
    alice: 3, bob: 5, charles: 9
  }})
  state = events.notes(state, {id: 'charles', value: {
    alice: 3, bob: 4, charles: 9
  }})
  console.log(state.peers)

  //we should only request transmissions from at most one peer.


  var notes = {}
  for(var peer_id in state.peers) {
    var peer = state.peers[peer_id]
    for(var feed_id in peer.notes) {
      t.equal(peer.notes[feed_id] >= 0, peer.replicating[feed_id].rx)
      if(peer.notes[feed_id] >= 0) {
        notes[feed_id] = (notes[feed_id] || 0) + 1
      }
    }
  }

  console.log(JSON.stringify(state.peers, null, 2))

//  t.deepEqual(notes, {alice: 1, bob: 1, charles: 1})

  t.end()
})





@@ -1117,16 +1117,118 @@
 %5D %3E= 0, 
+peer.replicating%5Bfeed_id%5D.rx, 'implied rx state should be recorded, seq:'+peer.notes%5Bfeed_id%5D+', rx='+
 peer.rep
@@ -1249,16 +1249,16 @@
 id%5D.rx)%0A
-
       if
@@ -1409,18 +1409,16 @@
 l, 2))%0A%0A
-//
   t.deep
@@ -1462,16 +1462,16 @@
 es: 1%7D)%0A
+
 %0A  t.end
@@ -1477,11 +1477,8 @@
 d()%0A
+
 %7D)%0A%0A
-%0A%0A%0A

test that each feed is replicated from only one peer