"use strict";
var yok_1 = require("../../yok");
var process_service_1 = require("../../services/process-service");
var chai_1 = require("chai");
var processExitSignals = ["exit", "SIGINT", "SIGTERM"];
var emptyFunction = function () { };
function createTestInjector() {
    var testInjector = new yok_1.Yok();
    testInjector.register("processService", process_service_1.ProcessService);
    return testInjector;
}
describe("Process service", function () {
    var testInjector;
    var $processService;
    beforeEach(function () {
        testInjector = createTestInjector();
        $processService = testInjector.resolve("processService");
    });
    it("should not add only one listener for the exit, SIGIN and SIGTERM events.", function () {
        $processService.attachToProcessExitSignals({}, emptyFunction);
        $processService.attachToProcessExitSignals({}, emptyFunction);
        _.each(processExitSignals, function (signal) {
            var actualListeners = _.filter(process.listeners(signal), function (listener) { return listener.toString().indexOf("executeAllCallbacks") >= 0; });
            chai_1.assert.deepEqual(actualListeners.length, 1);
        });
    });
    it("should add listener with context only once if there already is callback with the same context.", function () {
        var context = { test: "test" };
        var listener = function () { return 42; };
        $processService.attachToProcessExitSignals(context, listener);
        $processService.attachToProcessExitSignals(context, listener);
        chai_1.assert.deepEqual($processService.listenersCount, 1);
    });
    it("should add two different listeners for one context.", function () {
        var context = { test: "test" };
        var numberListener = function () { return 42; };
        var booleanListener = function () { return true; };
        $processService.attachToProcessExitSignals(context, numberListener);
        $processService.attachToProcessExitSignals(context, booleanListener);
        chai_1.assert.deepEqual($processService.listenersCount, 2);
    });
    it("should add one listener with different context twice.", function () {
        var listener = function () { return 42; };
        $processService.attachToProcessExitSignals({}, listener);
        $processService.attachToProcessExitSignals({}, listener);
        chai_1.assert.deepEqual($processService.listenersCount, 2);
    });
    it("should execute all attached listeners.", function () {
        var hasCalledFirstListener = false;
        var hasCalledSecondListener = false;
        var hasCalledThirdListener = false;
        var firstListener = function () {
            hasCalledFirstListener = true;
        };
        var secondListener = function () {
            hasCalledSecondListener = true;
        };
        var thirdListener = function () {
            hasCalledThirdListener = true;
        };
        $processService.attachToProcessExitSignals({}, firstListener);
        $processService.attachToProcessExitSignals({}, secondListener);
        $processService.attachToProcessExitSignals({}, thirdListener);
        global.process.emit("SIGTERM");
        chai_1.assert.isTrue(hasCalledFirstListener);
        chai_1.assert.isTrue(hasCalledSecondListener);
        chai_1.assert.isTrue(hasCalledThirdListener);
    });
});
