mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
feat(charts): medication chart matches other charts (7d trend + indicator + target-range)
Marc: "Bei Medikamenten zum Beispiel wird jetzt nicht 7-Tage-Schnitt, Trend und Zielbereiche angezeigt. Das möchte ich natürlich für alle Charts." The medication-compliance chart shipped in v1.4.15 A4 (replacing the static placeholder) was missing the trend chip + target-range visualization the BP/weight/mood charts already paint. Bringing it to parity: - 7-day trend chip in the header: signed delta in percentage points with a metric-aware sentiment colour (rising compliance → green, falling → orange) and an arrow icon. Mirrors the bg-muted/40 chip pattern HealthChart uses for its weekly/monthly bucket label. - New `computeMedicationTrend7d()` helper computes the delta as the mean of the most-recent 7 daily rates minus the mean of the prior 7 (capped at a 14-day window). Mean-difference rather than slope so a single outlier day doesn't wag the indicator. - Target-range visualization: added a 100 % goal ReferenceLine (green, solid-ish) ALONGSIDE the existing 80 % minimum-acceptable threshold (yellow, paler dash). Two lines together visualise the target range the way HealthChart's targetZone band does for BP/weight tiles. Tests: - 6 new unit tests pin computeMedicationTrend7d() including direction, delta, the 14-day cap, short windows, stable noise rejection. - The existing chart-render snapshot is extended with an explicit assertion that the trend chip is suppressed in the empty state (so the empty-state header doesn't paint a misleading "0 pp" indicator). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
af77e5ecf8
commit
9b01c86e30
5 files changed
+441
-11
No files matched your search
@@ -4,6 +4,7 @@ import { renderToStaticMarkup } from "react-dom/server";
|
||||
import {
|
||||
MedicationComplianceChart,
|
||||
aggregateMedicationCompliance,
|
||||
computeMedicationTrend7d,
|
||||
} from "../medication-compliance-chart";
|
||||
|
||||
/**
|
||||
@@ -77,6 +78,87 @@ describe("aggregateMedicationCompliance()", () => {
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* v1.4.16 A6 — 7-day trend computation.
|
||||
*
|
||||
* The chart header now paints a 7-day trend chip with a signed delta
|
||||
* in percentage points (pp) plus a direction arrow. The helper
|
||||
* compares the mean of the most recent 7 daily rates with the mean of
|
||||
* the prior 7, on a 14-day cap.
|
||||
*/
|
||||
describe("computeMedicationTrend7d()", () => {
|
||||
function pt(timestamp: number, rate: number) {
|
||||
return { date: "", rate, timestamp };
|
||||
}
|
||||
|
||||
it("returns null with fewer than 2 daily points", () => {
|
||||
expect(computeMedicationTrend7d([])).toBeNull();
|
||||
expect(computeMedicationTrend7d([pt(1, 90)])).toBeNull();
|
||||
});
|
||||
|
||||
it("returns 'up' when recent half is higher than prior half", () => {
|
||||
// 14 points: first 7 average to 70 %, second 7 average to 95 %.
|
||||
// Delta = +25 pp, direction = up.
|
||||
const points = [
|
||||
...Array.from({ length: 7 }, (_, i) => pt(i, 70)),
|
||||
...Array.from({ length: 7 }, (_, i) => pt(7 + i, 95)),
|
||||
];
|
||||
const trend = computeMedicationTrend7d(points);
|
||||
expect(trend).not.toBeNull();
|
||||
expect(trend!.direction).toBe("up");
|
||||
expect(trend!.delta).toBe(25);
|
||||
});
|
||||
|
||||
it("returns 'down' when recent half is lower than prior half", () => {
|
||||
const points = [
|
||||
...Array.from({ length: 7 }, (_, i) => pt(i, 95)),
|
||||
...Array.from({ length: 7 }, (_, i) => pt(7 + i, 70)),
|
||||
];
|
||||
const trend = computeMedicationTrend7d(points);
|
||||
expect(trend!.direction).toBe("down");
|
||||
expect(trend!.delta).toBe(-25);
|
||||
});
|
||||
|
||||
it("returns 'stable' when delta is below 1 pp", () => {
|
||||
// Both halves average to 90 % (with noise just below 1 pp).
|
||||
const points = [
|
||||
...Array.from({ length: 7 }, (_, i) => pt(i, 90)),
|
||||
pt(7, 90.4),
|
||||
pt(8, 89.6),
|
||||
pt(9, 90.3),
|
||||
pt(10, 89.7),
|
||||
pt(11, 90.5),
|
||||
pt(12, 89.5),
|
||||
pt(13, 90),
|
||||
];
|
||||
const trend = computeMedicationTrend7d(points);
|
||||
expect(trend!.direction).toBe("stable");
|
||||
expect(Math.abs(trend!.delta)).toBeLessThan(1);
|
||||
});
|
||||
|
||||
it("caps the comparison window at 14 days even with longer input", () => {
|
||||
// 30 points where the first 16 are 30 % and the last 14 are 100 %.
|
||||
// Without the 14-day cap the delta would average over the wider
|
||||
// window; with the cap, we only see the 100% half.
|
||||
const points = [
|
||||
...Array.from({ length: 16 }, (_, i) => pt(i, 30)),
|
||||
...Array.from({ length: 14 }, (_, i) => pt(16 + i, 100)),
|
||||
];
|
||||
const trend = computeMedicationTrend7d(points);
|
||||
// Recent 14 split into halves (7+7): both halves average 100 %.
|
||||
expect(trend!.delta).toBe(0);
|
||||
expect(trend!.direction).toBe("stable");
|
||||
});
|
||||
|
||||
it("handles short windows (< 14 points) by splitting available data in half", () => {
|
||||
// 4 points: first 2 average 50 %, second 2 average 100 %. Delta = +50 pp.
|
||||
const points = [pt(0, 50), pt(1, 50), pt(2, 100), pt(3, 100)];
|
||||
const trend = computeMedicationTrend7d(points);
|
||||
expect(trend!.delta).toBe(50);
|
||||
expect(trend!.direction).toBe("up");
|
||||
});
|
||||
});
|
||||
|
||||
describe("<MedicationComplianceChart>", () => {
|
||||
it("renders the section title and the empty-state message when data is empty", () => {
|
||||
const html = render(<MedicationComplianceChart />);
|
||||
@@ -92,4 +174,18 @@ describe("<MedicationComplianceChart>", () => {
|
||||
const html = render(<MedicationComplianceChart title="Adherence" />);
|
||||
expect(html).toContain("Adherence");
|
||||
});
|
||||
|
||||
/**
|
||||
* v1.4.16 A6 — the trend chip is suppressed when the dataset is
|
||||
* empty (the empty-state placeholder is shown instead). Once data
|
||||
* arrives, the chip + reference lines paint — those are exercised
|
||||
* end-to-end by the e2e suite; a unit-rendered SSR snapshot can't
|
||||
* mount the recharts SVG primitives. Here we only lock the no-data
|
||||
* variant: chip is absent so the empty header doesn't paint a
|
||||
* misleading "0 pp" indicator.
|
||||
*/
|
||||
it("does not paint the trend chip in the empty state", () => {
|
||||
const html = render(<MedicationComplianceChart />);
|
||||
expect(html).not.toContain('data-slot="medication-trend-chip"');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user