"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const parseExam_1 = __importDefault(require("@digabi/exam-engine-core/dist/parser/parseExam"));
const exam_engine_exams_1 = require("@digabi/exam-engine-exams");
const exam_engine_mastering_1 = require("@digabi/exam-engine-mastering");
const fs_1 = require("fs");
const lodash_es_1 = require("lodash-es");
const ResultsContext_1 = require("../../src/components/context/ResultsContext");
const dom_utils_1 = require("../../src/dom-utils");
const allScores = [
    {
        questionId: 330,
        answerId: 1,
        autograding: { score: 3 }
    },
    {
        questionId: 2,
        answerId: 2,
        pregrading: {
            score: 1
        },
        censoring: {
            scores: [
                { score: 4, shortCode: 'SE3' },
                { score: 3, shortCode: 'SE2' },
                { score: 2, shortCode: 'SE1' }
            ]
        },
        inspection: { score: 5, shortCodes: ['IN1', 'IN2'] }
    }
];
const answers = [
    {
        type: 'choice',
        questionId: 330,
        value: '93',
        displayNumber: '1.1'
    },
    {
        type: 'richText',
        questionId: 2,
        value: 'test',
        characterCount: 4,
        displayNumber: '1.2'
    }
];
const textAnswer = [
    {
        type: 'richText',
        questionId: 2,
        value: 'test',
        characterCount: 4
    }
];
const answersByQuestionId = (0, lodash_es_1.keyBy)(answers, 'questionId');
const testExam = 'SC.xml';
const language = 'fi-FI';
const getMediaMetadata = (__, type) => Promise.resolve(type === 'audio' ? { duration: 999 } : { width: 999, height: 999 });
const exam = (0, exam_engine_exams_1.listExams)().find(e => e.includes(testExam));
if (exam == null) {
    throw new Error(`Could not find the exam file to use in test: ${testExam}`);
}
let result;
let doc;
let topLevelQuestions;
describe(`Total score calculation for ${testExam}`, () => {
    beforeAll(async () => {
        const source = await fs_1.promises.readFile(exam, 'utf-8');
        const results = await (0, exam_engine_mastering_1.masterExam)(source, () => '', getMediaMetadata);
        result = results.find(r => r.language === language);
        doc = (0, parseExam_1.default)(result.xml, false);
        topLevelQuestions = (0, dom_utils_1.queryAll)(doc.documentElement, 'question', false);
    });
    it('calculate sum of all scores', () => {
        expect((0, ResultsContext_1.calculateQuestionsTotalSumScore)(topLevelQuestions, result.gradingStructure, allScores, answersByQuestionId)).toBe(5);
    });
    it('get text answer score with pregrading only', () => {
        const textAnswerScorePregradingOnly = [
            {
                questionId: 2,
                answerId: 2,
                pregrading: {
                    score: 1
                }
            }
        ];
        expect((0, ResultsContext_1.calculateQuestionSumScore)(topLevelQuestions[0], result.gradingStructure, textAnswerScorePregradingOnly, (0, lodash_es_1.keyBy)(textAnswer, 'questionId'))).toBe(1);
    });
    it('get text answer score with one censoring score', () => {
        const textAnswerScoreOneCensor = [
            {
                questionId: 2,
                answerId: 2,
                pregrading: {
                    score: 1
                },
                censoring: {
                    scores: [{ score: 2, shortCode: 'SE1' }]
                }
            }
        ];
        expect((0, ResultsContext_1.calculateQuestionSumScore)(topLevelQuestions[0], result.gradingStructure, textAnswerScoreOneCensor, (0, lodash_es_1.keyBy)(textAnswer, 'questionId'))).toBe(2);
    });
    it('get text answer score with two censoring scores', () => {
        const textAnswerScoreTwoCensors = [
            {
                questionId: 2,
                answerId: 2,
                pregrading: {
                    score: 1
                },
                censoring: {
                    scores: [
                        { score: 3, shortCode: 'SE2' },
                        { score: 2, shortCode: 'SE1' }
                    ]
                }
            }
        ];
        expect((0, ResultsContext_1.calculateQuestionSumScore)(topLevelQuestions[0], result.gradingStructure, textAnswerScoreTwoCensors, (0, lodash_es_1.keyBy)(textAnswer, 'questionId'))).toBe(3);
    });
    it('get text answer score with inspection score', () => {
        const textAnswerScoreInspection = [
            {
                questionId: 2,
                answerId: 2,
                pregrading: {
                    score: 1
                },
                censoring: {
                    scores: [
                        { score: 2, shortCode: 'SE1' },
                        { score: 3, shortCode: 'SE2' }
                    ]
                },
                inspection: { score: 7, shortCodes: ['IN1', 'IN2'] }
            }
        ];
        expect((0, ResultsContext_1.calculateQuestionSumScore)(topLevelQuestions[0], result.gradingStructure, textAnswerScoreInspection, (0, lodash_es_1.keyBy)(textAnswer, 'questionId'))).toBe(7);
    });
    it('get choice answer score', () => {
        const choiceAnswer = {
            type: 'choice',
            questionId: 1,
            value: '105',
            displayNumber: '1.1'
        };
        expect((0, ResultsContext_1.calculateQuestionSumScore)(topLevelQuestions[0], result.gradingStructure, [], (0, lodash_es_1.keyBy)([choiceAnswer], 'questionId'))).toBe(3);
    });
    it('get scored text answer score', () => {
        const scoredTextAnswer = [
            {
                type: 'text',
                questionId: 83,
                value: 'Willst du am Samstag mit ins Kino kommen',
                characterCount: 33,
                displayNumber: '18.1'
            },
            {
                type: 'text',
                questionId: 84,
                value: 'Bar Bar',
                characterCount: 6,
                displayNumber: '18.2'
            }
        ];
        const scoredTextScore = [
            {
                questionId: 83,
                answerId: 83,
                autograding: { score: 2 }
            },
            {
                questionId: 84,
                answerId: 84,
                autograding: { score: 0 }
            }
        ];
        expect((0, ResultsContext_1.calculateQuestionSumScore)(topLevelQuestions[17], result.gradingStructure, scoredTextScore, (0, lodash_es_1.keyBy)(scoredTextAnswer, 'questionId'))).toBe(2);
    });
});
//# sourceMappingURL=ResultsContext.test.js.map