<commit_before>var Backend = require('../lib/backend');
var expect = require('expect.js');
var types = require('../lib/types');

describe('middleware', function() {

  beforeEach(function() {
    this.backend = new Backend();
  });

  describe('connect', function() {

    it('passes the agent on connect', function() {
      var clientId;
      this.backend.use('connect', function(request, next) {
        clientId = request.agent.clientId;
        next();
      });

      var connection = this.backend.connect();
      expect(connection.id).equal(null);
      connection.on('connected', function() {
        expect(connection.id).equal(clientId);
        done();
      });
    });

    it('passing an error on connect stops the client', function(done) {
      this.backend.use('connect', function(request, next) {
        next({message: 'No good'});
      });

      var connection = this.backend.connect();
      connection.on('stopped', function() {<commit_msg>Add test for  chaining<commit_after>var Backend = require('../lib/backend');
var expect = require('expect.js');
var types = require('../lib/types');

describe('middleware', function() {

  beforeEach(function() {
    this.backend = new Backend();
  });

  describe('use', function() {

    it('returns itself to allow chaining', function() {
      var response = this.backend.use('submit', function(request, next) {});
      expect(response).equal(this.backend);
    });

  });

  describe('connect', function() {

    it('passes the agent on connect', function() {
      var clientId;
      this.backend.use('connect', function(request, next) {
        clientId = request.agent.clientId;
        next();
      });

      var connection = this.backend.connect();
      expect(connection.id).equal(null);
      connection.on('connected', function() {
        expect(connection.id).equal(clientId);
        done();
      });
    });

    it('passing an error on connect stops the client', function(done) {
      this.backend.use('connect', function(request, next) {
        next({message: 'No good'});
      });

      var connection = this.backend.connect();
      connection.on('stopped', function() {
{'fraction_non_alphanumeric': 0.16928044280442806, 'fraction_numerical': 0.0, 'mean_word_length': 3.046641791044776, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5067454', 'n_tokens_mistral': 617, 'n_tokens_neox': 599, 'n_words': 135}