import {DeadMansSwitch} from "../../../source/util/deadmansswitch.mjs";


describe('DeadMansSwitch', function () {

    describe('run instance', function () {

        it('should run', function (done) {

            const ms1 =  Date.now();

            new DeadMansSwitch(100, () => {

                const ms2 =  Date.now();

                const diff = ms2 - ms1;
            if (diff < 90) {
                done(new Error('to short ' + diff));
                return;
            }
            done();
            })


        });
    });

    describe('run instance and touch', function () {

        it('should run', function (done) {

            const ms1 = Date.now();

            const deadmansswitch = new DeadMansSwitch(100, () => {

                const ms2 = Date.now();

                const diff = ms2 - ms1;

                if (ms1 > ms2) {
                    done(new Error('timing error'));
                    return;
                }

                if (diff < 550) {
                    done(new Error('to short ' + diff));
                    return;
                }
                done();
            })

            //   0 ms: init() -> wait 100 ms 
            //  50 ms: touch() -> wait 100 ms
            //          -> wait 100
            // 100 ms: touch(500) -> wait 500 ms
            //          -> wait 500
            // 600 ms: execute callback
            setTimeout(() => {
                deadmansswitch.touch()

                setTimeout(() => {
                    deadmansswitch.touch(500)
                }, 50)

            }, 50)


        });
    });

});
