a15e37ed77
A semester that has just begun has no events and the chart opened on it, empty, while earlier ones had data. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
132 lines
9.2 KiB
TypeScript
132 lines
9.2 KiB
TypeScript
// What this module covers: the screening route chart's model and figure. The model turns the seven C3r payloads of the
|
|
// route (days from the sample and days from the previous event, per event) into one row per event for a period, and
|
|
// chains the steps one after another; the figure draws them as horizontal bars of days.
|
|
// Fixtures: `payload`, synthetic C3r /datos shaped like the WebApi's, with the values of the chart's mockup: a
|
|
// cytology result at 7.9 days, a colposcopy at 35.7, a biopsy result at 52.1 and a diagnosis after it at 64.9.
|
|
import { IndicadorDatos } from './indicadores.models';
|
|
import { C2_PALETTE } from './indicadores-c2-palette';
|
|
import { drawnRows, ROUTE_IDS, routeCellText, routeFigure, routeHasData, routeModel, RouteIncompatible } from './screening-route';
|
|
|
|
type Box = [number, number, number, number];
|
|
const row = (periodo: string, [n, q1, mediana, q3]: Box, extra: Record<string, unknown> = {}) =>
|
|
({ periodo, grupo: 'TOTAL', estrato: null, n, q1, mediana, q3, n_sin_evento: 0, n_excluido: 0, suprimido: 0, unidad: 'dias', ...extra });
|
|
const payload = (id: string, filas: Record<string, unknown>[], periodos = ['2025-S1', '2025-S2']): IndicadorDatos =>
|
|
({ id, contrato: 'C3r', tipo_periodo: 'semestre', periodos, filas, version: '1' });
|
|
const S2: Record<string, Box> = {
|
|
cacu_eu_tiempo_reporte: [4855, 4.9, 7.9, 12.2],
|
|
cacu_dias_toma_colposcopia: [864, 22.2, 35.7, 55.4],
|
|
cacu_dias_resultado_colposcopia: [864, 17, 27.8, 44],
|
|
cacu_dias_toma_biopsia: [454, 32.4, 52.1, 80.8],
|
|
cacu_dias_colposcopia_biopsia: [454, 10, 16.4, 26],
|
|
cacu_dias_toma_diagnostico: [4999, 11.4, 18.4, 28.5],
|
|
cacu_dias_biopsia_diagnostico: [454, 8, 12.8, 20],
|
|
};
|
|
function all(): Record<string, IndicadorDatos> {
|
|
return Object.fromEntries(ROUTE_IDS.map(id => [id, payload(id, [row('2025-S1', [10, 1, 2, 3]), row('2025-S2', S2[id])])]));
|
|
}
|
|
const figure = (view: 'cumulative' | 'steps', data = all(), period?: string) => routeFigure(routeModel(data, 'semestre', period), view, 'claro');
|
|
|
|
describe('Ruta del tamizaje: modelo', () => {
|
|
it('una fila por evento del periodo más reciente, con los pasos encadenados uno tras otro', () => {
|
|
// Pins down: the newest period is the default and each step starts where the previous one ends (7.9 + 27.8 = 35.7,
|
|
// + 16.4 = 52.1, + 12.8 = 64.9). Fails if the chain restarts at zero or follows another order.
|
|
const m = routeModel(all(), 'semestre');
|
|
expect(m.periods).toEqual(['2025-S2', '2025-S1']);
|
|
expect(m.period).toBe('2025-S2');
|
|
expect(m.rows.map(r => r.step.id)).toEqual(['cytology', 'colposcopy', 'biopsy', 'diagnosis']);
|
|
expect(m.rows.map(r => [r.start, r.end!.toFixed(1)])).toEqual([[0, '7.9'], [7.9, '35.7'], [35.7, '52.1'], [52.1, '64.9']]);
|
|
expect(m.rows[1].fromSample).toEqual({ estado: 'Publicado', n: 864, q1: 22.2, mediana: 35.7, q3: 55.4 });
|
|
});
|
|
it('un periodo pedido y publicado se usa; uno que no existe cae al más reciente', () => {
|
|
// Fails if a period nobody published is shown empty instead of falling back.
|
|
expect(routeModel(all(), 'semestre', '2025-S1').period).toBe('2025-S1');
|
|
expect(routeModel(all(), 'semestre', '2024-S1').period).toBe('2025-S2');
|
|
});
|
|
it('sin periodo pedido, el más reciente vacío no se elige: se usa el más reciente con cajas publicadas', () => {
|
|
// Fails if the chart opens on a semester that has just begun and shows nothing while an earlier one has data.
|
|
const vacio = (id: string) => payload(id, [row('2025-S2', S2[id]), row('2026-S1', [0, null as never, null as never, null as never], { suprimido: 1 })], ['2025-S2', '2026-S1']);
|
|
const data = Object.fromEntries(ROUTE_IDS.map(id => [id, vacio(id)]));
|
|
expect(routeModel(data, 'semestre').period).toBe('2025-S2');
|
|
expect(routeModel(data, 'semestre', '2026-S1').period).toBe('2026-S1');
|
|
});
|
|
it('un paso suprimido o sin datos no se dibuja y la cadena sigue desde el último publicado', () => {
|
|
// Pins down: nothing is drawn as zero and the next step starts at the last published end. Fails if a suppressed box
|
|
// becomes a bar or leaves a gap in the chain.
|
|
const d = all();
|
|
d['cacu_dias_colposcopia_biopsia'] = payload('cacu_dias_colposcopia_biopsia', [row('2025-S2', [454, 10, 16.4, 26], { suprimido: 1 })]);
|
|
const m = routeModel(d, 'semestre');
|
|
expect(m.rows[2]).toMatchObject({ start: null, end: null });
|
|
expect(m.rows[3].start!.toFixed(1)).toBe('35.7');
|
|
expect(drawnRows(m, 'steps').map(r => r.step.id)).toEqual(['cytology', 'colposcopy', 'diagnosis']);
|
|
d['cacu_dias_toma_biopsia'] = payload('cacu_dias_toma_biopsia', []);
|
|
expect(drawnRows(routeModel(d, 'semestre'), 'cumulative').map(r => r.step.id)).toEqual(['cytology', 'colposcopy', 'diagnosis']);
|
|
});
|
|
it('rechaza datos de otro indicador, contrato, tipo de periodo o unidad, y dos totales por periodo', () => {
|
|
// Fails if a payload that doesn't belong to the route is drawn as if it did.
|
|
const con = (id: string, cambio: Partial<IndicadorDatos>) => ({ ...all(), [id]: { ...all()[id], ...cambio } });
|
|
expect(() => routeModel(con('cacu_eu_tiempo_reporte', { id: 'otro' }), 'semestre')).toThrow(RouteIncompatible);
|
|
expect(() => routeModel(con('cacu_eu_tiempo_reporte', { contrato: 'C1' }), 'semestre')).toThrow(RouteIncompatible);
|
|
expect(() => routeModel(all(), 'mes')).toThrow(RouteIncompatible);
|
|
expect(() => routeModel(con('cacu_eu_tiempo_reporte', { filas: [row('2025-S2', S2['cacu_eu_tiempo_reporte'], { unidad: 'horas' })] }), 'semestre')).toThrow(RouteIncompatible);
|
|
expect(() => routeModel(con('cacu_eu_tiempo_reporte', { filas: [row('2025-S2', [1, 1, 1, 1]), row('2025-S2', [2, 2, 2, 2])] }), 'semestre')).toThrow(RouteIncompatible);
|
|
const sinUno = all(); delete sinUno['cacu_dias_toma_biopsia'];
|
|
expect(() => routeModel(sinUno, 'semestre')).toThrow(RouteIncompatible);
|
|
});
|
|
it('sin periodos publicados no hay nada que dibujar', () => {
|
|
// Fails if an empty catalog throws or draws an empty bar.
|
|
const vacio = Object.fromEntries(ROUTE_IDS.map(id => [id, payload(id, [], [])]));
|
|
const m = routeModel(vacio, 'semestre');
|
|
expect(m.period).toBeUndefined();
|
|
expect(routeHasData(m)).toBe(false);
|
|
expect(routeFigure(m, 'cumulative', 'claro')).toEqual({ data: [], layout: {} });
|
|
});
|
|
it('la celda de la tabla dice el estado si no hay caja publicada', () => {
|
|
expect(routeCellText({ estado: 'Suprimido' })).toBe('Suprimido');
|
|
expect(routeCellText(routeModel(all(), 'semestre').rows[0].fromSample)).toBe('n = 4,855, Q1 4.9, mediana 7.9, Q3 12.2 días');
|
|
});
|
|
});
|
|
|
|
describe('Ruta del tamizaje: figura', () => {
|
|
it('acumulado: barras horizontales hasta la mediana con el intervalo de Q1 a Q3 y el número de casos', () => {
|
|
// Pins down: orientation, bar lengths, the asymmetric interval, the case counts in the labels and the first
|
|
// event on top. Fails if the bars stop being horizontal or the interval stops being Q1 to Q3.
|
|
const f = figure('cumulative');
|
|
const trace = f.data[0] as Record<string, any>;
|
|
expect(trace['type']).toBe('bar');
|
|
expect(trace['orientation']).toBe('h');
|
|
expect(trace['x']).toEqual([7.9, 35.7, 52.1, 18.4]);
|
|
expect(trace['error_x'].array.map((v: number) => +v.toFixed(1))).toEqual([4.3, 19.7, 28.7, 10.1]);
|
|
expect(trace['error_x'].arrayminus.map((v: number) => +v.toFixed(1))).toEqual([3, 13.5, 19.7, 7]);
|
|
expect(trace['y'][0]).toBe('Resultado de citología<br>n = 4,855');
|
|
expect(trace['y']).toHaveLength(4);
|
|
expect((f.layout['yaxis'] as Record<string, unknown>)['autorange']).toBe('reversed');
|
|
expect(JSON.stringify(f.data)).not.toContain('null');
|
|
});
|
|
it('por pasos: cada barra flota desde donde termina la anterior y la más lenta lleva el color', () => {
|
|
// Pins down: `base` equals the chained starts, the length is the step's own median, the slowest step (colposcopy,
|
|
// 27.8 days) is the only one in colour. Fails if the steps stop floating or the emphasis moves.
|
|
const f = figure('steps');
|
|
const trace = f.data[0] as Record<string, any>;
|
|
expect(trace['base'].map((v: number) => +v.toFixed(1))).toEqual([0, 7.9, 35.7, 52.1]);
|
|
expect(trace['x']).toEqual([7.9, 27.8, 16.4, 12.8]);
|
|
expect(trace['text']).toEqual(['+7.9 d', '+27.8 d', '+16.4 d', '+12.8 d']);
|
|
expect(trace['marker'].color).toEqual([C2_PALETTE.states.noData, C2_PALETTE.series[0], C2_PALETTE.states.noData, C2_PALETTE.states.noData]);
|
|
expect(trace['y'][1]).toBe('Del resultado de citología a la colposcopía<br>n = 864');
|
|
expect(trace['hovertemplate']).toContain('Hasta el día');
|
|
});
|
|
it('un paso sin caja se omite de la figura sin dejar huecos', () => {
|
|
// Fails if an undrawn step keeps its label or leaves a null in the traces.
|
|
const d = all();
|
|
d['cacu_dias_toma_biopsia'] = payload('cacu_dias_toma_biopsia', [row('2025-S2', S2['cacu_dias_toma_biopsia'], { suprimido: 1 })]);
|
|
const trace = figure('cumulative', d).data[0] as Record<string, any>;
|
|
expect(trace['y']).toHaveLength(3);
|
|
expect(trace['x']).toEqual([7.9, 35.7, 18.4]);
|
|
});
|
|
it('el tema oscuro cambia fondo y texto, no los datos', () => {
|
|
const claro = figure('steps'), oscuro = routeFigure(routeModel(all(), 'semestre'), 'steps', 'oscuro');
|
|
expect(oscuro.layout['paper_bgcolor']).toBe('#081839');
|
|
expect(claro.layout['paper_bgcolor']).toBe('#FFFFFF');
|
|
expect(oscuro.data[0]['x']).toEqual(claro.data[0]['x']);
|
|
});
|
|
});
|