<commit_before>/* jshint node:true, mocha:true */

'use strict';

var http = require('http').Server;
var io = require('socket.io');
var ioc = require('socket.io-client');
var expect = require('expect.js');
var SocketModel = require('../lib/socket.model');
var Q = require('q');

var sio;
var port;

beforeEach(function (done) {
  var srv = http();
  sio = io(srv);

  srv.listen(function () {
    port = srv.address().port;
    done();
  });
});

function createClient() {
  return ioc('ws://127.0.0.1:' + port);
}

var expectedPost = {
  id: 1234,
  content: 'post content'
};

describe('server', function () {

  describe('model fields', function () {

    it('should have a name', function () {
      var Post = new SocketModel('post', sio);
      expect(Post).to.have.property('name');
    });

    it('should have collection as an empty array', function () {
      var Post = new SocketModel('post', sio);
      expect(Post).to.have.property('collection');
      expect(Post.collection).to.be.an('array').and.to.have.length(0);
    });

    it('should have mode', function () {
      var Post = new SocketModel('post', sio);
      expect(Post).to.have.property('mode');
      expect(Post.mode).to.be('server');
    });

  });

  describe('connect', function () {

    it('should trigger connect event when a client connects', function (done) {
      var Post = new SocketModel('post', sio);
      Post.on('connect', function () {
        done();
      });

      createClient();
    });

    it('should trigger connect event on the connected client', function (done) {
      var client = createClient();
      client.on('connect', function () {

        // new socket model after client is connected
        var Post = new SocketModel('post', sio);
        Post.on('connect', function () {
          done();
        });
      });
    });

    it('should trigger client connect event when response', function (done) {
      var expectedPosts = ['p1', 'p2', 'p3'];

      var Post = new SocketModel('post', sio);
      Post.on('connect', function (res) {
        res(expectedPosts);
      });

      var client = createClient();
      client.on('post:connect', function (actualPosts) {
        expect(actualPosts).to.eql(expectedPosts);
        done();
      });
    });

  });

  describe('create', function () {

    it('should not trigger any event handler when client connect', function (done) {
      var Post = new SocketModel('post', sio);
      Post.on('create', function () {
        expect().fail('');
      });

      var client = createClient();
      client.on('connect', function () {
        setTimeout(done, 500);
      });
    });

    it('should trigger create handler when client emit event', function (done) {
      var Post = new SocketModel('post', sio);
      Post.on('create', function () {
        done();
      });

      var client = createClient();
      client.emit('post:create');
    });

    it('should register event handler on connected sockets', function (done) {<commit_msg>Avoid the timer in unit test.<commit_after>/* jshint node:true, mocha:true */

'use strict';

var http = require('http').Server;
var io = require('socket.io');
var ioc = require('socket.io-client');
var expect = require('expect.js');
var SocketModel = require('../lib/socket.model');
var Q = require('q');

var sio;
var port;

beforeEach(function (done) {
  var srv = http();
  sio = io(srv);

  srv.listen(function () {
    port = srv.address().port;
    done();
  });
});

function createClient() {
  return ioc('ws://127.0.0.1:' + port);
}

var expectedPost = {
  id: 1234,
  content: 'post content'
};

describe('server', function () {

  describe('model fields', function () {

    it('should have a name', function () {
      var Post = new SocketModel('post', sio);
      expect(Post).to.have.property('name');
    });

    it('should have collection as an empty array', function () {
      var Post = new SocketModel('post', sio);
      expect(Post).to.have.property('collection');
      expect(Post.collection).to.be.an('array').and.to.have.length(0);
    });

    it('should have mode', function () {
      var Post = new SocketModel('post', sio);
      expect(Post).to.have.property('mode');
      expect(Post.mode).to.be('server');
    });

  });

  describe('connect', function () {

    it('should trigger connect event when a client connects', function (done) {
      var Post = new SocketModel('post', sio);
      Post.on('connect', function () {
        done();
      });

      createClient();
    });

    it('should trigger connect event on the connected client', function (done) {
      var client = createClient();
      client.on('connect', function () {

        // new socket model after client is connected
        var Post = new SocketModel('post', sio);
        Post.on('connect', function () {
          done();
        });
      });
    });

    it('should trigger client connect event when response', function (done) {
      var expectedPosts = ['p1', 'p2', 'p3'];

      var Post = new SocketModel('post', sio);
      Post.on('connect', function (res) {
        res(expectedPosts);
      });

      var client = createClient();
      client.on('post:connect', function (actualPosts) {
        expect(actualPosts).to.eql(expectedPosts);
        done();
      });
    });

  });

  describe('create', function () {

    it('should not trigger any event handler when client connect', function (done) {
      var Post = new SocketModel('post', sio);

      Post.on('create', function () {
        expect().fail('');
      });

      Post.on('edit', function () {
        done();
      });

      var client = createClient();
      client.on('connect', function () {
        client.emit('post:edit');
      });
    });

    it('should trigger create handler when client emit event', function (done) {
      var Post = new SocketModel('post', sio);
      Post.on('create', function () {
        done();
      });

      var client = createClient();
      client.emit('post:create');
    });

    it('should register event handler on connected sockets', function (done) {
{'fraction_non_alphanumeric': 0.1718775577017515, 'fraction_numerical': 0.0050744802750040926, 'mean_word_length': 2.7438725490196076, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '471841', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1777, 'n_words': 475}