"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@vived/core");
const StateMachine_1 = require("../../StateMachine");
const QuestionAnswerEntity_1 = require("../Entities/QuestionAnswerEntity");
const QuestionEntity_1 = require("../Entities/QuestionEntity");
const SerializeQuestionChallenge_1 = require("./SerializeQuestionChallenge");
function makeTestRig() {
    const appObjects = (0, core_1.makeAppObjectRepo)();
    const appAO = appObjects.getOrCreate("App");
    const stateMachine = (0, StateMachine_1.makeStateMachineEntity)(appAO);
    const questionEntity = (0, QuestionEntity_1.makeQuestionEntity)(appAO);
    questionEntity.answerFactory = (id) => {
        const index = questionEntity.getAllAnswers().length + 1;
        const ao = appObjects.getOrCreate(id ?? "answer" + index);
        return (0, QuestionAnswerEntity_1.makeQuestionAnswerEntity)(ao);
    };
    const mockRegister = jest.fn();
    stateMachine.registerSerializedSystem = mockRegister;
    const serializer = new SerializeQuestionChallenge_1.SerializeQuestionChallenge(appAO);
    return { questionEntity, stateMachine, mockRegister, serializer };
}
describe("Serialize Question Challenge", () => {
    it("Registers with the state machine", () => {
        const { mockRegister } = makeTestRig();
        expect(mockRegister).toHaveBeenCalled();
    });
    it("Forms the state as expected", () => {
        const { serializer, questionEntity } = makeTestRig();
        const ans1 = questionEntity.createNewAnswer();
        ans1.text = "Some Answer";
        ans1.feedbackText = "Feedback 1";
        ans1.isCorrect = true;
        const stateObj = serializer.getSystemState();
        expect(stateObj.answers).toHaveLength(1);
        expect(stateObj.answers[0].feedback).toEqual(ans1.feedbackText);
        expect(stateObj.answers[0].text).toEqual(ans1.text);
        expect(stateObj.answers[0].id).toEqual(ans1.id);
        expect(stateObj.answers[0].isCorrect).toEqual(ans1.isCorrect);
    });
    it("Applies v1 data as expected", () => {
        const { serializer, questionEntity } = makeTestRig();
        expect(questionEntity.getAllAnswers()).toHaveLength(0);
        serializer.applySystemState(dataV1, 1);
        expect(questionEntity.getAllAnswers()).toHaveLength(2);
        const ans1 = questionEntity.getAnswer("id1");
        expect(ans1?.text).toEqual("answer 1");
        expect(ans1?.feedbackText).toEqual("feedback 1");
        expect(ans1?.isCorrect).toEqual(true);
        const ans2 = questionEntity.getAnswer("id2");
        expect(ans2?.text).toEqual("answer 2");
        expect(ans2?.feedbackText).toEqual("feedback 2");
        expect(ans2?.isCorrect).toEqual(false);
    });
    it("Applies v2 data as expected", () => {
        const { serializer, questionEntity } = makeTestRig();
        expect(questionEntity.getAllAnswers()).toHaveLength(0);
        serializer.applySystemState(dataV2, 2);
        expect(questionEntity.getAllAnswers()).toHaveLength(2);
        const ans1 = questionEntity.getAnswer("id1");
        expect(ans1?.text).toEqual("answer 1");
        expect(ans1?.feedbackText).toEqual("feedback 1");
        expect(ans1?.isCorrect).toEqual(true);
        const ans2 = questionEntity.getAnswer("id2");
        expect(ans2?.text).toEqual("answer 2");
        expect(ans2?.feedbackText).toEqual("feedback 2");
        expect(ans2?.isCorrect).toEqual(false);
    });
    it("Applies v3 data as expected", () => {
        const { serializer, questionEntity } = makeTestRig();
        expect(questionEntity.getAllAnswers()).toHaveLength(0);
        serializer.applySystemState(dataV3, 3);
        expect(questionEntity.getAllAnswers()).toHaveLength(2);
        const ans1 = questionEntity.getAnswer("id1");
        expect(ans1?.text).toEqual("answer 1");
        expect(ans1?.feedbackText).toEqual("feedback 1");
        expect(ans1?.isCorrect).toEqual(true);
        const ans2 = questionEntity.getAnswer("id2");
        expect(ans2?.text).toEqual("answer 2");
        expect(ans2?.feedbackText).toEqual("feedback 2");
        expect(ans2?.isCorrect).toEqual(false);
    });
    it("Warns and then apply default if an unsupported version is passed in", () => {
        const { serializer, questionEntity } = makeTestRig();
        const mockApplyDefault = jest.fn();
        questionEntity.setDefaultState = mockApplyDefault;
        serializer.warn = jest.fn();
        serializer.applySystemState({ foo: "bar" }, -1);
        expect(mockApplyDefault).toHaveBeenCalled();
        expect(serializer.warn).toHaveBeenCalled();
    });
    it("Applies a default state", () => {
        const { serializer, questionEntity } = makeTestRig();
        const mockApplyDefault = jest.fn();
        questionEntity.setDefaultState = mockApplyDefault;
        serializer.applyDefaultState();
        expect(mockApplyDefault).toHaveBeenCalled();
    });
    it("Updates existing answers", () => {
        const { serializer, questionEntity } = makeTestRig();
        const ans1 = questionEntity.answerFactory("id1");
        ans1.text = "hello world";
        ans1.feedbackText = "some feedback";
        ans1.isCorrect = false;
        questionEntity.addAnswer(ans1);
        serializer.applySystemState(dataV3, 3);
        expect(ans1?.text).toEqual("answer 1");
        expect(ans1?.feedbackText).toEqual("feedback 1");
        expect(ans1?.isCorrect).toEqual(true);
    });
    it("Removes answers that are not in the state", () => {
        const { serializer, questionEntity } = makeTestRig();
        const otherAnswer = questionEntity.answerFactory("otherAnswer");
        questionEntity.addAnswer(otherAnswer);
        serializer.applySystemState(dataV3, 3);
        expect(questionEntity.hasAnswer(otherAnswer.id)).toEqual(false);
    });
});
const dataV1 = {
    answers: [
        {
            id: "id1",
            text: "answer 1",
            feedback: "feedback 1",
            isCorrect: true,
        },
        {
            id: "id2",
            text: "answer 2",
            feedback: "feedback 2",
            isCorrect: false,
        },
    ],
};
const dataV2 = {
    answers: [
        {
            id: "id1",
            text: "answer 1",
            feedback: "feedback 1",
            isCorrect: true,
        },
        {
            id: "id2",
            text: "answer 2",
            feedback: "feedback 2",
            isCorrect: false,
        },
    ],
    questionText: "The Question is?",
};
const dataV3 = {
    answers: [
        {
            id: "id1",
            text: "answer 1",
            feedback: "feedback 1",
            isCorrect: true,
        },
        {
            id: "id2",
            text: "answer 2",
            feedback: "feedback 2",
            isCorrect: false,
        },
    ],
};
//# sourceMappingURL=SerializeQuestionChallenge.test.js.map