Study/tests/openers.test.ts
Elijah 39a85ebb0c
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 30s
Write quiz questions in the style of the course's real exams
Modelled on a real 50-question course exam and the V1 question set:
- Stems are short and plain (usually 15-30 words, one paragraph, no
  scene-setting), end in the exam's own question phrasing, and about a
  third may ask directly about a fact the nurse uses.
- Bold and underline are stripped from generated stems (plainStem).
- Choices are concise and parallel: a term or value, a 4-12 word action
  or finding, or quoted speech; explanations go in "why" (1-2 sentences).
- Distractors come from related facts in the material.
- Select-all items have 6 choices with 2-5 correct, mostly 3 or 4.
- The planner varies the kind of question instead of setting and role.
- Review, proofread, "improve questions" and the outside-chatbot
  instructions follow the same rules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:56:14 -07:00

56 lines
3.5 KiB
TypeScript

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();
});
});
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.');
});
});