Files
WebApp/src/app/features/indicadores/indicadores-kpi.spec.ts
T
Angel Capetillo Zavala 7b8fd9e01d feat(indicadores): CaMa labels, single-period filters and the four-card strip
CaMa reads «cáncer de mama» everywhere on the indicators page and uses the compact filter: interval, one period and Aplicar on one row, without the mode selector, the multiple selection or the editing summaries. The opportunity strip is the four cards only, and the biopsy distribution card is no longer drawn in the summary (it stays in Datos y definiciones). Other domains keep their filter and strip.

(cherry picked from commit 7c9dcf634e5870fec35ab96347b0863b2e3bb86e)
2026-11-12 00:29:08 -06:00

69 lines
5.2 KiB
TypeScript

import { CAMA_TAMIZAJE_RESULTADO_KPI, CAMA_TASA_DETECCION_KPI, KPI_BY_DOMAIN, KpiDefinition, kpiResult } from './indicadores-kpi';
import { GlobalApplied } from './indicadores-memory';
import { Dominio, IndicadorDatos } from './indicadores.models';
const applied: GlobalApplied = { tipo: 'semestre', selection: { mode: 'explicit', periodos: ['2025-S1'] }, revision: 0 };
const total = { periodo: '2025-S1', grupo: 'TOTAL', estrato: null, suprimido: 0 };
// Keep C1 numerical-contract coverage even though CaCu no longer presents a C1 KPI.
// CaMa's C1 and C3r cards are the opportunity ones the catalog may not publish yet; the contract rules are the same.
const definitions = { ...KPI_BY_DOMAIN, cacu: [KPI_BY_DOMAIN.cacu[0],
{ id: 'cacu_eu13', title: 'Confirmación', contrato: 'C1', unit: '%' } as KpiDefinition, KPI_BY_DOMAIN.cacu[1]],
cama: [KPI_BY_DOMAIN.cama[0], CAMA_TASA_DETECCION_KPI, CAMA_TAMIZAJE_RESULTADO_KPI] };
function data(domain: Dominio, index: number, filas: Record<string, unknown>[]): IndicadorDatos {
const d = definitions[domain][index]; return { id: d.id, contrato: d.contrato, tipo_periodo: 'semestre', periodos: ['2025-S1'], categorias: Object.fromEntries(filas.filter(row => typeof row['categoria'] === 'string').map(row => [row['categoria'], {}])), filas };
}
describe.each(['tea', 'cacu', 'cama'] as const)('KPI %s', domain => {
const c1Group = domain === 'tea' ? 'TOTAL' : 'total';
const run = (index: number, rows: Record<string, unknown>[]) => kpiResult(data(domain, index, rows), definitions[domain][index], domain, applied);
it('C2 suma categorías disjuntas del único TOTAL incluyendo SIN_DATO y cero', () => {
expect(run(0, [{ ...total, categoria: 'A', n: 5 }, { ...total, categoria: 'SIN_DATO', n: 0 }]).value).toBe(5);
expect(run(0, [{ ...total, categoria: 'A', n: 0 }]).value).toBe(0);
});
it('C1 consume porcentaje sin recalcular y muestra numerador y denominador', () => {
const result = run(1, [{ ...total, grupo: c1Group, valor: 33.36, numerador: 2, denominador: 6 }]);
expect(result.value).toBe('33.4'); expect(result.info).toContain('2 de 6');
expect(run(1, [{ ...total, grupo: c1Group, valor: 0, numerador: 0, denominador: 6 }]).value).toBe('0.0');
});
it('C3r conserva mediana cero y Q1/Q3, sin reglas provisionales en la tarjeta', () => {
const result = run(2, [{ ...total, mediana: 0, n: 5, q1: 0, q3: 7, unidad: 'dias' }]);
expect(result.value).toBe(0);
expect(result.info).toContain(domain === 'cacu' ? 'primer cuartil: 0 días; tercer cuartil: 7 días' : '0 a 7 días');
expect(result.info).not.toContain('provisional');
});
it.each([0, 1, 2])('suprimido en %s oculta cualquier cifra', index => {
expect(run(index, [{ ...total, grupo: index === 1 ? c1Group : 'TOTAL', categoria: 'A', n: 5, valor: 100, mediana: 7, suprimido: 2 }])).toEqual({ state: 'Dato oculto por tamaño' });
});
it.each([0, 1, 2])('nulos en %s no se convierten en cero', index => {
const result = run(index, [{ ...total, grupo: index === 1 ? c1Group : 'TOTAL', categoria: 'A', n: null, valor: null, mediana: null }]);
expect(result.value).toBeUndefined(); expect(result.state).toBe('Sin valor disponible');
});
it('no suma grupos superpuestos, estratos ni categorías duplicadas', () => {
for (const rows of [[{ ...total, categoria: 'A', n: 5 }, { ...total, grupo: 'OTHER', categoria: 'A', n: 5 }],
[{ ...total, categoria: 'A', n: 5 }, { ...total, categoria: 'A', n: 5 }], [{ ...total, categoria: 'A', n: 5, estrato: 'edad' }]]) {
expect(run(0, rows).state).toBe('Selección no consolidable');
}
});
it('Todos con reporte con varios periodos es no consolidable, aun si uno no tiene filas', () => {
const d = data(domain, 0, [{ ...total, categoria: 'A', n: 5 }]); d.periodos.push('2025-S2');
expect(kpiResult(d, KPI_BY_DOMAIN[domain][0], domain, { ...applied, selection: { mode: 'todos' } }).state).toBe('Selección no consolidable');
});
it('sin filas tiene estado explícito y contrato equivocado no produce cifra', () => {
expect(run(0, []).state).toBe('Sin filas utilizables'); const d = data(domain, 0, []); d.contrato = 'C1';
expect(kpiResult(d, KPI_BY_DOMAIN[domain][0], domain, applied).state).toBe('Error de contrato');
});
});
it('categorías incompletas nunca producen un total parcial', () => {
const d = data('cacu', 0, [{ ...total, categoria: 'A', n: 5 }]); d['categorias'] = { A: {}, B: {} };
expect(kpiResult(d, KPI_BY_DOMAIN.cacu[0], 'cacu', applied).value).toBeUndefined();
});
it('TEA usa sus tres indicadores destacados y su grupo C1 es TOTAL', () => {
expect(KPI_BY_DOMAIN.tea.map(d => [d.id, d.contrato])).toEqual([
['M23_pacientes_evaluados', 'C2'], ['M24_confirmacion_diagnostico', 'C1'], ['M25_dias_mchat_diagnostico', 'C3r']]);
const d = data('tea', 1, [{ ...total, grupo: 'TOTAL', valor: 50, numerador: 3, denominador: 6 }]);
expect(kpiResult(d, KPI_BY_DOMAIN.tea[1], 'tea', applied)).toEqual({ state: 'Disponible', value: '50.0',
info: '3 de 6 pacientes con nota médica con diagnóstico CIE-10 F84.0.' });
expect(kpiResult(data('tea', 1, [{ ...total, grupo: 'total', valor: 50, numerador: 3, denominador: 6 }]), KPI_BY_DOMAIN.tea[1], 'tea', applied).state)
.toBe('Selección no consolidable');
});