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 30% 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 6 of one opener: 10 are reworded, the 6 kept are spread out. expect(groups[0]!.reword).toHaveLength(10); 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(); }); }); describe('plain stems', () => { it('drops bold and underline emphasis from generated stems but keeps the words', async () => { const { plainStem } = await import('../server/ai/generate.ts'); expect(plainStem('Which instruction is **most important for absorption**?')).toBe('Which instruction is most important for absorption?'); expect(plainStem('Give __elexacaftor__ with fat.')).toBe('Give elexacaftor with fat.'); expect(plainStem('A dose of 2 * 3 mg is ordered.')).toBe('A dose of 2 * 3 mg is ordered.'); }); }); describe('plain choices', () => { it('drops the closing period of a phrase but keeps quoted speech and longer text as written', async () => { const { plainChoice } = await import('../server/ai/generate.ts'); expect(plainChoice('Sit the client upright and lower the legs.')).toBe('Sit the client upright and lower the legs'); expect(plainChoice('0.9% sodium chloride')).toBe('0.9% sodium chloride'); expect(plainChoice('"I will take my anticoagulant every day as prescribed."')).toBe('"I will take my anticoagulant every day as prescribed."'); expect(plainChoice('Stop the drug. Notify the provider.')).toBe('Stop the drug. Notify the provider.'); expect(plainChoice('Wait...')).toBe('Wait...'); }); }); describe('quoted statements', () => { it('quotes first-person choices and leaves the rest alone', async () => { const { quoteStatement } = await import('../server/ai/generate.ts'); expect(quoteStatement('I will turn him on his side and time how long it lasts')).toBe('"I will turn him on his side and time how long it lasts."'); expect(quoteStatement("We'll keep the follow-up appointment.")).toBe('"We\'ll keep the follow-up appointment."'); expect(quoteStatement('"I will call my provider."')).toBe('"I will call my provider."'); expect(quoteStatement('Increased appetite with weight gain')).toBe('Increased appetite with weight gain'); expect(quoteStatement('IV fluid boluses of normal saline')).toBe('IV fluid boluses of normal saline'); }); });