test(medications): prove the dose history window against Postgres

The cases for the bounded read asserted the query's where clause on a
mocked client and cast their fixtures past the type checker. Against
Postgres they now show what the read promises: a rolling medication last
taken before the window shows the dose that fell due inside it, and a year
of earlier takes answers the same ledger as a short history.
This commit is contained in:
Marc-André Bombeck committed 2026-11-12 12:56:00 +01:00
1 parent ca0d80b85c
commit 05b5eb6942
2 files changed
+117 -71

No files matched your search

@@ -567,74 +567,3 @@ describe("GET /api/medications/[id]/dose-history — slots before creation (#102
});
}
});
/**
* The history reads the window's rows, not every intake the medication ever
* had: for a medication taken for years each open of the tab read thousands
* of rows to show at most a year. Of the rows before the window only the
* latest take is read, because a rolling grid re-starts from it.
*/
describe("GET /api/medications/[id]/dose-history — the read is bounded to the window", () => {
const FROM = new Date("2026-06-01T00:00:00Z");
const TO = new Date("2026-06-15T00:00:00Z");
const windowQuery = `?from=${encodeURIComponent(FROM.toISOString())}&to=${encodeURIComponent(TO.toISOString())}`;
it("asks only for rows anchored or taken in the window", async () => {
await GET(getReq(windowQuery), ROUTE_PARAMS);
const where = vi.mocked(prisma.medicationIntakeEvent.findMany).mock
.calls[0][0]?.where;
expect(where).toMatchObject({
OR: [{ scheduledFor: { gte: FROM } }, { takenAt: { gte: FROM } }],
});
const before = vi.mocked(prisma.medicationIntakeEvent.findFirst).mock
.calls[0][0];
expect(before?.where).toMatchObject({
skipped: false,
scheduledFor: { lt: FROM },
takenAt: { lt: FROM },
});
});
it("re-starts a rolling grid from the latest take before the window", async () => {
vi.mocked(prisma.medication.findUnique).mockResolvedValue({
id: "med-1",
startsOn: null,
endsOn: null,
oneShot: false,
createdAt: new Date("2026-01-01T00:00:00Z"),
schedules: [
{
id: "sched-1",
windowStart: "08:00",
windowEnd: "08:00",
daysOfWeek: null,
timesOfDay: ["08:00"],
reminderGraceMinutes: null,
rrule: null,
rollingIntervalDays: 7,
scheduleType: "SCHEDULED",
cyclicOnWeeks: null,
cyclicOffWeeks: null,
},
],
} as never);
// Taken four days before the window opens, nothing since: the next dose
// was due inside the window and reads as missed there.
vi.mocked(prisma.medicationIntakeEvent.findFirst).mockResolvedValue({
takenAt: new Date("2026-05-28T06:00:00Z"),
} as never);
const res = await GET(getReq(windowQuery), ROUTE_PARAMS);
expect(res.status).toBe(200);
const rows = (await res.json()).data.rows as Array<{
at: string;
kind: string;
status: string;
}>;
expect(rows.map((r) => [r.at.slice(0, 10), r.kind, r.status])).toEqual([
["2026-06-04", "slot", "missed"],
]);
});
});