'use strict'; const { scoreQuestion, normalizeAnswerText, normalizeNumberValue, normalizeDateValue, QUESTION_KINDS, QUIZ_MEDIA_EXT, } = require('./lib/quizzes'); let failed = 0; function ok(label, cond, extra) { console.log(`${cond ? 'PASS' : 'FAIL'} ${label}${extra !== undefined && !cond ? ' -> ' + JSON.stringify(extra) : ''}`); if (!cond) failed++; } const opts = [ { id: 1, text: 'A', is_correct: true, weight: 1 }, { id: 2, text: 'B', is_correct: true, weight: 2 }, { id: 3, text: 'C', is_correct: false, weight: 1 }, ]; const single = { kind: 'single', weight: 1.5, options: [ { id: 1, text: 'A', is_correct: true, weight: 2 }, { id: 3, text: 'C', is_correct: false, weight: 1 }, ], }; ok('single: верный ответ даёт вес варианта × вес вопроса', scoreQuestion(single, [1]).score === 3 && scoreQuestion(single, [1]).max_score === 3, scoreQuestion(single, [1])); ok('single: неверный ответ — 0', scoreQuestion(single, [3]).score === 0 && scoreQuestion(single, [3]).is_correct === false); ok('single: два выбранных — неверно', scoreQuestion(single, [1, 3]).score === 0); const multiple = { kind: 'multiple', weight: 2, partial_credit: false, options: opts }; ok('multiple: точное совпадение даёт максимум', scoreQuestion(multiple, [1, 2]).score === 6 && scoreQuestion(multiple, [1, 2]).max_score === 6); ok('multiple: неполный набор — 0', scoreQuestion(multiple, [1]).score === 0); ok('multiple: лишний вариант — 0', scoreQuestion(multiple, [1, 2, 3]).score === 0); const partial = { kind: 'multiple', weight: 1, partial_credit: true, options: opts }; ok('partial: верный + неверный взаимно гасятся', scoreQuestion(partial, [1, 3]).score === 0); ok('partial: только верные — сумма весов', scoreQuestion(partial, [1, 2]).score === 3); ok('partial: не уходит ниже нуля', scoreQuestion(partial, [3]).score === 0); const textExact = { kind: 'text', weight: 2, text_match: 'exact', accepted_answers: ['Четыре', '4'] }; ok('text exact: регистр, «ё» и пробелы не важны', scoreQuestion(textExact, [], ' ЧЕТЫРЕ ').score === 2 && scoreQuestion(textExact, [], 'четыре.').score === 2); ok('text exact: другой ответ — 0', scoreQuestion(textExact, [], 'пять').score === 0 && scoreQuestion(textExact, [], 'пять').is_correct === false); const textContains = { kind: 'text', weight: 3, text_match: 'contains', accepted_answers: ['потому что'] }; ok('text contains: ответ содержит принимаемый', scoreQuestion(textContains, [], 'Это так, потому что 2+2=4').score === 3); ok('text contains: нет вхождения — 0', scoreQuestion(textContains, [], 'не знаю').score === 0); const textManual = { kind: 'text', weight: 2, text_match: 'exact', accepted_answers: [] }; ok('text без принимаемых ответов не оценивается', scoreQuestion(textManual, [], 'что угодно').score === 0 && scoreQuestion(textManual, [], 'что угодно').max_score === 0 && scoreQuestion(textManual, [], 'что угодно').is_correct === null); ok('normalizeAnswerText приводит к нижнему регистру и убирает хвостовую пунктуацию', normalizeAnswerText(' Ёлка, ПАЛКА! ') === 'елка, палка', normalizeAnswerText(' Ёлка, ПАЛКА! ')); ok('QUIZ_MEDIA_EXT содержит картинки, аудио и видео', QUIZ_MEDIA_EXT.includes('jpg') && QUIZ_MEDIA_EXT.includes('mp3') && QUIZ_MEDIA_EXT.includes('mp4') && QUIZ_MEDIA_EXT.includes('pdf')); ok('QUESTION_KINDS содержит строку, список, число и дату', ['single', 'multiple', 'select', 'line', 'text', 'number', 'date'].every(k => QUESTION_KINDS.has(k)), [...QUESTION_KINDS]); const selectQ = { kind: 'select', weight: 1, options: [ { id: 1, text: 'A', is_correct: false, weight: 1 }, { id: 2, text: 'B', is_correct: true, weight: 1 }, ], }; ok('select оценивается как один ответ', scoreQuestion(selectQ, [2]).score === 1 && scoreQuestion(selectQ, [1]).score === 0, { right: scoreQuestion(selectQ, [2]), wrong: scoreQuestion(selectQ, [1]) }); const lineQ = { kind: 'line', weight: 1.5, text_match: 'exact', accepted_answers: ['Москва'] }; ok('line оценивается по принимаемым ответам', scoreQuestion(lineQ, [], ' москва ').score === 1.5 && scoreQuestion(lineQ, [], 'Казань').score === 0); const numberQ = { kind: 'number', weight: 2, accepted_answers: ['4', '4.0'] }; ok('number сравнивает числа, а не строки', scoreQuestion(numberQ, [], '4').score === 2 && scoreQuestion(numberQ, [], '4,0').score === 2 && scoreQuestion(numberQ, [], ' 4.00 ').score === 2 && scoreQuestion(numberQ, [], '5').score === 0, { four: scoreQuestion(numberQ, [], '4,0'), five: scoreQuestion(numberQ, [], '5') }); ok('number без принимаемых ответов не оценивается', scoreQuestion({ ...numberQ, accepted_answers: [] }, [], '4').max_score === 0); const dateQ = { kind: 'date', weight: 1, accepted_answers: ['2026-12-31', '31.12.2026'] }; ok('date сравнивает дату в разных форматах', scoreQuestion(dateQ, [], '2026-12-31').score === 1 && scoreQuestion(dateQ, [], '31.12.2026').score === 1 && scoreQuestion(dateQ, [], '01.01.2027').score === 0, { iso: scoreQuestion(dateQ, [], '2026-12-31'), dotted: scoreQuestion(dateQ, [], '31.12.2026') }); ok('normalizeNumberValue понимает запятую и пробелы', normalizeNumberValue(' 4,50 ') === 4.5 && normalizeNumberValue('abc') === null); ok('normalizeDateValue понимает ISO и ДД.ММ.ГГГГ', normalizeDateValue('2026-12-31') === '2026-12-31' && normalizeDateValue('1.2.2026') === '2026-02-01' && normalizeDateValue('nope') === null); console.log(failed ? `\n${failed} проверок провалено` : '\nQUIZ SELFTEST OK'); process.exit(failed ? 1 : 0);