"use strict";
var yok_1 = require("../../../yok");
var assert = require("assert");
var device_log_provider_1 = require("../../../appbuilder/device-log-provider");
var stubs_1 = require("../stubs");
function createTestInjector(loggingLevel, emptyFilteredData) {
    var testInjector = new yok_1.Yok();
    testInjector.register("logFilter", {
        loggingLevel: loggingLevel,
        filterData: function (platform, data, pid, logLevel) {
            return emptyFilteredData ? null : logLevel + " " + data;
        }
    });
    testInjector.register("logger", stubs_1.CommonLoggerStub);
    return testInjector;
}
;
describe("proton deviceLogProvider", function () {
    var testInjector, deviceLogProvider = null, testData = "testData", infoLogLevel = "INFO", fullLogLevel = "FULL", filteredInfoData = infoLogLevel + " " + testData, filteredFullData = fullLogLevel + " " + testData;
    describe("logData", function () {
        describe("when device identifier is not specified", function () {
            it("logs INFO messages when logging level is default", function () {
                testInjector = createTestInjector(infoLogLevel);
                deviceLogProvider = testInjector.resolve(device_log_provider_1.DeviceLogProvider);
                var emittedData = 'some default value that should be changed';
                deviceLogProvider.on("data", function (deviceIdentifier, data) {
                    emittedData = data;
                });
                deviceLogProvider.logData(testData, "platform");
                assert.deepEqual(emittedData, filteredInfoData);
            });
            it("does not emit data when whole data is filtered", function () {
                testInjector = createTestInjector(infoLogLevel, true);
                deviceLogProvider = testInjector.resolve(device_log_provider_1.DeviceLogProvider);
                var emittedData = 'some default value that should NOT be changed';
                deviceLogProvider.on("data", function (deviceIdentifier, data) {
                    emittedData = data;
                });
                deviceLogProvider.logData(testData, "platform");
                assert.deepEqual(emittedData, 'some default value that should NOT be changed');
            });
        });
        describe("when device identifier is specified", function () {
            it("logs INFO messages when logging level is INFO", function () {
                testInjector = createTestInjector(infoLogLevel);
                deviceLogProvider = testInjector.resolve(device_log_provider_1.DeviceLogProvider);
                var emittedData = 'some default value that should be changed';
                var expectedDeviceIdentifier = null;
                deviceLogProvider.on("data", function (deviceIdentifier, data) {
                    emittedData = data;
                    expectedDeviceIdentifier = deviceIdentifier;
                });
                deviceLogProvider.logData(testData, "platform", "deviceId");
                assert.deepEqual(emittedData, filteredInfoData);
                assert.deepEqual(expectedDeviceIdentifier, "deviceId");
            });
            it("does not emit data when whole data is filtered", function () {
                testInjector = createTestInjector(infoLogLevel, true);
                deviceLogProvider = testInjector.resolve(device_log_provider_1.DeviceLogProvider);
                var emittedData = 'some default value that should NOT be changed';
                var expectedDeviceIdentifier = null;
                deviceLogProvider.on("data", function (deviceIdentifier, data) {
                    emittedData = data;
                    expectedDeviceIdentifier = deviceIdentifier;
                });
                deviceLogProvider.logData(testData, "platform");
                assert.deepEqual(emittedData, 'some default value that should NOT be changed');
                assert.deepEqual(expectedDeviceIdentifier, null);
            });
        });
    });
    describe("setLogLevel", function () {
        it("changes logFilter's loggingLevel when device identifier is not specified", function () {
            testInjector = createTestInjector(infoLogLevel);
            deviceLogProvider = testInjector.resolve(device_log_provider_1.DeviceLogProvider);
            deviceLogProvider.setLogLevel(fullLogLevel);
            var logFilter = testInjector.resolve("logFilter");
            assert.deepEqual(logFilter.loggingLevel, fullLogLevel);
        });
        it("does not change logFilter's loggingLevel when device identifier is specified", function () {
            testInjector = createTestInjector(infoLogLevel);
            deviceLogProvider = testInjector.resolve(device_log_provider_1.DeviceLogProvider);
            deviceLogProvider.setLogLevel(fullLogLevel, "deviceID");
            var logFilter = testInjector.resolve("logFilter");
            assert.deepEqual(logFilter.loggingLevel, infoLogLevel);
        });
    });
    describe("keeps correct log level for each device", function () {
        beforeEach(function () {
            testInjector = createTestInjector(infoLogLevel);
            deviceLogProvider = testInjector.resolve(device_log_provider_1.DeviceLogProvider);
        });
        it("emits full log level for specific deviceIdentifier and info for the rest of the devices", function () {
            deviceLogProvider.setLogLevel(fullLogLevel, "device1");
            var emittedData = 'some default value that should be changed';
            var expectedDeviceIdentifier = null;
            deviceLogProvider.on("data", function (deviceIdentifier, data) {
                emittedData = data;
                expectedDeviceIdentifier = deviceIdentifier;
            });
            deviceLogProvider.logData(testData, "platform", "device1");
            assert.deepEqual(emittedData, filteredFullData);
            assert.deepEqual(expectedDeviceIdentifier, "device1");
            deviceLogProvider.logData(testData, "platform", "device2");
            assert.deepEqual(emittedData, filteredInfoData);
            assert.deepEqual(expectedDeviceIdentifier, "device2");
            deviceLogProvider.logData(testData, "platform", "device1");
            assert.deepEqual(emittedData, filteredFullData);
            assert.deepEqual(expectedDeviceIdentifier, "device1");
        });
        it("emits info log level for all devices, when setLogLevel is called without identifier", function () {
            deviceLogProvider.setLogLevel(fullLogLevel, "device1");
            var emittedData = 'some default value that should be changed';
            var expectedDeviceIdentifier = null;
            deviceLogProvider.on("data", function (deviceIdentifier, data) {
                emittedData = data;
                expectedDeviceIdentifier = deviceIdentifier;
            });
            deviceLogProvider.logData(testData, "platform", "device1");
            assert.deepEqual(emittedData, filteredFullData);
            assert.deepEqual(expectedDeviceIdentifier, "device1");
            deviceLogProvider.setLogLevel(infoLogLevel);
            deviceLogProvider.logData(testData, "platform", "device2");
            assert.deepEqual(emittedData, filteredInfoData);
            assert.deepEqual(expectedDeviceIdentifier, "device2");
            deviceLogProvider.logData(testData, "platform", "device1");
            assert.deepEqual(emittedData, filteredInfoData);
            assert.deepEqual(expectedDeviceIdentifier, "device1");
        });
    });
});
