/** @overview nodeunit test case. */

var testResource = require('../../../test_resource'),
    mockInjector = require('../../../mock_injector'),
    generateIndexTest = require('../../../../ci/worker/generate_index_test.js');

exports.setUp = function (done) {
    process.env.CI_LOG_DISABLED = true;
    mockInjector.restoreAll();
    done();
};

exports.tearDown = function (done) {
    process.env.CI_LOG_DISABLED = false;
    mockInjector.restoreAll();
    done();
};

exports['Load the template.'] = function (test) {
    generateIndexTest._loadIndexJsTmpl(function (err, tmpl) {
        test.ifError(err);
        test.ok(tmpl);
        test.done();
    });
};

exports['Generate index test.'] = function (test) {
    var destDir = testResource.resolveWorkFile('work_generate_index_test'),
        srcDir = testResource.resolveMockFile('mock_modules');
    generateIndexTest({
        srcDir: srcDir,
        destDir: destDir
    }, function (err) {
        testResource.prepareCleanWorkDir(destDir, function () {
            test.ifError(err);
            generateIndexTest({
                srcDir: srcDir,
                destDir: destDir
            }, function (err) {
                test.ifError(err);
                test.done();
            });
        });
    });
};

exports['Handle errors.'] = function (test) {
    mockInjector.inject(generateIndexTest, '_loadIndexJsTmpl', function (callback) {
        callback(new Error('Mock err.'));
    });
    var destDir = testResource.resolveWorkFile('work_generate_index_test'),
        srcDir = testResource.resolveMockFile('mock_modules');
    generateIndexTest({
        srcDir: srcDir,
        destDir: destDir
    }, function (err) {
        test.ok(err);
        test.done();
    });
};