import { describe, expect, it } from 'vitest'; import { acceptRewording, openerAdvice, repeatedOpeners, stemOpener } from '../server/ai/openers.ts'; const q = (stem: string, type: 'single' | 'multi' | 'numeric' = 'single') => ({ stem, type }); describe('question openers', () => { it('folds ages and numbers so "A 72-year-old" and "A 45 year old" count as one opener', () => { expect(stemOpener('A 72-year-old client with heart failure…')).toBe('a #-year-old'); expect(stemOpener('**A 45 year old** patient reports…')).toBe('a #-year-old'); expect(stemOpener('The nurse is caring for…')).toBe('the nurse'); }); it('flags an opener used by more than a quarter of the quiz and keeps a spread-out share of it', () => { const stems = [ ...Array.from({ length: 16 }, (_, i) => q(`A ${40 + i}-year-old client with condition ${i} reports pain. Which action is first?`)), ...Array.from({ length: 4 }, (_, i) => q(`The nurse is caring for client ${i}. Which finding requires follow-up?`)), ]; const groups = repeatedOpeners(stems); expect(groups).toHaveLength(1); expect(groups[0]!.opener).toBe('a #-year-old'); expect(groups[0]!.total).toBe(16); // 20 questions allow 5 of one opener: 11 are reworded, the 5 kept are spread out. expect(groups[0]!.reword).toHaveLength(11); expect(groups[0]!.reword).not.toContain(0); expect(openerAdvice(stems)).toMatch(/"A 40-year-old client…"/); }); it('leaves varied quizzes, short quizzes and calculations alone', () => { const varied = ['The nurse', 'A client', 'In the', 'During discharge', 'Which finding', 'The charge', 'After receiving', 'The provider'].map((o, i) => q(`${o} ${i} … what is the priority?`)); expect(repeatedOpeners(varied)).toEqual([]); expect(repeatedOpeners(Array.from({ length: 6 }, (_, i) => q(`A ${i}-year-old client…`)))).toEqual([]); const math = Array.from({ length: 10 }, (_, i) => q(`The provider orders ${i + 2} mg. How many mL?`, 'numeric')); expect(repeatedOpeners(math)).toEqual([]); }); it('accepts a rewording only when it keeps every number and the select-all ending', () => { const original = q('A 72-year-old client has a potassium of 3.1 mEq/L and a heart rate of 52. Select all that apply.', 'multi'); const repeated = new Set(['a #-year-old']); expect(acceptRewording(original, 'The nurse reviews the laboratory results of a 72-year-old client: potassium 3.1 mEq/L, heart rate 52.', repeated)).toBe( 'The nurse reviews the laboratory results of a 72-year-old client: potassium 3.1 mEq/L, heart rate 52. Select all that apply.', ); // A number was lost or changed. expect(acceptRewording(original, 'The nurse reviews the laboratory results of a 72-year-old client: potassium 3.4 mEq/L, heart rate 52.', repeated)).toBeNull(); // Still opens the repeated way. expect(acceptRewording(original, 'A 72-year-old client on the unit has a potassium of 3.1 mEq/L and a heart rate of 52.', repeated)).toBeNull(); }); });