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

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

function createSender() {
  return nodemock;
}

function createReceiver() {
  var mockedSocket = nodemock;
  var callbackController = {};
  var receiver = {
    // mocking receiver
    sockets:
      (mockedSocket =
        mockedSocket.mock('on')
          .takes('test.message', function() {})
          .ctrl(1, callbackController)),
    'set': function(key, value) {},

    // extra features as a mocked object
    triggerMessage: function(message) {
      callbackController.trigger(message);
    },
    mock: function(name) {
      return mockedSocket.mock(name);
    },
    assert: function() {
      mockedSocket.assert();
    }
  };
  return receiver;
}

suite('Connection', function() {
  var connection;
  var sender;
  var receiver;

  setup(function() {
    connection = new Connection({
      tag:        'test',
      listenPort: 3333,
      sender:     sender = createSender(),
      receiver:   receiver = createReceiver()
    });
  });

  teardown(function() {
    connection = undefined;
    sender = undefined;
    receiver = undefined;
  });

  function assertMessages() {
    sender.assert();
    receiver.assert();
  }

  test('message without response (volatile message)', function() {
    sender.mock('emit')
      .takes({ tag: 'message',
               message: { command: 'foobar' } });
    connection.emit({ command: 'foobar' });
    assertMessages();<commit_msg>Define mocked sender without nodemock (because sent message will be dynamically generated)<commit_after>var assert = require('chai').assert;
var nodemock = require('nodemock');

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

function createSender() {
  var sender = {
    emit: function(eventName, message) {
      this.messages.push({ eventName: eventName, message: message });
    },
    assertSent: function(eventName, message) {
      var firstMessage = this.messages.shift();
      var expectedMessage = { eventName: eventName, message: message };
      assert.deepEqual(firstMessage, expectedMessage);
    },
    messages: [],
  };
  return sender;
}

function createReceiver() {
  var mockedSockets;
  var mockedReceiverInternal = nodemock;
  var connactionCallbackController = {};
  var receiver = {
    // mocking receiver
    sockets:
      (mockedSockets = nodemock.mock('on')
         .takes('connection', function() {})
         .ctrl(1, connactionCallbackController)),
    'set': function(key, value) {},

    // extra features as a mocked object
    triggerConnect: function(tag) {
      mockedSockets.assert();
      var mockedSocket = nodemock.mock('on')
                           .takes(tag + '.message', function() {});
      connactionCallbackController.trigger(mockedSocket);
      mockedSocket.assert();
    },
    takes: function(message) {
      this.assert = function() {
        mockedReceiverInternal.assert();
      };
      mockedReceiverInternal = mockedReceiverInternal
                                 .mock('emit')
                                 .takes(message);
      return mockedReceiverInternal;
    }
  };
  return receiver;
}

suite('Connection', function() {
  var connection;
  var sender;
  var receiver;

  setup(function() {
    connection = new Connection({
      tag:        'test',
      listenPort: 3333,
      sender:     sender = createSender(),
      receiver:   receiver = createReceiver()
    });
    receiver.triggerConnect('test');
  });

  teardown(function() {
    connection = undefined;
    sender = undefined;
    receiver = undefined;
  });

  test('message without response (volatile message)', function() {
    connection.emitMessage({ command: 'foobar' });
    sender.assertSent('message', { body: { command: 'foobar' } });
{'fraction_non_alphanumeric': 0.13291796469366562, 'fraction_numerical': 0.002596053997923157, 'mean_word_length': 2.872361809045226, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1181638', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1061, 'n_words': 246}