Files
brain/tools/question-slots.test.mjs
T

55 lines
2.4 KiB
JavaScript

import { describe, it, expect } from 'vitest';
import { addQuestion, answerQuestion, hangingQuestions, allAnswered } from './question-slots.mjs';
describe('addQuestion (новый слот answered:false; id непустой уникальный)', () => {
it('добавляет слот', () => {
const s = addQuestion([], { id: 'q1', text: 'inline или субагенты?' });
expect(s).toEqual([{ id: 'q1', text: 'inline или субагенты?', answered: false, answer: null }]);
});
it('иммутабельно — исходный массив не меняется', () => {
const base = [];
addQuestion(base, { id: 'q1', text: 't' });
expect(base).toEqual([]);
});
it('пустой id → ошибка', () => {
expect(() => addQuestion([], { id: '', text: 't' })).toThrow();
});
it('дубль id → ошибка', () => {
const s = addQuestion([], { id: 'q1', text: 't' });
expect(() => addQuestion(s, { id: 'q1', text: 'другой' })).toThrow(/дубл/i);
});
});
describe('answerQuestion (пометка отвеченным; непустой ответ)', () => {
it('помечает answered + хранит ответ', () => {
let s = addQuestion([], { id: 'q1', text: 't' });
s = answerQuestion(s, 'q1', 'inline');
expect(s[0]).toMatchObject({ id: 'q1', answered: true, answer: 'inline' });
});
it('неизвестный id → ошибка', () => {
expect(() => answerQuestion([], 'qX', 'a')).toThrow(/неизвестн/i);
});
it('пустой ответ → ошибка', () => {
const s = addQuestion([], { id: 'q1', text: 't' });
expect(() => answerQuestion(s, 'q1', ' ')).toThrow();
});
});
describe('hangingQuestions / allAnswered (нет повисших)', () => {
it('повисшие = заданные без ответа', () => {
let s = addQuestion([], { id: 'q1', text: 't1' });
s = addQuestion(s, { id: 'q2', text: 't2' });
s = answerQuestion(s, 'q1', 'a');
expect(hangingQuestions(s).map((x) => x.id)).toEqual(['q2']);
expect(allAnswered(s)).toBe(false);
});
it('все отвечены → allAnswered true', () => {
let s = addQuestion([], { id: 'q1', text: 't' });
s = answerQuestion(s, 'q1', 'a');
expect(allAnswered(s)).toBe(true);
});
it('пустой список → allAnswered true (вопросов нет — ничего не повисло)', () => {
expect(allAnswered([])).toBe(true);
});
});