var generateIndex = require('../../../../ci/worker/generate_index'),
    fs = require('fs'),
    mockInjector = require('../../../mock_injector'),
    testResource = require('../../../test_resource');

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 index.js template'] = function (test) {
    generateIndex._loadIndexJsTmpl(function (err, tmpl) {
        test.ifError(err);
        test.ok(tmpl);
        test.done();
    });
};

exports['Get doc data'] = function (test) {
    var data = generateIndex._docData({
        overview: 'Foo.'
    });
    test.equal(data.overview, 'Foo.');
    test.done();
};

exports['Create a index file'] = function (test) {
    var dest = testResource.resolveWorkFile('work_index.js');
    generateIndex({
        dir: __dirname,
        dest: dest
    }, function (err) {
        test.ifError(err);
        test.done();
    });
};

exports['Render a data'] = function (test) {
    var data = generateIndex._renderData({});
    test.ok(data);
    test.done();
};
exports['Get modules data.'] = function (test) {
    var moduleDir = '/foo/bar/_not_exist';
    generateIndex._modulesData(moduleDir, '*', 'baz', function (err) {
        test.ifError(err);
        moduleDir = testResource.resolveMockFile('mock_modules');
        generateIndex._modulesData(moduleDir, '*', 'mock_do_some*', function (err) {
            test.ifError(err);
            test.done();
        });
    });
};


exports['Handle error.'] = function (test) {
    mockInjector.inject(generateIndex, '_modulesData', function (d, p, c, callback) {
        callback(new Error('mock modules data error'));
    });
    generateIndex({
        dir: 'foo'
    }, function (err) {
        test.ok(err);
        mockInjector.restore();
        mockInjector.inject(generateIndex, '_prototypeData', function (d, o, callback) {
            callback(new Error('mock prototype data error'));
        });
        generateIndex({
            dir: 'foo'
        }, function (err) {
            test.ok(err);
            mockInjector.restore();
            test.done();
        });
    });
};