mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
Eleven touch-disjoint waves landed on develop plus a six-axis QA reconcile round before tagging. See CHANGELOG.md for the user-facing prose; this commit message documents the wave + report inventory for release-management traceability. Waves shipped (each with a phase report under .planning/): - phase-W-POOL-v1440-report.md — p-limit(4) wrapper around the analytics 15-way fan-out + pg.Pool max raised 10 -> 20. Closes the v1.4.39 empirical-trace finding #1 (Prisma pool starvation). - phase-W-DELETED-v1440-report.md — 11 reader-tier helpers + 3 integration contracts close audit Critical Finding #3 (soft-delete invisibility) across dashboard, analytics, summaries, rollup tiers, and the Coach snapshot path. - phase-W-INSIGHTS-v1440-report.md — mood-rollup swap on the three /api/insights/{features,targets,cards} routes plus the compliance-rollup hook gap closure for bulk-projection mints; the post-QA reconcile patch closes the six remaining measurement.findMany sites in the cluster that still leaked tombstoned rows. - phase-W-INFRA-v1440-report.md — rollup helper umbrella consolidation under src/lib/rollups/* with zero orphan re-exports; lint, typecheck, knip, and 93/93 rollup tests green post-restructure. - phase-W-RSC-v1440-report.md — per-tile Suspense boundaries plus the queryKey factory-bypass walker test (extended to comparison/ in the reconcile pass). - phase-W-PRIVACY-v1440-report.md — bilingual /privacy page with 17 paired-section tests pinning the 9 SB-3 disclosure requirements. - phase-W-AASA-v1440-report.md — /.well-known/apple-app-site-association endpoint pinned to S8WDX4W5KX.dev.healthlog.app with app-ID parity assertions guarding against split rotation. - phase-W-APNS-NOTIFY-v1440-report.md — MEDICATION_REMINDER time-sensitive APNs payload (priority 10 + interruption-level) scoped via a parameterised test that pins all 6 other event-types do not bypass Focus. AP-2 .p8 install gates real delivery. - phase-W-CONSENT-v1440-report.md — AI consent receipts CRUD with append-only invariant pinned in test; revoke-then-regrant leaves both rows; idempotent DELETE returns 200 + receipt: null. - phase-W-WMY-WIRE-v1440-report.md — three previously-dead WMY readers (week/month/year) wired through summaries-slice and health-score-fast-path with a linear-composability parity test pinning the cross-consumer routing contract. - phase-W-GHOSTS-v1440-report.md — ~1 177 lines of dead code removed across 9 atomic commits; startOfUtcDay + wallClockInTz consolidation eliminates a sibling-drift risk; knip whitelist trimmed to three files pending follow-up. QA reconcile patches applied per the v1.4.40 QA round (six reports under .planning/round-v1440-QA-*-findings.md): - senior C1 — dashboard-suspense-boundaries regex aligned with the post-W-INFRA Thread 2 userTimezone-local lift. - senior H1 — soft-delete filter added to the six remaining insights measurement.findMany sites. - specialist H1 — Promise.allSettled around bulk-projection compliance recompute on both /api/medications/intake and /api/dashboard/summary. - ux H1 — compare-toggle migrated to queryKeys.dashboardWidgets factory; walker test extended to src/components/comparison/. - security M1 — consent artefact 64 KB cap enforced via Buffer.byteLength (UTF-8 bytes, not UTF-16 code units). Quality gates green at tag: pnpm typecheck, pnpm lint, pnpm knip --include files,dependencies,binaries,unlisted, pnpm test --run (4726 passing / 1 skipped). Tests 4525 -> 4726 unit + 1 skipped over the v1.4.39 -> v1.4.40 span. User-facing prose: see CHANGELOG.md.
281 lines
11 KiB
TypeScript
281 lines
11 KiB
TypeScript
/**
|
|
* v1.4.40 W-INSIGHTS — `/api/insights/targets` mood-rollup tier swap.
|
|
*
|
|
* Audit Critical Finding #2 (`.planning/round-v1439-arch-qa-infra-db.md`)
|
|
* flagged the unbounded `prisma.moodEntry.findMany({ where: { userId } })`
|
|
* at the bottom of the route as one of the six unbounded mood walks
|
|
* that paint the Insights surface 12 s cold on a power-user account.
|
|
* This file pins:
|
|
*
|
|
* 1. **The mood-rollup DAY tier replaces the raw findMany when
|
|
* populated.** Mood targets (`MOOD_SCORE`, `MOOD_STABILITY`)
|
|
* compute their `current` / `average30` / `consistency7d` from
|
|
* the rollup row set; `prisma.moodEntry.findMany` is never
|
|
* called on the fast path. A single `findFirst` reads the
|
|
* latest score (one bounded row).
|
|
*
|
|
* 2. **The coverage-fallback runs the live walk once when the
|
|
* rollup tier is empty but raw entries exist** (legacy account
|
|
* before the boot-time backfill has caught up). Bounded by the
|
|
* trailing 30-day window so the fallback never repeats the
|
|
* legacy unbounded scan.
|
|
*
|
|
* 3. **The `findMany distinct(["type"])` "latest ever per type"
|
|
* query carries a `measuredAt: { gte: oneYearAgo }` floor**
|
|
* (audit High finding 3). Without the floor Postgres still has
|
|
* to sort the full 347 k-row tenant before applying DISTINCT ON
|
|
* because Prisma's `distinct` does not compile to PG's
|
|
* `DISTINCT ON`.
|
|
*/
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { NextRequest } from "next/server";
|
|
|
|
vi.mock("@/lib/db", () => ({
|
|
prisma: {
|
|
user: { findUnique: vi.fn() },
|
|
measurement: { findMany: vi.fn() },
|
|
moodEntry: { findMany: vi.fn(), findFirst: vi.fn() },
|
|
moodEntryRollup: { findMany: vi.fn(), findFirst: vi.fn() },
|
|
medication: { findMany: vi.fn() },
|
|
medicationIntakeEvent: { findMany: vi.fn() },
|
|
appSettings: { findUnique: vi.fn() },
|
|
$queryRaw: vi.fn(),
|
|
$queryRawUnsafe: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock("@/lib/auth/session", () => ({ getSession: vi.fn() }));
|
|
|
|
vi.mock("@/lib/auth/audit", () => ({
|
|
auditLog: vi.fn().mockResolvedValue(undefined),
|
|
}));
|
|
|
|
vi.mock("@/lib/db-compat", () => ({
|
|
ensureDbCompatibility: vi.fn().mockResolvedValue(undefined),
|
|
}));
|
|
|
|
vi.mock("@/lib/logging/transports", () => ({ emitIfSampled: vi.fn() }));
|
|
|
|
vi.mock("next/headers", () => ({
|
|
headers: vi.fn(async () => ({ get: () => null })),
|
|
cookies: vi.fn(async () => ({
|
|
get: () => undefined,
|
|
set: () => {},
|
|
delete: () => {},
|
|
})),
|
|
}));
|
|
|
|
// Stub the warm-up so test runs don't fire the real
|
|
// `recomputeUserMoodRollups` aggregate. The warm-up is fire-and-forget
|
|
// on the route so its return value is irrelevant for parity.
|
|
vi.mock("@/lib/rollups/mood-rollups", async () => {
|
|
const actual = await vi.importActual<typeof import("@/lib/rollups/mood-rollups")>(
|
|
"@/lib/rollups/mood-rollups",
|
|
);
|
|
return {
|
|
...actual,
|
|
ensureUserMoodRollupsFresh: vi.fn().mockResolvedValue({ recomputed: false }),
|
|
};
|
|
});
|
|
|
|
import { GET } from "../route";
|
|
import { getSession } from "@/lib/auth/session";
|
|
import { prisma } from "@/lib/db";
|
|
import { __resetAllCachesForTests } from "@/lib/cache/server-cache";
|
|
|
|
const SESSION_USER = {
|
|
id: "user-targets-1",
|
|
username: "marc",
|
|
role: "USER" as const,
|
|
timezone: "Europe/Berlin",
|
|
heightCm: 180,
|
|
dateOfBirth: new Date("1985-01-01"),
|
|
locale: "en",
|
|
};
|
|
|
|
const SESSION_OK = {
|
|
session: { id: "sess-1", expiresAt: new Date(Date.now() + 3_600_000) },
|
|
user: SESSION_USER as never,
|
|
};
|
|
|
|
const callGet = GET as unknown as (req: NextRequest) => Promise<Response>;
|
|
function makeReq(): NextRequest {
|
|
return new NextRequest("http://localhost/api/insights/targets");
|
|
}
|
|
|
|
interface TargetsBody {
|
|
data: {
|
|
targets: Array<{
|
|
type: string;
|
|
current: number | null;
|
|
average30: number | null;
|
|
daysInRange7d: number;
|
|
daysLogged7d: number;
|
|
insufficientData: boolean;
|
|
}>;
|
|
};
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.resetAllMocks();
|
|
__resetAllCachesForTests();
|
|
vi.mocked(getSession).mockResolvedValue(SESSION_OK);
|
|
(prisma.user.findUnique as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
heightCm: 180,
|
|
dateOfBirth: new Date("1985-01-01"),
|
|
gender: "MALE",
|
|
glucoseUnit: "mg/dL",
|
|
thresholdsJson: null,
|
|
});
|
|
(prisma.measurement.findMany as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
[],
|
|
);
|
|
(prisma.medication.findMany as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
[],
|
|
);
|
|
(
|
|
prisma.medicationIntakeEvent.findMany as ReturnType<typeof vi.fn>
|
|
).mockResolvedValue([]);
|
|
(prisma.moodEntry.findMany as ReturnType<typeof vi.fn>).mockResolvedValue([]);
|
|
(prisma.moodEntry.findFirst as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
null,
|
|
);
|
|
(
|
|
prisma.moodEntryRollup.findMany as ReturnType<typeof vi.fn>
|
|
).mockResolvedValue([]);
|
|
});
|
|
|
|
describe("GET /api/insights/targets — mood-rollup tier swap", () => {
|
|
it("returns a mood target driven by the rollup DAY tier without touching the raw findMany", async () => {
|
|
// Five DAY-rollup rows over the trailing 30 days. The route reads
|
|
// them directly to drive `MOOD_SCORE.current/average30/consistency`
|
|
// — no raw `moodEntry.findMany` on the fast path.
|
|
const today = new Date();
|
|
const day = (offset: number) => {
|
|
const d = new Date(today);
|
|
d.setUTCHours(0, 0, 0, 0);
|
|
d.setUTCDate(d.getUTCDate() - offset);
|
|
return d;
|
|
};
|
|
(
|
|
prisma.moodEntryRollup.findMany as ReturnType<typeof vi.fn>
|
|
).mockResolvedValue([
|
|
{ userId: SESSION_USER.id, granularity: "DAY", bucketStart: day(0), count: 1, mean: 4, minScore: 4, maxScore: 4, sd: null, computedAt: new Date() },
|
|
{ userId: SESSION_USER.id, granularity: "DAY", bucketStart: day(1), count: 1, mean: 4, minScore: 4, maxScore: 4, sd: null, computedAt: new Date() },
|
|
{ userId: SESSION_USER.id, granularity: "DAY", bucketStart: day(2), count: 1, mean: 5, minScore: 5, maxScore: 5, sd: null, computedAt: new Date() },
|
|
{ userId: SESSION_USER.id, granularity: "DAY", bucketStart: day(3), count: 1, mean: 4, minScore: 4, maxScore: 4, sd: null, computedAt: new Date() },
|
|
{ userId: SESSION_USER.id, granularity: "DAY", bucketStart: day(4), count: 1, mean: 4.5, minScore: 4, maxScore: 5, sd: 0.5, computedAt: new Date() },
|
|
]);
|
|
// Latest score read — one bounded row.
|
|
(prisma.moodEntry.findFirst as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
score: 4,
|
|
moodLoggedAt: day(0),
|
|
});
|
|
|
|
const res = await callGet(makeReq());
|
|
expect(res.status).toBe(200);
|
|
|
|
// The unbounded raw `moodEntry.findMany` must not fire on the
|
|
// rollup-tier fast path.
|
|
expect(prisma.moodEntry.findMany).not.toHaveBeenCalled();
|
|
|
|
const body = (await res.json()) as TargetsBody;
|
|
const moodTarget = body.data.targets.find((t) => t.type === "MOOD_SCORE");
|
|
expect(moodTarget).toBeDefined();
|
|
expect(moodTarget?.current).toBe(4);
|
|
// Average over 5 daily means: (4+4+5+4+4.5)/5 = 4.3
|
|
expect(moodTarget?.average30).toBeCloseTo(4.3, 1);
|
|
});
|
|
|
|
it("falls back to the bounded raw walk when the rollup tier is empty but raw mood entries exist", async () => {
|
|
// Rollup empty → coverage-fallback fires the legacy live walk
|
|
// once. The fallback is bounded by the 30-day window — not the
|
|
// legacy unbounded scan over every mood the user ever wrote.
|
|
(
|
|
prisma.moodEntryRollup.findMany as ReturnType<typeof vi.fn>
|
|
).mockResolvedValue([]);
|
|
const today = new Date();
|
|
(prisma.moodEntry.findFirst as ReturnType<typeof vi.fn>).mockResolvedValue({
|
|
score: 4,
|
|
moodLoggedAt: today,
|
|
});
|
|
(prisma.moodEntry.findMany as ReturnType<typeof vi.fn>).mockResolvedValue([
|
|
{ score: 4, moodLoggedAt: new Date(today.getTime() - 2 * 86_400_000) },
|
|
{ score: 5, moodLoggedAt: new Date(today.getTime() - 1 * 86_400_000) },
|
|
{ score: 4, moodLoggedAt: today },
|
|
]);
|
|
|
|
const res = await callGet(makeReq());
|
|
expect(res.status).toBe(200);
|
|
|
|
// Live fallback fired exactly once. The fallback `findMany` call
|
|
// must carry a 30-day `moodLoggedAt: { gte: ... }` floor — i.e. a
|
|
// `where` clause with a timestamp filter, not the legacy
|
|
// unbounded `where: { userId }` shape.
|
|
expect(prisma.moodEntry.findMany).toHaveBeenCalledTimes(1);
|
|
const fallbackCall = (prisma.moodEntry.findMany as ReturnType<typeof vi.fn>)
|
|
.mock.calls[0][0];
|
|
expect(fallbackCall.where).toHaveProperty("moodLoggedAt");
|
|
expect(fallbackCall.where.moodLoggedAt).toHaveProperty("gte");
|
|
|
|
const body = (await res.json()) as TargetsBody;
|
|
const moodTarget = body.data.targets.find((t) => t.type === "MOOD_SCORE");
|
|
expect(moodTarget).toBeDefined();
|
|
expect(moodTarget?.current).toBe(4);
|
|
});
|
|
|
|
it("emits no mood target when neither rollup nor raw entries exist", async () => {
|
|
(
|
|
prisma.moodEntryRollup.findMany as ReturnType<typeof vi.fn>
|
|
).mockResolvedValue([]);
|
|
(prisma.moodEntry.findFirst as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
null,
|
|
);
|
|
|
|
const res = await callGet(makeReq());
|
|
expect(res.status).toBe(200);
|
|
const body = (await res.json()) as TargetsBody;
|
|
const moodTarget = body.data.targets.find((t) => t.type === "MOOD_SCORE");
|
|
expect(moodTarget).toBeUndefined();
|
|
});
|
|
|
|
it("floors the `findMany distinct` latest-ever-per-type read at one year", async () => {
|
|
// Audit High finding 3: the `findMany({orderBy: measuredAt desc,
|
|
// distinct: ["type"]})` for "absolute latest measurement per type"
|
|
// used to run WITHOUT a `measuredAt >=` floor. On a 347 k-row
|
|
// tenant Postgres still has to sort the full set before applying
|
|
// DISTINCT ON because Prisma's `distinct` does not compile to PG's
|
|
// `DISTINCT ON` — it dedups in the driver. v1.4.40 adds a
|
|
// 365-day floor so the planner-side walk is bounded.
|
|
(prisma.measurement.findMany as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
[],
|
|
);
|
|
|
|
await callGet(makeReq());
|
|
|
|
// Find the `distinct: ["type"]` call.
|
|
const calls = (
|
|
prisma.measurement.findMany as ReturnType<typeof vi.fn>
|
|
).mock.calls.map((c) => c[0]);
|
|
const distinctCall = calls.find(
|
|
(c) =>
|
|
c &&
|
|
Array.isArray(c.distinct) &&
|
|
c.distinct[0] === "type" &&
|
|
c.orderBy?.measuredAt === "desc",
|
|
);
|
|
expect(distinctCall).toBeDefined();
|
|
// The `measuredAt >= oneYearAgo` floor must be present.
|
|
expect(distinctCall?.where?.measuredAt).toBeDefined();
|
|
expect(distinctCall?.where?.measuredAt?.gte).toBeInstanceOf(Date);
|
|
const flooredAt = distinctCall?.where?.measuredAt?.gte as Date;
|
|
const oneYearMs = 365 * 24 * 60 * 60 * 1000;
|
|
const ageMs = Date.now() - flooredAt.getTime();
|
|
// Floor is somewhere around one year ago — give a ±1-hour window
|
|
// for clock drift between the route's `Date.now()` and the test
|
|
// assertion.
|
|
expect(ageMs).toBeGreaterThan(oneYearMs - 3_600_000);
|
|
expect(ageMs).toBeLessThan(oneYearMs + 3_600_000);
|
|
});
|
|
});
|