<commit_before>'use strict';

const HapiAutoRoute = require('../index.js');
const Hapi = require('hapi');

const { expect } = require('code');
const lab = exports.lab = require('lab').script();

lab.experiment('plugin', () => {

    lab.it('pluging.pkg should set with package.json', () => {

        const plugin = HapiAutoRoute.plugin;
        expect(plugin).to.not.undefined();
        expect(plugin.pkg).to.not.undefined();
        expect(plugin.pkg).include(['name', 'version', 'description']);
    });

    lab.it('Should have register function', () => {

        const plugin = HapiAutoRoute.plugin;
        expect(plugin.register).to.not.undefined();
        expect(plugin.register).to.be.function();
    });

    lab.it('Register route to the server', async () => {

        const server = Hapi.Server({ port: 3000 });
        await server.register({
            plugin: HapiAutoRoute,
            options: {
                routes_dir: 'test/fixtures/routes',
                pattern: '**/!(_)*.js'
            }
        });
        const result = await server.inject('/');
        expect(result.statusCode).to.be.equal(200);
    });

    lab.it('Apply default options', async () => {

        const server = Hapi.Server({ port: 3000 });
        await server.register({
            plugin: HapiAutoRoute,
            options: {
                routes_dir: 'test/fixtures/routes'
            }
        });
        const result = await server.inject('/');
        expect(result.statusCode).to.be.equal(200);
    });


    lab.it('Change route path if use_prefix is true', async () => {

        const server = Hapi.Server({ port: 3000 });
        await server.register({
            plugin: HapiAutoRoute,
            options: {
                routes_dir: 'test/fixtures/routes',
                use_prefix: true
            }
        });
        const result = await server.inject('/auth/login');
        expect(result.statusCode).to.be.equal(200);
    });
});<commit_msg>Refactor tests<commit_after>'use strict';

const HapiAutoRoute = require('../index.js');
const Hapi = require('hapi');

const { expect } = require('code');
const lab = exports.lab = require('lab').script();

lab.experiment('plugin', () => {

    let server = null;
    let plugin = null;
    let hapiAutoRouteOptions = null;

    lab.beforeEach(() => {

        server = Hapi.Server({ port: 3000 });
        plugin = HapiAutoRoute.plugin;
        hapiAutoRouteOptions = {
            routes_dir: 'test/fixtures/routes',
            pattern: '**/*.js',
            use_prefix: true
        };
    });

    lab.afterEach(() => {

        server = null;
    });

    lab.it('pluging.pkg should set with package.json', () => {

        expect(plugin).to.not.undefined();
        expect(plugin.pkg).to.not.undefined();
        expect(plugin.pkg).include(['name', 'version', 'description']);
    });

    lab.it('Should have register function', () => {

        expect(plugin.register).to.not.undefined();
        expect(plugin.register).to.be.function();
    });

    lab.it('Register route to the server', async () => {

        hapiAutoRouteOptions.use_prefix = false;
        await server.register({
            plugin: HapiAutoRoute,
            options: hapiAutoRouteOptions
        });
        const result = await server.inject('/');
        expect(result.statusCode).to.be.equal(200);
    });

    lab.it('Apply default options', async () => {

        delete hapiAutoRouteOptions.use_prefix;
        delete hapiAutoRouteOptions.pattern;
        await server.register({
            plugin: HapiAutoRoute,
            options: hapiAutoRouteOptions
        });
        const result = await server.inject('/');
        expect(result.statusCode).to.be.equal(200);
    });


    lab.it('Change route path if use_prefix is true', async () => {

        await server.register({
            plugin: HapiAutoRoute,
            options: hapiAutoRouteOptions
        });
        const result = await server.inject('/auth/login');
        expect(result.statusCode).to.be.equal(200);
    });
});
{'fraction_non_alphanumeric': 0.15613474305384806, 'fraction_numerical': 0.008359970494221785, 'mean_word_length': 2.19309262166405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '6232167', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1135, 'n_words': 227}