Files
HealthLog/tests/integration/demo-seed-shape.test.ts
T
Marc-André Bombeck a61fe1c715 fix(demo): give the short-window blood pressure a shape to show
Every 90-day series in 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 the view the demo
exists to show.

A busy stretch three to four weeks back now lifts blood pressure and
pulse and lets them settle again, the mood dips with it, and weekend
readings sit a touch higher. Blood pressure also pulls back towards its
target harder, so a seed no longer ends several mmHg above the band its
stored texts describe; today's reading settles on the days after the
stretch. The two stored texts that summarise the last weeks mention the
stretch.

An integration test runs the real seed and holds the 7-day mean to a
visible swing and today's reading to the settled side of it.
2026-10-24 18:28:19 +02:00

66 lines
2.3 KiB
TypeScript

/**
* The demo's short-window charts need a shape to show. Every 90-day series in
* 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.
*
* 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.
*/
import { beforeAll, describe, expect, it, vi } from "vitest";
import { getPrismaClient, truncateAllTables } from "./setup";
vi.mock("@/lib/db-compat", () => ({
ensureDbCompatibility: vi.fn().mockResolvedValue(undefined),
}));
let systolic: number[];
let means: number[];
beforeAll(async () => {
const prisma = getPrismaClient();
await truncateAllTables(prisma);
const { seed } = await import("../../scripts/seed-demo");
await seed();
const user = await prisma.user.findUniqueOrThrow({
where: { username: "demo" },
select: { id: true },
});
const since = new Date(Date.now() - 31 * 24 * 60 * 60 * 1000);
const rows = await prisma.measurement.findMany({
where: {
userId: user.id,
type: "BLOOD_PRESSURE_SYS",
source: "WITHINGS",
deletedAt: null,
measuredAt: { gte: since },
},
orderBy: { measuredAt: "asc" },
select: { value: true },
});
systolic = rows.map((r) => Number(r.value));
means = [];
for (let i = 6; i < systolic.length; i++) {
const week = systolic.slice(i - 6, i + 1);
means.push(week.reduce((a, b) => a + b, 0) / week.length);
}
}, 180_000);
describe("demo seed, short-window shape", () => {
it("gives the 7-day systolic mean over 30 days a visible rise and fall", () => {
expect(systolic.length).toBeGreaterThanOrEqual(28);
const swing = Math.max(...means) - Math.min(...means);
expect(swing).toBeGreaterThanOrEqual(6);
});
it("brings today's systolic reading back down from the stretch", () => {
expect(systolic.at(-1)).toBeLessThanOrEqual(Math.max(...means) - 3);
});
});