<commit_before>var assert = require('assert');
    proxyquire = require('proxyquire'),
    utilStub = {
        initializeModulesWithin: function() {
            return 'validators to be used';
        },
        lastStatusWasExternal: function(repoClient, sha, cb) {
            cb(true);
        }
    };


describe('github hook handler', function() {
    var validationPerformed = false,
        executedHookCommands = [],
        validatedSHA, validatedUser, validatorsUsed, validationPosted,
        githubHook = proxyquire('./../githubHook', {
            'child_process': {
                exec: function(cmd, cb) {
                    executedHookCommands.push(cmd);
                    cb(null, 'stdout', 'stderr');
                }
            },
            './utils/general': utilStub,
            './utils/shaValidator': {
                performCompleteValidation: function(sha, githubUser, _, validators, postStatus, cb) {
                    console.log(arguments);
                    validationPerformed = true;
                    validatedSHA = sha;
                    validatedUser = githubUser;
                    validatorsUsed = validators;
                    validationPosted = postStatus;
                    cb();
                }
            }
        }),
        mockClients = {'numenta/experiments': {
            hooks: {
                build: 'build hook'
            },
            getCommit: function() {},
            github: {
                statuses: {
                    create: function (statusObj) {
                        validationPosted = statusObj;
                    }
                }
            }
        }},
        handler = githubHook.initializer(mockClients, 'mockConfig');

    it('calls pr handler when sent a mergeable pull_request event', function() {
        var mockPayload = {
                name: 'numenta/experiments',
                pull_request: {
                    action: 'closed',
                    user: {login: 'login'},
                    head: {sha: 'sha'},
                    base: {label: 'label', ref: 'master'},
                    // a travis passing and mergeable PR:
                    merged: false,
                    mergeable: true,
                    mergeable_state: "clean"
                }
            },
            mockRequest = {
                body: {
                    payload: JSON.stringify(mockPayload)
                }
            },
            endCalled = false,
            mockResponse = {
                end: function() {
                    endCalled = true;
                }
            };

        validationPerformed = false;

        handler(mockRequest, mockResponse);

        assert(validationPerformed, 'validation against PR was not performed');
        assert.equal(validatedSHA, 'sha', 'validated wrong SHA');
        assert.equal(validatedUser, 'login', 'validated wrong user');
        assert.equal(validatorsUsed, 'validators to be used', 'used wrong validators');
        assert(validationPosted, 'validation status was not posted');
        assert(endCalled, 'request was not closed');

        // Reset just in case further tests use them.
        validationPerformed = undefined;
        validatedSHA = undefined;
        validatedUser = undefined;
        validatorsUsed = undefined;
        validationPosted = undefined;
    });

    it('does not call pr handler when sent a non-mergeable pull_request event', function(done) {
        var mockPayload = require('./github_payloads/pr_non_mergeable'),
            mockRequest = {
                body: {
                    payload: JSON.stringify(mockPayload)
                }
            },
            mockResponse = {
                end: function() {
                    assert(!validationPerformed, 'validation against PR should not be performed');

                    assert(validationPosted, 'validation status should be posted');

                    assert.equal(validationPosted.state, 'error', 
                        'PR state is wrong');

                    assert.equal(validationPosted.description, 
                        'NuPIC Status: This pull request contains merge conflicts. ' + 
                        'Please merge `numenta:master` into `DR:a-feature` and resolve them.', 
                        'PR status description is wrong');

                    assert.equal(validationPosted.target_url, 
                        'https://github.com/numenta/experiments/compare/DR:a-feature...numenta:master#commits_bucket',
                        'PR status detail url is wrong');

                    // Reset just in case further tests use them.
                    validationPerformed = undefined;
                    validatedSHA = undefined;
                    validatedUser = undefined;
                    validatorsUsed = undefined;
                    validationPosted = undefined;
                    done();
                }
            };

        handler = githubHook.initializer(mockClients, 'mockConfig');
<commit_msg>Fix expected wording in test<commit_after>var assert = require('assert');
    proxyquire = require('proxyquire'),
    utilStub = {
        initializeModulesWithin: function() {
            return 'validators to be used';
        },
        lastStatusWasExternal: function(repoClient, sha, cb) {
            cb(true);
        }
    };


describe('github hook handler', function() {
    var validationPerformed = false,
        executedHookCommands = [],
        validatedSHA, validatedUser, validatorsUsed, validationPosted,
        githubHook = proxyquire('./../githubHook', {
            'child_process': {
                exec: function(cmd, cb) {
                    executedHookCommands.push(cmd);
                    cb(null, 'stdout', 'stderr');
                }
            },
            './utils/general': utilStub,
            './utils/shaValidator': {
                performCompleteValidation: function(sha, githubUser, _, validators, postStatus, cb) {
                    console.log(arguments);
                    validationPerformed = true;
                    validatedSHA = sha;
                    validatedUser = githubUser;
                    validatorsUsed = validators;
                    validationPosted = postStatus;
                    cb();
                }
            }
        }),
        mockClients = {'numenta/experiments': {
            hooks: {
                build: 'build hook'
            },
            getCommit: function() {},
            github: {
                statuses: {
                    create: function (statusObj) {
                        validationPosted = statusObj;
                    }
                }
            }
        }},
        handler = githubHook.initializer(mockClients, 'mockConfig');

    it('calls pr handler when sent a mergeable pull_request event', function() {
        var mockPayload = {
                name: 'numenta/experiments',
                pull_request: {
                    action: 'closed',
                    user: {login: 'login'},
                    head: {sha: 'sha'},
                    base: {label: 'label', ref: 'master'},
                    // a travis passing and mergeable PR:
                    merged: false,
                    mergeable: true,
                    mergeable_state: "clean"
                }
            },
            mockRequest = {
                body: {
                    payload: JSON.stringify(mockPayload)
                }
            },
            endCalled = false,
            mockResponse = {
                end: function() {
                    endCalled = true;
                }
            };

        validationPerformed = false;

        handler(mockRequest, mockResponse);

        assert(validationPerformed, 'validation against PR was not performed');
        assert.equal(validatedSHA, 'sha', 'validated wrong SHA');
        assert.equal(validatedUser, 'login', 'validated wrong user');
        assert.equal(validatorsUsed, 'validators to be used', 'used wrong validators');
        assert(validationPosted, 'validation status was not posted');
        assert(endCalled, 'request was not closed');

        // Reset just in case further tests use them.
        validationPerformed = undefined;
        validatedSHA = undefined;
        validatedUser = undefined;
        validatorsUsed = undefined;
        validationPosted = undefined;
    });

    it('does not call pr handler when sent a non-mergeable pull_request event', function(done) {
        var mockPayload = require('./github_payloads/pr_non_mergeable'),
            mockRequest = {
                body: {
                    payload: JSON.stringify(mockPayload)
                }
            },
            mockResponse = {
                end: function() {
                    assert(!validationPerformed, 'validation against PR should not be performed');

                    assert(validationPosted, 'validation status should be posted');

                    assert.equal(validationPosted.state, 'error', 
                        'PR state is wrong');

                    assert.equal(validationPosted.description, 
                        'NuPIC Status: ' + 
                        'Please merge `numenta:master` into `DR:a-feature` and resolve merge conflicts.', 
                        'PR status description is wrong');

                    assert.equal(validationPosted.target_url, 
                        'https://github.com/numenta/experiments/compare/DR:a-feature...numenta:master#commits_bucket',
                        'PR status detail url is wrong');

                    // Reset just in case further tests use them.
                    validationPerformed = undefined;
                    validatedSHA = undefined;
                    validatedUser = undefined;
                    validatorsUsed = undefined;
                    validationPosted = undefined;
                    done();
                }
            };

        handler = githubHook.initializer(mockClients, 'mockConfig');

{'fraction_non_alphanumeric': 0.09244946492271106, 'fraction_numerical': 0.0, 'mean_word_length': 1.4048129616392662, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3708254', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2214, 'n_words': 563}