var app = require('../src/app');
var http = require('http');
var should = require('should');
var Primus = require('primus');
var http = require('http');

var config = {
  host: 'localhost',
  port: 54321,
  auth: 'thisisauth'
};

describe('Client part', function() {
  it('Should connect to the specified server', function(done) {
    var server = http.createServer();
    var wss = new Primus(server,
      { transformer: 'engine.io' }
    );
    server.listen(config.port, function() {
      app.start(config);
    });
    wss.on('connection', function(ws) {
      ws.on('auth', function(message) {
        if (message === config.auth) {
          ws.send('authed', 'whatevs');
          wss.end();
          done();
          server.close();
        }
      });
    });
  });
  var s;
  it('Should call callback when sent data', function(done) {
    var server = http.createServer();
    var wss2 = new Primus(server,
      { transformer: 'engine.io' }
    );
    config.port = config.port + 1;
    server.listen(config.port, function() {
      app.start(config);
    });
    config.interval = 10000;
    config.repeat = function() {
      if (config.i) {
        clearInterval(config.i);
      }
    };
    wss2.on('connection', function(ws) {
      s = wss2;
      ws.on('auth', function(message) {
        if (message === config.auth) {
          ws.send('authed', 'whatevs');
          ws.write('world2');
        }
      });
    });
    config.callback = function(err, data) {
      should(err).equal(null);
      data.should.equal('world2');
      done(err);
    };
  });

  it('Should call callback with error', function(done) {
    s.end();
    var server = http.createServer();
    var wss3 = new Primus(server,
      { transformer: 'engine.io' }
    );
    config.port = config.port + 1;
    wss3.on('connection', function(ws) {
    });
    config.callback = function(err, data) {
      should(err).not.equal(null);
      done();
    };
    config.ws = function(webss) {
      webss.emit('error');
    };
    server.listen(config.port, function() {
      app.start(config);
    });
  });

  it('Should have an app.log function exposed', function() {
    // Just see if it is there.
    app.log.should.be.type('function');
    // Just call it, for coverage.
    app.log('Testing log function.');
  });
});

@@ -1328,32 +1328,62 @@
  config.auth) %7B%0A
+          ws.write('world2');%0A
           ws.sen
@@ -1418,42 +1418,20 @@
     
-  ws.write('world2');
+%7D
 %0A      
-  %7D%0A  
+%7D);%0A
     
@@ -1434,26 +1434,40 @@
     %7D);%0A    
-%7D)
+var recv = false
 ;%0A    config
@@ -1492,32 +1492,93 @@
 on(err, data) %7B%0A
+      if (recv) %7B%0A        return;%0A      %7D%0A      recv = true;%0A
       should(err

Fix tests.