mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
fix(medications): cap intake snap tolerance to the inter-dose gap so multi-dose orals don't collapse
A daily oral with multiple timesOfDay and a wide schedule window had its dose-slot capture zones overlap, so a second same-day dose could snap onto an already-actioned slot and be dropped by the no-downgrade guard — the intake never landed as its own row. A twice-daily 08:00 / 20:00 schedule with an 08:00-22:00 window yielded a +/-7h half-span tolerance that exceeded half the 12h inter-slot gap, so the evening write collapsed onto the morning slot. Weekly and single-dose schedules (one slot/day) were immune. snapToleranceMs now caps the tolerance at half the minimum gap between adjacent sorted timesOfDay entries (the midnight wrap counted as one of those gaps) whenever a schedule has more than one slot, keeping the two nearest slots' capture zones disjoint. Single-slot schedules keep the full half-window-span behaviour, and the >=1-minute drift floor is preserved, so weekly injectables and once-daily orals are unchanged. Adds unit coverage for the wide-window two-dose case (distinct slots, no collapse) plus an explicit weekly single-dose regression, and an integration test driving both same-day writes through the DB-backed slot resolver and canonical-slot upsert to assert two distinct live rows.
This commit is contained in:
1 parent
d5eb887757
commit
793602f464
3 files changed
+269
-11
No files matched your search
@@ -25,6 +25,10 @@ import {
|
||||
dedupeUserIntakeSlots,
|
||||
enqueueBootTimeIntakeSlotDedup,
|
||||
} from "@/lib/medications/intake-slot-dedup";
|
||||
import {
|
||||
applyCanonicalSlotWrite,
|
||||
resolveSlotInstantForWrite,
|
||||
} from "@/lib/medications/scheduling/slot-upsert";
|
||||
import { localHmAsUtc } from "@/lib/timezone";
|
||||
|
||||
const TEST_USER_ID = "user-intake-slot-dedup";
|
||||
@@ -306,6 +310,115 @@ describe("intake-slot-dedup — duplicate slot collapse", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("intake write path — multi-dose wide window does not collapse", () => {
|
||||
// The LIVE twice-daily-Ramipril regression, end-to-end through the
|
||||
// DB-backed slot resolver + the canonical-slot upsert: two distinct
|
||||
// same-day doses on an 08:00 / 20:00 schedule with a WIDE 08:00–22:00
|
||||
// window must land as TWO separate live rows, not collapse onto one.
|
||||
async function createWideWindowTwiceDailyMed(): Promise<string> {
|
||||
const prisma = getPrismaClient();
|
||||
const med = await prisma.medication.create({
|
||||
data: {
|
||||
userId: TEST_USER_ID,
|
||||
name: "Ramipril",
|
||||
dose: "5mg",
|
||||
active: true,
|
||||
schedules: {
|
||||
create: {
|
||||
windowStart: "08:00",
|
||||
windowEnd: "22:00", // wide window — the pre-fix overlap trigger
|
||||
timesOfDay: ["08:00", "20:00"],
|
||||
daysOfWeek: null,
|
||||
scheduleType: "SCHEDULED",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
return med.id;
|
||||
}
|
||||
|
||||
it("resolves morning and evening writes to two distinct canonical slots and lands two rows", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
const medId = await createWideWindowTwiceDailyMed();
|
||||
|
||||
const morningWrite = new Date("2026-03-10T07:05:00.000Z"); // 08:05 CET
|
||||
const eveningWrite = new Date("2026-03-10T19:03:00.000Z"); // 20:03 CET
|
||||
|
||||
const morningSlot = await resolveSlotInstantForWrite({
|
||||
userId: TEST_USER_ID,
|
||||
medicationId: medId,
|
||||
userTz: "Europe/Berlin",
|
||||
incoming: morningWrite,
|
||||
client: prisma,
|
||||
});
|
||||
const eveningSlot = await resolveSlotInstantForWrite({
|
||||
userId: TEST_USER_ID,
|
||||
medicationId: medId,
|
||||
userTz: "Europe/Berlin",
|
||||
incoming: eveningWrite,
|
||||
client: prisma,
|
||||
});
|
||||
|
||||
expect(morningSlot).not.toBeNull();
|
||||
expect(eveningSlot).not.toBeNull();
|
||||
// The two doses must resolve to two distinct canonical slot instants.
|
||||
expect(morningSlot?.toISOString()).not.toBe(eveningSlot?.toISOString());
|
||||
expect(morningSlot?.toISOString()).toBe(
|
||||
localHmAsUtc(DAY, "Europe/Berlin", 8, 0).toISOString(),
|
||||
);
|
||||
expect(eveningSlot?.toISOString()).toBe(
|
||||
localHmAsUtc(DAY, "Europe/Berlin", 20, 0).toISOString(),
|
||||
);
|
||||
|
||||
// Apply the morning taken write.
|
||||
const morningResult = await applyCanonicalSlotWrite({
|
||||
client: prisma,
|
||||
userId: TEST_USER_ID,
|
||||
medicationId: medId,
|
||||
canonicalSlot: morningSlot!,
|
||||
takenAt: morningWrite,
|
||||
skipped: false,
|
||||
isExplicitTaken: true,
|
||||
isExplicitSkip: false,
|
||||
idempotencyKey: null,
|
||||
createSource: "WEB",
|
||||
});
|
||||
expect(morningResult.outcome).toBe("inserted");
|
||||
|
||||
// Apply the evening taken write — must NOT collapse onto the morning
|
||||
// slot (the bug) and must land as its own row.
|
||||
const eveningResult = await applyCanonicalSlotWrite({
|
||||
client: prisma,
|
||||
userId: TEST_USER_ID,
|
||||
medicationId: medId,
|
||||
canonicalSlot: eveningSlot!,
|
||||
takenAt: eveningWrite,
|
||||
skipped: false,
|
||||
isExplicitTaken: true,
|
||||
isExplicitSkip: false,
|
||||
idempotencyKey: null,
|
||||
createSource: "WEB",
|
||||
});
|
||||
expect(eveningResult.outcome).toBe("inserted");
|
||||
expect(eveningResult.noDowngradeNoOp).toBe(false);
|
||||
|
||||
// Two distinct live rows, one per dose slot.
|
||||
const live = await prisma.medicationIntakeEvent.findMany({
|
||||
where: { userId: TEST_USER_ID, medicationId: medId, deletedAt: null },
|
||||
orderBy: { scheduledFor: "asc" },
|
||||
});
|
||||
expect(live).toHaveLength(2);
|
||||
expect(live[0]?.takenAt).not.toBeNull();
|
||||
expect(live[1]?.takenAt).not.toBeNull();
|
||||
expect(live[0]?.scheduledFor.toISOString()).toBe(
|
||||
localHmAsUtc(DAY, "Europe/Berlin", 8, 0).toISOString(),
|
||||
);
|
||||
expect(live[1]?.scheduledFor.toISOString()).toBe(
|
||||
localHmAsUtc(DAY, "Europe/Berlin", 20, 0).toISOString(),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("intake-slot-dedup — boot discovery scoping", () => {
|
||||
it("returns 0 when no user holds a duplicate pair", async () => {
|
||||
const prisma = getPrismaClient();
|
||||
|
||||
Reference in new issue
Block a user