var SocketPeer = require('../server');
var test = require('tape');


test('connect_attempt event gets emitted', function (t) {

  var peer = new SocketPeer({
    pairCode: 'yolo',
    url: 'http://localhost:3000'
  });

  peer.on('connect_attempt', function () {
    t.pass('got connect_attempt event');
    t.equal(peer.socketConnected, false);
    t.equal(peer._connections.socket.attempt, 1);
    t.equal(peer._connections.socket.success, 0);
    t.equal(peer._connections.socket.error, 0);
    peer.destroy();
    t.end();
  });

});


test('basic lifecycle', function (t) {

  var peer1 = new SocketPeer({
    pairCode: 'yolo',
    url: 'http://localhost:3000',
    reconnect: false,
  });

  var peer2 = new SocketPeer({
    pairCode: 'yolo',
    url: 'http://localhost:3000',
    reconnect: false,
  });

  peer1.on('connect', function () {
    t.equal(peer1.socketConnected, true);
    t.equal(peer1._connections.socket.attempt, 0);
    t.equal(peer1._connections.socket.success, 1);
    t.equal(peer1._connections.socket.error, 0);
    tryConnect();
  });
  peer2.on('connect', function () {
    t.equal(peer2.socketConnected, true);
    t.equal(peer2._connections.socket.attempt, 0);
    t.equal(peer2._connections.socket.success, 1);
    t.equal(peer2._connections.socket.error, 0);
    tryConnect();
  });

  function tryConnect() {
    if (peer1.socketConnected && peer2.socketConnected) {
      t.ok(true, 'connected');

      peer1.send('ping');

      peer2.on('data', function (data) {
        if (data === 'ping') {
          t.ok(true, 'peer2 ping');
          peer2.send('pong');
        }
      });

      peer1.on('data', function (data) {
        if (data === 'pong') {
          t.ok(true, 'peer1 pong');

          peer1.destroy();
          peer2.destroy();

          t.end();
        }
      });
    }
  }

});


test('connect_timeout event gets emitted', function (t) {

  var peer = new SocketPeer({
    pairCode: 'yolo',
    url: 'http://localhost:3000',
    timeout: 500
  });

  peer.on('connect_timeout', function () {
    t.ok(true, 'got connect_timeout event');
    t.equal(peer.socketConnected, false);
  });

  peer.on('connect_error', function () {
    t.ok(true, 'got connect_error event');
  });

  peer.on('error', function () {
    t.ok(true, 'got error event');
    t.equal(peer._connections.socket.attempt, 1, 'connect_timeout.attempt');
    t.equal(peer._connections.socket.success, 0, 'connect_timeout.success');
    t.equal(peer._connections.socket.error, 1, 'connect_timeout.error');
    peer.destroy();
    t.end();
  });

});

@@ -332,24 +332,48 @@
 ected, false
+, 'peer.socketConnected'
 );%0A    t.equ
@@ -410,16 +410,52 @@
 tempt, 1
+, 'peer._connections.socket.attempt'
 );%0A    t
@@ -496,16 +496,52 @@
 ccess, 0
+, 'peer._connections.socket.success'
 );%0A    t
@@ -576,24 +576,58 @@
 ket.error, 0
+, 'peer._connections.socket.error'
 );%0A    peer.
@@ -1002,32 +1002,57 @@
 tConnected, true
+, 'peer1.socketConnected'
 );%0A    t.equal(p
@@ -1078,32 +1078,69 @@
 ocket.attempt, 0
+, 'peer1._connections.socket.attempt'
 );%0A    t.equal(p
@@ -1166,32 +1166,69 @@
 ocket.success, 1
+, 'peer1._connections.socket.success'
 );%0A    t.equal(p
@@ -1252,32 +1252,67 @@
 .socket.error, 0
+, 'peer1._connections.socket.error'
 );%0A    tryConnec
@@ -1397,16 +1397,41 @@
 ed, true
+, 'peer2.socketConnected'
 );%0A    t
@@ -1473,16 +1473,53 @@
 tempt, 0
+, 'peer2._connections.socket.attempt'
 );%0A    t
@@ -1561,16 +1561,53 @@
 ccess, 1
+, 'peer2._connections.socket.success'
 );%0A    t
@@ -1647,16 +1647,51 @@
 error, 0
+, 'peer2._connections.socket.error'
 );%0A    t
@@ -2528,16 +2528,40 @@
 d, false
+, 'peer.socketConnected'
 );%0A  %7D);
@@ -2771,30 +2771,39 @@
 pt, 1, '
+peer._
 connect
-_timeou
+ions.socke
 t.attemp
@@ -2857,30 +2857,39 @@
 ss, 0, '
+peer._
 connect
-_timeou
+ions.socke
 t.succes
@@ -2941,30 +2941,39 @@
 or, 1, '
+peer._
 connect
-_timeou
+ions.socke
 t.error'

add helpful mesages for equal assertions (issue #7)