import type { Parameters } from 'fhir/r4'; import { groupReference, horizonLabel, outcomeText, referenceView } from './reference-risk'; const part = (name: string, extra: object) => ({ name, ...extra }) as any; const answer = (...parameter: object[]): Parameters => ({ resourceType: 'Parameters', parameter }) as Parameters; const resolved = (comparison: object[] = []) => answer( part('status', { valueCode: 'resolved' }), part('cutoff', { valueDate: '2026-11-30' }), part('scenario', { part: [ part('table', { valueCode: 'screening' }), part('label', { valueString: 'Tamizaje' }), part('key', { part: [ part('name', { valueCode: 'history' }), part('value', { valueString: 'Cotest-negative' }), ], }), ], }), part('horizon', { part: [ part('years', { valueInteger: 0 }), part('published', { valueBoolean: true }), part('reference', { valueDecimal: 0 }), ...comparison, ], }), part('horizon', { part: [part('years', { valueInteger: 5 }), part('published', { valueBoolean: false })], }), ); describe('the reference answer as the section shows it', () => { it('names the horizons and never calls a year the immediate risk', () => { expect(horizonLabel(0)).toBe('Inmediato (al índice)'); expect(horizonLabel(1)).toBe('1 año'); expect(horizonLabel(5)).toBe('5 años'); }); it('keeps a published zero as zero and a blank as absent', () => { const view = referenceView(resolved()); expect(view.horizons.map((h) => [h.years, h.reference, h.published])).toEqual([ [0, 0, true], [5, undefined, false], ]); expect(view.scenario).toEqual({ label: 'Tamizaje', keys: [{ name: 'Antecedente más reciente', value: 'Cotest-negative' }], }); }); it('reads a comparison with its model, its figures and its reasons, and a difference only when one is given', () => { const view = referenceView( resolved([ part('comparison', { part: [ part('model', { valueCode: 'cacu.cin2_pim' }), part('title', { valueString: 'Riesgo LIEAG+' }), part('institutional', { valueDecimal: 0.05 }), part('status', { valueCode: 'not_comparable' }), part('reason', { valueCode: 'outcome_differs' }), ], }), ]), ); const [c] = view.horizons[0].comparisons; expect(c).toMatchObject({ model: 'cacu.cin2_pim', institutional: 0.05, status: 'not_comparable', reasons: ['outcome_differs'], }); expect(c.differencePp).toBeUndefined(); }); it('an unresolved answer has its state and reasons and no horizons', () => { const view = referenceView( answer( part('status', { valueCode: 'history_not_demonstrable' }), part('reason', { valueCode: 'no_earlier_test_registered' }), ), ); expect(view).toMatchObject({ status: 'history_not_demonstrable', reasons: ['no_earlier_test_registered'], horizons: [], used: [], }); }); }); const cmp = ( model: string, outcome: string, status: string, reasons: string[], extra: object[] = [], ) => part('comparison', { part: [ part('model', { valueCode: model }), part('title', { valueString: model }), part('outcome', { valueString: outcome }), ...extra, part('status', { valueCode: status }), ...reasons.map((r) => part('reason', { valueCode: r })), ], }); const two = (a: object[], b: object[]) => answer( part('status', { valueCode: 'resolved' }), part('horizon', { part: [ part('years', { valueInteger: 0 }), part('published', { valueBoolean: true }), part('reference', { valueDecimal: 0.001 }), ...a, ], }), part('horizon', { part: [ part('years', { valueInteger: 5 }), part('published', { valueBoolean: true }), part('reference', { valueDecimal: 0.0027 }), ...b, ], }), ); describe('outcome names', () => { it('translates the known outcomes for display and leaves the API identifier untouched', () => { expect(outcomeText('cancer')).toBe('Cáncer'); expect(outcomeText('CIN2+ within one year')).toBe('CIN2+ en un año'); expect(outcomeText('CIN3+')).toBe('CIN3 o más grave (CIN3+)'); expect(outcomeText('something new')).toBe('something new'); expect(outcomeText(undefined)).toBe(''); const view = referenceView( two([cmp('m', 'cancer', 'not_comparable', ['outcome_differs'])], []), ); expect(view.horizons[0].comparisons[0].outcome).toBe('cancer'); }); }); describe('grouping the comparison', () => { const reasons = ['outcome_differs', 'population_not_verified']; it('keeps every model, horizon, value and interval, and says shared reasons once', () => { const g = groupReference( referenceView( two( [ cmp( 'a', 'CIN2+', 'not_comparable', [...reasons, 'index_date_differs'], [part('institutional', { valueDecimal: 0.05 })], ), cmp('b', 'cancer', 'not_comparable', reasons), ], [ cmp('a', 'CIN2+', 'not_comparable', reasons, [ part('institutional', { valueDecimal: 0.07 }), part('institutional-low', { valueDecimal: 0.05 }), part('institutional-high', { valueDecimal: 0.09 }), ]), cmp('b', 'cancer', 'not_comparable', [...reasons, 'horizon_not_published_by_model']), ], ), ), ); expect(g.reference.map((r) => [r.years, r.reference])).toEqual([ [0, 0.001], [5, 0.0027], ]); expect(g.common).toEqual(reasons); // every model gives them expect(g.models.map((m) => [m.model, m.outcome, m.rows.length])).toEqual([ ['a', 'CIN2+', 2], ['b', 'cancer', 2], ]); expect(g.models[0].reasons).toEqual([]); // nothing else shared by both of a's horizons expect(g.models[0].rows[0].reasons).toEqual(['index_date_differs']); expect(g.models[0].rows[1]).toMatchObject({ institutional: 0.07, low: 0.05, high: 0.09, reasons: [], }); expect(g.models[1].rows[1].reasons).toEqual(['horizon_not_published_by_model']); }); it('never merges across outcomes: reasons are only shared where every compared row has them', () => { const g = groupReference( referenceView( two( [ cmp('a', 'CIN2+', 'not_comparable', ['outcome_differs']), cmp('b', 'cancer', 'not_comparable', ['no_reference']), ], [], ), ), ); expect(g.common).toEqual([]); expect(g.models.flatMap((m) => m.rows.flatMap((r) => r.reasons))).toEqual([ 'outcome_differs', 'no_reference', ]); }); it('a comparable row keeps its difference and a missing interval stays missing', () => { const g = groupReference( referenceView( two( [ cmp( 'a', 'CIN3+', 'comparable', [], [ part('institutional', { valueDecimal: 0.01 }), part('difference-pp', { valueDecimal: 0.9 }), ], ), ], [], ), ), ); expect(g.models[0].rows[0]).toMatchObject({ status: 'comparable', differencePp: 0.9, low: undefined, }); }); it('without a model it says so once, with its reasons', () => { const g = groupReference( referenceView( two( [ part('comparison', { part: [ part('status', { valueCode: 'not_comparable' }), part('reason', { valueCode: 'no_institutional_model' }), ], }), ], [ part('comparison', { part: [ part('status', { valueCode: 'not_comparable' }), part('reason', { valueCode: 'no_institutional_model' }), ], }), ], ), ), ); expect(g.models).toEqual([]); expect(g.withoutModel).toEqual(['no_institutional_model']); }); });