Files
learning-lab-express/src/express/results.test.ts
Iqbal 3df72c68ef Show results whose plan item stopped as failed
The engine can stop a plan item without updating its drafts: a run paused
on material_quote_changed leaves the draft "preparing" for good, so the
result showed Preparing forever. A draft still at work now counts as
failed once its plan item is failed, blocked, cancelled or skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 11:02:10 +07:00

68 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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')
})
})