<commit_before>var expect = require('chai').expect;
var Connection = require('../lib/connection');
var FakeSock = require('./lib/fake_sock');

function App() {this.connections = []; this.messages = {welcome:''};}
App.prototype.log = function() {};

describe('Connection', function() {
  describe('constructor', function() {
    it('should have this.ip and this.port equal to sock.address()', function() {
      var sock = new FakeSock();
      var conn = new Connection(null, sock);
      expect(conn.ip).to.equal('127.0.0.1');
      expect(conn.port).to.equal(54321);
    });
  });

  describe('#init', function() {
    it('sets a timeout to check if the connection is alive', function() {
      var sock = new FakeSock();
      var conn = new Connection(new App(), sock);
      conn.init();
      expect(conn.timeoutInt).to.not.be.null;
      expect(conn.timeoutInt).to.not.be.undefined;
      conn.deinit();
    });

    it('sets an event handler for sock close', function() {
      var sock = new FakeSock();
      var conn = new Connection(new App(), sock);
      conn.init();
      expect(sock.evs).to.include.keys('close');
      expect(sock.evs.close).to.be.a('function');
      conn.deinit();
    });

    it('sets an event handler for sock error', function() {
      var sock = new FakeSock();
      var conn = new Connection(new App(), sock);
      conn.init();
      expect(sock.evs).to.include.keys('error');
      expect(sock.evs.error).to.be.a('function');
      conn.deinit();
    });
  });

  describe('#deinit', function() {
    it('removes itself from the app connections list', function() {
      var app = new App();
      var sock = new FakeSock();
      var conn = new Connection(app, sock);
      app.connections.push(conn);
      conn.init();
      conn.deinit();
      expect(app.connections).to.be.empty;
    });

    it('ends the socket', function() {
      var app = new App();
      var sock = new FakeSock();
      var conn = new Connection(app, sock);
      app.connections.push(conn);
      conn.init();
      conn.deinit();
      expect(sock.ended).to.be.true;
    });

    it('destroys the socket', function() {
      var app = new App();
      var sock = new FakeSock();
      var conn = new Connection(app, sock);
      app.connections.push(conn);
      conn.init();
      conn.deinit();
      expect(sock.destroyed).to.be.true;
    });
  });

  describe('#print', function() {
    it('prints a line to the sock', function() {
      var app = new App();
      var sock = new FakeSock();
      var conn = new Connection(app, sock);
      app.connections.push(conn);
      conn.init();
      conn.print('Hello world');
      conn.deinit();
      expect(sock.str).to.be.equal('Hello world');
    });

    it('converts LF newlines to CRLF', function() {
      var app = new App();
      var sock = new FakeSock();
      var conn = new Connection(app, sock);
      app.connections.push(conn);
      conn.init();
      conn.print('Hello\nworld\n');
      conn.deinit();
      expect(sock.str).to.be.equal('Hello\r\nworld\r\n');
    });
  });

  describe('#puts', function() {
    it('appends a newline to output', function() {
      var app = new App();
      var sock = new FakeSock();
      var conn = new Connection(app, sock);
      app.connections.push(conn);
      conn.init();
      conn.puts('Hello');
      conn.deinit();
      expect(sock.str).to.be.equal('Hello\r\n');
    });
  });
});
<commit_msg>test for Connection#gets<commit_after>var expect = require('chai').expect;
var Connection = require('../lib/connection');
var FakeSock = require('./lib/fake_sock');

function App() {this.connections = []; this.messages = {welcome:''};}
App.prototype.log = function() {};

describe('Connection', function() {
  describe('constructor', function() {
    it('should have this.ip and this.port equal to sock.address()', function() {
      var sock = new FakeSock();
      var conn = new Connection(null, sock);
      expect(conn.ip).to.equal('127.0.0.1');
      expect(conn.port).to.equal(54321);
    });
  });

  describe('#init', function() {
    it('sets a timeout to check if the connection is alive', function() {
      var sock = new FakeSock();
      var conn = new Connection(new App(), sock);
      conn.init();
      expect(conn.timeoutInt).to.not.be.null;
      expect(conn.timeoutInt).to.not.be.undefined;
      conn.deinit();
    });

    it('sets an event handler for sock close', function() {
      var sock = new FakeSock();
      var conn = new Connection(new App(), sock);
      conn.init();
      expect(sock.evs).to.include.keys('close');
      expect(sock.evs.close).to.be.a('function');
      conn.deinit();
    });

    it('sets an event handler for sock error', function() {
      var sock = new FakeSock();
      var conn = new Connection(new App(), sock);
      conn.init();
      expect(sock.evs).to.include.keys('error');
      expect(sock.evs.error).to.be.a('function');
      conn.deinit();
    });
  });

  describe('#deinit', function() {
    it('removes itself from the app connections list', function() {
      var app = new App();
      var sock = new FakeSock();
      var conn = new Connection(app, sock);
      app.connections.push(conn);
      conn.init();
      conn.deinit();
      expect(app.connections).to.be.empty;
    });

    it('ends the socket', function() {
      var app = new App();
      var sock = new FakeSock();
      var conn = new Connection(app, sock);
      app.connections.push(conn);
      conn.init();
      conn.deinit();
      expect(sock.ended).to.be.true;
    });

    it('destroys the socket', function() {
      var app = new App();
      var sock = new FakeSock();
      var conn = new Connection(app, sock);
      app.connections.push(conn);
      conn.init();
      conn.deinit();
      expect(sock.destroyed).to.be.true;
    });
  });

  describe('#print', function() {
    it('prints a line to the sock', function() {
      var app = new App();
      var sock = new FakeSock();
      var conn = new Connection(app, sock);
      app.connections.push(conn);
      conn.init();
      conn.print('Hello world');
      conn.deinit();
      expect(sock.str).to.be.equal('Hello world');
    });

    it('converts LF newlines to CRLF', function() {
      var app = new App();
      var sock = new FakeSock();
      var conn = new Connection(app, sock);
      app.connections.push(conn);
      conn.init();
      conn.print('Hello\nworld\n');
      conn.deinit();
      expect(sock.str).to.be.equal('Hello\r\nworld\r\n');
    });
  });

  describe('#puts', function() {
    it('appends a newline to output', function() {
      var app = new App();
      var sock = new FakeSock();
      var conn = new Connection(app, sock);
      app.connections.push(conn);
      conn.init();
      conn.puts('Hello');
      conn.deinit();
      expect(sock.str).to.be.equal('Hello\r\n');
    });
  });

  describe('#gets', function() {
    it('can read a line of text from the sock', function(done) {
      var app = new App();
      var sock = new FakeSock();
      var conn = new Connection(app, sock);
      app.connections.push(conn);
      conn.init();
      process.nextTick(function() {
        if(sock.evs.data === undefined) conn.deinit();
        expect(sock.evs.data).to.not.be.undefined;        
        sock.evs.data(new Buffer('Hello world\r\n'));
      });
      conn.gets(function(err, str) {
        expect(err).to.be.null;
        expect(str).to.equal('Hello world\r\n');
        conn.deinit();
        done();
      });
    });
  });
});

{'fraction_non_alphanumeric': 0.1765641569459173, 'fraction_numerical': 0.0029162248144220574, 'mean_word_length': 2.5522598870056497, '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': '465906', 'n_tokens_mistral': 2406, 'n_tokens_neox': 2330, 'n_words': 562}