"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const exam_engine_core_1 = require("@digabi/exam-engine-core");
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 path_1 = __importDefault(require("path"));
const react_1 = __importDefault(require("react"));
const react_test_renderer_1 = require("react-test-renderer");
const _ = __importStar(require("lodash-es"));
const examServerApi_1 = require("./examServerApi");
const exam_engine_generator_1 = require("@digabi/exam-engine-generator");
describe.each((0, exam_engine_exams_1.listExams)().map(exam => [path_1.default.basename(exam), exam]))('%s', (_basename, exam) => {
    const resolveAttachment = (filename) => path_1.default.resolve(path_1.default.dirname(exam), 'attachments', filename);
    beforeEach(() => {
        // @ts-expect-error fix "IntersectionObserver is not defined" error in tests
        global.IntersectionObserver = jest.fn((cb, options = {}) => {
            const instance = {
                observe: jest.fn,
                takeRecords: jest.fn,
                unobserve: jest.fn,
                disconnect: jest.fn
            };
            return instance;
        });
    });
    it('renders properly', async () => {
        const source = await fs_1.promises.readFile(exam, 'utf-8');
        const results = await (0, exam_engine_mastering_1.masterExam)(source, () => '', (0, exam_engine_mastering_1.getMediaMetadataFromLocalFile)(resolveAttachment), {
            removeCorrectAnswers: false
        });
        for (const { xml, language, gradingStructure } of results) {
            const doc = (0, parseExam_1.default)(xml, true);
            const commonProps = {
                doc,
                answers: [],
                attachmentsURL: '/attachments',
                resolveAttachment: (filename) => `/attachments/${encodeURIComponent(filename)}`,
                language
            };
            const examProps = {
                ...commonProps,
                casStatus: 'forbidden',
                examServerApi: examServerApi_1.examServerApi,
                restrictedAudioPlaybackStats: [],
                studentName: '[Kokelaan Nimi]',
                showUndoView: false,
                undoViewProps: {
                    questionId: 0,
                    title: '',
                    close: () => undefined,
                    restoreAnswer: () => undefined
                }
            };
            const resultsProps = {
                ...commonProps,
                gradingStructure,
                answers: (0, exam_engine_generator_1.generateAnswers)(gradingStructure),
                scores: mkScores(gradingStructure),
                returnToExam: () => { },
                endSession: () => Promise.resolve()
            };
            _.set(window, 'TEST_VERSION_NUMBER', '1.0.0');
            expect((0, react_test_renderer_1.create)(react_1.default.createElement(exam_engine_core_1.Exam, { ...examProps })).toJSON()).toMatchSnapshot('<Exam />');
            expect((0, react_test_renderer_1.create)(react_1.default.createElement(exam_engine_core_1.Attachments, { ...examProps })).toJSON()).toMatchSnapshot('<Attachments />');
            expect((0, react_test_renderer_1.create)(react_1.default.createElement(exam_engine_core_1.Results, { ...resultsProps })).toJSON()).toMatchSnapshot('<Results />');
            expect((0, react_test_renderer_1.create)(react_1.default.createElement(exam_engine_core_1.GradingInstructions, { ...commonProps })).toJSON()).toMatchSnapshot('<GradingInstructions />');
        }
    });
});
function mkScores(gradingStructure) {
    return gradingStructure.questions
        .filter((question) => question.type === 'text')
        .map((question, i) => generateScore(question, i));
}
function generateScore(question, i) {
    return {
        questionId: question.id,
        answerId: question.id,
        pregrading: {
            score: Math.min(question.maxScore, i),
            comment: `Pregading comment to question ${question.displayNumber}`,
            annotations: [{ startIndex: 0, length: 0, message: `Pregading annotation to question ${question.displayNumber}` }]
        },
        censoring: {
            scores: [
                {
                    score: Math.min(question.maxScore, i),
                    shortCode: 'TestCen1'
                },
                {
                    score: Math.min(question.maxScore, i),
                    shortCode: 'TestCen2'
                },
                {
                    score: Math.min(question.maxScore, i),
                    shortCode: 'TestCen3'
                }
            ],
            comment: `Censor comment to question ${question.displayNumber}`,
            annotations: [
                { startIndex: 0, length: 0, message: `Censoring annotation to question ${question.displayNumber}` }
            ],
            nonAnswerDetails: { shortCode: 'CEN' }
        },
        inspection: {
            score: Math.min(question.maxScore, i),
            shortCodes: ['TestIns1', 'TestIns2']
        }
    };
}
//# sourceMappingURL=testExamRendering.js.map