188 lines
11 KiB
TypeScript
188 lines
11 KiB
TypeScript
// What this covers: reading the WebApi's FHIR `MeasureReport` bundles (`/<domain>/indicadores/<id>`) into a series, drawing
|
|
// it here, the service that asks for it and the component that shows it.
|
|
// Fixtures: `cacu_ind1_measure_report_bundle.json` (IND1, 2025-S1 and 2025-S2) and
|
|
// `cacu_eu13_measure_report_bundle.json` (EU13, 2025-S1, with the `grupo_edad` stratifier) and
|
|
// `cacu_eu7_measure_report_bundle.json` (EU7, a composition, 2025-S1 and 2025-S2) and
|
|
// `cacu_dias_toma_diagnostico_measure_report_bundle.json` (days to diagnosis, a duration, 2025-S2): written by the
|
|
// WebApi's route tests over the synthetic fixture database, counts and days only.
|
|
import { Component, input } from '@angular/core';
|
|
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 { PlotlyChart } from '../../shared/plotly/plotly-chart';
|
|
import ind1 from './fixtures/cacu_ind1_measure_report_bundle.json';
|
|
import eu13 from './fixtures/cacu_eu13_measure_report_bundle.json';
|
|
import eu7 from './fixtures/cacu_eu7_measure_report_bundle.json';
|
|
import dias from './fixtures/cacu_dias_toma_diagnostico_measure_report_bundle.json';
|
|
import { IndicadorMeasureReport } from './indicador-measure-report';
|
|
import { compositionFigure, compositionFrom, durationFigure, durationFrom, InvalidMeasureReport, MeasureReportBundle, periodLabel, proportionFigure, seriesFrom } from './measure-report';
|
|
import { KpisPage } from './kpis-page';
|
|
import { MeasureReportService } from './measure-report.service';
|
|
|
|
@Component({ selector: 'app-plotly-chart', template: '' })
|
|
class ChartStub { figure = input<unknown>(); label = input<string>(); }
|
|
|
|
const IND1 = ind1 as unknown as MeasureReportBundle;
|
|
const EU13 = eu13 as unknown as MeasureReportBundle;
|
|
const EU7 = eu7 as unknown as MeasureReportBundle;
|
|
const DIAS = dias as unknown as MeasureReportBundle;
|
|
|
|
describe('measure reports', () => {
|
|
it('names a period from its FHIR dates', () => {
|
|
// Pins down: semesters and months read as the WebApp names them. Fails if FHIR's inclusive end is misread.
|
|
expect(periodLabel({ start: '2025-01-01', end: '2025-06-30' })).toBe('2025-S1');
|
|
expect(periodLabel({ start: '2025-07-01', end: '2025-12-31' })).toBe('2025-S2');
|
|
expect(periodLabel({ start: '2025-03-01', end: '2025-03-31' })).toBe('2025-03');
|
|
expect(periodLabel({ start: '2025-01-01', end: '2025-03-31' })).toBe('2025-01-01 a 2025-03-31');
|
|
});
|
|
|
|
it('reads one point per period from a bundle', () => {
|
|
// Pins down: the counts come from the populations, the order from the bundle. Fails if the reading of FHIR's
|
|
// population codes or of `improvementNotation` changes.
|
|
const series = seriesFrom(IND1);
|
|
expect(series.measure).toBe('cacu/IND1|0.3.2');
|
|
expect(series.lowerIsBetter).toBe(true);
|
|
expect(series.total).toEqual([
|
|
{ period: '2025-S1', numerator: 1, denominator: 4, value: 0.25 },
|
|
{ period: '2025-S2', numerator: 1, denominator: 4, value: 0.25 },
|
|
]);
|
|
});
|
|
|
|
it('reads the strata of a stratifier', () => {
|
|
// Pins down: each stratum's value is a series of its own. Fails if `stratifier` stops being read.
|
|
expect(seriesFrom(EU13).strata).toEqual({
|
|
grupo_edad: { '45-54': [{ period: '2025-S1', numerator: 0, denominator: 4, value: 0 }] },
|
|
});
|
|
});
|
|
|
|
it('rejects what is not a bundle of reports of one measure', () => {
|
|
// Pins down: a malformed answer fails loudly instead of drawing zeros. Fails if validation is dropped.
|
|
expect(() => seriesFrom({ resourceType: 'Bundle', entry: [] } as MeasureReportBundle)).toThrow(InvalidMeasureReport);
|
|
const mixed = { ...IND1, entry: [IND1.entry[0], { resource: { ...IND1.entry[1].resource, measure: 'otro' } }] };
|
|
expect(() => seriesFrom(mixed)).toThrow(InvalidMeasureReport);
|
|
});
|
|
|
|
it('draws the figure here: percentages per period, one line per stratum value', () => {
|
|
// Pins down: the WebApp decides the figure from the data. Fails if values stop being percentages, or a
|
|
// stratum stops giving one trace per value.
|
|
const total = proportionFigure(seriesFrom(IND1), 'Pacientes con citología anómala');
|
|
expect(total.data).toHaveLength(1);
|
|
expect(total.data[0]['x']).toEqual(['2025-S1', '2025-S2']);
|
|
expect(total.data[0]['y']).toEqual([25, 25]);
|
|
const byAge = proportionFigure(seriesFrom(EU13), 'Tasa de detección', 'grupo_edad');
|
|
expect(byAge.data.map(trace => trace['name'])).toEqual(['45-54']);
|
|
});
|
|
});
|
|
|
|
describe('MeasureReportService and IndicadorMeasureReport', () => {
|
|
beforeEach(() => TestBed.configureTestingModule({ providers: [provideHttpClient(), provideHttpClientTesting()] }));
|
|
|
|
it('asks the WebApi for the periods and cut-offs and reads the bundle', async () => {
|
|
// Pins down the request: the route, `periodos` comma-separated and the cut-offs only when given. Fails if
|
|
// the URL or a parameter's name drifts from the WebApi's.
|
|
const answer = firstValueFrom(TestBed.inject(MeasureReportService)
|
|
.series('cacu', 'IND1', ['2025-S1', '2025-S2'], { evento: '2025-12-31' }));
|
|
const request = TestBed.inject(HttpTestingController).expectOne(r => r.url === '/cacu/indicadores/IND1');
|
|
expect(request.request.params.get('periodos')).toBe('2025-S1,2025-S2');
|
|
expect(request.request.params.get('corte_evento')).toBe('2025-12-31');
|
|
expect(request.request.params.has('corte_conocimiento')).toBe(false);
|
|
request.flush(IND1);
|
|
expect((await answer).total.map(p => p.value)).toEqual([0.25, 0.25]);
|
|
});
|
|
|
|
it('shows the figure it drew from the answer', () => {
|
|
// Pins down: the component hands the chart the figure built from the bundle. Fails if it stops drawing.
|
|
TestBed.overrideComponent(IndicadorMeasureReport, { remove: { imports: [PlotlyChart] }, add: { imports: [ChartStub] } });
|
|
const fixture = TestBed.createComponent(IndicadorMeasureReport);
|
|
Object.assign(fixture.componentInstance, { dominio: 'cacu', indicador: 'IND1', titulo: 'IND1', periodos: ['2025-S1', '2025-S2'] });
|
|
fixture.componentInstance.ngOnChanges();
|
|
TestBed.inject(HttpTestingController).expectOne(r => r.url === '/cacu/indicadores/IND1').flush(IND1);
|
|
fixture.detectChanges();
|
|
expect(fixture.componentInstance.status()).toBe('ready');
|
|
expect(fixture.componentInstance.figure()?.data[0]['y']).toEqual([25, 25]);
|
|
});
|
|
});
|
|
|
|
describe('compositions', () => {
|
|
it('reads the categories of each period from the `resultado` stratifier', () => {
|
|
// Pins down: a composition is a count per category and period, zero where a period lacks one. Fails if the
|
|
// stratifier stops being read or a missing category stops being zero.
|
|
const series = compositionFrom(EU7);
|
|
expect(series.periods).toEqual(['2025-S1', '2025-S2']);
|
|
expect(series.categories).toEqual({ LIEBG: [1, 0], NEGATIVO: [2, 1], SIN_DATO: [1, 2], LIEAG: [0, 1] });
|
|
});
|
|
|
|
it('takes the categories from the first stratifier, whatever its column is called', () => {
|
|
// Pins down: a composition whose category isn't `resultado` (the confirmation by biopsia_grado) is read too, and
|
|
// a stratifier can still be asked for by name. Fails if the reader goes back to a fixed name.
|
|
const renamed = { ...EU7, entry: EU7.entry.map(e => ({ resource: { ...e.resource, group: [{ ...e.resource.group[0],
|
|
stratifier: e.resource.group[0].stratifier!.map((s, i) => (i === 0 ? { ...s, code: [{ text: 'biopsia_grado' }] } : s)) }] } })) };
|
|
expect(compositionFrom(renamed).categories).toEqual(compositionFrom(EU7).categories);
|
|
expect(() => compositionFrom(EU7, 'no_existe')).toThrow(InvalidMeasureReport);
|
|
});
|
|
|
|
it('draws stacked bars, one segment per category', () => {
|
|
// Pins down: the WebApp draws compositions from the same contract. Fails if a category stops being a trace.
|
|
const figure = compositionFigure(compositionFrom(EU7), 'EU7');
|
|
expect(figure.layout['barmode']).toBe('stack');
|
|
expect(figure.data.map(trace => trace['name']).sort()).toEqual(['LIEAG', 'LIEBG', 'NEGATIVO', 'SIN_DATO']);
|
|
});
|
|
});
|
|
|
|
describe('durations', () => {
|
|
it('reads the samples measurable and measured, the median and the quartiles of each period', () => {
|
|
// Pins down: a duration's populations and its score with the quartiles as extensions. Fails if the WebApi's
|
|
// duration contract or its reading drifts.
|
|
expect(durationFrom(DIAS).points).toEqual([
|
|
{ period: '2025-S2', initial: 4, n: 2, q1: 70.25, median: 100.5, q3: 130.75 },
|
|
]);
|
|
expect(durationFrom(DIAS).lowerIsBetter).toBe(true);
|
|
});
|
|
|
|
it('draws a box per period from the served quartiles, without whiskers', () => {
|
|
// Pins down: the WebApp draws durations from the same contract. Fails if the box stops using the quartiles or
|
|
// starts drawing extremes it wasn't given.
|
|
const [box] = durationFigure(durationFrom(DIAS), 'Días a diagnóstico').data;
|
|
expect(box['type']).toBe('box');
|
|
expect([box['q1'], box['median'], box['q3']]).toEqual([[70.25], [100.5], [130.75]]);
|
|
expect([box['lowerfence'], box['upperfence']]).toEqual([[70.25], [130.75]]);
|
|
});
|
|
|
|
it('rejects a duration measured without its median', () => {
|
|
// Pins down: a malformed answer fails loudly. Fails if the check on the score is dropped.
|
|
const [entry] = DIAS.entry;
|
|
const broken = { ...DIAS, entry: [{ resource: { ...entry.resource, group: [{ ...entry.resource.group[0], measureScore: undefined }] } }] };
|
|
expect(() => durationFrom(broken)).toThrow(InvalidMeasureReport);
|
|
});
|
|
});
|
|
|
|
describe('KpisPage', () => {
|
|
beforeEach(() => TestBed.configureTestingModule({ providers: [provideHttpClient(), provideHttpClientTesting()] }));
|
|
|
|
it('draws every measure the WebApi lists, each from its reports', () => {
|
|
// Pins down: the page needs nothing per measure: it lists `/cacu/indicadores` and draws proportions, compositions
|
|
// and durations from the one contract. Fails if a scoring stops being drawn or the page starts naming measures.
|
|
TestBed.overrideComponent(KpisPage, { remove: { imports: [PlotlyChart] }, add: { imports: [ChartStub] } });
|
|
const fixture = TestBed.createComponent(KpisPage);
|
|
fixture.componentInstance.ngOnInit();
|
|
const http = TestBed.inject(HttpTestingController);
|
|
http.expectOne('/cacu/indicadores').flush([
|
|
{ id: 'IND1', url: 'cacu/IND1', version: '0.3.2', scoring: 'proportion' },
|
|
{ id: 'CACU_EU13', url: 'cacu/CACU_EU13', version: '0.3.2', scoring: 'proportion' },
|
|
{ id: 'CACU_EU7', url: 'cacu/CACU_EU7', version: '0.3.3', scoring: 'composition' },
|
|
{ id: 'CACU_DIAS_TOMA_DIAGNOSTICO', url: 'cacu/CACU_DIAS_TOMA_DIAGNOSTICO', version: '0.2.3', scoring: 'continuous-variable' },
|
|
]);
|
|
http.expectOne(r => r.url === '/cacu/indicadores/IND1').flush(IND1);
|
|
http.expectOne(r => r.url === '/cacu/indicadores/CACU_EU13').flush(EU13);
|
|
http.expectOne(r => r.url === '/cacu/indicadores/CACU_EU7').flush(EU7);
|
|
http.expectOne(r => r.url === '/cacu/indicadores/CACU_DIAS_TOMA_DIAGNOSTICO').flush(DIAS);
|
|
fixture.detectChanges();
|
|
expect(fixture.componentInstance.status()).toBe('ready');
|
|
expect(fixture.componentInstance.figures().map(f => f.id)).toEqual(['IND1', 'CACU_EU13', 'CACU_EU7', 'CACU_DIAS_TOMA_DIAGNOSTICO']);
|
|
expect(fixture.componentInstance.figures()[2].figure.layout['barmode']).toBe('stack');
|
|
expect(fixture.componentInstance.figures()[3].figure.data[0]['type']).toBe('box');
|
|
});
|
|
});
|
|
|