<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 socketIoAdapter = require('../../lib/adapter/socket.io');
var command = require('../../lib/adapter/command');
var api = require('../../lib/adapter/api');
var scoketIoAPI = require('../../lib/adapter/api/socket.io');

suite('Socket.IO Adapter', function() {
  var connection;
  var server;
  var clientSockets;
  var backend;

  var testPlugin = {
    'reqrep': new command.SocketRequestResponse(),
    'reqrep-mod-event': new command.SocketRequestResponse({
      onRequest: function(data, connection) {
        connection.emit('reqrep-mod-event.mod', data);
      },
      onResponse: function(data, socket) {
        socket.emit('reqrep-mod-event.response.mod', data);
      }
    }),
    'reqrep-mod-body': new command.SocketRequestResponse({
      onRequest: function(data, connection) {
        connection.emit('reqrep-mod-body', 'modified request');
      },
      onResponse: function(data, socket) {
        socket.emit('reqrep-mod-body.response', 'modified response');
      }
    }),
    'pubsub': new command.SocketPublishSubscribe(),
    'pubsub-mod-event': new command.SocketPublishSubscribe({
      onSubscribe: function(data, connection) {
        connection.emit('pubsub-mod-event.mod.subscribe', data);
      },
      onSubscribed: function(data, socket) {
        socket.emit('pubsub-mod-event.mod.subscribe.response', data);
      },
      onUnsubscribe: function(data, connection) {
        connection.emit('pubsub-mod-event.mod.unsubscribe', data);
      },
      onUnsubscribed: function(data, socket) {
        socket.emit('pubsub-mod-event.mod.unsubscribe.response', data);
      },
      onNotify: function(data, socket) {
        socket.emit('pubsub-mod-event.mod.notification', data);
      }
    }),
    'pubsub-mod-body': new command.SocketPublishSubscribe({
      onSubscribe: function(data, connection) {
        connection.emit('pubsub-mod-body.subscribe', 'modified request');
      },
      onSubscribed: function(data, socket) {
        socket.emit('pubsub-mod-body.subscribe.response', 'modified response');
      },
      onUnsubscribe: function(data, connection) {
        connection.emit('pubsub-mod-body.unsubscribe', 'modified request');
      },
      onUnsubscribed: function(data, socket) {
        socket.emit('pubsub-mod-body.unsubscribe.response', 'modified response');
      },
      onNotify: function(data, socket) {
        socket.emit('pubsub-mod-body.notification', 'modified response');
      }
    })
  };

  function setupEnvironment() {
    clientSockets = [];
  }
  function teardownEnvironment() {
    if (clientSockets.length) {
      clientSockets.forEach(function(clientSocket) {
        clientSocket.disconnect();
      });
    }
    utils.teardownApplication({ backend:    backend,
                                server:     server,
                                connection: connection });
  }

  suite('registration', function() {
    setup(setupEnvironment);
    teardown(teardownEnvironment);

    test('registration of plugin commands', function(done) {
      var basePlugin = {
        getCommand: new command.SocketRequestResponse(),
        putCommand: new command.SocketRequestResponse(),
        postCommand: new command.SocketRequestResponse(),
        deleteCommand: new command.SocketRequestResponse(),
        ignored: new command.HTTPRequestResponse()
      };
      var overridingPlugin = {
        postCommand: new command.SocketRequestResponse(),
        deleteCommand: new command.SocketRequestResponse()
      };

      var application = express();
      utils.setupServer(application)
        .next(function(newServer) {
          server = newServer;

          var registeredCommands = socketIoAdapter.register(application, server, {
            connection: utils.createStubbedBackendConnection(),
            plugins: [
              api.API_REST,
              api.API_SOCKET_IO,
              api.API_GROONGA,
              api.API_DROONGA,
              basePlugin,
              overridingPlugin
            ]
          });

          registeredCommands = registeredCommands.map(function(command) {
            return {
              name:       command.name,
              definition: command.definition
            };
          });
          assert.deepEqual(registeredCommands,
                           [{ name:       'search',
                              definition: scoketIoAPI.search },
                            { name:       'watch',
                              definition: scoketIoAPI.watch },
                            { name:       'getCommand',
                              definition: basePlugin.getCommand },
                            { name:       'putCommand',
                              definition: basePlugin.putCommand },
                            { name:       'postCommand',
                              definition: overridingPlugin.postCommand },
                            { name:       'deleteCommand',
                              definition: overridingPlugin.deleteCommand }]);
          done();
        })
        .error(function(error) {
          done(error);
        });
    });

    test('initialization', function(done) {
      var mockedListener = nodemock
        .mock('connected');

      var application = express();
      application.on('connection', function(socket) {
        mockedListener.connected();
      });

      utils.setupServer(application)
        .next(function(newServer) {
          server = newServer;
          socketIoAdapter.register(application, server, {
            connection: utils.createStubbedBackendConnection(),
            plugins: [
              api.API_REST,
              api.API_SOCKET_IO,<commit_msg>Store used connection to the global variable "connection" correctly<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 socketIoAdapter = require('../../lib/adapter/socket.io');
var command = require('../../lib/adapter/command');
var api = require('../../lib/adapter/api');
var scoketIoAPI = require('../../lib/adapter/api/socket.io');

suite('Socket.IO Adapter', function() {
  var connection;
  var server;
  var clientSockets;
  var backend;

  var testPlugin = {
    'reqrep': new command.SocketRequestResponse(),
    'reqrep-mod-event': new command.SocketRequestResponse({
      onRequest: function(data, connection) {
        connection.emit('reqrep-mod-event.mod', data);
      },
      onResponse: function(data, socket) {
        socket.emit('reqrep-mod-event.response.mod', data);
      }
    }),
    'reqrep-mod-body': new command.SocketRequestResponse({
      onRequest: function(data, connection) {
        connection.emit('reqrep-mod-body', 'modified request');
      },
      onResponse: function(data, socket) {
        socket.emit('reqrep-mod-body.response', 'modified response');
      }
    }),
    'pubsub': new command.SocketPublishSubscribe(),
    'pubsub-mod-event': new command.SocketPublishSubscribe({
      onSubscribe: function(data, connection) {
        connection.emit('pubsub-mod-event.mod.subscribe', data);
      },
      onSubscribed: function(data, socket) {
        socket.emit('pubsub-mod-event.mod.subscribe.response', data);
      },
      onUnsubscribe: function(data, connection) {
        connection.emit('pubsub-mod-event.mod.unsubscribe', data);
      },
      onUnsubscribed: function(data, socket) {
        socket.emit('pubsub-mod-event.mod.unsubscribe.response', data);
      },
      onNotify: function(data, socket) {
        socket.emit('pubsub-mod-event.mod.notification', data);
      }
    }),
    'pubsub-mod-body': new command.SocketPublishSubscribe({
      onSubscribe: function(data, connection) {
        connection.emit('pubsub-mod-body.subscribe', 'modified request');
      },
      onSubscribed: function(data, socket) {
        socket.emit('pubsub-mod-body.subscribe.response', 'modified response');
      },
      onUnsubscribe: function(data, connection) {
        connection.emit('pubsub-mod-body.unsubscribe', 'modified request');
      },
      onUnsubscribed: function(data, socket) {
        socket.emit('pubsub-mod-body.unsubscribe.response', 'modified response');
      },
      onNotify: function(data, socket) {
        socket.emit('pubsub-mod-body.notification', 'modified response');
      }
    })
  };

  function setupEnvironment() {
    clientSockets = [];
  }
  function teardownEnvironment() {
    if (clientSockets.length) {
      clientSockets.forEach(function(clientSocket) {
        clientSocket.disconnect();
      });
    }
    utils.teardownApplication({ backend:    backend,
                                server:     server,
                                connection: connection });
  }

  suite('registration', function() {
    setup(setupEnvironment);
    teardown(teardownEnvironment);

    test('registration of plugin commands', function(done) {
      var basePlugin = {
        getCommand: new command.SocketRequestResponse(),
        putCommand: new command.SocketRequestResponse(),
        postCommand: new command.SocketRequestResponse(),
        deleteCommand: new command.SocketRequestResponse(),
        ignored: new command.HTTPRequestResponse()
      };
      var overridingPlugin = {
        postCommand: new command.SocketRequestResponse(),
        deleteCommand: new command.SocketRequestResponse()
      };

      var application = express();
      utils.setupServer(application)
        .next(function(newServer) {
          server = newServer;
          connection = utils.createStubbedBackendConnection();
          var registeredCommands = socketIoAdapter.register(application, server, {
            connection: connection,
            plugins: [
              api.API_REST,
              api.API_SOCKET_IO,
              api.API_GROONGA,
              api.API_DROONGA,
              basePlugin,
              overridingPlugin
            ]
          });

          registeredCommands = registeredCommands.map(function(command) {
            return {
              name:       command.name,
              definition: command.definition
            };
          });
          assert.deepEqual(registeredCommands,
                           [{ name:       'search',
                              definition: scoketIoAPI.search },
                            { name:       'watch',
                              definition: scoketIoAPI.watch },
                            { name:       'getCommand',
                              definition: basePlugin.getCommand },
                            { name:       'putCommand',
                              definition: basePlugin.putCommand },
                            { name:       'postCommand',
                              definition: overridingPlugin.postCommand },
                            { name:       'deleteCommand',
                              definition: overridingPlugin.deleteCommand }]);
          done();
        })
        .error(function(error) {
          done(error);
        });
    });

    test('initialization', function(done) {
      var mockedListener = nodemock
        .mock('connected');

      var application = express();
      application.on('connection', function(socket) {
        mockedListener.connected();
      });

      utils.setupServer(application)
        .next(function(newServer) {
          server = newServer;
          connection = utils.createStubbedBackendConnection();
          socketIoAdapter.register(application, server, {
            connection: connection,
            plugins: [
              api.API_REST,
              api.API_SOCKET_IO,
{'fraction_non_alphanumeric': 0.12190827534166178, 'fraction_numerical': 0.0, 'mean_word_length': 2.115987460815047, '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': '4308503', 'n_tokens_mistral': 3062, 'n_tokens_neox': 3038, 'n_words': 537}