87 lines
3.7 KiB
TypeScript
87 lines
3.7 KiB
TypeScript
import { horizon, isRiskModel, riskGroups, riskScales } from './risk-scales';
|
|
|
|
const at = (years: number, p: number, low?: number, high?: number) => ({ years, p, low, high });
|
|
|
|
describe('the risk scales', () => {
|
|
it('keeps zero as a value, and a missing horizon as none', () => {
|
|
expect(horizon([at(0, 0), at(5, 0.5)], 0)).toMatchObject({ p: 0 });
|
|
expect(horizon([at(5, 0.5)], 0)).toBeUndefined(); // never taken from another horizon
|
|
expect(horizon([at(0, 0.02)], 5)).toBeUndefined();
|
|
expect(horizon([], 5)).toBeUndefined();
|
|
});
|
|
it('reads 0, 2, 50, 98 and 100 % as they are', () => {
|
|
for (const p of [0, 0.02, 0.5, 0.98, 1]) expect(horizon([at(5, p)], 5)?.p).toBe(p);
|
|
});
|
|
it('a value outside 0 to 1 is not a prediction; an interval needs both ends inside 0 to 1', () => {
|
|
expect(horizon([at(5, 1.2)], 5)).toBeUndefined();
|
|
expect(horizon([at(5, -0.1)], 5)).toBeUndefined();
|
|
expect(horizon([at(5, Number.NaN)], 5)).toBeUndefined();
|
|
expect(horizon([at(5, 0.4, 0.3, 0.5)], 5)).toMatchObject({ low: 0.3, high: 0.5 });
|
|
expect(horizon([at(5, 0.4, 0.3)], 5)).toMatchObject({
|
|
p: 0.4,
|
|
low: undefined,
|
|
high: undefined,
|
|
});
|
|
expect(horizon([at(5, 0.4, 0.6, 0.5)], 5)?.low).toBeUndefined();
|
|
expect(horizon([at(5, 0.4, -0.1, 0.5)], 5)?.low).toBeUndefined();
|
|
});
|
|
it('draws both horizons of every model, each model with its own title and index day', () => {
|
|
const groups = riskGroups([
|
|
{
|
|
text: 'Supervivencia v3',
|
|
index: '2026-02-01',
|
|
predictions: [at(0, 0), at(5, 0.5, 0.4, 0.6)],
|
|
},
|
|
{ text: 'Recurrencia v2', index: '2026-03-01', predictions: [at(5, 0.98)] },
|
|
{ text: 'Otro', index: '2026-03-02', predictions: [at(3, 0.3)] }, // no horizon of its own
|
|
]);
|
|
expect(groups.map((g) => g.title)).toEqual(['Supervivencia', 'Recurrencia', 'Otro']);
|
|
expect(groups.map((g) => g.index)).toEqual(['2026-02-01', '2026-03-01', '2026-03-02']);
|
|
const labels = ['Riesgo inmediato (al índice)', 'Riesgo a 5 años'];
|
|
for (const g of groups) expect(g.scales.map((s) => s.label)).toEqual(labels);
|
|
expect(groups[0].scales.map((s) => s.risk?.p)).toEqual([0, 0.5]);
|
|
expect(groups[1].scales.map((s) => s.risk?.p)).toEqual([undefined, 0.98]);
|
|
expect(groups[2].scales.map((s) => s.risk)).toEqual([undefined, undefined]);
|
|
});
|
|
it('with no model, both horizons are drawn unavailable', () => {
|
|
const [none, ...rest] = riskGroups([]);
|
|
expect(rest).toEqual([]);
|
|
expect(none.title).toBeUndefined();
|
|
expect(none.scales.map((s) => [s.label, s.risk])).toEqual([
|
|
['Riesgo inmediato (al índice)', undefined],
|
|
['Riesgo a 5 años', undefined],
|
|
]);
|
|
});
|
|
it('the PDF keeps its flat list, with no scale for a page with no model', () => {
|
|
expect(riskScales([])).toEqual([]);
|
|
expect(riskScales([{ text: 'M', predictions: [at(5, 0.1)] }]).map((s) => s.label)).toEqual([
|
|
'M, riesgo inmediato',
|
|
'M, riesgo a 5 años',
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('which models feed the risk scales', () => {
|
|
it('excludes survival and recurrence estimates, which are not risks of the scales outcome', () => {
|
|
expect(['cacu.cin2_pim', 'cacu.cancer_pim', 'cacu.cin2_logistic'].every(isRiskModel)).toBe(
|
|
true,
|
|
);
|
|
expect(isRiskModel('cacu.survival')).toBe(false);
|
|
expect(isRiskModel('cama.recurrence')).toBe(false);
|
|
});
|
|
it('with no risk model both horizons are unavailable (nothing is converted from a survival)', () => {
|
|
const risk = [
|
|
{
|
|
text: 'm',
|
|
index: '2026-01-01',
|
|
predictions: [{ years: 5, p: 0.7 }],
|
|
model: 'cacu.survival',
|
|
},
|
|
].filter((m) => isRiskModel(m.model));
|
|
expect(riskGroups(risk).flatMap((g) => g.scales.map((s) => s.risk))).toEqual([
|
|
undefined,
|
|
undefined,
|
|
]);
|
|
});
|
|
});
|