diff --git a/src/app/features/indicadores/fhir.ts b/src/app/features/indicadores/fhir.ts index e184f26..71d0754 100644 --- a/src/app/features/indicadores/fhir.ts +++ b/src/app/features/indicadores/fhir.ts @@ -6,7 +6,7 @@ export interface Concept { coding?: Coding[]; text?: string } export interface Quantity { value?: number; unit?: string } export interface Extension { url: string; - valueCode?: string; valueString?: string; valueInteger?: number; valueDecimal?: number; + valueCode?: string; valueString?: string; valueInteger?: number; valueDecimal?: number; valueBoolean?: boolean; valueDate?: string; valueDateTime?: string; valueCoding?: Coding; valueReference?: { reference?: string }; extension?: Extension[]; } diff --git a/src/app/features/indicadores/indicadores-fhir.spec.ts b/src/app/features/indicadores/indicadores-fhir.spec.ts index 1f62fc4..ddf545f 100644 --- a/src/app/features/indicadores/indicadores-fhir.spec.ts +++ b/src/app/features/indicadores/indicadores-fhir.spec.ts @@ -2,12 +2,14 @@ // technical sheet from `Measure`, the data rows from `MeasureReport`, the periods from two one-report searches, the // C3r box plot, and the follow-up list and patient summary from `EpisodeOfCare` and `$resumen-tea`; each mapped into // the shapes the page reads and passed through the same checks. -// Fixtures: `indicadores-fhir.fixtures.ts`, real responses of the WebApi over its TEA fixture database. +// Fixtures: `indicadores-fhir.fixtures.ts`, real responses of the WebApi over its TEA fixture database; `conModelo` +// below adds to its patient summary the age at the EEG, a metric's age-norm curve and the ADOS-2 association model, +// in the shape `webapi/fhir/tea.py` emits them. import { TestBed } from '@angular/core/testing'; import { provideHttpClient } from '@angular/common/http'; import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; import { firstValueFrom } from 'rxjs'; -import { Bundle, resources } from './fhir'; +import { Bundle, Resource, resources } from './fhir'; import { cajaDeDatos, datosDeReportes, FhirIndicadoresPeriodos, FhirIndicadoresService, indicadorDeMeasure, Measure, periodoIniciado, periodosDesde, Report, @@ -20,6 +22,49 @@ import { FhirTeaSeguimientoService, mapaDeElectrodos } from './tea-seguimiento-f import { IndicadorDatos } from './indicadores.models'; const measure = (id: string) => resources(fx.measures).find(m => m.id === id) as Measure; + +const SD = 'https://isssteleon.org/fhir/StructureDefinition'; +const CS = 'https://isssteleon.org/fhir/CodeSystem'; +const CURVA = `${SD}/curva-referencia`; +const curva = { + url: CURVA, extension: [ + { url: `${CURVA}/fuente`, valueString: 'tea-ritmo_posterior/3' }, { url: `${CURVA}/estado`, valueString: 'validada' }, + ...[12, 24, 36].map(v => ({ url: `${CURVA}/edad-meses`, valueDecimal: v })), + ...([['p5', [4, 4.5, 5]], ['p25', [5, 5.5, 6]], ['mediana', [5.5, 6, 6.5]], ['p75', [6, 6.5, 7]], ['p95', [6.5, 7, 7.5]]] as [string, number[]][]) + .flatMap(([p, vs]) => vs.map(v => ({ url: `${CURVA}/${p}`, valueDecimal: v }))), + ], +}; +const prediccion = (zona: string, z: number | null) => ({ + outcome: { coding: [{ system: `${CS}/tea-catalogo`, code: 'ados2.sa_b9', display: 'Mostrar' }] }, + probabilityDecimal: 0.41, + extension: [ + { url: `${SD}/biomarcador`, valueCoding: { system: `${CS}/tea-eeg-metrica`, code: 'RITMO_POSTERIOR', display: 'Ritmo posterior' } }, + { url: `${SD}/zona`, valueCoding: { system: `${CS}/tea-eeg-zona`, code: zona, display: 'Occipital' } }, + { url: `${SD}/tipo`, valueString: 'item' }, { url: `${SD}/evento`, valueString: '>=2' }, + ...(z === null ? [] : [{ url: `${SD}/valor-z`, valueDecimal: z }]), + { url: `${SD}/odds-ratio`, valueDecimal: 0.5 }, { url: `${SD}/ic-inf`, valueDecimal: 0.3 }, { url: `${SD}/ic-sup`, valueDecimal: 0.8 }, + { url: `${SD}/q`, valueDecimal: 0.01 }, { url: `${SD}/probabilidad-referencia`, valueDecimal: 0.2 }, { url: `${SD}/observado`, valueInteger: 2 }, + ], +}); +// The fixture's patient summary with what the WebApi adds when the analysis and the association model have data. +function conModelo(): Bundle { + const bundle = structuredClone(fx.resumenPaciente) as Bundle; + for (const r of resources(bundle)) { + if (r.resourceType === 'DiagnosticReport') r.extension = [...(r.extension ?? []), { url: `${SD}/edad-meses`, valueDecimal: 23.6 }]; + const codigo = (r['code'] as { coding?: { code?: string }[] } | undefined)?.coding?.[0]?.code; + if (r.resourceType === 'Observation' && codigo === 'ritmo_posterior' && !r.id?.startsWith('electrodos-')) + r.extension = [...(r.extension ?? []), curva]; + } + const riesgo: Resource = { + resourceType: 'RiskAssessment', id: 'relacion-ados2-P-4A59E9D8B4', status: 'final', + meta: { tag: resources(bundle)[0].meta?.tag }, + extension: [{ url: `${SD}/modelo-version`, valueString: '1.0.0' }, { url: `${SD}/modelo-validado`, valueBoolean: false }, + { url: `${SD}/modelo-nivel`, valueString: 'item' }, { url: `${SD}/modelo-n-cohorte`, valueInteger: 40 }], + prediction: [prediccion('occipital', -2.6), prediccion('parietal', null)], + }; + bundle.entry = [...(bundle.entry ?? []), { resource: riesgo }]; + return bundle; +} const reports = (bundle: Bundle) => resources(bundle, 'MeasureReport') as Report[]; describe('Indicadores sobre FHIR', () => { @@ -210,6 +255,49 @@ describe('Seguimiento de TEA sobre FHIR', () => { expect(codigos.filter(c => c === 'ritmo_posterior').length).toBe(1); }); + it('el resumen lleva la edad al EEG y la norma por edad de la métrica cuando la WebApi las manda', async () => { + // Pins down: the report's `edad-meses` extension as the EEG's age and a metric observation's `curva-referencia` + // sub-extensions read back in order as the age norm (ages, five percentiles, source and state), so the page's + // checks accept it. Fails if the adapter stops reading either extension or scrambles the series. + const answer = firstValueFrom(service.detalle('P-4A59E9D8B4')); + http.expectOne('/tea/fhir/Patient/P-4A59E9D8B4/$resumen-tea').flush(conModelo()); + http.expectOne('/tea/fhir/CodeSystem/tea-eeg-metrica').flush(fx.metricasEeg); + const detalle = await answer; + expect(detalle.eeg!.edad_meses).toBe(23.6); + expect(detalle.eeg!.alteraciones.find(a => a.codigo === 'ritmo_posterior')!.curva_referencia).toEqual({ + fuente: 'tea-ritmo_posterior/3', estado: 'validada', edad_meses: [12, 24, 36], + p5: [4, 4.5, 5], p25: [5, 5.5, 6], mediana: [5.5, 6, 6.5], p75: [6, 6.5, 7], p95: [6.5, 7, 7.5] }); + }); + + it('el resumen arma la relación con el ADOS-2 del RiskAssessment y deja fuera la fila incompleta', async () => { + // Pins down: the model's version, validation, level and cohort from the resource's extensions, and each + // prediction as a row with the metric's and zone's labels, the outcome, OR, interval, q, z, both probabilities + // and the observed score; a prediction without its z is dropped instead of failing the page's checks. Fails if + // the adapter stops reading the RiskAssessment or lets an incomplete row through. + const answer = firstValueFrom(service.detalle('P-4A59E9D8B4')); + http.expectOne('/tea/fhir/Patient/P-4A59E9D8B4/$resumen-tea').flush(conModelo()); + http.expectOne('/tea/fhir/CodeSystem/tea-eeg-metrica').flush(fx.metricasEeg); + const relacion = (await answer).relacion_ados2!; + expect(relacion).toMatchObject({ version: '1.0.0', validado: false, nivel: 'item', n_cohorte: 40 }); + expect(relacion.filas).toEqual([{ + biomarcador: 'Ritmo posterior', zona: 'occipital', zona_etiqueta: 'Occipital', valor_z: -2.6, tipo: 'item', + resultado: 'ados2.sa_b9', resultado_etiqueta: 'Mostrar', evento: '>=2', or: 0.5, ic_inf: 0.3, ic_sup: 0.8, q: 0.01, + probabilidad: 0.41, probabilidad_referencia: 0.2, observado: 2 }]); + }); + + it('sin edad, norma ni modelo el resumen no inventa esos campos', async () => { + // Pins down: a summary without the new extensions or a RiskAssessment (the fixture as the WebApi served it before + // the analysis wrote them) leaves `edad_meses`, `curva_referencia` and `relacion_ados2` out, so the page hides + // the chart and the panel. Fails if the adapter fills them with defaults. + const answer = firstValueFrom(service.detalle('P-4A59E9D8B4')); + http.expectOne('/tea/fhir/Patient/P-4A59E9D8B4/$resumen-tea').flush(fx.resumenPaciente); + http.expectOne('/tea/fhir/CodeSystem/tea-eeg-metrica').flush(fx.metricasEeg); + const detalle = await answer; + expect('edad_meses' in detalle.eeg!).toBe(false); + expect(detalle.eeg!.alteraciones.some(a => 'curva_referencia' in a)).toBe(false); + expect('relacion_ados2' in detalle).toBe(false); + }); + it('el mapa toma el valor de cada electrodo y deja en blanco lo que ninguno alcanza', () => { // Pins down: the electrode's own z at its position, regions averaged from the measured electrodes only, and no // value far from every electrode. Fails if the map paints the whole head from a few occipital electrodes. diff --git a/src/app/features/indicadores/tea-paciente-resumen.html b/src/app/features/indicadores/tea-paciente-resumen.html index 3d8c515..94862ac 100644 --- a/src/app/features/indicadores/tea-paciente-resumen.html +++ b/src/app/features/indicadores/tea-paciente-resumen.html @@ -98,7 +98,7 @@ } @if (vista === 'norma' && normaActual(e); as m) {