import { scenarioFigure } from '../gobernanza/figures'; import { insufficientText, sortUndrawn } from './survival-scenarios'; const base = (over: object = {}) => ({ km: { months: [0, 12, 24, 36], S: [1, 0.9, 0.8, 0.7], low: [1, 0.8, 0.7, 0.55], high: [1, 0.95, 0.9, 0.82], }, weibull: { t: [0, 36], S: [1, 0.7] }, weibull_fractional: { t: [0, 36], S: [1, 0.72] }, points: { COMB: { S: [0.9, 0.7], low: [0.8, 0.55], high: [0.95, 0.82] } }, ...over, }); const draw = (e: object) => scenarioFigure('t', e, [12, 36], 'S(t)').figure.data as any[]; const bands = (d: any[]) => d.filter((t) => t.fill === 'toself'); const model = (d: any[]) => d.find((t) => t.name?.startsWith('LR + HBN')); describe('survival curves with absent values', () => { it('complete data draw one band and symmetric-free error bars, as before', () => { const d = draw(base()); expect(bands(d).length).toBe(1); expect(bands(d)[0].x.length).toBe(8); expect(model(d).error_y.array.map((v: number) => +v.toFixed(2))).toEqual([0.05, 0.12]); expect(model(d).error_y.arrayminus.map((v: number) => +v.toFixed(2))).toEqual([0.1, 0.15]); }); it('a band is drawn only where both limits exist, never from an absent one', () => { const km = { months: [0, 12, 24, 36, 48], S: [1, 0.9, 0.8, 0.7, 0.6], low: [1, 0.8, null, 0.55, 0.4], high: [1, 0.95, null, 0.82, 0.7], }; const d = draw(base({ km })); expect(bands(d).map((b) => b.x.length)).toEqual([4, 4]); // [0,12] and [36,48]; each polygon out and back for (const b of bands(d)) expect(b.y.every((v: unknown) => typeof v === 'number')).toBe(true); const none = draw( base({ km: { ...km, low: [null, null, null, null, null], high: [null, null, null, null, null] }, }), ); expect(bands(none).length).toBe(0); // no limits: no band at all expect(none.find((t) => t.name?.startsWith('Kaplan')).y).toEqual([1, 0.9, 0.8, 0.7, 0.6]); // the curve stays }); it('a point without its limits has no bar; zero is a value and unavailable is not drawn', () => { const d = draw( base({ points: { COMB: { S: [0, null], low: [null, null], high: [null, null] } } }), ); const m = model(d); expect(m.y).toEqual([0, null]); // 0 % is drawn at 0, an absent value is a gap expect(m.error_y).toBeUndefined(); // no invented limits expect(model(draw(base({ points: { COMB: { S: [0, null] } } }))).error_y).toBeUndefined(); const half = model( draw(base({ points: { COMB: { S: [0.9, 0.7], low: [0.8, null], high: [0.95, null] } } })), ); expect(half.error_y.array).toEqual([expect.closeTo(0.05, 5), null]); expect(half.error_y.arrayminus).toEqual([expect.closeTo(0.1, 5), null]); }); }); describe('scenarios without a curve', () => { const report = { escenarios_sin_curvas: [ { escenario: 'VPH negativo', n: 9, fallecidos: 2 }, { escenario: 'Citología · ASC-H', n: 0, fallecidos: 0 }, ], min_fallecidos_escenario: 5, }; it('those the report lists as lacking deaths keep its numbers and the minimum', () => { expect(sortUndrawn(report, ['VPH negativo', 'Citología · ASC-H']).insufficient).toEqual([ { name: 'VPH negativo', n: 9, deaths: 2, minimum: 5 }, { name: 'Citología · ASC-H', n: 0, deaths: 0, minimum: 5 }, // zero deaths is a count, not a gap ]); }); it('one the report does not mention is absent, never "insufficient"', () => { expect(sortUndrawn(report, ['VPH negativo', 'Edad · 45-54']).missing).toEqual(['Edad · 45-54']); expect(sortUndrawn({}, ['a'])).toEqual({ insufficient: [], missing: ['a'] }); expect(sortUndrawn(null, ['a'])).toEqual({ insufficient: [], missing: ['a'] }); expect(sortUndrawn({ escenarios_sin_curvas: 'x' }, ['a']).missing).toEqual(['a']); }); it('a listing without counts is still insufficient, without inventing numbers', () => { expect( sortUndrawn({ escenarios_sin_curvas: [{ escenario: 'a' }] }, ['a']).insufficient, ).toEqual([{ name: 'a', n: undefined, deaths: undefined, minimum: undefined }]); }); }); describe('the insufficient-data text', () => { it("says the report's numbers and leaves out the ones it did not give", () => { expect(insufficientText({ name: 'a', n: 9, deaths: 2, minimum: 5 })).toBe( 'Datos insuficientes para trazar la curva (n = 9, fallecidos = 2; mínimo requerido: 5 fallecidos).', ); expect(insufficientText({ name: 'a', n: 0, deaths: 0 })).toBe( 'Datos insuficientes para trazar la curva (n = 0, fallecidos = 0).', ); expect(insufficientText({ name: 'a', n: null, deaths: 0, minimum: 5 })).toBe( 'Datos insuficientes para trazar la curva (fallecidos = 0; mínimo requerido: 5 fallecidos).', ); expect(insufficientText({ name: 'a' })).toBe('Datos insuficientes para trazar la curva.'); }); });