import { describe, expect, it } from 'vitest' import { correctIndex, questionsOf, toMarkdown, withPlanItem, type Material } from './results' import { sentencesOf, scriptLang } from '../components/AudioPlayer' describe('correctIndex', () => { const options = ['Listen first', 'Interrupt', 'Leave'] it('finds the answer as option text, letter or index', () => { expect(correctIndex(options, 'listen first')).toBe(0) expect(correctIndex(options, 'B')).toBe(1) expect(correctIndex(options, 'C. Leave')).toBe(2) expect(correctIndex(options, '1')).toBe(1) }) it('returns -1 for an answer outside the options', () => { expect(correctIndex(options, 'D')).toBe(-1) expect(correctIndex([], 'An essay answer')).toBe(-1) }) }) describe('questionsOf', () => { it('reads the engine’s question shapes', () => { const [q] = questionsOf({ questions: [{ text: 'Q', choices: [{ text: 'x' }, { label: 'y' }], correct_answer: 'y', explanation: 'because' }] }) expect(q).toEqual({ question: 'Q', options: ['x', 'y'], answer: 'y', explanation: 'because', correct: 1 }) }) }) describe('toMarkdown', () => { const material = (kind: Material['kind']): Material => ({ id: 1, agentId: 1, kind, status: 'ready', revision: 1, parts: [], planTitle: 'Onboarding', composeSetId: null, sources: ['Handbook'], createdAt: '2026-10-01T00:00:00Z', rule: { type: 'content', kind, count: 1, per: 'week', topic: '' }, }) it('writes a SCORM module as the lesson followed by its quiz', () => { const md = toMarkdown(material('scorm'), { title: 'Onboarding', text: 'Welcome.', questions: [{ question: 'Q?', options: ['a', 'b'], answer: 'b' }] }) expect(md).toMatch(/^# Onboarding\n\nWelcome\.\n\n## Quiz\n\n1\. Q\?\n {3}- a\n {3}- b\n/) expect(md).toContain('Sources: Handbook') }) it('writes an audio lesson as its script', () => { expect(toMarkdown(material('audio'), { text: 'Hello team.' })).toBe('# Onboarding\n\nHello team.\n\n---\nSources: Handbook\n') }) }) describe('audio script helpers', () => { it('splits a script into sentences without Markdown', () => { expect(sentencesOf('# Intro\nHello **team**. Ready? Let’s go!\n\n- one')).toEqual(['Intro', 'Hello team.', 'Ready?', 'Let’s go!', 'one']) }) it('guesses the script language for the voice', () => { expect(scriptLang('Ini adalah pelajaran yang penting untuk kita semua dan tim.')).toBe('id') expect(scriptLang('This is the lesson that we will share with the team.')).toBe('en') }) }) describe('withPlanItem', () => { it('fails a draft still at work when its plan item stopped', () => { expect(withPlanItem('preparing', 'failed')).toBe('failed') expect(withPlanItem('generating', 'blocked')).toBe('failed') }) it('keeps the draft status otherwise', () => { expect(withPlanItem('preparing', 'generating')).toBe('preparing') expect(withPlanItem('ready', 'failed')).toBe('ready') expect(withPlanItem('preparing', undefined)).toBe('preparing') }) })