/**
 * Tests DelayEventHandler
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
var chai_1 = require("chai");
var DelayEventHandler_1 = require("../../../src/client/event/DelayEventHandler");
describe('DelayEventHandler', function () {
    it('handles delay event with the expected delay', function (done) {
        var handle_event_called = false;
        var set_timeout_called = false;
        var actual_action = '';
        var actual_data = null;
        var actual_delay = 0;
        var expected_delay = 25000;
        var expected_action = 'foobar';
        var expected_data = { indv: 'retry' };
        var client = {
            handleEvent: function (event_type, data) {
                actual_action = event_type;
                actual_data = data;
                handle_event_called = true;
            },
        };
        var action = {
            action: 'delay',
            seconds: 25,
            then: {
                action: expected_action,
            },
        };
        var sut = new DelayEventHandler_1.DelayEventHandler(client);
        var old_setTimeout = global.setTimeout;
        global.setTimeout = function (callback, delay) {
            set_timeout_called = true;
            actual_delay = delay;
            callback();
            return new NodeJS.Timeout();
        };
        try {
            sut.handle('delay', function () {
                chai_1.expect(actual_action).to.equal(expected_action);
                chai_1.expect(actual_data).to.equal(expected_data);
                done();
            }, action);
        }
        catch (e) {
        }
        finally {
            global.setTimeout = old_setTimeout;
        }
        chai_1.expect(set_timeout_called).to.equal(true);
        chai_1.expect(handle_event_called).to.equal(true);
        chai_1.expect(actual_delay).to.equal(expected_delay);
        done();
    }),
        it('Delay defaults to zero on non-numeric seconds', function (done) {
            var set_timeout_called = false;
            var actual_delay = -1;
            var expected_delay = 0;
            var client = {
                handleEvent: function (_, __) { },
            };
            var action = {
                action: 'delay',
                seconds: 'string-foo',
                then: {
                    action: 'foo',
                },
            };
            var sut = new DelayEventHandler_1.DelayEventHandler(client);
            var old_setTimeout = global.setTimeout;
            global.setTimeout = function (callback, delay) {
                set_timeout_called = true;
                actual_delay = delay;
                callback();
                return new NodeJS.Timeout();
            };
            try {
                sut.handle('delay', function () { }, action);
            }
            catch (e) {
            }
            finally {
                global.setTimeout = old_setTimeout;
            }
            chai_1.expect(set_timeout_called).to.equal(true);
            chai_1.expect(actual_delay).to.equal(expected_delay);
            done();
        }),
        it('Calls callback function and returns itself', function (done) {
            var callback_called = false;
            var returned = null;
            var client = {
                handleEvent: function (_, __, cb) {
                    cb();
                },
            };
            var action = {
                action: 'delay',
                seconds: 0,
                then: {
                    action: 'foo',
                },
            };
            var sut = new DelayEventHandler_1.DelayEventHandler(client);
            var old_setTimeout = global.setTimeout;
            global.setTimeout = function (callback, _) {
                callback();
                return {};
            };
            try {
                returned = sut.handle('delay', function () {
                    callback_called = true;
                }, action);
            }
            catch (e) {
                console.log(e);
            }
            finally {
                global.setTimeout = old_setTimeout;
            }
            chai_1.expect(callback_called).to.equal(true);
            chai_1.expect(returned).to.deep.equal(sut);
            done();
        });
});
//# sourceMappingURL=data:application/json;base64, 