var hat = require('hat')
var portfinder = require('portfinder')
var Swarm = require('../')
var test = require('tape')

var infoHash = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36'
var peerId1 = '-WW0001-' + hat(48)
var peerId2 = '-WW0001-' + hat(48)

test('create swarm, check invariants', function (t) {
  var swarm = new Swarm(infoHash, peerId1)

  t.equal(swarm.infoHash.toString('hex'), infoHash)
  t.equal(swarm.peerId.toString('utf8'), peerId1)
  t.equal(swarm.downloaded, 0)
  t.equal(swarm.uploaded, 0)
  t.ok(Array.isArray(swarm.wires))
  t.equal(swarm.wires.length, 0)
  t.end()
})

test('swarm listen', function (t) {
  t.plan(2)

  var swarm = new Swarm(infoHash, peerId1)
  t.equal(swarm.port, 0, 'port param initialized to 0')
  portfinder.getPort(function (err, port) {
    if (err) throw err
    swarm.listen(port)

    swarm.on('listening', function () {
      t.equal(swarm.port, port, 'listened on requested port ' + port)
      swarm.destroy()
    })
  })
})


@@ -966,12 +966,987 @@
 %7D)%0A  %7D)%0A%7D)%0A%0A
+test('swarm join', function (t) %7B%0A  t.plan(10)%0A%0A  var swarm1 = new Swarm(infoHash, peerId1)%0A  portfinder.getPort(function (err, port) %7B%0A    if (err) throw err%0A    swarm1.listen(port)%0A%0A    swarm1.on('listening', function () %7B%0A      var swarm2 = new Swarm(infoHash, peerId2)%0A%0A      t.equal(swarm1.wires.length, 0)%0A      t.equal(swarm2.wires.length, 0)%0A%0A      swarm1.on('wire', function (wire) %7B%0A        t.ok('Peer join our swarm via listening port')%0A%0A        t.equal(swarm1.wires.length, 1)%0A        t.ok(wire.remoteAddress.indexOf('127.0.0.1:') === 0)%0A        t.equal(wire.peerId.toString('utf8'), peerId2)%0A%0A        swarm1.destroy()%0A      %7D)%0A%0A      swarm2.on('wire', function (wire) %7B%0A        t.ok(wire, 'Joined swarm, got wire')%0A%0A        t.equal(swarm2.wires.length, 1)%0A        t.equal(wire.remoteAddress, '127.0.0.1:8000')%0A        t.equal(wire.peerId.toString('utf8'), peerId1)%0A%0A        swarm2.destroy()%0A      %7D)%0A%0A      swarm2.add('127.0.0.1:' + swarm1.port)%0A    %7D)%0A  %7D)%0A%7D)%0A%0A

swarm join