mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
Merge remote-tracking branch 'origin/release/v1.46.0' into feat/v146-dump
This commit is contained in:
commit
adc96fde3e
56 files changed
+719
-390
No files matched your search
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* The steps of a long Coach answer survive a reload against a real Postgres.
|
||||
*
|
||||
* The turn records up to `MAX_TURN_STEPS` steps on `metricSource.steps`; the
|
||||
* read path restores them from `coach_messages.metric_source_json`. Both ends
|
||||
* read the one constant, so a full turn's worth comes back whole and an
|
||||
* over-long stored blob is cut at that cap, not before it.
|
||||
*/
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { MAX_TURN_STEPS } from "@/lib/ai/coach/activity/contract";
|
||||
import {
|
||||
appendMessage,
|
||||
createConversation,
|
||||
fetchConversationWithMessages,
|
||||
} from "@/lib/ai/coach/persistence";
|
||||
import type { CoachStep } from "@/lib/ai/coach/types";
|
||||
|
||||
import { getPrismaClient, truncateAllTables } from "./setup";
|
||||
|
||||
beforeEach(async () => {
|
||||
await truncateAllTables(getPrismaClient());
|
||||
});
|
||||
|
||||
function step(n: number): CoachStep {
|
||||
return {
|
||||
id: `s${n}`,
|
||||
tool: "get_metric_series",
|
||||
labelKey: "coach.step.readWindow",
|
||||
label: "Read the last 30 days",
|
||||
domain: "bp",
|
||||
window: "last30days",
|
||||
status: "done",
|
||||
count: n,
|
||||
};
|
||||
}
|
||||
|
||||
const RECORDED = Array.from({ length: MAX_TURN_STEPS }, (_, i) => step(i + 1));
|
||||
|
||||
async function seedConversation(): Promise<{
|
||||
userId: string;
|
||||
conversationId: string;
|
||||
}> {
|
||||
const user = await getPrismaClient().user.create({
|
||||
data: { username: "steps-owner", role: "USER" },
|
||||
});
|
||||
const convo = await createConversation({ userId: user.id, title: "long" });
|
||||
return { userId: user.id, conversationId: convo.id };
|
||||
}
|
||||
|
||||
describe("Coach steps across a reload", () => {
|
||||
it("restores every step of a full turn", async () => {
|
||||
const { userId, conversationId } = await seedConversation();
|
||||
const reply = await appendMessage({
|
||||
conversationId,
|
||||
role: "assistant",
|
||||
content: "A long answer.",
|
||||
metricSource: { windows: [], metrics: [], steps: RECORDED },
|
||||
});
|
||||
|
||||
const reloaded = await fetchConversationWithMessages(
|
||||
userId,
|
||||
conversationId,
|
||||
);
|
||||
const message = reloaded?.messages.find((m) => m.id === reply.id);
|
||||
expect(message?.metricSource?.steps).toHaveLength(MAX_TURN_STEPS);
|
||||
expect(message?.metricSource?.steps).toEqual(RECORDED);
|
||||
});
|
||||
|
||||
it("cuts an over-long stored blob at the cap, not before it", async () => {
|
||||
const { userId, conversationId } = await seedConversation();
|
||||
const reply = await appendMessage({
|
||||
conversationId,
|
||||
role: "assistant",
|
||||
content: "An over-long blob.",
|
||||
metricSource: { windows: [], metrics: [] },
|
||||
});
|
||||
await getPrismaClient().coachMessage.update({
|
||||
where: { id: reply.id },
|
||||
data: {
|
||||
metricSourceJson: JSON.stringify({
|
||||
windows: [],
|
||||
metrics: [],
|
||||
steps: [...RECORDED, step(MAX_TURN_STEPS + 1)],
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
const reloaded = await fetchConversationWithMessages(
|
||||
userId,
|
||||
conversationId,
|
||||
);
|
||||
const message = reloaded?.messages.find((m) => m.id === reply.id);
|
||||
expect(message?.metricSource?.steps).toEqual(RECORDED);
|
||||
});
|
||||
});
|
||||
@@ -3,14 +3,20 @@
|
||||
* the seed is a mean-reverting walk, and around a settled target that is flat
|
||||
* noise: a 7-day trend line over the last 30 days of blood pressure drew a
|
||||
* straight line, which is exactly the view the demo exists to show off. The
|
||||
* seed now carries a busy stretch a few weeks back that lifts blood pressure
|
||||
* and lets it settle again.
|
||||
* seed carries a busy stretch a few weeks back that lifts blood pressure to
|
||||
* about 144/91 for some ten days, and ramipril goes up on the day it
|
||||
* turns.
|
||||
*
|
||||
* This runs the real `scripts/seed-demo.ts` and holds the last 30 days of
|
||||
* systolic readings to two things at once: the 7-day mean moves visibly, and
|
||||
* today's reading has come back down from the stretch. Before the stretch the
|
||||
* swing measured 2 to 3 mmHg; with it, about 8 to 14. The seed is random, so the
|
||||
* bounds sit between the two rather than at either.
|
||||
* The blood-pressure chart scales one axis to fit systolic and diastolic, so a
|
||||
* swing has to be large to read: a first version lifted systolic by 12 mmHg,
|
||||
* its 7-day mean swung 8 to 14 mmHg, and on a 60 to 140 axis that still drew a
|
||||
* nearly flat line. This runs the real `scripts/seed-demo.ts` and holds the
|
||||
* last 30 days to a swing of the size that reads, to day-to-day noise small
|
||||
* enough for the 7-day mean to draw an arc, to today's reading back in the
|
||||
* settled band, and to the dose change landing on the turn. The seed is random,
|
||||
* so the bounds leave room around the measured values (swing about 23 mmHg,
|
||||
* day-to-day steps around 1 to 2 mmHg outside the climb and the fall). The
|
||||
* peak also has a ceiling: past 148 the chart's padded axis jumps to 50-200.
|
||||
*/
|
||||
import { beforeAll, describe, expect, it, vi } from "vitest";
|
||||
|
||||
@@ -21,7 +27,9 @@ vi.mock("@/lib/db-compat", () => ({
|
||||
}));
|
||||
|
||||
let systolic: number[];
|
||||
let diastolic: number[];
|
||||
let means: number[];
|
||||
let doseChanges: Array<{ effectiveFrom: Date; doseValue: number }>;
|
||||
|
||||
beforeAll(async () => {
|
||||
const prisma = getPrismaClient();
|
||||
@@ -45,6 +53,30 @@ beforeAll(async () => {
|
||||
select: { value: true },
|
||||
});
|
||||
systolic = rows.map((r) => Number(r.value));
|
||||
const diaRows = await prisma.measurement.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
type: "BLOOD_PRESSURE_DIA",
|
||||
source: "WITHINGS",
|
||||
deletedAt: null,
|
||||
measuredAt: { gte: since },
|
||||
},
|
||||
orderBy: { measuredAt: "asc" },
|
||||
select: { value: true },
|
||||
});
|
||||
diastolic = diaRows.map((r) => Number(r.value));
|
||||
const ramipril = await prisma.medication.findFirstOrThrow({
|
||||
where: { userId: user.id, name: "Ramipril" },
|
||||
select: { id: true },
|
||||
});
|
||||
const changes = await prisma.medicationDoseChange.findMany({
|
||||
where: { medicationId: ramipril.id, effectiveFrom: { gte: since } },
|
||||
select: { effectiveFrom: true, doseValue: true },
|
||||
});
|
||||
doseChanges = changes.map((c) => ({
|
||||
effectiveFrom: c.effectiveFrom,
|
||||
doseValue: Number(c.doseValue),
|
||||
}));
|
||||
means = [];
|
||||
for (let i = 6; i < systolic.length; i++) {
|
||||
const week = systolic.slice(i - 6, i + 1);
|
||||
@@ -53,13 +85,48 @@ beforeAll(async () => {
|
||||
}, 180_000);
|
||||
|
||||
describe("demo seed, short-window shape", () => {
|
||||
it("gives the 7-day systolic mean over 30 days a visible rise and fall", () => {
|
||||
it("gives the 7-day systolic mean over 30 days a rise and fall that reads", () => {
|
||||
expect(systolic.length).toBeGreaterThanOrEqual(28);
|
||||
const swing = Math.max(...means) - Math.min(...means);
|
||||
expect(swing).toBeGreaterThanOrEqual(6);
|
||||
expect(swing).toBeGreaterThanOrEqual(18);
|
||||
expect(Math.max(...systolic)).toBeGreaterThanOrEqual(140);
|
||||
// One mmHg past 148 and the padded axis rounds out to 50-200 in steps of
|
||||
// 50, which flattens the rise again.
|
||||
expect(Math.max(...systolic)).toBeLessThanOrEqual(148);
|
||||
expect(Math.max(...diastolic)).toBeGreaterThanOrEqual(88);
|
||||
});
|
||||
|
||||
it("keeps the day-to-day steps small enough for the 7-day mean to draw an arc", () => {
|
||||
const steps = systolic.slice(1).map((v, i) => Math.abs(v - systolic[i]));
|
||||
const sorted = [...steps].sort((a, b) => a - b);
|
||||
expect(sorted[Math.floor(sorted.length / 2)]).toBeLessThanOrEqual(3);
|
||||
});
|
||||
|
||||
it("leaves the median of the window in the settled band", () => {
|
||||
// The usual range is the median ± 3 MAD of the last 30 days. A stretch
|
||||
// that fills half the window moves that median onto the stretch and
|
||||
// widens the band to something like 87-181 mmHg.
|
||||
const sorted = [...systolic].sort((a, b) => a - b);
|
||||
const median = sorted[Math.floor(sorted.length / 2)];
|
||||
expect(median).toBeLessThanOrEqual(124);
|
||||
const deviations = systolic
|
||||
.map((v) => Math.abs(v - median))
|
||||
.sort((a, b) => a - b);
|
||||
const mad = deviations[Math.floor(deviations.length / 2)];
|
||||
expect(3 * 1.4826 * mad).toBeLessThanOrEqual(20);
|
||||
});
|
||||
|
||||
it("raises ramipril on the day the stretch turns", () => {
|
||||
expect(doseChanges).toHaveLength(1);
|
||||
expect(doseChanges[0].doseValue).toBe(10);
|
||||
const daysBefore =
|
||||
(Date.now() - doseChanges[0].effectiveFrom.getTime()) / 86_400_000;
|
||||
expect(daysBefore).toBeGreaterThan(8);
|
||||
expect(daysBefore).toBeLessThan(14);
|
||||
});
|
||||
|
||||
it("brings today's systolic reading back down from the stretch", () => {
|
||||
expect(systolic.at(-1)).toBeLessThanOrEqual(Math.max(...means) - 3);
|
||||
expect(systolic.at(-1)).toBeLessThanOrEqual(Math.max(...means) - 15);
|
||||
expect(systolic.at(-1)).toBeLessThanOrEqual(125);
|
||||
});
|
||||
});
|
||||
@@ -313,6 +313,9 @@ describe("GET /api/fhir/metadata", () => {
|
||||
z.looseObject({
|
||||
type: z.string(),
|
||||
interaction: z.array(z.object({ code: z.string() })),
|
||||
searchParam: z.array(
|
||||
z.object({ name: z.string(), type: z.string() }),
|
||||
),
|
||||
operation: z.array(z.object({ name: z.string() })).optional(),
|
||||
}),
|
||||
),
|
||||
@@ -330,6 +333,21 @@ describe("GET /api/fhir/metadata", () => {
|
||||
expect(patient).toHaveLength(1);
|
||||
expect(patient[0].operation?.map((o) => o.name)).toEqual(["everything"]);
|
||||
|
||||
// Paging on every search; the Observation search alone honours the
|
||||
// time-series range and the code filter, and says so.
|
||||
for (const resource of resources) {
|
||||
expect(resource.searchParam, resource.type).toEqual([
|
||||
{ name: "_count", type: "number" },
|
||||
{ name: "_offset", type: "number" },
|
||||
...(resource.type === "Observation"
|
||||
? [
|
||||
{ name: "date", type: "date" },
|
||||
{ name: "code", type: "token" },
|
||||
]
|
||||
: []),
|
||||
]);
|
||||
}
|
||||
|
||||
const root = path.join(process.cwd(), "src/app/api/fhir");
|
||||
for (const resource of resources) {
|
||||
for (const { code } of resource.interaction) {
|
||||
|
||||
Reference in new issue
Block a user