var test = require('tape')

var _ = require('icebreaker')
require('icebreaker-peer-net')
require('./')

var localPeers = {}
var remotePeers = {}
var connections = []

test('start peers', function (t) {
  t.plan(30)

  for (var i = 0; i < 10; ++i) {
    var peer = _.peers.net({
      port: '568' + i,
      name: 'net'
    })

    peer.once('started', function (port, i) {
      return function () {
        t.equals(this.name, 'net')
        i % 2 ? localPeers[this.name + i] = this :
        remotePeers[this.name + i] = this
        t.equals(this.port, port)
        t.ok(true, 'peer net' + i + ' started')
      }
    }('568' + i, i))

    peer.on('connection', function (c) {
      connections.push(c)
    })

    peer.once('stop', function () {
      return function () {
        delete localPeers[this.name + i]
        delete remotePeers[this.name + i]
      }
    }(i))

    peer.start()
  }
})

var agent
var localAgent
var remoteAgent

test('start agents', function (t) {
  t.plan(2)

  agent = _.agent({
    name: 'test-agent',
    start: function () {
      if (this.name === 'remote') _(_.values(localPeers), this.connect())
      else _(_.values(remotePeers), this.connect())
      this.emit('started')
    },
    stop: function () {
      clearTimeout(this.timer)
      this.emit('stopped')
    }
  })

  localAgent = agent({
    peers: localPeers,
    name: 'local'
  })

  remoteAgent = agent({
    peers: remotePeers,
    name: 'remote'
  })

  localAgent.once('started', t.ok.bind(null, true, 'local agent started'))
  localAgent.start()

  remoteAgent.once('started', t.ok.bind(null, true, 'remote agent started'))
  remoteAgent.start()
})

test('connections', function next(t) {
  t.plan(11)
  t.equal(connections.length, 20)
  for (var i in connections) {
    var connection = connections[i]
    if (connection.direction === 1) {
      _('test1', connection, _.drain(function (item) {
        t.equal(item.toString(), 'echotest1')
      }))
    }
    else _(
      connection,
      _.map(function (m) {
        return 'echo' + m.toString()
      }),
      connection
    )
  }

})

test('stop peers', function (t) {
  t.plan(10)
  for (var i in localPeers) {
    var peer = localPeers[i]
    peer.once('stopped', t.ok.bind(null, true, 'local peer ' + i + ' stopped'))
    peer.stop()
  }
  for (var i in remotePeers) {
    var peer = remotePeers[i]
    peer.once('stopped', t.ok.bind(null, true, 'remote peer ' + i + ' stopped'))
    peer.stop()
  }
})

test('stop agents', function (t) {
  t.plan(2)
  localAgent.once('stopped', t.ok.bind(null, true, 'local agent stopped'))
  localAgent.stop()
  remoteAgent.once('stopped', t.ok.bind(null, true, 'remote agent stopped'))
  remoteAgent.stop()
})

@@ -20,17 +20,16 @@
 'tape')%0A
-%0A
 var _ = 
@@ -54,39 +54,18 @@
 r')%0A
-require('icebreaker-peer-net')%0A
+var Agent=
 requ
@@ -78,70 +78,17 @@
 /')%0A
-%0A
 var 
-localPeers = %7B%7D%0Avar remotePeers = %7B%7D%0Avar connections = %5B%5D
+agent
 %0A%0Ate
@@ -97,20 +97,21 @@
 ('start 
-peer
+agent
 s', func
@@ -134,987 +134,88 @@
 lan(
-30)%0A%0A  for (var i = 0; i %3C 10; ++i) %7B%0A    var peer = _.peers.net(%7B%0A      port: '568' + i,%0A      name: 'net'%0A    %7D)%0A%0A    peer.once('started', function (port, i) %7B%0A      return function () %7B%0A        t.equals(this.name, 'net')%0A        i %25 2 ? localPeers%5Bthis.name + i%5D = this :%0A        remotePeers%5Bthis.name + i%5D = this%0A        t.equals(this.port, port)%0A        t.ok(true, 'peer net' + i + ' started')%0A      %7D%0A    %7D('568' + i, i))%0A%0A    peer.on('connection', function (c) %7B%0A      connections.push(c)%0A    %7D)%0A%0A    peer.once('stop', function () %7B%0A      return function () %7B%0A        delete localPeers%5Bthis.name + i%5D%0A        delete remotePeers%5Bthis.name + i%5D%0A      %7D%0A    %7D(i))%0A%0A    peer.start()%0A  %7D%0A%7D)%0A%0Avar agent%0Avar localAgent%0Avar remoteAgent%0A%0Atest('start agents', function (t) %7B%0A  t.plan(2)%0A%0A  agent = _.agent(%7B%0A    name: 'test-agent',%0A    start: function () %7B%0A      if (this.name === 'remote') _(_.values(localPeers), this.connect())%0A      else _(_.values(remotePeers), this.connect())
+1)%0A%0A  agent = Agent.bind(null,%7B%0A    name: 'test-agent',%0A    start: function () %7B
 %0A   
@@ -273,39 +273,8 @@
 ) %7B%0A
-      clearTimeout(this.timer)%0A
     
@@ -310,161 +310,38 @@
   %7D)
-%0A%0A  localAgent = agent(%7B%0A    peers: localPeers,%0A    name: 'local'%0A  %7D)%0A%0A  remoteAgent = agent(%7B%0A    peers: remotePeers,%0A    name: 'remote'%0A  %7D)%0A%0A  localA
+(%7B%0A    name: 'local'%0A  %7D)%0A%0A  a
 gent
@@ -372,38 +372,32 @@
 nd(null, true, '
-local 
 agent started'))
@@ -403,590 +403,42 @@
 )%0A  
-localAgent.start()%0A%0A  remoteAgent.once('started', t.ok.bind(null, true, 'remote agent started'))%0A  remoteAgent.start()%0A%7D)%0A%0Atest('connections', function next(t) %7B%0A  t.plan(11)%0A  t.equal(connections.length, 20)%0A  for (var i in connections) %7B%0A    var connection = connections%5Bi%5D%0A    if (connection.direction === 1) %7B%0A      _('test1', connection, _.drain(function (item) %7B%0A        t.equal(item.toString(), 'echotest1')%0A      %7D))%0A    %7D%0A    else _(%0A      connection,%0A      _.map(function (m) %7B%0A        return 'echo' + m.toString()%0A      %7D),%0A      connection%0A    )%0A  %7D%0A%0A%7D)%0A%0Atest('stop peer
+agent.start()%0A%7D)%0A%0Atest('stop agent
 s', 
@@ -466,391 +466,13 @@
 an(1
-0
 )%0A  
-for (var i in localPeers) %7B%0A    var peer = localPeers%5Bi%5D%0A    peer.once('stopped', t.ok.bind(null, true, 'local peer ' + i + ' stopped'))%0A    peer.stop()%0A  %7D%0A  for (var i in remotePeers) %7B%0A    var peer = remotePeers%5Bi%5D%0A    peer.once('stopped', t.ok.bind(null, true, 'remote peer ' + i + ' stopped'))%0A    peer.stop()%0A  %7D%0A%7D)%0A%0Atest('stop agents', function (t) %7B%0A  t.plan(2)%0A  localA
+a
 gent
@@ -515,14 +515,8 @@
 e, '
-local 
 agen
@@ -534,112 +534,9 @@
 )%0A  
-localAgent.stop()%0A  remoteAgent.once('stopped', t.ok.bind(null, true, 'remote agent stopped'))%0A  remoteA
+a
 gent

Update test cases