Files
HealthLog/src/lib/medications/__tests__/injection-site-write.test.ts
T
Marc-André Bombeck b99a015266 feat(medications): make injection-site tracking and rotation real
The injection_site column on intake events was read across several
surfaces but never written, so site tracking was effectively dead. Wire
it end-to-end as a per-medication opt-in, gated on an INJECTION delivery
form and default off.

- Schema: add medications.track_injection_sites + allowed_injection_sites
  and users.global_excluded_injection_sites (migrations 0099, 0100;
  additive, defaulted, no backfill).
- Intake write path: accept an optional injectionSite on the per-med,
  status-toggle, and bulk intake routes. Persist it only on a taken
  injection with tracking enabled, validated against the medication's
  effective allowed set (per-med allowed sites minus the user-level
  global exclusion, deny wins) — a disallowed site returns 422 (the bulk
  route skips the entry).
- Rotation: extend the least-recently-used recommender with an allowed
  filter; the picker disables sites outside the effective set.
- UI: per-medication tracking toggle + allowed-sites editor in the dose
  wizard step, a skippable post-dose site prompt on both medication
  cards, and a Settings card for the user-level global exclusion.
- Contract: injectionSite is additive on the intake request; OpenAPI
  regenerated. New GET/PATCH /api/auth/me/injection-site-prefs.

The site capture is always optional and never blocks logging a dose.
2026-06-01 20:25:34 +02:00

80 lines
2.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { resolveInjectionSiteForWrite } from "@/lib/medications/injection-site-write";
const base = {
taken: true,
deliveryForm: "INJECTION",
trackInjectionSites: true,
allowedInjectionSites: [] as const,
globalExcludedInjectionSites: [] as const,
};
describe("resolveInjectionSiteForWrite()", () => {
it("persists a valid site on a taken injection with tracking on", () => {
const r = resolveInjectionSiteForWrite({ ...base, submitted: "THIGH_LEFT" });
expect(r).toEqual({ kind: "ok", site: "THIGH_LEFT" });
});
it("drops the site when the medication is not an INJECTION", () => {
const r = resolveInjectionSiteForWrite({
...base,
deliveryForm: "ORAL",
submitted: "THIGH_LEFT",
});
expect(r).toEqual({ kind: "ok", site: null });
});
it("drops the site when tracking is disabled (default-off)", () => {
const r = resolveInjectionSiteForWrite({
...base,
trackInjectionSites: false,
submitted: "THIGH_LEFT",
});
expect(r).toEqual({ kind: "ok", site: null });
});
it("drops the site on a skipped write (optional, never blocking)", () => {
const r = resolveInjectionSiteForWrite({
...base,
taken: false,
submitted: "THIGH_LEFT",
});
expect(r).toEqual({ kind: "ok", site: null });
});
it("treats an omitted site as no-site (optional capture)", () => {
const r = resolveInjectionSiteForWrite({ ...base, submitted: undefined });
expect(r).toEqual({ kind: "ok", site: null });
});
it("rejects a site outside the per-medication allowed set (422)", () => {
const r = resolveInjectionSiteForWrite({
...base,
allowedInjectionSites: ["THIGH_RIGHT"],
submitted: "ABDOMEN_LEFT",
});
expect(r).toEqual({ kind: "disallowed", site: "ABDOMEN_LEFT" });
});
it("rejects a globally excluded site even when per-med-preferred (deny wins)", () => {
const r = resolveInjectionSiteForWrite({
...base,
allowedInjectionSites: ["ABDOMEN_LEFT"],
globalExcludedInjectionSites: ["ABDOMEN_LEFT"],
submitted: "ABDOMEN_LEFT",
});
expect(r).toEqual({ kind: "disallowed", site: "ABDOMEN_LEFT" });
});
it("accepts a per-med-allowed site that is not globally excluded", () => {
const r = resolveInjectionSiteForWrite({
...base,
allowedInjectionSites: ["ABDOMEN_LEFT", "THIGH_RIGHT"],
globalExcludedInjectionSites: ["ABDOMEN_LEFT"],
submitted: "THIGH_RIGHT",
});
expect(r).toEqual({ kind: "ok", site: "THIGH_RIGHT" });
});
});