import {expect} from "chai"
import {Processing} from "../../../source/util/processing.mjs";


describe('Processing', function () {

    describe('run instance', function () {

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

            let counter = 0;
            let t = +new Date();

            const desiredDelay = 200;
            this.timeout(desiredDelay+100);
            
            // delay 12 
            new Processing((v) => {
                counter++;
                return v;
            }, (v) => {
                counter++
                return v;
            }, function (v) {
                counter++
                return v;
            }, desiredDelay,function (v) {
                counter++
                return v;
            }).run('test').then((v) => {

                try {
                    expect(counter).to.be.equal(4);
                    expect(v === 'test').to.be.true;
                    // check delay
                    expect(+new Date() > t + desiredDelay+1).to.be.true;
                } catch (e) {
                    return done(e);
                }

                done();
            })


        });

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

            let counter = 0;

            new Processing((value) => {
                counter++
                return value;
            }).run('test').then((v) => {
                try {
                    expect(v === 'test').to.be.true;
                } catch (e) {
                    return done(e);
                }

                done()
            }).catch(e => done(e));


        });

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

            let counter = 0;

            new Processing().run('test').then((v) => {

                try {
                    expect(v === 'test').to.be.true;
                } catch (e) {
                    return done(e);
                }

                done()
            }).catch(e => done(e));


        });


    });

    describe('create new instance', function () {

        it('should return a Processing object', function () {
            expect(new Processing()).to.be.a('object');
        });

        it('should return a Processing object', function () {
            expect(new Processing(function () {
            })).to.be.a('object');
        });

        it('should throw TypeError', function () {
            expect(() => new Processing(true)).to.throw(TypeError);
        });

        it('should throw TypeError', function () {
            expect(() => new Processing("test")).to.throw(TypeError);
        });


    });

});