<commit_before>var expect = require('chai').expect,
    assert = require('chai').assert,
    sinon  = require('sinon'),
    rewire = require('rewire');


describe("Setup", function() {
    var observer,
        loggerMock,
        sonumiLoggerMock,
        clientMock,
        subscriptionPromise,
        commandObserver = rewire("../index");

    beforeEach(function() {
        loggerMock = sinon.stub();
        loggerMock.log = sinon.stub();
        loggerMock.addLogFile = sinon.stub();
        sonumiLoggerMock = sinon.stub();
        sonumiLoggerMock.init = sinon.stub().returns(loggerMock);

        configMock = {
            "logging": {
                "logDir": "/tmp/"
            }
        };

        commandObserver.__set__({
            config: configMock,
            logger: loggerMock,
            sonumiLogger: sonumiLoggerMock
        });

        subscriptionPromise = new Promise(function(resolve, reject) {});

        clientMock = sinon.stub();
        clientMock.observe = sinon.stub().returns({});
        clientMock.collections = {'commands': []};

        observer = new commandObserver(clientMock);
    });

    it('should observe the collection', function() {
        assert(clientMock.observe.calledWith('commands'));
    });

    it('should register new handlers', function() {
        var label = 'test_handler';

        expect(observer.handlers[label]).to.be.undefined;

        observer.register_handler(label, {});

        expect(observer.handlers[label]).to.not.be.undefined;
    });
});



describe("Observe Commands", function() {
    var observer,
        loggerMock,
        sonumiLoggerMock,
        clientMock,
        commandObserver = rewire("../index");

    beforeEach(function() {
        loggerMock = sinon.stub();
        loggerMock.log = sinon.stub();
        loggerMock.error = sinon.stub();
        loggerMock.addLogFile = sinon.stub();
        sonumiLoggerMock = sinon.stub();
        sonumiLoggerMock.init = sinon.stub().returns(loggerMock);

        configMock = {
            "logging": {
                "logDir": "/tmp/"
            }
        };

        commandObserver.__set__({
            config: configMock,
            logger: loggerMock,
            sonumiLogger: sonumiLoggerMock,
            execute: sinon.spy()
        });

        clientMock = sinon.stub();
        clientMock.subscribe = sinon.spy();
        clientMock.observe = sinon.stub().returns({});
        clientMock.collections = {
            'commands': [
                {'text': 'my.test'},
                {'text': 'my.test.action'}
            ]
        };

        observer = new commandObserver(clientMock);
    });

    it('should reject badly formatted commands', function() {
        var commandId = 0;

        observer.status_fail = sinon.spy();

        observer.added(commandId);

        assert(observer.status_fail.calledWith(commandId));
    });

    it('should acknowledge and execute new commands', function() {
        var commandId = 1;

        observer.status_ack = sinon.spy();

        observer.added(commandId);

        assert(observer.status_ack.calledWith(commandId));
        assert(commandObserver.__get__('execute').calledWith(commandId, 'my', 'test', 'action'));
    });
});

