716 lines
23 KiB
TypeScript
716 lines
23 KiB
TypeScript
import type { Bundle, DiagnosticReport, Observation, Patient, RiskAssessment } from 'fhir/r4';
|
||
import { EXT } from '../fhir';
|
||
import { teaSteps } from '../paciente/tea-data';
|
||
import {
|
||
ageShare,
|
||
ageStack,
|
||
boxFigure,
|
||
historyFigure,
|
||
nestedDonut,
|
||
prevalenceHeadline,
|
||
sexCountBars,
|
||
shareDonut,
|
||
teaAlterations,
|
||
teaSankey,
|
||
teaSexBars,
|
||
} from './figures';
|
||
import { reading } from './measures';
|
||
import type { Catalog } from './pages';
|
||
import {
|
||
ageMonths,
|
||
associationRows,
|
||
formatIc,
|
||
formatOr,
|
||
formatProbability,
|
||
formatQ,
|
||
matchesTeaSearch,
|
||
teaParams,
|
||
teaRows,
|
||
teaWorkbook,
|
||
} from './tea-rows';
|
||
|
||
const cssPair = 'spindles|central|CSS';
|
||
const DAY = new Date('2026-11-30T12:00:00Z');
|
||
|
||
function fixture(): Bundle {
|
||
const subject = { reference: 'Patient/child' };
|
||
const patient: Patient = {
|
||
resourceType: 'Patient',
|
||
id: 'child',
|
||
name: [{ text: 'Ana López' }],
|
||
identifier: [{ system: 'urn:isssteleon:clinic:source', value: '123' }],
|
||
extension: [
|
||
{ url: EXT + 'follow-up-status', valueCoding: { code: 'finished' } },
|
||
{ url: EXT + 'tea-step', valueCode: 'eeg' },
|
||
{ url: EXT + 'tea-case', valueCode: 'confirmed' },
|
||
{ url: EXT + 'tea-eeg-result', valueCode: 'normal' },
|
||
{ url: EXT + 'tea-mchat', valueDecimal: 9 },
|
||
{ url: EXT + 'tea-css', valueInteger: 8 },
|
||
{ url: EXT + 'tea-alterations', valueInteger: 2 },
|
||
],
|
||
};
|
||
const ados: Observation = {
|
||
resourceType: 'Observation',
|
||
id: 'evaluation',
|
||
status: 'final',
|
||
code: { coding: [{ code: 'ados2' }] },
|
||
subject,
|
||
effectiveDateTime: '2026-03-02',
|
||
valueInteger: 8,
|
||
component: [
|
||
{
|
||
code: {
|
||
coding: [{ code: 'ados2.vocalization_directed' }, { code: 'A-2' }],
|
||
text: 'Frecuencia de la vocalización espontánea dirigida a otros',
|
||
},
|
||
extension: [
|
||
{ url: EXT + 'ados2-domain', valueCoding: { display: 'Afectación social (AS)' } },
|
||
{ url: EXT + 'ados2-subdomain', valueCoding: { display: 'Comunicación' } },
|
||
],
|
||
valueQuantity: { value: 1 },
|
||
},
|
||
],
|
||
};
|
||
const risk: RiskAssessment = {
|
||
resourceType: 'RiskAssessment',
|
||
status: 'final',
|
||
subject,
|
||
basis: [{ reference: 'Observation/evaluation' }],
|
||
prediction: [
|
||
{
|
||
outcome: { coding: [{ code: cssPair }] },
|
||
probabilityDecimal: 0.35,
|
||
extension: [
|
||
{ url: EXT + 'association-headline', valueBoolean: true },
|
||
{ url: EXT + 'association-reference-probability', valueDecimal: 0.35 },
|
||
{ url: EXT + 'association-odds-ratio', valueDecimal: 2 },
|
||
{ url: EXT + 'association-ci-low', valueDecimal: 1.2 },
|
||
{ url: EXT + 'association-ci-high', valueDecimal: 4.5 },
|
||
{ url: EXT + 'association-q', valueDecimal: 0.01 },
|
||
],
|
||
},
|
||
],
|
||
};
|
||
const eeg: DiagnosticReport = {
|
||
resourceType: 'DiagnosticReport',
|
||
status: 'final',
|
||
code: { coding: [{ code: 'eeg' }] },
|
||
subject,
|
||
effectiveDateTime: '2026-03-04',
|
||
conclusionCode: [{ coding: [{ code: 'good' }], text: 'Bueno' }],
|
||
extension: [{ url: EXT + 'eeg-channels', valueInteger: 19 }],
|
||
};
|
||
const neurology: Observation = {
|
||
resourceType: 'Observation',
|
||
status: 'final',
|
||
subject,
|
||
code: { coding: [{ code: 'clinical_note.specialty' }] },
|
||
valueCodeableConcept: { coding: [{ code: 'pediatric_neurology' }] },
|
||
effectiveDateTime: '2026-03-03',
|
||
};
|
||
return {
|
||
resourceType: 'Bundle',
|
||
type: 'searchset',
|
||
entry: [
|
||
{ resource: patient, search: { mode: 'match' } },
|
||
{ resource: { resourceType: 'Patient', id: 'other' }, search: { mode: 'match' } },
|
||
{ resource: ados, search: { mode: 'include' } },
|
||
{ resource: risk, search: { mode: 'include' } },
|
||
{ resource: neurology, search: { mode: 'include' } },
|
||
{ resource: eeg, search: { mode: 'include' } },
|
||
...['2026-03-01', '2026-02-27'].map((date) => ({
|
||
search: { mode: 'include' as const },
|
||
resource: {
|
||
resourceType: 'Observation' as const,
|
||
status: 'final' as const,
|
||
subject,
|
||
code: { coding: [{ code: 'mchat.score' }] },
|
||
valueQuantity: { value: 7 },
|
||
effectiveDateTime: date,
|
||
},
|
||
})),
|
||
],
|
||
};
|
||
}
|
||
|
||
describe('TEA indicators', () => {
|
||
it('uses integer ticks and spaces them using the stacked totals', () => {
|
||
const panel = { title: 'Pacientes', measure: 'M23', kind: 'stack' } as const;
|
||
const figure = (count: number) =>
|
||
historyFigure(
|
||
panel,
|
||
[
|
||
{
|
||
label: 'Enero',
|
||
r: {
|
||
populations: {},
|
||
strata: {
|
||
category: {
|
||
confirmed: { 'initial-population': count },
|
||
in_process: { 'initial-population': count },
|
||
},
|
||
},
|
||
},
|
||
},
|
||
],
|
||
'#123456',
|
||
'Mes',
|
||
);
|
||
|
||
expect(figure(1).layout?.yaxis).toMatchObject({ type: 'linear', tickformat: 'd', dtick: 1 });
|
||
expect(figure(100).layout?.yaxis).toMatchObject({ type: 'linear', tickformat: 'd' });
|
||
expect(figure(100).layout?.yaxis).not.toHaveProperty('dtick');
|
||
});
|
||
|
||
it('builds the category and group rings from the supplied counts', () => {
|
||
const catalog: Catalog = {
|
||
groups: [{ id: 'female', label: 'Femenino', color: '#123456' }],
|
||
categories: [
|
||
{ code: 'female-16-23', label: 'Femenino · 16-23', group: 'female', color: '#123456' },
|
||
{ code: 'female-24-29', label: 'Femenino · 24-29', group: 'female', color: '#123456' },
|
||
],
|
||
};
|
||
const reading = {
|
||
populations: {},
|
||
strata: {
|
||
category: {
|
||
'female-16-23': { 'initial-population': 2 },
|
||
'female-24-29': { 'initial-population': 1 },
|
||
},
|
||
},
|
||
};
|
||
expect(nestedDonut(reading, catalog, 'Pacientes').data).toEqual([
|
||
expect.objectContaining({ name: 'centro' }),
|
||
expect.objectContaining({ labels: ['Femenino'], values: [3] }),
|
||
expect.objectContaining({
|
||
labels: ['Femenino · 16-23', 'Femenino · 24-29'],
|
||
values: [2, 1],
|
||
}),
|
||
]);
|
||
expect(
|
||
shareDonut(reading, catalog, 'Pacientes').data.filter((trace) => 'hole' in trace),
|
||
).toHaveLength(1);
|
||
});
|
||
|
||
it('maps every search filter and encodes closed ranges as repeated ge/le values', () => {
|
||
expect(
|
||
teaParams({
|
||
step: 'eeg',
|
||
case: 'confirmed',
|
||
eeg: 'normal',
|
||
model: 'no',
|
||
age: '16-23',
|
||
sex: 'female',
|
||
alteration: 'spindles_n2',
|
||
mchatMin: 0,
|
||
mchatMax: 20,
|
||
cssMin: 1,
|
||
cssMax: 7,
|
||
window: 3,
|
||
start: '2026-01-01',
|
||
cutoff: '2026-03-31',
|
||
status: 'finished',
|
||
}),
|
||
).toEqual({
|
||
'tea-step': 'eeg',
|
||
'tea-case': 'confirmed',
|
||
'tea-eeg-result': 'normal',
|
||
'tea-model': 'no',
|
||
'tea-age-group': '16-23',
|
||
'tea-sex': 'female',
|
||
'tea-alteration': 'spindles_n2',
|
||
'tea-mchat': ['ge0', 'le20'],
|
||
'tea-css': ['ge1', 'le7'],
|
||
'follow-up-window': 3,
|
||
'follow-up-start': '2026-01-01',
|
||
'follow-up-cutoff': '2026-03-31',
|
||
'follow-up-status': 'finished',
|
||
});
|
||
expect(teaParams({ cssMax: 0 })['tea-css']).toEqual(['le0']);
|
||
});
|
||
|
||
it('reads the follow-up extensions and assembles the stepper and association headline', () => {
|
||
const [row, other] = teaRows(fixture());
|
||
expect(row.steps.map(({ date }) => date)).toEqual([
|
||
'2026-02-27',
|
||
'2026-03-02',
|
||
'2026-03-03',
|
||
'2026-03-04',
|
||
]);
|
||
expect(row.steps.map(({ completed }) => completed)).toEqual([true, true, true, true]);
|
||
expect(row).toMatchObject({
|
||
name: 'Ana López',
|
||
eegResult: 'normal',
|
||
recordNumbers: ['123'],
|
||
mchatScore: 9,
|
||
css: 8,
|
||
alterations: 2,
|
||
headline: {
|
||
pair: cssPair,
|
||
title: 'Husos N2 · Central · CSS',
|
||
or: 2,
|
||
low: 1.2,
|
||
high: 4.5,
|
||
q: 0.01,
|
||
},
|
||
});
|
||
expect(other.headline).toBeUndefined();
|
||
});
|
||
|
||
it('titles an item association with the patient’s ADOS-2 item', () => {
|
||
const page = fixture();
|
||
const risk = page.entry!.find((e) => e.resource?.resourceType === 'RiskAssessment')!
|
||
.resource as RiskAssessment;
|
||
risk.prediction![0].outcome = { coding: [{ code: 'posterior_rhythm|central|ados2_item_01' }] };
|
||
expect(teaRows(page)[0].headline?.title).toBe(
|
||
'Ritmo posterior · Central · Frecuencia de la vocalización espontánea dirigida a otros',
|
||
);
|
||
});
|
||
|
||
it('formats the association values like the reference list', () => {
|
||
expect(formatOr(2)).toBe('2.00');
|
||
expect(formatOr(undefined)).toBe('—');
|
||
expect(formatIc(1.2, 4.5)).toBe('1.20–4.50');
|
||
expect(formatIc(undefined, 4.5)).toBe('—');
|
||
expect(formatQ(0.0004)).toBe('< 0.001');
|
||
expect(formatQ(0.0123)).toBe('0.012');
|
||
expect(formatQ(undefined)).toBe('—');
|
||
expect(formatProbability(0.35)).toBe('35 %');
|
||
expect(formatProbability(undefined)).toBe('—');
|
||
});
|
||
|
||
it('defaults to all children and filters by association results', () => {
|
||
const rows = teaRows(fixture());
|
||
expect(associationRows(rows)).toEqual(rows);
|
||
expect(associationRows(rows, 'with')).toEqual([rows[0]]);
|
||
expect(associationRows(rows, 'without')).toEqual([rows[1]]);
|
||
});
|
||
|
||
it('matches names and record numbers in the risk list', () => {
|
||
const [row] = teaRows(fixture());
|
||
expect(matchesTeaSearch(row, 'lopez an')).toBe(true);
|
||
expect(matchesTeaSearch(row, '123')).toBe(true);
|
||
expect(matchesTeaSearch(row, '456')).toBe(false);
|
||
});
|
||
|
||
it('calculates completed months at the cut-off', () => {
|
||
expect(ageMonths('2024-01-15', '2026-01-14')).toBe(23);
|
||
expect(ageMonths('2024-01-15', '2026-01-15')).toBe(24);
|
||
expect(ageMonths(undefined, '2026-01-15')).toBeUndefined();
|
||
});
|
||
|
||
it('builds the follow-up workbook', () => {
|
||
expect(teaWorkbook(teaRows(fixture()), false, '2026-03-31', DAY)).toEqual({
|
||
name: 'tea-seguimiento-2026-11-30',
|
||
sheets: [
|
||
expect.objectContaining({
|
||
name: 'Seguimiento',
|
||
rows: expect.arrayContaining([
|
||
expect.objectContaining({
|
||
id: 'child',
|
||
eeg: '2026-03-04',
|
||
mchat: 9,
|
||
css: 8,
|
||
eegResult: 'Normal',
|
||
alterations: 2,
|
||
}),
|
||
]),
|
||
}),
|
||
],
|
||
});
|
||
});
|
||
|
||
it('builds the risk workbook with children and pair details', () => {
|
||
expect(teaWorkbook(associationRows(teaRows(fixture())), true, '2026-03-31', DAY)).toEqual({
|
||
name: 'tea-riesgos-2026-11-30',
|
||
sheets: [
|
||
expect.objectContaining({
|
||
name: 'Niños',
|
||
rows: expect.arrayContaining([
|
||
expect.objectContaining({
|
||
id: 'child',
|
||
pair: 'Husos N2 · Central · CSS',
|
||
or: 2,
|
||
low: 1.2,
|
||
high: 4.5,
|
||
probability: 0.35,
|
||
reference: 0.35,
|
||
}),
|
||
]),
|
||
}),
|
||
expect.objectContaining({
|
||
name: 'Pares',
|
||
rows: [
|
||
expect.objectContaining({
|
||
id: 'child',
|
||
pair: cssPair,
|
||
probability: 0.35,
|
||
reference: 0.35,
|
||
}),
|
||
],
|
||
}),
|
||
],
|
||
});
|
||
});
|
||
});
|
||
|
||
describe('TEA clinical figures', () => {
|
||
it('conserves children at each stage and omits empty stops', () => {
|
||
const figure = teaSankey({
|
||
populations: { 'initial-population': 9 },
|
||
strata: {
|
||
category: {
|
||
mchat_only: { 'initial-population': 2 },
|
||
neuropediatrics: { 'initial-population': 3 },
|
||
eeg: { 'initial-population': 4 },
|
||
},
|
||
},
|
||
});
|
||
expect(figure.data[0]).toMatchObject({
|
||
type: 'sankey',
|
||
link: { source: [0, 0, 1, 2, 2], target: [1, 5, 2, 3, 4], value: [7, 2, 7, 4, 3] },
|
||
});
|
||
});
|
||
|
||
it('carries the count in every node label, including the stops', () => {
|
||
const figure = teaSankey({
|
||
populations: { 'initial-population': 18 },
|
||
strata: {
|
||
category: {
|
||
mchat_only: { 'initial-population': 6 },
|
||
psychology: { 'initial-population': 2 },
|
||
neuropediatrics: { 'initial-population': 5 },
|
||
eeg: { 'initial-population': 5 },
|
||
},
|
||
},
|
||
});
|
||
expect(figure.data[0]).toMatchObject({
|
||
node: {
|
||
label: [
|
||
'M-CHAT · 18',
|
||
'Evaluación<br>psicológica · 12',
|
||
'Evaluación<br>neuropediátrica · 10',
|
||
'EEG de<br>buena calidad · 5',
|
||
'Se quedó en evaluación<br>neuropediátrica · 5',
|
||
'Se quedó en evaluación<br>psicológica · 2',
|
||
'Se quedó en M-CHAT · 6',
|
||
],
|
||
},
|
||
});
|
||
});
|
||
|
||
it('annotates only the median and the quartiles on the box', () => {
|
||
const figure = boxFigure({ populations: {}, strata: {}, score: 12.5, low: 8, high: 20 }, '2025-S1');
|
||
expect(figure.layout?.annotations?.map((a) => a.text)).toEqual([
|
||
'Q3 20',
|
||
'Mediana 12.5',
|
||
'Q1 8',
|
||
]);
|
||
expect(figure.data[0]).toMatchObject({ hoverinfo: 'skip' });
|
||
});
|
||
|
||
const prevalence = {
|
||
populations: { 'initial-population': 12 },
|
||
score: 3333.3333,
|
||
unit: 'por 10 000',
|
||
strata: {
|
||
category: { confirmed: { 'initial-population': 4 } },
|
||
sex: {
|
||
female: { 'initial-population': 4 },
|
||
male: { 'initial-population': 6 },
|
||
no_data: { 'initial-population': 2 },
|
||
},
|
||
'sex-category': {
|
||
'female|confirmed': { 'initial-population': 1 },
|
||
'female|in_population': { 'initial-population': 3 },
|
||
'male|confirmed': { 'initial-population': 3 },
|
||
'male|in_population': { 'initial-population': 3 },
|
||
'no_data|in_population': { 'initial-population': 2 },
|
||
},
|
||
},
|
||
rates: { sex: { female: 2500, male: 5000, no_data: 0 } },
|
||
};
|
||
|
||
it('shows the prevalence rate per 10,000 as the bar text and the counts in the hover', () => {
|
||
const figure = teaSexBars(prevalence, 'female');
|
||
expect(figure.layout?.barmode).toBeUndefined();
|
||
expect(figure.data).toHaveLength(1);
|
||
expect(figure.data[0]).toMatchObject({
|
||
type: 'bar',
|
||
x: ['Masculino', 'Femenino'],
|
||
y: [5000, 2500],
|
||
text: ['5,000', '2,500'],
|
||
customdata: [
|
||
[3, 6],
|
||
[1, 4],
|
||
],
|
||
marker: { color: ['#4269C2', '#C24269'], line: { width: [0, 2] } },
|
||
});
|
||
expect(figure.data[0]).toMatchObject({
|
||
hovertemplate: expect.stringContaining(
|
||
'%{customdata[0]} confirmados de %{customdata[1]} niños',
|
||
),
|
||
});
|
||
});
|
||
|
||
it('puts the total or the chosen sex rate in the headline', () => {
|
||
expect(prevalenceHeadline(prevalence)).toEqual({
|
||
value: '3,333.3',
|
||
detail: 'por 10 000 niños · 4 confirmados de 12 niños',
|
||
});
|
||
expect(prevalenceHeadline(prevalence, 'male')).toEqual({
|
||
value: '5,000',
|
||
detail: 'por 10 000 niños · 3 confirmados de 6 niños',
|
||
});
|
||
expect(prevalenceHeadline({ populations: {}, strata: {} })).toBeUndefined();
|
||
});
|
||
|
||
it('reads the rate of each stratum from the report', () => {
|
||
const r = reading({
|
||
resourceType: 'MeasureReport',
|
||
status: 'complete',
|
||
type: 'summary',
|
||
measure: 'M31',
|
||
period: { start: '2025-01-01', end: '2025-06-30' },
|
||
group: [
|
||
{
|
||
measureScore: { value: 3333.3, unit: 'por 10 000' },
|
||
stratifier: [
|
||
{
|
||
code: [{ coding: [{ code: 'sex' }] }],
|
||
stratum: [
|
||
{ value: { text: 'male' }, measureScore: { value: 5000 } },
|
||
{ value: { text: 'female' }, measureScore: { value: 2500 } },
|
||
],
|
||
},
|
||
{
|
||
code: [{ coding: [{ code: 'category' }] }],
|
||
stratum: [{ value: { text: 'confirmed' } }],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
});
|
||
expect(r).toMatchObject({ score: 3333.3, unit: 'por 10 000' });
|
||
expect(r?.rates).toEqual({ sex: { male: 5000, female: 2500 } });
|
||
});
|
||
|
||
it('plots the prevalence rate over time as one line per sex and one for the total', () => {
|
||
const figure = historyFigure(
|
||
{ title: 'Prevalencia', measure: 'M31', kind: 'rate', axis: 'Casos por 10 000 niños' },
|
||
[
|
||
{ label: 'Ene', r: prevalence },
|
||
{ label: 'Feb', r: undefined },
|
||
],
|
||
'#9C42C2',
|
||
'Mes',
|
||
);
|
||
expect(figure.data).toMatchObject([
|
||
{ name: 'Total', y: [3333.3333, null], mode: 'lines+markers' },
|
||
{ name: 'Masculino', y: [5000, null] },
|
||
{ name: 'Femenino', y: [2500, null] },
|
||
]);
|
||
expect(figure.layout?.yaxis?.title).toMatchObject({ text: 'Casos por 10 000 niños' });
|
||
});
|
||
|
||
it('groups diagnostic results by sex using crossed counts', () => {
|
||
const catalog: Catalog = {
|
||
groups: [],
|
||
categories: [
|
||
{ code: 'confirmed', label: 'Confirmados', color: '#A8C243', group: '' },
|
||
{ code: 'in_process', label: 'En proceso', color: '#B8433A', group: '' },
|
||
],
|
||
};
|
||
const result = sexCountBars(
|
||
{
|
||
populations: {},
|
||
strata: {
|
||
'sex-category': {
|
||
'female|confirmed': { 'initial-population': 2 },
|
||
'female|in_process': { 'initial-population': 1 },
|
||
'male|in_process': { 'initial-population': 3 },
|
||
'no_data|confirmed': { 'initial-population': 1 },
|
||
},
|
||
},
|
||
},
|
||
catalog,
|
||
'Pacientes',
|
||
'2025-S1',
|
||
);
|
||
expect(result.layout?.barmode).toBe('group');
|
||
expect(result.data).toMatchObject([
|
||
{
|
||
name: 'Masculino',
|
||
x: [0, 3],
|
||
y: ['Confirmados', 'En proceso'],
|
||
marker: { color: '#4269C2' },
|
||
},
|
||
{
|
||
name: 'Femenino',
|
||
x: [2, 1],
|
||
y: ['Confirmados', 'En proceso'],
|
||
marker: { color: '#C24269' },
|
||
},
|
||
]);
|
||
});
|
||
|
||
const byAgeAndSex = {
|
||
populations: {},
|
||
strata: {
|
||
age_sex: {
|
||
'24-36|male': { numerator: 1, denominator: 1 },
|
||
'16-23|female': { numerator: 1, denominator: 2 },
|
||
'16-23|male': { numerator: 1, denominator: 4 },
|
||
'37-48|no_data': { numerator: 1, denominator: 1 },
|
||
},
|
||
},
|
||
};
|
||
const ageGroups = ['16 a 23 meses', '24 a 36 meses'];
|
||
|
||
it('stacks diagnostic status in one bar per sex in each age group', () => {
|
||
const figure = ageStack(byAgeAndSex, ['Confirmados', 'En proceso']);
|
||
expect(figure.layout?.barmode).toBe('stack');
|
||
expect(figure.data).toMatchObject([
|
||
{ x: ageGroups, offsetgroup: 'male', y: [1, 1], marker: { color: '#4269C2' } },
|
||
{ x: ageGroups, offsetgroup: 'male', y: [3, 0], marker: { color: 'rgba(66,105,194,0.25)' } },
|
||
{ x: ageGroups, offsetgroup: 'female', y: [1, 0], marker: { color: '#C24269' } },
|
||
{
|
||
x: ageGroups,
|
||
offsetgroup: 'female',
|
||
y: [1, 0],
|
||
marker: { color: 'rgba(194,66,105,0.25)' },
|
||
},
|
||
]);
|
||
});
|
||
|
||
it('groups EEG percentages by age and sex and omits missing denominators', () => {
|
||
const figure = ageShare(byAgeAndSex, '% de pacientes con EEG');
|
||
expect(figure.layout?.barmode).toBe('group');
|
||
expect(figure.data).toMatchObject([
|
||
{
|
||
name: 'Masculino',
|
||
x: ageGroups,
|
||
y: [25, 100],
|
||
text: ['25 % (1/4)', '100 % (1/1)'],
|
||
marker: { color: '#4269C2' },
|
||
},
|
||
{
|
||
name: 'Femenino',
|
||
x: ageGroups,
|
||
y: [50, null],
|
||
text: ['50 % (1/2)', ''],
|
||
marker: { color: '#C24269' },
|
||
},
|
||
]);
|
||
});
|
||
|
||
it('includes the total and independent alteration rates', () => {
|
||
const figure = teaAlterations({
|
||
populations: { numerator: 3, denominator: 4 },
|
||
strata: {
|
||
alteration: {
|
||
posterior_rhythm: { numerator: 2, denominator: 4 },
|
||
spindles_n2: { numerator: 2, denominator: 4 },
|
||
},
|
||
},
|
||
});
|
||
expect(figure.data[0]).toMatchObject({
|
||
x: ['Alguna alteración', 'Ritmo posterior', 'Husos N2'],
|
||
y: [75, 50, 50],
|
||
});
|
||
});
|
||
});
|
||
|
||
const observation = (field: string, day: string, value?: string): Observation => ({
|
||
resourceType: 'Observation',
|
||
status: 'final',
|
||
code: { coding: [{ code: field }] },
|
||
effectiveDateTime: day,
|
||
...(value ? { valueCodeableConcept: { coding: [{ code: value }] } } : {}),
|
||
});
|
||
|
||
const eeg = (day: string, quality: string): DiagnosticReport => ({
|
||
resourceType: 'DiagnosticReport',
|
||
status: 'final',
|
||
code: { coding: [{ code: 'eeg' }] },
|
||
effectiveDateTime: day,
|
||
conclusionCode: [{ coding: [{ code: quality }] }],
|
||
});
|
||
|
||
describe('Etapas del tamizaje TEA', () => {
|
||
it('toma la primera fecha de cada etapa en el orden del proceso', () => {
|
||
const steps = teaSteps(
|
||
[
|
||
observation('mchat.score', '2026-03-01'),
|
||
observation('mchat.score', '2026-02-27'),
|
||
observation('ados2', '2026-03-04'),
|
||
observation('clinical_note.specialty', '2026-03-06', 'pediatric_neurology'),
|
||
],
|
||
[eeg('2026-03-10', 'good')],
|
||
);
|
||
expect(steps.map(({ code, label }) => ({ code, label }))).toEqual([
|
||
{ code: 'mchat_only', label: 'M-CHAT' },
|
||
{ code: 'psychology', label: 'Psicología' },
|
||
{ code: 'neurology', label: 'Neuropediatría' },
|
||
{ code: 'eeg', label: 'EEG' },
|
||
]);
|
||
expect(steps.map((step) => step.date)).toEqual([
|
||
'2026-02-27',
|
||
'2026-03-04',
|
||
'2026-03-06',
|
||
'2026-03-10',
|
||
]);
|
||
expect(steps.every((step) => step.completed)).toBe(true);
|
||
});
|
||
|
||
it('completa el EEG solo con un estudio de buena calidad', () => {
|
||
const observations = [
|
||
observation('mchat.score', '2026-02-27'),
|
||
observation('ados2', '2026-03-04'),
|
||
observation('clinical_note.specialty', '2026-03-06', 'pediatric_neurology'),
|
||
observation('eeg.lab_record_id', '2026-03-08'),
|
||
];
|
||
const steps = teaSteps(observations, [eeg('2026-03-10', 'bad'), eeg('2026-03-12', 'good')]);
|
||
expect(steps[3]).toMatchObject({ date: '2026-03-12', completed: true });
|
||
expect(teaSteps(observations, [eeg('2026-03-10', 'bad')])[3]).toMatchObject({
|
||
date: undefined,
|
||
completed: false,
|
||
});
|
||
expect(teaSteps(observations, [])[3]).toMatchObject({ date: undefined, completed: false });
|
||
});
|
||
|
||
it('ignora las notas de otras especialidades', () => {
|
||
const steps = teaSteps(
|
||
[
|
||
observation('mchat.score', '2026-02-27'),
|
||
observation('ados2', '2026-03-04'),
|
||
observation('clinical_note.specialty', '2026-03-06', 'pediatrics'),
|
||
],
|
||
[],
|
||
);
|
||
expect(steps[2]).toMatchObject({ date: undefined, completed: false });
|
||
});
|
||
|
||
it('deja pendientes las etapas tras un hueco', () => {
|
||
const steps = teaSteps(
|
||
[observation('mchat.score', '2026-02-27'), observation('ados2', '2026-03-04')],
|
||
[eeg('2026-03-10', 'good')],
|
||
);
|
||
expect(steps.map((step) => step.completed)).toEqual([true, true, false, false]);
|
||
expect(steps[3].date).toBeUndefined();
|
||
});
|
||
|
||
it('usa la etapa registrada para mostrar el avance del seguimiento', () => {
|
||
const steps = teaSteps([], [], 'neurology');
|
||
expect(steps.map((step) => step.completed)).toEqual([true, true, true, false]);
|
||
});
|
||
|
||
it('fecha cada etapa a partir de la anterior cuando el estudio ocurrió antes', () => {
|
||
const steps = teaSteps(
|
||
[observation('mchat.score', '2026-02-27'), observation('ados2', '2026-02-26')],
|
||
[],
|
||
);
|
||
expect(steps[1]).toMatchObject({ date: '2026-02-27', completed: true });
|
||
});
|
||
});
|