mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
Merge branch 'wave/v125-delight' into release/v1.25.0
This commit is contained in:
commit
ccbf4e07ae
28 files changed
+1877
-8
No files matched your search
@@ -0,0 +1,58 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
|
||||
import {
|
||||
summariseBreathing,
|
||||
type BreathingRow,
|
||||
} from "@/lib/insights/breathing-screening";
|
||||
|
||||
function night(daysAgo: number, value: number): BreathingRow {
|
||||
return {
|
||||
value,
|
||||
measuredAt: new Date(Date.now() - daysAgo * 24 * 60 * 60 * 1000),
|
||||
};
|
||||
}
|
||||
|
||||
describe("summariseBreathing", () => {
|
||||
it("is absent with no rows at all", () => {
|
||||
const s = summariseBreathing([], []);
|
||||
expect(s.present).toBe(false);
|
||||
expect(s.nights).toBe(0);
|
||||
expect(s.classification).toBeNull();
|
||||
expect(s.trend).toBeNull();
|
||||
});
|
||||
|
||||
it("classifies not-elevated with index nights and no events", () => {
|
||||
const s = summariseBreathing([night(2, 3), night(1, 4)], []);
|
||||
expect(s.present).toBe(true);
|
||||
expect(s.nights).toBe(2);
|
||||
expect(s.recentMeanIndex).toBe(3.5);
|
||||
expect(s.classification).toBe("not-elevated");
|
||||
});
|
||||
|
||||
it("classifies elevated when the device flagged events", () => {
|
||||
const s = summariseBreathing([night(1, 5)], [night(1, 1)]);
|
||||
expect(s.present).toBe(true);
|
||||
expect(s.eventCount).toBe(1);
|
||||
expect(s.classification).toBe("elevated");
|
||||
});
|
||||
|
||||
it("computes an upward trend over enough nights", () => {
|
||||
const rows = [
|
||||
night(8, 2),
|
||||
night(7, 2),
|
||||
night(6, 2),
|
||||
night(5, 8),
|
||||
night(4, 8),
|
||||
night(3, 8),
|
||||
night(2, 8),
|
||||
night(1, 8),
|
||||
];
|
||||
const s = summariseBreathing(rows, []);
|
||||
expect(s.trend).toBe("up");
|
||||
});
|
||||
|
||||
it("leaves the trend null with too few nights", () => {
|
||||
const s = summariseBreathing([night(2, 4), night(1, 5)], []);
|
||||
expect(s.trend).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,62 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
|
||||
import { summariseHealthStatus } from "@/lib/insights/health-status";
|
||||
import type { VitalDeviation } from "@/lib/insights/derived/coincident-deviation";
|
||||
import type { ChangepointSignal } from "@/lib/insights/derived/changepoint";
|
||||
|
||||
function vital(
|
||||
outside: boolean,
|
||||
direction: VitalDeviation["direction"],
|
||||
): VitalDeviation {
|
||||
return {
|
||||
type: "RESTING_HEART_RATE",
|
||||
value: 70,
|
||||
center: 60,
|
||||
low: 55,
|
||||
high: 65,
|
||||
outside,
|
||||
direction,
|
||||
};
|
||||
}
|
||||
|
||||
function shift(): ChangepointSignal {
|
||||
return {
|
||||
metric: "WEIGHT",
|
||||
breakDate: "2026-06-10",
|
||||
beforeMean: 80,
|
||||
afterMean: 82,
|
||||
direction: "up",
|
||||
magnitude: 1.6,
|
||||
};
|
||||
}
|
||||
|
||||
describe("summariseHealthStatus", () => {
|
||||
it("is absent when nothing is outside and no shift fired", () => {
|
||||
const s = summariseHealthStatus([vital(false, "in")], []);
|
||||
expect(s.present).toBe(false);
|
||||
expect(s.deviations).toHaveLength(0);
|
||||
expect(s.shifts).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("surfaces an out-of-band deviation", () => {
|
||||
const s = summariseHealthStatus([vital(true, "above")], []);
|
||||
expect(s.present).toBe(true);
|
||||
expect(s.deviations).toHaveLength(1);
|
||||
expect(s.deviations[0]).toMatchObject({
|
||||
type: "RESTING_HEART_RATE",
|
||||
direction: "above",
|
||||
});
|
||||
});
|
||||
|
||||
it("surfaces a changepoint shift even with no deviation", () => {
|
||||
const s = summariseHealthStatus([vital(false, "in")], [shift()]);
|
||||
expect(s.present).toBe(true);
|
||||
expect(s.shifts).toHaveLength(1);
|
||||
expect(s.shifts[0]).toMatchObject({ metric: "WEIGHT", direction: "up" });
|
||||
});
|
||||
|
||||
it("drops a vital flagged outside but with an 'in' direction", () => {
|
||||
const s = summariseHealthStatus([vital(true, "in")], []);
|
||||
expect(s.present).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
|
||||
import {
|
||||
summariseLabChanges,
|
||||
type LabChangeRow,
|
||||
} from "@/lib/insights/labs-changes";
|
||||
|
||||
function row(
|
||||
analyte: string,
|
||||
value: number,
|
||||
day: string,
|
||||
opts: { unit?: string; low?: number | null; high?: number | null } = {},
|
||||
): LabChangeRow {
|
||||
return {
|
||||
analyte,
|
||||
unit: opts.unit ?? "mg/dL",
|
||||
value,
|
||||
referenceLow: opts.low ?? null,
|
||||
referenceHigh: opts.high ?? null,
|
||||
takenAt: new Date(`${day}T08:00:00.000Z`),
|
||||
};
|
||||
}
|
||||
|
||||
describe("summariseLabChanges", () => {
|
||||
it("is absent with no rows", () => {
|
||||
expect(summariseLabChanges([])).toMatchObject({ present: false });
|
||||
});
|
||||
|
||||
it("is absent with a single panel", () => {
|
||||
const s = summariseLabChanges([row("LDL", 130, "2026-06-01")]);
|
||||
expect(s.present).toBe(false);
|
||||
});
|
||||
|
||||
it("is absent when no analyte is shared across panels", () => {
|
||||
const s = summariseLabChanges([
|
||||
row("LDL", 130, "2026-06-01"),
|
||||
row("HbA1c", 5.4, "2026-05-01"),
|
||||
]);
|
||||
expect(s.present).toBe(false);
|
||||
});
|
||||
|
||||
it("pairs the two most-recent panels for a shared analyte", () => {
|
||||
const s = summariseLabChanges([
|
||||
row("LDL", 120, "2026-06-01", { high: 116 }),
|
||||
row("LDL", 140, "2026-05-01"),
|
||||
row("LDL", 150, "2026-04-01"),
|
||||
]);
|
||||
expect(s.present).toBe(true);
|
||||
expect(s.latestDate).toBe("2026-06-01");
|
||||
expect(s.previousDate).toBe("2026-05-01");
|
||||
expect(s.changes).toHaveLength(1);
|
||||
const c = s.changes[0];
|
||||
expect(c.latest).toBe(120);
|
||||
expect(c.previous).toBe(140);
|
||||
expect(c.delta).toBe(-20);
|
||||
expect(c.direction).toBe("down");
|
||||
expect(c.status).toBe("above");
|
||||
});
|
||||
|
||||
it("skips qualitative (non-finite) values", () => {
|
||||
const s = summariseLabChanges([
|
||||
row("LDL", Number.NaN, "2026-06-01"),
|
||||
row("LDL", 140, "2026-05-01"),
|
||||
]);
|
||||
expect(s.present).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user