"use strict";
var yok_1 = require("../../yok");
var xcode_select_service_1 = require("../../services/xcode-select-service");
var chai_1 = require("chai");
var path = require("path");
var Future = require("fibers/future");
var executionStopped = false;
function createTestInjector(config) {
    var testInjector = new yok_1.Yok();
    testInjector.register("childProcess", {
        spawnFromEvent: function (command, args, event) { return Future.fromResult({
            stdout: config.xcodeSelectStdout
        }); }
    });
    testInjector.register("errors", {
        failWithoutHelp: function (message) {
            var args = [];
            for (var _i = 1; _i < arguments.length; _i++) {
                args[_i - 1] = arguments[_i];
            }
            executionStopped = true;
        }
    });
    testInjector.register("hostInfo", {
        isDarwin: config.isDarwin
    });
    testInjector.register("xcodeSelectService", xcode_select_service_1.XcodeSelectService);
    return testInjector;
}
describe("xcode-select-service", function () {
    var injector, service, defaultXcodeSelectStdout = "/Applications/Xcode.app/Contents/Developer/";
    beforeEach(function () {
        executionStopped = false;
    });
    it("gets correct path to Developer directory on Mac OS X whitout whitespaces", function () {
        injector = createTestInjector({ xcodeSelectStdout: "  /Applications/Xcode.app/Contents/Developer/  ", isDarwin: true });
        service = injector.resolve("$xcodeSelectService");
        chai_1.assert.deepEqual(service.getDeveloperDirectoryPath().wait(), defaultXcodeSelectStdout, "xcode-select service should get correct trimmed  path to Developer directory on Mac OS X.");
    });
    it("gets correct path to Developer directory on Mac OS X whitout new lines", function () {
        injector = createTestInjector({ xcodeSelectStdout: "\r\n/Applications/Xcode.app/Contents/Developer/\n", isDarwin: true });
        service = injector.resolve("$xcodeSelectService");
        chai_1.assert.deepEqual(service.getDeveloperDirectoryPath().wait(), defaultXcodeSelectStdout, "xcode-select service should get correct trimmed  path to Developer directory on Mac OS X.");
    });
    it("gets correct path to Developer directory on Mac OS X", function () {
        injector = createTestInjector({ xcodeSelectStdout: defaultXcodeSelectStdout, isDarwin: true });
        service = injector.resolve("$xcodeSelectService");
        chai_1.assert.deepEqual(service.getDeveloperDirectoryPath().wait(), defaultXcodeSelectStdout, "xcode-select service should get correct path to Developer directory on Mac OS X.");
    });
    it("gets correct path to Contents directory on Mac OS X", function () {
        var expected = path.join("/Applications", "Xcode.app", "Contents");
        injector = createTestInjector({ xcodeSelectStdout: defaultXcodeSelectStdout, isDarwin: true });
        service = injector.resolve("$xcodeSelectService");
        chai_1.assert.deepEqual(service.getContentsDirectoryPath().wait(), expected, "xcode-select service should get correct path to Contents directory on Mac OS X.");
    });
    it("stops execution when trying to get Developer directory if not on Mac OS X", function () {
        injector = createTestInjector({ xcodeSelectStdout: defaultXcodeSelectStdout, isDarwin: false });
        service = injector.resolve("$xcodeSelectService");
        service.getDeveloperDirectoryPath().wait();
        chai_1.assert.deepEqual(executionStopped, true, "xcode-select service should stop executon unless on Mac OS X.");
    });
    it("stops execution when trying to get Contents directory if not on Mac OS X", function () {
        injector = createTestInjector({ xcodeSelectStdout: defaultXcodeSelectStdout, isDarwin: false });
        service = injector.resolve("$xcodeSelectService");
        service.getContentsDirectoryPath().wait();
        chai_1.assert.deepEqual(executionStopped, true, "xcode-select service should stop executon unless on Mac OS X.");
    });
    it("stops execution when Developer directory is empty on Mac OS X", function () {
        injector = createTestInjector({ xcodeSelectStdout: "", isDarwin: true });
        service = injector.resolve("$xcodeSelectService");
        service.getDeveloperDirectoryPath().wait();
        chai_1.assert.deepEqual(executionStopped, true, "xcode-select service should stop executon when Developer directory is empty on Mac OS X.");
    });
    it("stops execution when Contents directory is empty on Mac OS X", function () {
        injector = createTestInjector({ xcodeSelectStdout: "", isDarwin: true });
        service = injector.resolve("$xcodeSelectService");
        service.getContentsDirectoryPath().wait();
        chai_1.assert.deepEqual(executionStopped, true, "xcode-select service should stop executon when Contents directory is empty on Mac OS X.");
    });
});
