import { test } from 'node:test'; import assert from 'node:assert/strict'; import { parseJsonFromLlmText, validateAndNormalizeDraft, } from './documentGenService.js'; test('parseJsonFromLlmText: чистый JSON', () => { const o = parseJsonFromLlmText('{"title":"T","questions":[{"text":"Q","options":[{"text":"a","isCorrect":true},{"text":"b","isCorrect":false}]}]}'); assert.equal(o.title, 'T'); assert.equal(o.questions.length, 1); }); test('parseJsonFromLlmText: JSON в markdown-заборе', () => { const raw = '```json\n{"title":"X","questions":[{"text":"1","options":[{"text":"+","isCorrect":true},{"text":"-","isCorrect":false}]}]}\n```'; const o = parseJsonFromLlmText(raw); assert.equal(o.title, 'X'); }); test('parseJsonFromLlmText: невалидный JSON — ошибка', () => { assert.throws( () => parseJsonFromLlmText('not json'), /JSON/i ); }); test('validateAndNormalizeDraft: валидный черновик', () => { const d = validateAndNormalizeDraft({ title: ' Экзамен ', description: ' оп ', questions: [ { text: '2+2?', hasMultipleAnswers: false, options: [ { text: '4', isCorrect: true }, { text: '5', isCorrect: false }, ], }, ], }); assert.equal(d.title, 'Экзамен'); assert.equal(d.description, 'оп'); assert.equal(d.questions[0].options.length, 2); }); test('validateAndNormalizeDraft: нет title', () => { assert.throws( () => validateAndNormalizeDraft({ questions: [ { text: 'Q', options: [ { text: 'a', isCorrect: true }, { text: 'b', isCorrect: false }, ], }, ], }), /title/i ); });