<commit_before>/* Setup */
var Lab = require('lab');
var Code = require('code');
var lab = exports.lab = Lab.script();

var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var beforeEach = lab.beforeEach;
var after = lab.after;
var afterEach = lab.afterEach;
var expect = Code.expect;

/* Modules */
var simple = require('simple-mock');
var mock = simple.mock;
var msb = require('..');
var Originator = msb.Originator;

/* Tests */
describe('Originator', function() {
  var mocks;

  beforeEach(function(done) {
    mocks = {};
    done();
  });

  afterEach(function(done) {
    simple.restore();
    done();
  });

  it('can be initialized', function(done) {
    mocks.messageFactory_createOriginalMessage = simple.mock(msb.messageFactory, 'createOriginalMessage');
    mocks.Originator_super_ = simple.mock(Originator, 'super_');

    var obj = new Originator({});

    expect(mocks.messageFactory_createOriginalMessage.called).to.equal(true);
    expect(mocks.Originator_super_.called).to.equal(true);
    done();
  });

  describe('publish()', function() {
    var producer;

    beforeEach(function(done) {
      producer = {};

      simple.mock(msb.channelManager, 'findOrCreateProducer', function(topic) {
        return producer;
      });

      simple.mock(producer, 'publish');

      done();
    });

    it('can emit error', function(done) {
      var errHandler = simple.mock();
      var expectedErr = new Error();
      producer.publish.callbackWith(expectedErr);

      var obj = new Originator({});

      obj
      .on('error', errHandler)
      .publish();

      expect(producer.publish.called).to.equal(true);
      expect(errHandler.called).to.equal(true);
      expect(errHandler.lastCall.args[0]).to.equal(expectedErr);

      done();
    });

    it('can emit message immediately', function(done) {
      producer.publish.callbackWith();

      var finalHandler = simple.mock();

      var obj = new Originator({
        waitForContribs: 0
      });

      obj
      .on('final', finalHandler)
      .publish();

      expect(producer.publish.called).to.equal(true);
      expect(finalHandler.called).to.equal(true);
      expect(finalHandler.lastCall.args[0]).to.equal(obj.message);

      done();
    });

    it('can start collecting contributions', function(done) {
      producer.publish.callbackWith();

      var finalHandler = simple.mock();

      var obj = new Originator({
        waitForContribs: 1
      });

      mocks.originator_shouldAcceptMessageFn = simple.mock(obj.shouldAcceptMessageFn, 'bind').returnWith('testValue');
      simple.mock(obj, 'listenForContribs').returnWith();
      simple.mock(obj, 'listenForAcks').returnWith();

      obj
      .on('final', finalHandler)
      .publish();

      expect(mocks.originator_shouldAcceptMessageFn.lastCall.args[0]).to.equal(obj);
      expect(obj.listenForContribs.lastCall.args[0]).to.equal(obj.message.topics.contrib);
      expect(obj.listenForContribs.lastCall.args[1]).to.equal('testValue');
      expect(obj.listenForAcks.lastCall.args[0]).to.equal(obj.message.topics.ack);
      expect(obj.listenForAcks.lastCall.args[1]).to.equal('testValue');
      expect(producer.publish.called).to.equal(true);
      expect(finalHandler.called).to.equal(false);

      done();
    });
  });
});<commit_msg>Added test for originator message matcher<commit_after>/* Setup */
var Lab = require('lab');
var Code = require('code');
var lab = exports.lab = Lab.script();

var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var beforeEach = lab.beforeEach;
var after = lab.after;
var afterEach = lab.afterEach;
var expect = Code.expect;

/* Modules */
var simple = require('simple-mock');
var mock = simple.mock;
var msb = require('..');
var Originator = msb.Originator;

/* Tests */
describe('Originator', function() {
  var mocks;

  beforeEach(function(done) {
    mocks = {};
    done();
  });

  afterEach(function(done) {
    simple.restore();
    done();
  });

  it('can be initialized', function(done) {
    mocks.messageFactory_createOriginalMessage = simple.mock(msb.messageFactory, 'createOriginalMessage');
    mocks.Originator_super_ = simple.mock(Originator, 'super_');

    var obj = new Originator({});

    expect(mocks.messageFactory_createOriginalMessage.called).to.equal(true);
    expect(mocks.Originator_super_.called).to.equal(true);
    done();
  });

  describe('publish()', function() {
    var producer;

    beforeEach(function(done) {
      producer = {};

      simple.mock(msb.channelManager, 'findOrCreateProducer', function(topic) {
        return producer;
      });

      simple.mock(producer, 'publish');

      done();
    });

    it('can emit error', function(done) {
      var errHandler = simple.mock();
      var expectedErr = new Error();
      producer.publish.callbackWith(expectedErr);

      var obj = new Originator({});

      obj
      .on('error', errHandler)
      .publish();

      expect(producer.publish.called).to.equal(true);
      expect(errHandler.called).to.equal(true);
      expect(errHandler.lastCall.args[0]).to.equal(expectedErr);

      done();
    });

    it('can emit message immediately', function(done) {
      producer.publish.callbackWith();

      var finalHandler = simple.mock();

      var obj = new Originator({
        waitForContribs: 0
      });

      obj
      .on('final', finalHandler)
      .publish();

      expect(producer.publish.called).to.equal(true);
      expect(finalHandler.called).to.equal(true);
      expect(finalHandler.lastCall.args[0]).to.equal(obj.message);

      done();
    });

    it('can start collecting contributions', function(done) {
      producer.publish.callbackWith();

      var finalHandler = simple.mock();

      var obj = new Originator({
        waitForContribs: 1
      });

      mocks.originator_shouldAcceptMessageFn = simple.mock(obj.shouldAcceptMessageFn, 'bind').returnWith('testValue');
      simple.mock(obj, 'listenForContribs').returnWith();
      simple.mock(obj, 'listenForAcks').returnWith();

      obj
      .on('final', finalHandler)
      .publish();

      expect(mocks.originator_shouldAcceptMessageFn.lastCall.args[0]).to.equal(obj);
      expect(obj.listenForContribs.lastCall.args[0]).to.equal(obj.message.topics.contrib);
      expect(obj.listenForContribs.lastCall.args[1]).to.equal('testValue');
      expect(obj.listenForAcks.lastCall.args[0]).to.equal(obj.message.topics.ack);
      expect(obj.listenForAcks.lastCall.args[1]).to.equal('testValue');
      expect(producer.publish.called).to.equal(true);
      expect(finalHandler.called).to.equal(false);

      done();
    });
  });

  describe('shouldAcceptMessageFn()', function(done) {

    it('should per default match on message id', function(done) {
      var obj = new Originator({});

      expect(obj.shouldAcceptMessageFn({
        id: 'other'
      })).false();

      expect(obj.shouldAcceptMessageFn({
        id: obj.message.id
      })).true();

      done();
    });

  });
});
{'fraction_non_alphanumeric': 0.15677061085006408, 'fraction_numerical': 0.0025630072618539087, 'mean_word_length': 3.2829268292682925, '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': '4591096', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2133, 'n_words': 366}