<commit_before>var assert = require('chai').assert;
var nodemock = require('nodemock');
var Deferred = require('jsdeferred').Deferred;
var express = require('express');

var utils = require('./test-utils');

var adaptor = require('../index');
var Connection = require('../lib/backend/connection').Connection;

suite('Adaption for express application', function() {
  suite('REST API registeration', function() {
    function defineCommand(command, path) {
      return {
        method: 'GET',
        path: path,
        requestBuilder: function() { return command + ' requested'; },
        responseBuilder: function() { return command + ' OK'; }
      };
    }
    var testPlugin = {
      api: defineCommand('api', '/path/to/api')
    };

    var connection;
    var application;
    var server;

    setup(function(done) {
      connection = utils.createMockedBackendConnection();
      application = express();
      utils.setupServer(application)
        .next(function(newServer) {
          server = newServer;
          done();
        });
    });

    teardown(function() {
      if (connection) {
        utils.readyToDestroyMockedConnection(connection);
        connection = undefined;
      }
      if (server) {
        server.close();
        server = undefined;
      }
    });

    test('to the document root', function(done) {
      var onReceive = {};
      connection
        .mock('emitMessage')
          .takes('api', 'api requested', function() {}, { 'timeout': null })
          .ctrl(2, onReceive);

      application.kotoumi({
        prefix:     '',
        connection: connection,
        plugins:    [testPlugin]
      });

      var responseBody;
      Deferred
        .wait(0.01)
        .next(function() {
          utils.get('/path/to/api')
            .next(function(response) {
              responseBody = response.body;
            });
        })
        .wait(0.01)
        .next(function() {
          onReceive.trigger(null, { body: 'API OK?' });
        })
        .wait(0.01)
        .next(function() {
          assert.equal(responseBody, 'api OK');
          done();
        })
        .error(function(error) {
          done(error);
        });
    });

    test('under specified path', function(done) {
      var onReceive = {};
      connection
        .mock('emitMessage')
          .takes('api', 'api requested', function() {}, { 'timeout': null })
          .ctrl(2, onReceive);

      application.kotoumi({
        prefix:     '/path/to/kotoumi',
        connection: connection,
        plugins:    [testPlugin]
      });

      var responseBody;
      Deferred
        .wait(0.01)
        .next(function() {
          utils.get('/path/to/kotoumi/path/to/api')
            .next(function(response) {
              responseBody = response.body;
            });
        })
        .wait(0.01)
        .next(function() {
          onReceive.trigger(null, { body: 'API OK?' });
        })
        .wait(0.01)
        .next(function() {
          assert.equal(responseBody, 'api OK');
          done();
        })
        .error(function(error) {
          done(error);
        });
    });
  });

  suite('Socket.IO API', function() {
    var connection;
    var server;
    var clientSocket;

    teardown(function() {
      if (connection) {
        utils.readyToDestroyMockedConnection(connection);
        connection = undefined;
      }
      if (clientSocket) {
        clientSocket.disconnect();
        clientSocket = undefined;
      }
      if (server) {
        server.close();
        server = undefined;
      }
    });

    test('front to back', function(done) {
      var handlersFactory = utils.createMockedHandlersFactory();
      connection = utils.createMockedBackendConnection()
        .mock('emitMessage')
          .takes('search', { requestMessage: true });

      var application = express();
      utils.setupServer(application)
        .next(function(newServer) {
          server = newServer;
          application.kotoumi({
            connection: connection,
            server:     server,
            handlers:   handlersFactory
          });
          handlersFactory.assertThrows();

          return utils.createClientSocket();
        })
        .next(function(newClientSocket) {
          clientSocket = newClientSocket;
          clientSocket.emit('search', { requestMessage: true });
        })
        .wait(0.01)
        .next(function() {
          connection.assertThrows();
          done();
        })
        .error(function(error) {
          done(error);
        });
    });

    test('back to front', function(done) {
      var handlersFactory = utils.createMockedHandlersFactory();
      connection = utils.createMockedBackendConnection();
      connection.emitMessage = function() {}; // stubbing

      var clientReceiver = nodemock
            .mock('receive')
            .takes({
              statusCode: 200,
              body:       { searchResult: true }
            });

      var application = express();
      utils.setupServer(application)
        .next(function(newServer) {
          server = newServer;
          application.kotoumi({
            connection: connection,
            server:     server,
            handlers:   handlersFactory
          });
          handlersFactory.assertThrows();

          return utils.createClientSocket();
        })
        .next(function(newClientSocket) {
          clientSocket = newClientSocket;

          connection.assertThrows();

          clientSocket.on('search.result', function(data) {
            clientReceiver.receive(data);
          });

          var envelope = {
            type:       'search.result',
            statusCode: 200,
            body:       { searchResult: true}
          };
          connection.controllers.search.trigger(envelope);
        })
        .wait(0.01)
        .next(function() {
          clientReceiver.assertThrows();
          done();
        })
        .error(function(error) {
          done(error);
        });
    });<commit_msg>test: Update tests around socket.io API registeration by app.kotoumi()<commit_after>var assert = require('chai').assert;
var nodemock = require('nodemock');
var Deferred = require('jsdeferred').Deferred;
var express = require('express');

var utils = require('./test-utils');

var adaptor = require('../index');
var Connection = require('../lib/backend/connection').Connection;

suite('Adaption for express application', function() {
  suite('REST API registeration', function() {
    function defineCommand(command, path) {
      return {
        method: 'GET',
        path: path,
        requestBuilder: function() { return command + ' requested'; },
        responseBuilder: function() { return command + ' OK'; }
      };
    }
    var testPlugin = {
      api: defineCommand('api', '/path/to/api')
    };

    var connection;
    var application;
    var server;

    setup(function(done) {
      connection = utils.createMockedBackendConnection();
      application = express();
      utils.setupServer(application)
        .next(function(newServer) {
          server = newServer;
          done();
        });
    });

    teardown(function() {
      if (connection) {
        utils.readyToDestroyMockedConnection(connection);
        connection = undefined;
      }
      if (server) {
        server.close();
        server = undefined;
      }
    });

    test('to the document root', function(done) {
      var onReceive = {};
      connection
        .mock('emitMessage')
          .takes('api', 'api requested', function() {}, { 'timeout': null })
          .ctrl(2, onReceive);

      application.kotoumi({
        prefix:     '',
        connection: connection,
        plugins:    [testPlugin]
      });

      var responseBody;
      Deferred
        .wait(0.01)
        .next(function() {
          utils.get('/path/to/api')
            .next(function(response) {
              responseBody = response.body;
            });
        })
        .wait(0.01)
        .next(function() {
          onReceive.trigger(null, { body: 'API OK?' });
        })
        .wait(0.01)
        .next(function() {
          assert.equal(responseBody, 'api OK');
          done();
        })
        .error(function(error) {
          done(error);
        });
    });

    test('under specified path', function(done) {
      var onReceive = {};
      connection
        .mock('emitMessage')
          .takes('api', 'api requested', function() {}, { 'timeout': null })
          .ctrl(2, onReceive);

      application.kotoumi({
        prefix:     '/path/to/kotoumi',
        connection: connection,
        plugins:    [testPlugin]
      });

      var responseBody;
      Deferred
        .wait(0.01)
        .next(function() {
          utils.get('/path/to/kotoumi/path/to/api')
            .next(function(response) {
              responseBody = response.body;
            });
        })
        .wait(0.01)
        .next(function() {
          onReceive.trigger(null, { body: 'API OK?' });
        })
        .wait(0.01)
        .next(function() {
          assert.equal(responseBody, 'api OK');
          done();
        })
        .error(function(error) {
          done(error);
        });
    });
  });

  suite('Socket.IO API registeration', function() {
    var connection;
    var server;
    var clientSocket;

    teardown(function() {
      if (connection) {
        utils.readyToDestroyMockedConnection(connection);
        connection = undefined;
      }
      if (clientSocket) {
        clientSocket.disconnect();
        clientSocket = undefined;
      }
      if (server) {
        server.close();
        server = undefined;
      }
    });

    test('front to back', function(done) {
      connection = utils.createMockedBackendConnection()
        .mock('emitMessage')
          .takes('search', { requestMessage: true });

      var application = express();
      utils.setupServer(application)
        .next(function(newServer) {
          server = newServer;
          application.kotoumi({
            connection: connection,
            server:     server
          });

          return utils.createClientSocket();
        })
        .next(function(newClientSocket) {
          clientSocket = newClientSocket;
          clientSocket.emit('search', { requestMessage: true });
        })
        .wait(0.01)
        .next(function() {
          connection.assertThrows();
          done();
        })
        .error(function(error) {
          done(error);
        });
    });

    test('back to front', function(done) {
      connection = utils.createMockedBackendConnection();
      connection.emitMessage = function() {}; // stubbing

      var clientReceiver = nodemock
            .mock('receive')
            .takes({
              statusCode: 200,
              body:       { searchResult: true }
            });

      var application = express();
      utils.setupServer(application)
        .next(function(newServer) {
          server = newServer;
          application.kotoumi({
            connection: connection,
            server:     server
          });

          return utils.createClientSocket();
        })
        .next(function(newClientSocket) {
          clientSocket = newClientSocket;

          connection.assertThrows();

          clientSocket.on('search.result', function(data) {
            clientReceiver.receive(data);
          });

          var envelope = {
            type:       'search.result',
            statusCode: 200,
            body:       { searchResult: true}
          };
          connection.controllers.search.trigger(envelope);
        })
        .wait(0.01)
        .next(function() {
          clientReceiver.assertThrows();
          done();
        })
        .error(function(error) {
          done(error);
        });
    });
{'fraction_non_alphanumeric': 0.13415970992495152, 'fraction_numerical': 0.005396745088118728, 'mean_word_length': 1.7879642689233664, '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': '4712185', 'n_tokens_mistral': 3225, 'n_tokens_neox': 3143, 'n_words': 599}