Files
HealthLog/src/components/timeline/__tests__/chronicle-model.test.ts
T
Marc-André Bombeck 2638174b66 fix(timeline): word every code an entry carries, and read a pause as one
The timeline and the day view showed what the server sends as it was sent:
a life event's category as FAMILY, a visit's kind as PREVENTIVE, a
document's kind as DISCHARGE_LETTER, a trip and a cycle under their
sentinel labels, a lab day as a bare "5", a mood as SUPER_GUT, a
screener as PHQ9 with its band code. A pause read as "Ramipril" at its
start and "Ramipril ended" at its end.

Every surface that shows a timeline item (lane labels, hover titles,
the screen-reader table, the selection bar, the phone chronicle) now
reads it through one wording, item-words.ts: a code goes through the
bundle's existing labels, a lab day reads "5 lab values", and a code
the bundle does not know is left out rather than shown. The day view's
title and second line do the same per event kind (mood, screener and
band, sport, severity, visit kind or planned, dose number, intensity,
functional impact, document kind), and a medication that starts, ends,
pauses or resumes says so, in the day view and in the visit
preparation alike.

A pause reads "Ramipril paused" at its start and "Ramipril resumed ·
after 11 days" at its end, on the lanes, in the selection bar and in
the chronicle.

A guard renders a record with every item kind, every category and every
kind code on each surface, in two languages, and fails on any upper-case
code or sentinel in the visible text. The wire schema now says which
fields carry codes.
2026-10-09 12:53:56 +02:00

170 lines
5.1 KiB
TypeScript

/**
* v1.42 (#613) — the phone chronicle: newest first, grouped, empty stretches
* said out loud, open things once in the Ongoing block, closed periods as
* rails.
*/
import { describe, expect, it } from "vitest";
import {
buildChronicle,
chronicleEntries,
standingChips,
type ChronicleRow,
} from "../chronicle-model";
import { TODAY, fullTimeline } from "./timeline-fixture";
function headers(rows: ChronicleRow[]) {
return rows
.filter((r) => r.type === "header")
.map((r) => (r.type === "header" ? r.group : ""));
}
describe("chronicleEntries", () => {
it("lists points, period starts and period ends, newest first", () => {
const entries = chronicleEntries(fullTimeline(), TODAY);
const dates = entries.map((e) => e.date);
expect([...dates].sort().reverse()).toEqual(dates);
const cold = entries.filter(
(e) => e.kind === "item" && e.item.id === "ill-5",
);
expect(cold.map((e) => (e.kind === "item" ? e.role : null))).toEqual([
"end",
"start",
]);
const end = cold[0];
expect(end.kind === "item" && end.days).toBe(9);
});
it("never lists anything after today", () => {
const tl = fullTimeline();
tl.lanes[0].items.push({
id: "future",
kind: "lifeEvent",
start: "2027-01-01",
end: null,
open: false,
precision: "DAY",
startKnown: true,
label: "x",
sub: null,
href: null,
});
expect(chronicleEntries(tl, TODAY).some((e) => e.date > TODAY)).toBe(false);
});
it("carries the quiet notable line", () => {
const entries = chronicleEntries(fullTimeline(), TODAY);
expect(entries.find((e) => e.kind === "notable")).toMatchObject({
date: "2026-01-04",
notable: "extremeHigh",
});
});
});
describe("buildChronicle", () => {
it("folds a run of empty months into one honest gap row", () => {
const rows = buildChronicle(fullTimeline(), TODAY, "month");
// The last entry is the course ending on 31 March 2026: April to
// October 2026 hold nothing.
const gap = rows.find((r) => r.type === "gap" && r.to === "2026-10-01");
expect(gap).toEqual({
type: "gap",
from: "2026-04-01",
to: "2026-10-01",
rails: {},
});
expect(headers(rows)[0]).toBe("2026-03-01");
});
it("keeps a course's rail through an empty stretch it spans", () => {
const rows = buildChronicle(fullTimeline(), TODAY, "month");
// The 2024/25 winter course runs from November to March; nothing else
// is dated in December to February.
const gap = rows.find((r) => r.type === "gap" && r.from === "2024-12-01");
expect(gap?.type === "gap" && gap.rails.medications).toBe("full");
});
it("never shows two gap rows next to each other", () => {
const rows = buildChronicle(fullTimeline(), TODAY, "month");
for (let i = 1; i < rows.length; i++) {
expect(rows[i].type === "gap" && rows[i - 1].type === "gap").toBe(false);
}
});
it("groups by year when asked", () => {
const rows = buildChronicle(fullTimeline(), TODAY, "year");
expect(headers(rows)).toEqual([
"2026-01-01",
"2025-01-01",
"2024-01-01",
"2023-01-01",
"2022-01-01",
"2021-01-01",
"2020-01-01",
"2019-01-01",
// The grass-pollen allergy began in 2018.
"2018-01-01",
]);
});
it("draws a closed episode as a rail from its end row down to its start row", () => {
const rows = buildChronicle(fullTimeline(), TODAY, "month");
const railOf = (id: string, role: string) => {
const row = rows.find(
(r) =>
r.type === "entry" &&
r.entry.kind === "item" &&
r.entry.item.id === id &&
r.entry.role === role,
);
return row && row.type === "entry" ? row.rails.illness : undefined;
};
expect(railOf("ill-5", "end")).toBe("end");
expect(railOf("ill-5", "start")).toBe("start");
// The visit on 3 January sits inside the cold.
const visit = rows.find(
(r) =>
r.type === "entry" &&
r.entry.kind === "item" &&
r.entry.item.id === "v-4",
);
expect(visit?.type === "entry" && visit.rails.illness).toBe("full");
});
it("is empty for a record without anything dated", () => {
expect(
buildChronicle(
{
lanes: [],
notable: [],
range: { from: "2026-01-01", to: "2026-12-31", dataFrom: null },
},
TODAY,
"month",
),
).toEqual([]);
});
});
describe("standingChips", () => {
it("collects open things once and folds several allergies into one chip", () => {
const chips = standingChips(fullTimeline(), (i) => i.label);
expect(chips.map((c) => c.id)).toEqual([
"ill-chronic",
"med-1",
"allergies",
]);
expect(chips.at(-1)).toMatchObject({ lane: "allergies", count: 2 });
});
it("lists a standing item without a start", () => {
const tl = fullTimeline();
tl.standing = [
{ lane: "illness", id: "pre", label: "Asthma", since: null, href: null },
];
expect(standingChips(tl, (i) => i.label).map((c) => c.label)).toContain(
"Asthma",
);
});
});