var test = require('tape');
var quickconnect = require('..');
var connections = [];
var dcs = [];
var room = require('uuid').v4();
// var freeice = require('freeice');
// var signallingServer = 'http://rtc.io/switchboard/';
var signallingServer = location.origin;

// require('cog/logger').enable('*');

test('create connector 0', function(t) {
  t.plan(3);
  t.ok(connections[0] = quickconnect(signallingServer, {
    room: room
  }), 'created');

  t.equal(typeof connections[0].createDataChannel, 'function', 'has a createDataChannel function');

  // create the data channel
  connections[0].createDataChannel('test');
  setTimeout(t.pass.bind(t, 'dc created'), 500);
});

test('create connector 1', function(t) {
  t.plan(3);
  t.ok(connections[1] = quickconnect(signallingServer, {
    room: room
  }), 'created');

  t.equal(typeof connections[1].createDataChannel, 'function', 'has a createDataChannel function');

  // create the data channel
  connections[1].createDataChannel('test');
  setTimeout(t.pass.bind(t, 'dc created'), 500);
});

test('check call active', function(t) {
  t.plan(connections.length * 3);

  connections.forEach(function(conn, index) {
    conn.waitForCall(connections[index ^ 1].id, function(err, pc) {
      t.ifError(err, 'call available');
      t.ok(pc, 'have peer connection');
      t.equal(pc.iceConnectionState, 'connected', 'call connected');
    });
  });
});

test('data channels opened', function(t) {
  t.plan(4);
  connections[0].requestChannel(connections[1].id, 'test', function(err, dc) {
    t.ifError(err);
    dcs[0] = dc;
    t.equal(dc.readyState, 'open', 'connection test dc 0 open');
  });

  connections[1].requestChannel(connections[0].id, 'test', function(err, dc) {
    t.ifError(err);
    dcs[1] = dc;
    t.equal(dc.readyState, 'open', 'connection test dc 1 open');
  });
});

test('dc 0 send', function(t) {
  dcs[1].onmessage = function(evt) {
    t.equal(evt.data, 'hi', 'dc:1 received hi');
    dcs[1].onmessage = null;
  };

  t.plan(1);
  dcs[0].send('hi');
});

test('dc 1 send', function(t) {
  dcs[0].onmessage = function(evt) {
    t.equal(evt.data, 'hi', 'dc:1 received hi');
    dcs[0].onmessage = null;
  };

  t.plan(1);
  dcs[1].send('hi');
});

test('release references', function(t) {
  t.plan(1);
  connections.splice(0).forEach(function(conn, index) {
    conn.close();
  });

  dcs = [];
  t.pass('done');
});
@@ -1316,29 +1316,25 @@
 ');%0A
+%0A
       
-t.equal(pc.iceC
+// check c
 onne
@@ -1342,26 +1342,94 @@
 tion
-S
+ s
 tate
-, 'connected'
+ valid%0A      t.ok(%5B'connected', 'completed'%5D.indexOf(pc.iceConnectionState) %3E= 0
 , 'c
@@ -2294,79 +2294,338 @@
 st('
-release references', function(t) %7B%0A  t.plan(1);%0A  connections.splice(0)
+close connection 0 and wait for dc close notifications', function(t) %7B%0A  var timer = setTimeout(t.fail.bind(t, 'timed out'), 10000);%0A  var closedCount = 0;%0A%0A  function handleClose() %7B%0A    t.pass('channel closed');%0A    closedCount += 1;%0A%0A    if (closedCount === 2) %7B%0A      clearTimeout(timer);%0A    %7D%0A  %7D%0A%0A  t.plan(2);%0A  connections
 .for
@@ -2637,35 +2637,33 @@
 function(conn, i
-nde
+d
 x) %7B%0A    conn.cl
@@ -2664,23 +2664,188 @@
 onn.
-close();%0A  %7D);%0A
+once('channel:closed:test', handleClose);%0A  %7D);%0A%0A  connections%5B0%5D.close();%0A%7D);%0A%0Atest('release references', function(t) %7B%0A  t.plan(1);%0A%0A  connections%5B1%5D.close();%0A  connections = %5B%5D;
 %0A  d
@@ -2853,16 +2853,17 @@
 s = %5B%5D;%0A
+%0A
   t.pass
@@ -2871,12 +2871,13 @@
 'done');%0A%7D);
+%0A

Add closing event test replicating #17