<commit_before>/**
 * There probably should be real tests at some point.
 */
// System
var assert = require('assert')

// 3rd party
var request = require('supertest')

// Package
var lib = require('./lib')


describe("healthful", function () {
    it("loads", function () {
        var health = require('./index')
        assert(health, "Healthful didn't load.")
    })
})


describe("Healthful", function (done) {
    var health
    var req

    before(function () {
        health = new lib.Healthful({
            service: 'mocha',
            http: true,
            interval: 10,
        })
        req = request(health.server)
    })

    after(function (done) {
        if (!health) return done()
        health.server.close(done)
        health = null
    })

    it("works", function () {
        assert(health, "Healthful instance not created")
        assert(health.server, "Healthful server not created")
    })

    it("responds with a 503 if unhealthy", function (done) {
        req.get('/')
        .expect(503, done)
    })

    it("defaults to responding with JSON", function (done) {
        req.get('/')
        .expect('Content-type', 'application/json', done)
    })

    it("responds with the service name and healthful status", function (done) {
        req.get('/')
        .expect({service: 'mocha', healthful: false}, done)
    })

    it("responds on any path", function (done) {
        req.get('/blah')
        .expect(503, done)
    })

    it("changes health status after a ping", function (done) {
        health.ping()
        req.get('/')
        .expect(200)
        .expect({service: 'mocha', healthful: true}, done)
    })

    it("changes health status after ping interval", function (done) {
        setTimeout(function () {
            req.get('/')
            .expect({service: 'mocha', healthful: false}, done)
        }, health.interval)
    })
})
<commit_msg>test: Add statsd tests.<commit_after>/**
 * There probably should be real tests at some point.
 */
// System
var assert = require('assert')

// 3rd party
var request = require('supertest')

// Package
var lib = require('./lib')


describe("healthful", function () {
    it("loads", function () {
        var health = require('./index')
        assert(health, "Healthful didn't load.")
    })
})


describe("HTTP", function () {
    var health
    var req

    before(function () {
        health = new lib.Healthful({
            service: 'mocha',
            http: true,
            interval: 10,
        })
        req = request(health.server)
    })

    after(function (done) {
        if (!health) return done()
        health.close(done)
        health = null
    })

    it("works", function () {
        assert(health, "Healthful instance not created")
        assert(health.server, "Healthful server not created")
    })

    it("responds with a 503 if unhealthy", function (done) {
        req.get('/')
        .expect(503, done)
    })

    it("defaults to responding with JSON", function (done) {
        req.get('/')
        .expect('Content-type', 'application/json', done)
    })

    it("responds with the service name and healthful status", function (done) {
        req.get('/')
        .expect({service: 'mocha', healthful: false}, done)
    })

    it("responds on any path", function (done) {
        req.get('/blah')
        .expect(503, done)
    })

    it("changes health status after a ping", function (done) {
        health.ping()
        req.get('/')
        .expect(200)
        .expect({service: 'mocha', healthful: true}, done)
    })

    it("changes health status after ping interval", function (done) {
        setTimeout(function () {
            req.get('/')
            .expect({service: 'mocha', healthful: false}, done)
        }, health.interval)
    })
})


describe("StatsD", function () {
    var health
    var client
    var req

    before(function () {
        health = new lib.Healthful({
            service: 'mocha',
            statsd: true,
            interval: 10,
        })

        client = {
            args: [],
            count: function () {
                client.args.push(Array.prototype.slice.call(arguments))
            },
            orig: health.statsd_client
        }

        health.statsd_client = client
    })

    after(function (done) {
        if (!health) return done()
        health.close(done)
        health = null
    })

    it("works", function () {
        assert(health, "Healthful instance not created")
    })

    it("generates a healthy ping", function () {
        health.ping()
        assert(JSON.stringify(health.statsd_client.args) ==
            JSON.stringify([['mocha.healthy', 1]]),
            "StatsD got wrong arguments")
    })

    it("generates an unhealthy timeout", function (done) {
        setTimeout(function () {
            assert(JSON.stringify(health.statsd_client.args) ==
                JSON.stringify([['mocha.healthy', 1], ['mocha.unhealthy', 1]]),
                "StatsD got wrong arguments")
            done()
        }, health.interval)
    })
})
{'fraction_non_alphanumeric': 0.14746997440441031, 'fraction_numerical': 0.00689112029927151, 'mean_word_length': 2.0455635491606716, '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': '3324005', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1413, 'n_words': 382}