import { FigureSelection, PlotlyFigure } from './indicadores.models'; import { figurePresentation } from './indicadores.presentation'; import { BASE_VISUAL_PROFILE, DEMO_VISUAL_PROFILE } from './indicadores-visual-profile'; import { syntheticFigures } from './indicadores.synthetic-fixtures'; const selection: FigureSelection = { dominio: 'tea', indicador: 'synthetic', figura: 'fig_c1_grupos', tema: 'claro' }; const footer = { text: 'Explicación sintética. Los valores suprimidos no son cero.
Base depurada del proyecto TEA · Centro Médico ISSSTELEÓN – UANL · Fecha de corte: 2025-08-01
Uso exclusivo de investigación. No usar para decisiones clínicas.', xref: 'paper', yref: 'paper', x: 0, y: -0.22, xanchor: 'left', yanchor: 'top', align: 'left', showarrow: false, font: { size: 11, color: '#52514e' }, }; function freezeDeep(value: unknown) { if (value && typeof value === 'object') { Object.values(value).forEach(freezeDeep); Object.freeze(value); } } function original(): PlotlyFigure { return { data: [{ type: 'bar', x: [['2025-S1', '2025-S2'], ['Área extensa de ejemplo', 'Otra área']], y: [null, 0], marker: { color: ['gray', 'blue'] }, customdata: [[null, 'SUPRIMIDO'], [0, 10]], future: { untouched: true } }], layout: { title: 'Título', xaxis: { type: 'multicategory', future: 17 }, yaxis: { range: [0, 100] }, font: { future: 2 }, margin: { b: 150, future: 3 }, annotations: [structuredClone(footer)], future: { keep: true } }, config: { staticPlot: false }, future: { keep: [1, null] }, }; } describe('UX-07B perfiles y pies metodológicos', () => { it('deriva desde el original congelado, preserva íntegramente trazas, frames y atributos desconocidos', () => { const source = original(); source.frames = [{ name: 'frame', data: [{ y: [null, 0] }], layout: { annotations: [footer] }, future: 9 }]; const before = structuredClone(source); freezeDeep(source); const result = figurePresentation(source, selection, DEMO_VISUAL_PROFILE); expect(source).toEqual(before); expect(result.figure.data).toEqual(before.data); expect(result.figure.data).not.toBe(source.data); expect(result.figure.frames).toEqual(before.frames); expect(result.figure.frames).not.toBe(source.frames); expect(result.figure.config).toEqual(before.config); expect(result.figure['future']).toEqual(before['future']); expect(result.figure.layout['future']).toEqual(before.layout['future']); expect(result.figure.layout['xaxis']).toMatchObject({ type: 'multicategory', future: 17 }); expect(result.figure.layout['font']).toMatchObject({ future: 2 }); expect(result.figure.layout['margin']).toMatchObject({ b: 150, future: 3 }); expect(result.footnotes).toEqual([]); // An animated annotation could otherwise reappear. expect(result.figure.layout['annotations']).toEqual(before.layout['annotations']); }); it('aplica y revierte el perfil sin acumular estilos ni mutaciones del runtime', () => { const source = original(), snapshot = structuredClone(source); const base = figurePresentation(source, selection); const demo = figurePresentation(source, selection, DEMO_VISUAL_PROFILE); expect(demo.figure.layout).toMatchObject({ paper_bgcolor: '#f2f7fc', plot_bgcolor: '#ffffff', height: 640, font: { family: 'Arial, sans-serif', size: 15 }, xaxis: { type: 'multicategory', showgrid: true, gridcolor: '#c6d7e8' } }); demo.figure.data[0]['y'] = [999]; demo.figure.layout['annotations'] = [{ text: 'runtime' }]; expect(figurePresentation(source, selection, BASE_VISUAL_PROFILE)).toEqual(base); expect(source).toEqual(snapshot); }); it('extrae sólo el pie reconocido, preserva texto y orden sin duplicarlo en Plotly', () => { const source = original(); const annotation = { text: 'Meta', x: 1, y: 90 }; const second = { ...footer, text: footer.text.replace('Explicación sintética.', 'Segunda explicación.') }; source.layout['annotations'] = [footer, annotation, second]; const result = figurePresentation(source, selection); expect(result.footnotes).toEqual([footer.text.replaceAll('
', '\n'), second.text.replaceAll('
', '\n')]); expect(result.figure.layout['annotations']).toEqual([annotation]); expect(source.layout['annotations']).toEqual([footer, annotation, second]); expect(result.figure.layout['margin']).toEqual(source.layout['margin']); }); it.each([ { xref: 'x' }, { yref: 'y' }, { y: -0.23 }, { x: 0.1 }, { showarrow: true }, { xanchor: 'center' }, { yanchor: 'bottom' }, { align: 'center' }, { opacity: 0.4 }, { font: { size: 11, future: true } }, { text: 'Nota desconocida' }, { text: footer.text.replace('Explicación sintética.', 'Explicación sintética.') }, { text: footer.text.replace('Explicación sintética.', '<script>') }, { text: footer.text.replace('Explicación sintética.', '') }, ])('conserva anotaciones ambiguas o marcado no trasladable: %j', change => { const source = original(); source.layout['annotations'] = [{ ...footer, ...change }]; const result = figurePresentation(source, selection); expect(result.footnotes).toEqual([]); expect(result.figure.layout['annotations']).toEqual(source.layout['annotations']); }); it.each([{ dominio: 'cacu' as const }, { figura: 'fig_c3r_cajas' }, { dominio: undefined }])('no infiere familias ni dominios: %j', change => { const source = original(); const result = figurePresentation(source, { ...selection, ...change }); expect(result.footnotes).toEqual([]); expect(result.figure.layout['annotations']).toEqual(source.layout['annotations']); }); it.each([{ data: [] }, { data: [{ y: [null, null], customdata: ['SUPRIMIDO', 'SIN_DATO'] }] }, { data: [{ y: [0, NaN, null] }] }])('preserva vacíos, supresión, nulos y cero: %j', ({ data }) => { const source = { ...original(), data }; const snapshot = structuredClone(source); expect(figurePresentation(source, selection, DEMO_VISUAL_PROFILE).figure.data).toEqual(snapshot.data); expect(source).toEqual(snapshot); }); it('admite saltos br equivalentes y conserva cada línea sin trim ni HTML', () => { const source = original(); const text = footer.text.replaceAll('
', '
'); source.layout['annotations'] = [{ ...footer, text }]; expect(figurePresentation(source, selection).footnotes).toEqual([footer.text.replaceAll('
', '\n')]); }); it('reconoce los tres pies de fixtures sintéticos existentes sin alterar los datos', () => { const tea = syntheticFigures.filter(item => item.name.startsWith('tea')); expect(tea).toHaveLength(3); for (const item of tea) { const result = figurePresentation(item.figure, selection); expect(result.footnotes).toHaveLength(1); const annotations = item.figure.layout['annotations'] as { text: string }[]; expect(result.footnotes[0]).toBe(annotations[0].text.replaceAll('
', '\n')); expect(result.figure.layout['annotations']).toEqual([]); expect(result.figure.data).toEqual(item.figure.data); } }); it('respeta dimensiones fijas y no crea ejes para figuras que no los publican', () => { const source = { data: [], layout: { width: 800, height: 400, autosize: false } }; const result = figurePresentation(source, selection, DEMO_VISUAL_PROFILE); expect(result.figure.layout).toMatchObject({ width: 800, height: 400, autosize: false }); expect(result.figure.layout['xaxis']).toBeUndefined(); }); it('reduce sólo la reserva conocida de un eje simple con automargin efectivo', () => { const source = original(); source.data = [{ type: 'bar', x: ['Grupo'], y: [0] }]; source.layout['xaxis'] = { type: 'category', automargin: true }; expect(figurePresentation(source, selection).figure.layout['margin']).toMatchObject({ b: 80, future: 3 }); expect(source.layout['margin']).toMatchObject({ b: 150 }); for (const change of [{ xaxis: { type: 'multicategory', automargin: true } }, { legend: { y: -0.2 } }, { legend: { orientation: 'h' } }, { legend: { y: 0, yanchor: 'top' } }, { shapes: [] }, { xaxis2: { automargin: true } }, { height: 620 }, { xaxis: { type: 'category' } }]) { expect(figurePresentation({ ...source, layout: { ...source.layout, ...change } }, selection).figure.layout['margin']).toMatchObject({ b: 150 }); } }); it.each(['cacu', 'cama'] as const)('conserva adaptación mensual de %s con ambos perfiles', dominio => { const source: PlotlyFigure = { data: [{ type: 'scatter', mode: 'markers', x: ['2025-03', '2025-01'], y: [null, 0] }], layout: { xaxis: { future: true } } }; for (const profile of [BASE_VISUAL_PROFILE, DEMO_VISUAL_PROFILE]) { const result = figurePresentation(source, { ...selection, dominio, figura: 'fig_c1_puntos', tipoPeriodo: 'mes' }, profile); expect(result.figure.layout['xaxis']).toMatchObject({ type: 'category', categoryarray: ['2025-01', '2025-03'], ticktext: ['2025-01', '2025-03'], automargin: true, future: true }); expect(result.figure.data).toEqual(source.data); } }); }); describe('UX-07D.2 pies documentales de familias verificadas', () => { const note = { ...footer, text: 'Fuente: ejemplo sintético.' }; const donutTotal = { text: '12', xref: 'paper', yref: 'paper', x: 0.5, y: 0.5, showarrow: false, font: { size: 18, color: '#000000' } }; const selected = (dominio: 'tea' | 'cacu' | 'cama', figura = 'fig_c2_barras'): FigureSelection => ({ ...selection, dominio, figura }); const figure = (annotations: unknown[] = [note]): PlotlyFigure => ({ data: [{ type: 'bar', x: ['Categoría íntegra'], y: [null, 0], customdata: ['SUPRIMIDO', 'SIN_DATO'], hovertemplate: '%{x}: %{y}', marker: { color: 'gray' }, future: { untouched: true } }], layout: { margin: { b: 150 }, annotations, future: { untouched: true } }, config: { displaylogo: false }, future: { unchanged: true }, }); it.each(['cacu', 'cama'] as const)('extrae pies de barras, donas y cajas %s con la firma completa', dominio => { for (const id of ['fig_c2_barras', 'fig_c2_donas', 'fig_c3r_caja']) { const source = figure([donutTotal, note]); const result = figurePresentation(source, selected(dominio, id)); expect(result.footnotes).toEqual([note.text]); expect(result.figure.layout['annotations']).toEqual([donutTotal]); expect(result.figure.data).toEqual(source.data); expect(result.figure.layout['margin']).toEqual({ b: 150 }); } }); it.each(['fig_c2_barras', 'fig_c2_donas'])('amplía TEA %s conservando su firma de fuente e investigación', id => { expect(figurePresentation(figure([donutTotal, footer]), selected('tea', id)).footnotes) .toEqual([footer.text.replaceAll('
', '\n')]); expect(figurePresentation(figure(), selected('tea', id)).footnotes).toEqual([]); }); it.each(['Fuente: X.', 'Nota: ' + 'Texto extenso. '.repeat(100), 'Primera\n\nSegunda
Tercera
Cuarta', ' Fuente: A & B · «Sin dato» ≤ 5 — 日本語\n '])('preserva texto independientemente de longitud o saltos: %s', text => { const result = figurePresentation(figure([{ ...note, text }]), selected('cama')); expect(result.footnotes).toEqual([text.replace(//gi, '\n')]); expect(result.figure.layout['annotations']).toEqual([]); }); it.each([ { text: 'Nota' }, { text: '' }, { text: '<img>' }, { text: 'Fuente' }, { text: 'Texto & más' }, { text: ' \n ' }, { text: 17 }, { xref: 'x' }, { yref: 'y' }, { yref: 'y domain' }, { y: -0.23 }, { x: 0.5 }, { y: 0.5 }, { showarrow: true }, { ax: 0, ay: 20 }, { xanchor: 'center' }, { yanchor: 'bottom' }, { align: 'center' }, { opacity: 0.5 }, { visible: false }, { name: 'unknown' }, { font: { size: 11, future: true } }, ])('conserva en Plotly marcado, geometría o atributos incompatibles: %j', change => { const source = figure([{ ...note, ...change }]); for (const domain of ['cacu', 'cama'] as const) { const result = figurePresentation(source, selected(domain)); expect(result.footnotes).toEqual([]); expect(result.figure.layout['annotations']).toEqual(source.layout['annotations']); } }); it('particiona cada ocurrencia una sola vez, en orden, conservando anotaciones internas', () => { const internal = [{ text: 'Dato', xref: 'x', yref: 'y', x: 1, y: 3, showarrow: false }, { text: 'Observación', x: 1, y: 3, showarrow: true }, donutTotal, { text: 'Larga\n'.repeat(100), xref: 'paper', yref: 'paper', y: -0.22 }]; const annotations = [note, internal[0], { ...note, text: 'Segunda
línea' }, ...internal.slice(1), note]; const source = figure(annotations), before = structuredClone(source); freezeDeep(source); const result = figurePresentation(source, selected('cacu')); expect(result.footnotes).toEqual([note.text, 'Segunda\nlínea', note.text]); expect(result.figure.layout['annotations']).toEqual(internal); expect(result.footnotes.length + internal.length).toBe(annotations.length); expect(source).toEqual(before); expect(result.figure.data).toEqual(before.data); expect(result.figure.config).toEqual(before.config); expect(result.figure['future']).toEqual(before['future']); expect(result.figure.layout['future']).toEqual(before.layout['future']); }); it('conserva animaciones, plantilla, ejes multicategoría y atributos desconocidos', () => { const source = figure(); source.frames = [{ name: 'frame', layout: { annotations: [note] }, future: 1 }]; source.layout['template'] = { layout: { annotations: [note] } }; source.layout['xaxis'] = { type: 'multicategory', future: 3 }; const result = figurePresentation(source, selected('cama')); expect(result.footnotes).toEqual([]); expect(result.figure.frames).toEqual(source.frames); expect(result.figure.layout['annotations']).toEqual([note]); expect(result.figure.layout['template']).toEqual(source.layout['template']); expect(result.figure.layout['xaxis']).toEqual(source.layout['xaxis']); }); it('no impone nuevas reducciones de márgenes ni cambia multicategoría al extraer', () => { const source = figure(); source.layout['xaxis'] = { type: 'multicategory', automargin: true }; source.layout['legend'] = { orientation: 'h', y: -0.3 }; source.layout['height'] = 620; const result = figurePresentation(source, selected('cama')); expect(result.footnotes).toEqual([note.text]); expect(result.figure.layout['xaxis']).toEqual(source.layout['xaxis']); expect(result.figure.layout['margin']).toEqual(source.layout['margin']); expect(result.figure.layout['legend']).toEqual(source.layout['legend']); expect(result.figure.layout['height']).toBe(620); }); it('no inventa pies para figuras sin notas ni familias desconocidas', () => { expect(figurePresentation(figure([]), selected('cacu')).footnotes).toEqual([]); const source = figure(); for (const id of ['fig_c1_puntos', 'fig_desconocida']) { const result = figurePresentation(source, selected('cacu', id)); expect(result.footnotes).toEqual([]); expect(result.figure.layout['annotations']).toEqual([note]); } }); it('deriva las mismas notas desde el original al alternar perfiles y anchos', () => { const source = figure(), snapshot = structuredClone(source); for (const profile of [BASE_VISUAL_PROFILE, DEMO_VISUAL_PROFILE, BASE_VISUAL_PROFILE]) { for (const width of [230, 320, 768, 1440]) { const result = figurePresentation(source, selected('cama'), profile, width); expect(result.footnotes).toEqual([note.text]); expect(result.figure.data).toEqual(snapshot.data); } } expect(source).toEqual(snapshot); }); });