/** @overview nodeunit test case. */

var testResource = require('../../../test_resource'),
    fs = require('fs'),
    file = require('../../../../lib/file'),
    mockInjector = require('../../../mock_injector'),
    generateApiguide = require('../../../../ci/worker/generate_apiguide.js');

exports.setUp = function (done) {
    done();
};

exports.tearDown = function (done) {
    mockInjector.restoreAll();
    done();
};

exports['Prepare API guide tmpl.'] = function (test) {
    generateApiguide._prepareAPIGuideTmpl({}, function (err) {
        test.ifError(err);
        mockInjector.inject(generateApiguide, '_apiguideTmplFile', function () {
            return 'foo/bar/';
        });
        generateApiguide._prepareAPIGuideTmpl({}, function (err) {
            mockInjector.restore();
            test.ok(err);
            test.done();
        });
    });
};
exports['Prepare API guide work dir.'] = function (test) {
    generateApiguide._prepareAPIGuideWorkDir(function (err, workDir) {
        test.ifError(err);
        test.ok(workDir);
        test.done();
    });
};
exports['Conf file data.'] = function (test) {
    var data = generateApiguide._confFileData('b');
    test.ok(data);
    test.done();
};
exports['Write json file.'] = function (test) {
    var workFile = testResource.resolveWorkFile('work_generate_apiguide_json.json');
    generateApiguide._writeJSONFile(workFile, {foo: 'bar'}, function (err) {
        test.ifError(err);
        test.done();
    });
};

exports['Find the apiguide tmpl.'] = function (test) {
    var filename = generateApiguide._apiguideTmplFile();
    fs.exists(filename, function (exists) {
        test.ok(generateApiguide);
        test.done();
    });
};

exports['Look up jsdoc binary file.'] = function (test) {
    generateApiguide._lookUpJsdocBin('/foo/bar', function (err) {
        test.ok(err);
        generateApiguide._lookUpJsdocBin('.', function (err, bin) {
            test.ifError(err);
            test.ok(bin);
            test.done();
        });
    });
};

exports['Generate api doc.'] = function (test) {
    testResource.prepareCleanWorkDirForTestFile(__filename, function (err, workDir) {
        generateApiguide({
            src: testResource.resolveMockFile('mock_modules'),
            destDir: workDir
        }, function (err) {
            test.ifError(err);
            test.done();
        });
    });
};


exports['Handle err.'] = function (test) {
    testResource.prepareCleanWorkDirForTestFile(__filename, function (err, workDir) {
        mockInjector.inject(generateApiguide, '_prepareAPIGuideWorkDir', function (callback) {
            callback(new Error('mock error'));
        });
        generateApiguide({
            src: testResource.resolveMockFile('mock_modules'),
            destDir: workDir
        }, function (err) {
            test.ok(err);
            mockInjector.restore();
            mockInjector.inject(generateApiguide, '_lookUpJsdocBin', function (data, callback) {
                callback(new Error('mock error'));
            });
            generateApiguide({
                src: testResource.resolveMockFile('mock_modules'),
                destDir: workDir
            }, function (err) {
                test.ok(err);
                mockInjector.restore();
                mockInjector.inject(generateApiguide, '_writeJSONFile', function (c1, c2, callback) {
                    callback(new Error('mock error'));
                });
                generateApiguide({
                    src: testResource.resolveMockFile('mock_modules'),
                    destDir: workDir
                }, function (err) {
                    test.ok(err);
                    mockInjector.restore();
                    mockInjector.inject(file, 'mkdirP', function (f, callback) {
                        callback(new Error('mock error'));
                    });
                    generateApiguide({
                        src: testResource.resolveMockFile('mock_modules'),
                        destDir: workDir
                    }, function (err) {
                        mockInjector.restore();
                        test.done();
                    });
                });
            });
        });
    });
};
