<commit_before>var sinon = require('sinon');
var expect = require('chai').expect;
var assert = require('assert');
var Q = require('q');
var cases = require('cases');

describe('runner', function() {
    it('follows redirects', cases([['Location'], ['location']], function(headerName, done) {
        // Arrange
        var client = {
            request: sinon.stub()
        };
        var redirectFrom = 'http://test.example.com/redirectFrom';
        var redirectTo = 'http://test.example.com/redirectTo';

        var urls = [redirectFrom];
        var reporter = {
            report: sinon.spy(),
            summarise: function() {
                return Q('Done');
            }
        };
        var checker = { check: sinon.spy() };
        var runner = require('../runner.js');

        var headers = {};
        headers[headerName] = redirectTo;

        client.request.withArgs(redirectFrom).returns(Q({
            status: 302,
            headers: headers
        }));

        client.request.withArgs(redirectTo).returns(Q({
            status: 200
        }));

        // Act
        runner.run(client, urls, reporter, checker).then(function() {
            // Assert
            assert(reporter.report.calledWith(redirectFrom));
            assert(reporter.report.calledWith(redirectTo));
            assert(!isNaN(reporter.report.firstCall.args[2]));

            assert(!checker.check.calledWith(redirectFrom));
            assert(checker.check.calledWith(redirectTo));
        }).then(done).done();
    }))
})<commit_msg>Add unit test for reporting fetch errors<commit_after>var sinon = require('sinon');
var expect = require('chai').expect;
var assert = require('assert');
var Q = require('q');
var cases = require('cases');

describe('runner', function() {
    it('follows redirects', cases([['Location'], ['location']], function(headerName, done) {
        // Arrange
        var client = {
            request: sinon.stub()
        };
        var redirectFrom = 'http://test.example.com/redirectFrom';
        var redirectTo = 'http://test.example.com/redirectTo';

        var urls = [redirectFrom];
        var reporter = {
            report: sinon.spy(),
            summarise: function() {
                return Q('Done');
            }
        };
        var checker = { check: sinon.spy() };
        var runner = require('../runner.js');

        var headers = {};
        headers[headerName] = redirectTo;

        client.request.withArgs(redirectFrom).returns(Q({
            status: 302,
            headers: headers
        }));

        client.request.withArgs(redirectTo).returns(Q({
            status: 200
        }));

        // Act
        runner.run(client, urls, reporter, checker).then(function() {
            // Assert
            assert(reporter.report.calledWith(redirectFrom));
            assert(reporter.report.calledWith(redirectTo));
            assert(!isNaN(reporter.report.firstCall.args[2]));

            assert(!checker.check.calledWith(redirectFrom));
            assert(checker.check.calledWith(redirectTo));
        }).then(done).done();
    }));

    it('returns a failed request as a failure', function(done) {
        // Arrange
        var client = {
            request: sinon.stub()
        };
        var failingUrl = 'https://test.example.com/failsRequest';

        var urls = [failingUrl];
        var reporter = {
            report: sinon.spy(),
            summarise: sinon.spy()
        };
        var checker = { check: sinon.spy() };
        var runner = require('../runner.js');

        client.request.withArgs(failingUrl).returns(
            Q.fcall(function() {
                throw 'Request failure error message';
            })
        );

        // Act
        runner.run(client, urls, reporter, checker).then(function() {
            // Assert
            assert(reporter.summarise.calledWith([], [failingUrl]));
        }).then(done).done();
    });
})
{'fraction_non_alphanumeric': 0.15096251266464034, 'fraction_numerical': 0.0035460992907801418, 'mean_word_length': 2.0076161462300077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '2715641', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1074, 'n_words': 206}