Indicadores TEA: Resumen, Trazabilidad, Seguimiento, Análisis de riesgo, periodicities

This commit is contained in:
2026-11-30 14:19:03 -06:00
parent 56d1ef2e8a
commit adff5bac5c
16 changed files with 1876 additions and 361 deletions
+239
View File
@@ -0,0 +1,239 @@
import type {
Bundle,
DiagnosticReport,
Measure,
Observation,
Patient,
RiskAssessment,
} from 'fhir/r4';
import { EXT } from '../fhir';
import { teaRows } from './follow-up';
import { boxes, donut, sankey, type Reading } from './measures';
import { associationRows, teaParams, teaWorkbook } from './tea-list';
const measure: Measure = { resourceType: 'Measure', status: 'active' };
const r: Reading = { strata: {}, populations: {}, score: 12, low: 7, high: 18 };
const cssPair = 'spindles|central|CSS';
function fixture(): Bundle {
const subject = { reference: 'Patient/child' };
const patient: Patient = {
resourceType: 'Patient',
id: 'child',
name: [{ text: 'Ana' }],
identifier: [{ system: 'urn:isssteleon:clinic:source', value: '123' }],
extension: [{ url: EXT + 'follow-up-status', valueCoding: { code: 'finished' } }],
};
const ados: Observation = {
resourceType: 'Observation',
id: 'evaluation',
status: 'final',
code: { coding: [{ code: 'ados2' }] },
subject,
effectiveDateTime: '2026-03-02',
valueInteger: 8,
component: [
{ code: { coding: [{ code: 'ados2.vocalization_directed' }] }, valueQuantity: { value: 1 } },
],
};
const risk: RiskAssessment = {
resourceType: 'RiskAssessment',
status: 'final',
subject,
basis: [{ reference: 'Observation/evaluation' }],
prediction: [
{
outcome: { coding: [{ code: cssPair }] },
probabilityDecimal: 0.35,
extension: [
{ url: EXT + 'association-headline', valueBoolean: true },
{ url: EXT + 'association-reference-probability', valueDecimal: 0.35 },
{ url: EXT + 'association-odds-ratio', valueDecimal: 2 },
{ url: EXT + 'association-q', valueDecimal: 0.01 },
],
},
],
};
const eeg: DiagnosticReport = {
resourceType: 'DiagnosticReport',
status: 'final',
code: { coding: [{ code: 'eeg' }] },
subject,
effectiveDateTime: '2026-03-04',
conclusionCode: [{ text: 'Bueno' }],
extension: [{ url: EXT + 'eeg-channels', valueInteger: 19 }],
};
return {
resourceType: 'Bundle',
type: 'searchset',
entry: [
{ resource: patient, search: { mode: 'match' } },
{ resource: { resourceType: 'Patient', id: 'other' }, search: { mode: 'match' } },
{ resource: ados, search: { mode: 'include' } },
{ resource: risk, search: { mode: 'include' } },
{ resource: eeg, search: { mode: 'include' } },
...['2026-03-01', '2026-02-27'].map((date) => ({
search: { mode: 'include' as const },
resource: {
resourceType: 'Observation' as const,
status: 'final' as const,
subject,
code: { coding: [{ code: 'mchat.score' }] },
valueQuantity: { value: 9 },
effectiveDateTime: date,
},
})),
],
};
}
describe('TEA indicators', () => {
it('uses reported quartiles and skips a measure without a distribution', () => {
expect(
boxes([
{ label: 'M26', r },
{ label: 'M27', r: undefined },
]).data,
).toEqual([expect.objectContaining({ type: 'box', q1: [7], median: [12], q3: [18] })]);
});
it('links reached steps, departures and all three EEG outcomes', () => {
expect(sankey([10, 5, 4, 6], { valid: 6, normal: 2, abnormal: 3 }).data[0]).toMatchObject({
node: {
label: [
'M-CHAT',
'Psicología',
'Neuropediatría',
'EEG',
'Solo M-CHAT',
'Hasta Psicología',
'Sin EEG válido',
'Normal',
'Anormal',
'Sin puntuar',
],
},
link: {
source: [0, 1, 2, 0, 1, 2, 3, 3, 3],
target: [1, 2, 3, 4, 5, 6, 7, 8, 9],
value: [15, 10, 6, 10, 5, 4, 2, 3, 1],
},
});
});
it('builds two concentric rings from the supplied counts', () => {
const inner = { labels: ['Femenino'], values: [3] };
const outer = { labels: ['Femenino · 16-23', 'Femenino · 24-29'], values: [2, 1] };
expect(donut(measure, r, 'category', inner, outer).data).toEqual([
expect.objectContaining(outer),
expect.objectContaining(inner),
]);
expect(donut(measure, r).data).toHaveLength(1);
});
it('maps every search filter and encodes closed ranges as repeated ge/le values', () => {
expect(
teaParams({
step: 'eeg',
eeg: 'normal',
model: 'no',
age: '16-23',
sex: 'female',
alteration: 'spindles_n2',
mchatMin: 0,
mchatMax: 20,
cssMin: 1,
cssMax: 7,
window: 3,
start: '2026-01-01',
cutoff: '2026-03-31',
status: 'finished',
}),
).toEqual({
'tea-step': 'eeg',
'tea-eeg-result': 'normal',
'tea-model': 'no',
'tea-age-group': '16-23',
'tea-sex': 'female',
'tea-alteration': 'spindles_n2',
'tea-mchat': ['ge0', 'le20'],
'tea-css': ['ge1', 'le7'],
'follow-up-window': 3,
'follow-up-start': '2026-01-01',
'follow-up-cutoff': '2026-03-31',
'follow-up-status': 'finished',
});
expect(teaParams({ cssMax: 0 })['tea-css']).toEqual(['le0']);
});
it('uses earliest included dates and builds the four detail cards and headline', () => {
const [row, other] = teaRows(fixture());
expect(row.steps.map(({ date, reached }) => [date, reached])).toEqual([
['2026-02-27', true],
['2026-03-02', true],
[undefined, true],
['2026-03-04', true],
]);
expect(row).toMatchObject({
name: 'Ana',
recordNumbers: ['123'],
mchatScore: 9,
ados: { css: 8 },
eegs: [{ quality: 'Bueno', channels: 19 }],
headline: { pair: cssPair, or: 2, q: 0.01 },
concordance: 'Concordante',
});
expect(other.concordance).toBe('—');
});
it('compares the headline item score with the prediction and reference', () => {
const page = fixture();
const risk = page.entry!.find((e) => e.resource?.resourceType === 'RiskAssessment')!
.resource as RiskAssessment;
const prediction = risk.prediction![0];
prediction.outcome = { coding: [{ code: 'rhythm|central|ados2_item_01' }] };
prediction.probabilityDecimal = 0.1;
expect(teaRows(page)[0].concordance).toBe('No concordante');
});
it('defaults to children with association results and supports the other two modes', () => {
const rows = teaRows(fixture());
expect(associationRows(rows)).toEqual([rows[0]]);
expect(associationRows(rows, 'without')).toEqual([rows[1]]);
expect(associationRows(rows, 'all')).toEqual(rows);
});
it('builds the follow-up workbook', () => {
expect(teaWorkbook(teaRows(fixture()), false)).toEqual({
name: 'seguimiento-tea',
sheets: [
expect.objectContaining({
name: 'Seguimiento',
rows: expect.arrayContaining([
expect.objectContaining({ id: 'child', pair: cssPair, eeg: '2026-03-04' }),
]),
}),
],
});
});
it('builds the risk workbook with children and pair details', () => {
expect(teaWorkbook(associationRows(teaRows(fixture())), true)).toEqual({
name: 'analisis-de-riesgo-tea',
sheets: [
expect.objectContaining({ name: 'Niños' }),
expect.objectContaining({
name: 'Pares',
rows: [
expect.objectContaining({
id: 'child',
pair: cssPair,
probability: 0.35,
reference: 0.35,
}),
],
}),
],
});
});
});