describe("Handlers", function() {
    var observer,
        loggerMock,
        sonumiLoggerMock,
        clientMock,
        commandObserver = rewire("../index");

    beforeEach(function() {
        loggerMock = sinon.stub();
        loggerMock.log = sinon.stub();
        loggerMock.error = sinon.stub();
        loggerMock.addLogFile = sinon.stub();
        sonumiLoggerMock = sinon.stub();
        sonumiLoggerMock.init = sinon.stub().returns(loggerMock);

        configMock = {
            "logging": {
                "logDir": "/tmp/"
            }
        };

        commandObserver.__set__({
            config: configMock,
            logger: loggerMock,
            sonumiLogger: sonumiLoggerMock
        });

        clientMock = sinon.stub();
        clientMock.subscribe = sinon.spy();
        clientMock.observe = sinon.stub().returns({});
        clientMock.collections = {
            'commands': [
                {'text': 'my.test'},
                {'text': 'my.test.action'}
            ]
        };

        observer = new commandObserver(clientMock);
        observer.status_ack = sinon.spy();
    });

    it('should acknowledge and fail when no handler is found for a command', function() {
        var commandId = 1;

        observer.status_fail = sinon.spy();

        observer.added(commandId);
<commit_msg>Fix issue with tests breaking because clientMock.connections was not a function<commit_after>var expect = require('chai').expect,
    assert = require('chai').assert,
    sinon  = require('sinon'),
    rewire = require('rewire');


describe("Setup", function() {
    var observer,
        loggerMock,
        sonumiLoggerMock,
        clientMock,
        subscriptionPromise,
        commandObserver = rewire("../index");

    beforeEach(function() {
        loggerMock = sinon.stub();
        loggerMock.log = sinon.stub();
        loggerMock.addLogFile = sinon.stub();
        sonumiLoggerMock = sinon.stub();
        sonumiLoggerMock.init = sinon.stub().returns(loggerMock);

        configMock = {
            "logging": {
                "logDir": "/tmp/"
            }
        };

        commandObserver.__set__({
            config: configMock,
            logger: loggerMock,
            sonumiLogger: sonumiLoggerMock
        });

        subscriptionPromise = new Promise(function(resolve, reject) {});

        clientMock = sinon.stub();
        clientMock.observe = sinon.stub().returns({});
        clientMock.collections = function() {
            return {
                'commands': []
            };
        };

        observer = new commandObserver(clientMock);
    });

    it('should observe the collection', function() {
        assert(clientMock.observe.calledWith('commands'));
    });

    it('should register new handlers', function() {
        var label = 'test_handler';

        expect(observer.handlers[label]).to.be.undefined;

        observer.register_handler(label, {});

        expect(observer.handlers[label]).to.not.be.undefined;
    });
});



describe("Observe Commands", function() {
    var observer,
        loggerMock,
        sonumiLoggerMock,
        clientMock,
        commandObserver = rewire("../index");

    beforeEach(function() {
        loggerMock = sinon.stub();
        loggerMock.log = sinon.stub();
        loggerMock.error = sinon.stub();
        loggerMock.addLogFile = sinon.stub();
        sonumiLoggerMock = sinon.stub();
        sonumiLoggerMock.init = sinon.stub().returns(loggerMock);

        configMock = {
            "logging": {
                "logDir": "/tmp/"
            }
        };

        commandObserver.__set__({
            config: configMock,
            logger: loggerMock,
            sonumiLogger: sonumiLoggerMock,
            execute: sinon.spy()
        });

        clientMock = sinon.stub();
        clientMock.subscribe = sinon.spy();
        clientMock.observe = sinon.stub().returns({});
        clientMock.collections = function() {
            return {
                'commands': [
                    {'text': 'my.test'},
                    {'text': 'my.test.action'}
                ]
            };
        };

        observer = new commandObserver(clientMock);
    });

    it('should reject badly formatted commands', function() {
        var commandId = 0;

        observer.status_fail = sinon.spy();

        observer.added(commandId);

        assert(observer.status_fail.calledWith(commandId));
    });

    it('should acknowledge and execute new commands', function() {
        var commandId = 1;

        observer.status_ack = sinon.spy();

        observer.added(commandId);

        assert(observer.status_ack.calledWith(commandId));
        assert(commandObserver.__get__('execute').calledWith(commandId, 'my', 'test', 'action'));
    });
});

describe("Handlers", function() {
    var observer,
        loggerMock,
        sonumiLoggerMock,
        clientMock,
        commandObserver = rewire("../index");

    beforeEach(function() {
        loggerMock = sinon.stub();
        loggerMock.log = sinon.stub();
        loggerMock.error = sinon.stub();
        loggerMock.addLogFile = sinon.stub();
        sonumiLoggerMock = sinon.stub();
        sonumiLoggerMock.init = sinon.stub().returns(loggerMock);

        configMock = {
            "logging": {
                "logDir": "/tmp/"
            }
        };

        commandObserver.__set__({
            config: configMock,
            logger: loggerMock,
            sonumiLogger: sonumiLoggerMock
        });

        clientMock = sinon.stub();
        clientMock.subscribe = sinon.spy();
        clientMock.observe = sinon.stub().returns({});
        clientMock.collections = function() {
            return {
                'commands': [
                    {'text': 'my.test'},
                    {'text': 'my.test.action'}
                ]
            };
        };

        observer = new commandObserver(clientMock);
        observer.status_ack = sinon.spy();
    });

    it('should acknowledge and fail when no handler is found for a command', function() {
        var commandId = 1;

        observer.status_fail = sinon.spy();

        observer.added(commandId);

{'fraction_non_alphanumeric': 0.13248542660307366, 'fraction_numerical': 0.0006359300476947536, 'mean_word_length': 1.9635678391959799, 'pattern_counts': {'":': 12, '<': 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': '5542775', 'n_tokens_mistral': 2598, 'n_tokens_neox': 2533, 'n_words': 460}