import type { RiskAssessment } from 'fhir/r4'; import { ageGroup, currentRisk, windowStart, ageAt, bornFor } from './follow-up'; import { historyPeriods, periods } from './measures'; describe('follow-up list', () => { it('starts the window the day after the same day months before (core.follow_up)', () => { expect(windowStart('2026-11-22', 3)).toBe('2026-08-23'); expect(windowStart('2026-05-31', 3)).toBe('2026-03-01'); // 2026-02-28, plus one expect(windowStart('2026-01-15', 1)).toBe('2025-12-16'); }); it('groups ages at the cut-off, open at both ends', () => { expect(ageGroup('1980-11-23', '2026-11-22')).toBe('45-54'); expect(ageGroup('2002-01-01', '2026-11-22')).toBe('<25'); expect(ageGroup('1950-01-01', '2026-11-22')).toBe('65+'); expect(ageGroup(undefined, '2026-11-22')).toBe('Sin dato'); }); it('takes the latest index date, the highest risk there', () => { const risk = (model: string, at: string, p: number): RiskAssessment => ({ resourceType: 'RiskAssessment', status: 'final', subject: {}, occurrenceDateTime: at, method: { coding: [{ code: model }] }, prediction: [{ probabilityDecimal: p, whenRange: { high: { value: 5 } } }], }); const risks = [ risk('cacu.cin2_pim', '2025-01-01', 0.9), risk('cacu.cin2_logistic', '2026-01-01', 0.2), risk('cacu.cin2_pim_post_colposcopy', '2026-01-01', 0.3), risk('cacu.cancer_pim', '2026-01-01', 0.05), ]; expect(currentRisk(risks, 'cin2', 5)).toBe(0.3); expect(currentRisk(risks, 'cancer', 5)).toBe(0.05); expect(currentRisk(risks, 'cin2', 3)).toBeUndefined(); }); it('ends history at the selected complete period for months and semesters', () => { const monthly = historyPeriods('month', '2025-03'); expect(monthly.slice(-3)).toEqual(['2025-01', '2025-02', '2025-03']); expect(monthly).toHaveLength(72); const firstSemester = historyPeriods('semester', '2025-S1'); expect(firstSemester.slice(-3)).toEqual(['2024-S1', '2024-S2', '2025-S1']); const secondSemester = historyPeriods('semester', '2025-S2'); expect(secondSemester.slice(-2)).toEqual(['2025-S1', '2025-S2']); }); it('lists periods most recent first', () => { expect(periods('semester', 1, new Date(2026, 10, 25))).toEqual(['2026-S2', '2026-S1']); expect(periods('month', 1, new Date(2026, 1, 3)).slice(0, 3)).toEqual([ '2026-02', '2026-01', '2025-12', ]); }); }); describe('ages', () => { it('ask for the birth dates whose completed years fall in the range', () => { expect(bornFor('2026-06-30', 36, 40)).toEqual(['le1990-06-30', 'gt1985-06-30']); expect(bornFor('2026-06-30', null, null)).toEqual([]); expect(bornFor('2024-02-29', 1, null)).toEqual(['le2023-02-28']); expect([ageAt('1990-06-30', '2026-06-30'), ageAt('1990-07-01', '2026-06-30')]).toEqual([ 36, 35, ]); }); });