"use strict";
var chai_1 = require("chai");
var device_app_data_factory_1 = require("../../../mobile/device-app-data/device-app-data-factory");
var device_platforms_constants_1 = require("../../../mobile/device-platforms-constants");
var errors_1 = require("../../../errors");
var file_system_1 = require("../../../file-system");
var Future = require("fibers/future");
var host_info_1 = require("../../../host-info");
var local_to_device_path_data_factory_1 = require("../../../mobile/local-to-device-path-data-factory");
var mobile_helper_1 = require("../../../mobile/mobile-helper");
var project_files_manager_1 = require("../../../services/project-files-manager");
var logger_1 = require("../../../logger");
var path = require("path");
var yok_1 = require("../../../yok");
var project_files_provider_base_1 = require("../../../services/project-files-provider-base");
var temp = require("temp");
temp.track();
var testedApplicationIdentifier = "com.telerik.myApp";
var iOSDeviceProjectRootPath = "/Library/Application Support/LiveSync/app";
var androidDeviceProjectRootPath = "/data/local/tmp/sync";
var IOSAppIdentifierMock = (function () {
    function IOSAppIdentifierMock() {
        this.platform = "iOS";
        this.appIdentifier = testedApplicationIdentifier;
        this.device = null;
        this.deviceProjectRootPath = iOSDeviceProjectRootPath;
    }
    IOSAppIdentifierMock.prototype.isLiveSyncSupported = function () {
        return Future.fromResult(true);
    };
    return IOSAppIdentifierMock;
})();
var AndroidAppIdentifierMock = (function () {
    function AndroidAppIdentifierMock() {
        this.platform = "Android";
        this.appIdentifier = testedApplicationIdentifier;
        this.device = null;
        this.deviceProjectRootPath = androidDeviceProjectRootPath;
    }
    AndroidAppIdentifierMock.prototype.isLiveSyncSupported = function () {
        return Future.fromResult(true);
    };
    return AndroidAppIdentifierMock;
})();
var DeviceAppDataProvider = (function () {
    function DeviceAppDataProvider() {
    }
    DeviceAppDataProvider.prototype.createFactoryRules = function () {
        return {
            iOS: {
                vanilla: IOSAppIdentifierMock
            },
            Android: {
                vanilla: AndroidAppIdentifierMock
            }
        };
    };
    return DeviceAppDataProvider;
})();
var MobilePlatformsCapabilitiesMock = (function () {
    function MobilePlatformsCapabilitiesMock() {
    }
    MobilePlatformsCapabilitiesMock.prototype.getPlatformNames = function () {
        return _.keys(this.getAllCapabilities());
    };
    MobilePlatformsCapabilitiesMock.prototype.getAllCapabilities = function () {
        return {
            iOS: {
                wirelessDeploy: false,
                cableDeploy: true,
                companion: false,
                hostPlatformsForDeploy: ["darwin"]
            },
            Android: {
                wirelessDeploy: false,
                cableDeploy: true,
                companion: false,
                hostPlatformsForDeploy: ["win32", "darwin", "linux"]
            }
        };
    };
    return MobilePlatformsCapabilitiesMock;
})();
function createTestInjector() {
    var testInjector = new yok_1.Yok();
    testInjector.register("deviceAppDataFactory", device_app_data_factory_1.DeviceAppDataFactory);
    testInjector.register("deviceAppDataProvider", DeviceAppDataProvider);
    testInjector.register("devicePlatformsConstants", device_platforms_constants_1.DevicePlatformsConstants);
    testInjector.register("errors", errors_1.Errors);
    testInjector.register("fs", file_system_1.FileSystem);
    testInjector.register("hostInfo", host_info_1.HostInfo);
    testInjector.register("localToDevicePathDataFactory", local_to_device_path_data_factory_1.LocalToDevicePathDataFactory);
    testInjector.register("mobileHelper", mobile_helper_1.MobileHelper);
    testInjector.register("mobilePlatformsCapabilities", MobilePlatformsCapabilitiesMock);
    testInjector.register("projectFilesProvider", project_files_provider_base_1.ProjectFilesProviderBase);
    testInjector.register("projectFilesManager", project_files_manager_1.ProjectFilesManager);
    testInjector.register("options", {});
    testInjector.register("staticConfig", {
        disableAnalytics: true
    });
    testInjector.register("logger", logger_1.Logger);
    testInjector.register("config", {});
    return testInjector;
}
function createFiles(testInjector, filesToCreate) {
    return (function () {
        var fs = testInjector.resolve("fs");
        var directoryPath = temp.mkdirSync("Project Files Manager Tests");
        _.each(filesToCreate, function (file) { return fs.writeFile(path.join(directoryPath, file), "").wait(); });
        return directoryPath;
    }).future()();
}
describe("Project Files Manager Tests", function () {
    var testInjector, projectFilesManager, deviceAppDataFactory, mobileHelper;
    beforeEach(function () {
        testInjector = createTestInjector();
        projectFilesManager = testInjector.resolve("projectFilesManager");
        deviceAppDataFactory = testInjector.resolve("deviceAppDataFactory");
        mobileHelper = testInjector.resolve("mobileHelper");
    });
    it("maps non-platform specific files to device file paths for ios platform", function () {
        var deviceAppData = deviceAppDataFactory.create(testedApplicationIdentifier, "iOS", null);
        var files = ["~/TestApp/app/test.js", "~/TestApp/app/myfile.js"];
        var localToDevicePaths = projectFilesManager.createLocalToDevicePaths(deviceAppData, "~/TestApp/app", files);
        _.each(localToDevicePaths, function (localToDevicePathData, index) {
            chai_1.assert.equal(files[index], localToDevicePathData.getLocalPath());
            chai_1.assert.equal(mobileHelper.buildDevicePath(iOSDeviceProjectRootPath, path.basename(files[index])), localToDevicePathData.getDevicePath());
            chai_1.assert.equal(path.basename(files[index]), localToDevicePathData.getRelativeToProjectBasePath());
        });
    });
    it("maps non-platform specific files to device file paths for android platform", function () {
        var deviceAppData = deviceAppDataFactory.create(testedApplicationIdentifier, "Android", null);
        var files = ["~/TestApp/app/test.js", "~/TestApp/app/myfile.js"];
        var localToDevicePaths = projectFilesManager.createLocalToDevicePaths(deviceAppData, "~/TestApp/app", files);
        _.each(localToDevicePaths, function (localToDevicePathData, index) {
            chai_1.assert.equal(files[index], localToDevicePathData.getLocalPath());
            chai_1.assert.equal(mobileHelper.buildDevicePath(androidDeviceProjectRootPath, path.basename(files[index])), localToDevicePathData.getDevicePath());
            chai_1.assert.equal(path.basename(files[index]), localToDevicePathData.getRelativeToProjectBasePath());
        });
    });
    it("maps ios platform specific file to device file path", function () {
        var deviceAppData = deviceAppDataFactory.create(testedApplicationIdentifier, "iOS", null);
        var filePath = "~/TestApp/app/test.ios.js";
        var localToDevicePathData = projectFilesManager.createLocalToDevicePaths(deviceAppData, "~/TestApp/app", [filePath])[0];
        chai_1.assert.equal(filePath, localToDevicePathData.getLocalPath());
        chai_1.assert.equal(mobileHelper.buildDevicePath(iOSDeviceProjectRootPath, "test.js"), localToDevicePathData.getDevicePath());
        chai_1.assert.equal("test.ios.js", localToDevicePathData.getRelativeToProjectBasePath());
    });
    it("maps android platform specific file to device file path", function () {
        var deviceAppData = deviceAppDataFactory.create(testedApplicationIdentifier, "Android", null);
        var filePath = "~/TestApp/app/test.android.js";
        var localToDevicePathData = projectFilesManager.createLocalToDevicePaths(deviceAppData, "~/TestApp/app", [filePath])[0];
        chai_1.assert.equal(filePath, localToDevicePathData.getLocalPath());
        chai_1.assert.equal(mobileHelper.buildDevicePath(androidDeviceProjectRootPath, "test.js"), localToDevicePathData.getDevicePath());
        chai_1.assert.equal("test.android.js", localToDevicePathData.getRelativeToProjectBasePath());
    });
    it("filters android specific files", function () {
        var files = ["test.ios.x", "test.android.x"];
        var directoryPath = createFiles(testInjector, files).wait();
        projectFilesManager.processPlatformSpecificFiles(directoryPath, "android").wait();
        var fs = testInjector.resolve("fs");
        chai_1.assert.isFalse(fs.exists(path.join(directoryPath, "test.ios.x")).wait());
        chai_1.assert.isTrue(fs.exists(path.join(directoryPath, "test.x")).wait());
        chai_1.assert.isFalse(fs.exists(path.join(directoryPath, "test.android.x")).wait());
    });
    it("filters ios specific files", function () {
        var files = ["index.ios.html", "index1.android.html", "a.test"];
        var directoryPath = createFiles(testInjector, files).wait();
        projectFilesManager.processPlatformSpecificFiles(directoryPath, "ios").wait();
        var fs = testInjector.resolve("fs");
        chai_1.assert.isFalse(fs.exists(path.join(directoryPath, "index1.android.html")).wait());
        chai_1.assert.isFalse(fs.exists(path.join(directoryPath, "index1.html")).wait());
        chai_1.assert.isTrue(fs.exists(path.join(directoryPath, "index.html")).wait());
        chai_1.assert.isTrue(fs.exists(path.join(directoryPath, "a.test")).wait());
    });
    it("doesn't filter non platform specific files", function () {
        var files = ["index1.js", "index2.js", "index3.js"];
        var directoryPath = createFiles(testInjector, files).wait();
        projectFilesManager.processPlatformSpecificFiles(directoryPath, "ios").wait();
        var fs = testInjector.resolve("fs");
        chai_1.assert.isTrue(fs.exists(path.join(directoryPath, "index1.js")).wait());
        chai_1.assert.isTrue(fs.exists(path.join(directoryPath, "index2.js")).wait());
        chai_1.assert.isTrue(fs.exists(path.join(directoryPath, "index3.js")).wait());
    });
});
