import chai = require("chai");
const expect = chai.expect;
import "mocha";
import AntlrStepParser from "../../src/_infra/AntlrStepParser";
import Action from "../../src/domain/Action";
import InteractionFactory from "../../src/domain/InteractionFactory";
import SequenceEvaluator from "../../src/domain/SequenceEvaluator";
import StepFactory from "../../src/domain/StepFactory";

describe("Domain - SequenceEvaluator", () => {

    const stepParser = new AntlrStepParser();
    const stepFactory = new StepFactory(stepParser);

    describe("complex evaluator", () => {
        const stepTextList = [
            "(clickSearchBar typeProductName clickOnSearchButton) or (clickSearchBar typeProductName pressEnterKey)",
            "addProductToBasket$1 if 'asksForWaranty' closeWarantyModal clickShowBasketButton clickOrderButton if 'Does everything went ok' OK else NOK",
        ];
        const stepList = [];
        let evaluator: SequenceEvaluator;
        const evaluatorName = "Complex evaluator";
        const sessionID = "1";

        it("create the evaluator", () => {
            evaluator = new SequenceEvaluator(evaluatorName, sessionID);
        });

        it("add a step to the evaluator", () => {
            return stepFactory.createStep(stepTextList[0]).then((step) => {
                stepList.push(step);
                evaluator.addStep(step);
            });
        });

        it("add a step to the evaluator", () => {
            return stepFactory.createStep(stepTextList[1]).then((step) => {
                stepList.push(step);
                evaluator.addStep(step);
            });
        });

        describe("evaluate 'start' ", () => {
            it("should have an entering action and not continuing action", () => {
                const interactions = [InteractionFactory.parseInteraction("start")];
                return evaluator.evaluate(interactions).then((evaluation) => {
                    expect(evaluation.enteringInteractionList).to.eql([new Action("clickSearchBar")]);
                    expect(evaluation.continuingActionList).to.eql([]);
                    expect(evaluation.finishingInteractionList).to.eql([]);
                    expect(evaluation.questionList).to.eql([]);
                    expect(evaluation.validatedStepList).to.eql([]);
                    expect(evaluation.notValidatedStepList.length).eql(2);
                    expect(evaluation.notValidatedStepList.map((s) => s.expression)).to.eql(stepList.map((s) => s.expression ));
                });
            });
        });

        describe("evaluate 'start clickSearchBar", () => {
            it("should have a continuing action", () => {
                const interactions = [
                    InteractionFactory.parseInteraction("start"),
                    InteractionFactory.parseInteraction("clickSearchBar"),
            ];
                return evaluator.evaluate(interactions).then((evaluation) => {
                    expect(evaluation.enteringInteractionList).to.eql([]);
                    expect(evaluation.continuingActionList).to.eql([new Action("typeProductName")]);
                    expect(evaluation.finishingInteractionList).to.eql([]);
                    expect(evaluation.questionList).to.eql([]);
                    expect(evaluation.validatedStepList).to.eql([]);
                    expect(evaluation.notValidatedStepList.length).eql(2);
                    expect(evaluation.notValidatedStepList.map((s) => s.expression)).to.eql(stepList.map((s) => s.expression ));
                });
            });
        });
    });

});
