<commit_before>var ab = require('../lib/express-ab');
var assert = require('assert');
var express = require('express');
var request = require('supertest');

describe('weighted', function () {
    function setReqVarMiddleware(req, res, next) {
        req.ab = {
            random: req.get('ab-random'),
            weightSum: 0
        };
        next();
    }

    function skipRouteMiddleware(req, res, next) {
        next('route');
    }

    var app = express();
    var abTest = ab.test('unit-test');

    app.get('/', setReqVarMiddleware, abTest(null, 0.2), function (req, res) {
        res.send('variantA');
    });

    app.get('/', setReqVarMiddleware, abTest(null, 0.8), function (req, res) {
        res.send('variantB');
    });

    app.get('/random', abTest(null, 1), function (req, res) {
        res.send(req.ab);
    });

    describe('variant selection', function () {
        it('should set ab object on req', function (done) {
            request(app)
                .get('/random')
                .expect(function (res) {
                    assert('random' in res.body);
                    assert('weightSum' in res.body);
                })
                .end(done);
        });

        it('should select route A', function (done) {
            request(app)
                .get('/')
                .set('ab-random', 0.11)
                .expect(200)
                .expect('variantA', done);
        });

        it('should select route B', function (done) {
            request(app)
                .get('/')
                .set('ab-random', 0.42)
                .expect(200)
                .expect('variantB', done);
        });
    });

    describe('fallthrough', function () {

        app.get('/fallthrough', skipRouteMiddleware, setReqVarMiddleware, abTest(null, 0.5), function (req, res) {
            res.send('variantA');
        });

        app.get('/fallthrough', setReqVarMiddleware, abTest(null, 0.5), function (req, res) {
            res.send('variantB');
        });

        it('should fallthrough if first is skipped', function (done) {
            request(app)
                .get('/fallthrough')
                .set('ab-random', 0.42)
                .expect(200)
                .expect('variantB', done);
        });
    });
});<commit_msg>Formatting.<commit_after>var ab = require('../lib/express-ab');
var assert = require('assert');
var express = require('express');
var request = require('supertest');

describe('weighted', function () {
    function setReqVarMiddleware(req, res, next) {
        req.ab = {
            random: req.get('ab-random'),
            weightSum: 0
        };
        next();
    }

    function skipRouteMiddleware(req, res, next) {
        next('route');
    }

    var app = express();

    describe('variant selection', function () {
        var abTest = ab.test('variant-test');

        app.get('/', setReqVarMiddleware, abTest(null, 0.2), function (req, res) {
            res.send('variantA');
        });

        app.get('/', setReqVarMiddleware, abTest(null, 0.8), function (req, res) {
            res.send('variantB');
        });

        app.get('/random', abTest(null, 1), function (req, res) {
            res.send(req.ab);
        });

        it('should set ab object on req', function (done) {
            request(app)
                .get('/random')
                .expect(function (res) {
                    assert('random' in res.body);
                    assert('weightSum' in res.body);
                })
                .end(done);
        });

        it('should select route A', function (done) {
            request(app)
                .get('/')
                .set('ab-random', 0.11)
                .expect(200)
                .expect('variantA', done);
        });

        it('should select route B', function (done) {
            request(app)
                .get('/')
                .set('ab-random', 0.42)
                .expect(200)
                .expect('variantB', done);
        });
    });

    describe('fallthrough', function () {
        var abTest = ab.test('fallthrough-test');

        app.get('/fallthrough', skipRouteMiddleware, setReqVarMiddleware, abTest(null, 0.5), function (req, res) {
            res.send('variantA');
        });

        app.get('/fallthrough', setReqVarMiddleware, abTest(null, 0.5), function (req, res) {
            res.send('variantB');
        });

        it('should fallthrough if first is skipped', function (done) {
            request(app)
                .get('/fallthrough')
                .set('ab-random', 0.42)
                .expect(200)
                .expect('variantB', done);
        });
    });
});
{'fraction_non_alphanumeric': 0.16354742662696725, 'fraction_numerical': 0.011909825606125054, 'mean_word_length': 1.6495774647887325, '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': '2528628', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1302, 'n_words': 283}