mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
Pull Oura's daily_cycle_phases collection (undocumented in the published OpenAPI spec, but confirmed live and auth-gated identically to every other daily_* collection) on the existing hourly Oura poll. A luteal→follicular transition, or a literal "menstrual" phase day, writes a single-day CycleDayLog(flow=LIGHT, source=OURA) — the same conservative "logged but unspecified intensity" marker the HealthKit unspecified-flow mapping uses. Manual entries always win: the write only ever creates a day that has no existing CycleDayLog row at all, regardless of source, and never updates a row it did not create itself. No MenstrualCycle span is synthesised, mirroring the Apple Health importer precedent. A 403/404 on the endpoint (the expected outcome for most connections — Oura's reproductive-health resources are all absent from its public spec) is swallowed without touching the rest of the Oura connection's sync status. No migration: reuses the existing CycleDayLog.source column and the OURA value already present on MeasurementSource.
167 lines
5.8 KiB
TypeScript
167 lines
5.8 KiB
TypeScript
/**
|
|
* `syncUserOuraCyclePhases` — Oura Cycle Insights → CycleDayLog, subordinate
|
|
* to any existing row. The core invariant under test: a day that already
|
|
* carries ANY CycleDayLog row (regardless of source) is never touched —
|
|
* Oura only ever fills a genuinely empty day.
|
|
*/
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const { fetchCyclePhasesMock, isCycleAvailableMock, findUserMock, createMock } =
|
|
vi.hoisted(() => ({
|
|
fetchCyclePhasesMock: vi.fn(),
|
|
isCycleAvailableMock: vi.fn(),
|
|
findUserMock: vi.fn(),
|
|
createMock: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("@/lib/db", () => ({
|
|
prisma: {
|
|
user: { findUnique: findUserMock },
|
|
cycleDayLog: { create: createMock },
|
|
},
|
|
}));
|
|
|
|
vi.mock("@/lib/cycle/gate", () => ({
|
|
isCycleAvailableForUser: isCycleAvailableMock,
|
|
}));
|
|
|
|
vi.mock("../client", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("../client")>();
|
|
return {
|
|
...actual,
|
|
fetchDailyCyclePhases: fetchCyclePhasesMock,
|
|
};
|
|
});
|
|
|
|
import { syncUserOuraCyclePhases } from "../cycle-sync";
|
|
|
|
function uniqueViolation() {
|
|
const err = new Error("Unique constraint failed") as Error & {
|
|
code: string;
|
|
};
|
|
err.code = "P2002";
|
|
return err;
|
|
}
|
|
|
|
beforeEach(() => {
|
|
fetchCyclePhasesMock.mockReset().mockResolvedValue([]);
|
|
isCycleAvailableMock.mockReset().mockResolvedValue(true);
|
|
findUserMock.mockReset().mockResolvedValue({ timezone: "Europe/Berlin" });
|
|
createMock.mockReset().mockResolvedValue({ id: "cdl-1" });
|
|
});
|
|
|
|
describe("syncUserOuraCyclePhases", () => {
|
|
it("no-ops without touching the DB when the collection is empty", async () => {
|
|
const created = await syncUserOuraCyclePhases("u1", "tok", 7);
|
|
expect(created).toBe(0);
|
|
expect(isCycleAvailableMock).not.toHaveBeenCalled();
|
|
expect(createMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("no-ops when the series carries no derivable period day", async () => {
|
|
fetchCyclePhasesMock.mockResolvedValue([
|
|
{ day: "2026-06-08", phase: "luteal" },
|
|
{ day: "2026-06-09", phase: "luteal" },
|
|
]);
|
|
const created = await syncUserOuraCyclePhases("u1", "tok", 7);
|
|
expect(created).toBe(0);
|
|
expect(isCycleAvailableMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("respects the two-layer cycle gate — no write when disabled", async () => {
|
|
fetchCyclePhasesMock.mockResolvedValue([
|
|
{ day: "2026-06-05", phase: "menstrual" },
|
|
]);
|
|
isCycleAvailableMock.mockResolvedValue(false);
|
|
const created = await syncUserOuraCyclePhases("u1", "tok", 7);
|
|
expect(created).toBe(0);
|
|
expect(createMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("creates a source=OURA flow=LIGHT row for a detected period day", async () => {
|
|
fetchCyclePhasesMock.mockResolvedValue([
|
|
{ day: "2026-06-05", phase: "menstrual" },
|
|
]);
|
|
const created = await syncUserOuraCyclePhases("u1", "tok", 7);
|
|
expect(created).toBe(1);
|
|
expect(createMock).toHaveBeenCalledWith({
|
|
data: {
|
|
userId: "u1",
|
|
date: "2026-06-05",
|
|
tz: "Europe/Berlin",
|
|
source: "OURA",
|
|
flow: "LIGHT",
|
|
},
|
|
});
|
|
});
|
|
|
|
it("skips a day that already has ANY existing row (manual wins) — unique-violation swallowed silently", async () => {
|
|
fetchCyclePhasesMock.mockResolvedValue([
|
|
{ day: "2026-06-05", phase: "menstrual" },
|
|
]);
|
|
createMock.mockRejectedValue(uniqueViolation());
|
|
const created = await syncUserOuraCyclePhases("u1", "tok", 7);
|
|
expect(created).toBe(0);
|
|
});
|
|
|
|
it("never calls .update — a pre-existing row (its own or another source's) is left exactly as-is", async () => {
|
|
fetchCyclePhasesMock.mockResolvedValue([
|
|
{ day: "2026-06-05", phase: "menstrual" },
|
|
{ day: "2026-06-06", phase: "menstrual" },
|
|
]);
|
|
createMock
|
|
.mockRejectedValueOnce(uniqueViolation()) // 06-05 already logged (manual)
|
|
.mockResolvedValueOnce({ id: "cdl-2" }); // 06-06 genuinely empty
|
|
const created = await syncUserOuraCyclePhases("u1", "tok", 7);
|
|
expect(created).toBe(1);
|
|
expect(createMock).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("propagates a non-unique-violation write error without crashing the whole batch", async () => {
|
|
fetchCyclePhasesMock.mockResolvedValue([
|
|
{ day: "2026-06-05", phase: "menstrual" },
|
|
{ day: "2026-06-06", phase: "menstrual" },
|
|
]);
|
|
createMock
|
|
.mockRejectedValueOnce(new Error("connection reset"))
|
|
.mockResolvedValueOnce({ id: "cdl-2" });
|
|
const created = await syncUserOuraCyclePhases("u1", "tok", 7);
|
|
// The failing day is skipped (warned, not thrown); the healthy sibling
|
|
// still writes.
|
|
expect(created).toBe(1);
|
|
});
|
|
|
|
it("propagates the Oura fetch error to the caller (the sync layer decides how to swallow it)", async () => {
|
|
fetchCyclePhasesMock.mockRejectedValue(new Error("403 forbidden"));
|
|
await expect(syncUserOuraCyclePhases("u1", "tok", 7)).rejects.toThrow(
|
|
"403 forbidden",
|
|
);
|
|
expect(createMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("falls back to a null tz when the user row / timezone is unresolvable", async () => {
|
|
fetchCyclePhasesMock.mockResolvedValue([
|
|
{ day: "2026-06-05", phase: "menstrual" },
|
|
]);
|
|
findUserMock.mockResolvedValue(null);
|
|
await syncUserOuraCyclePhases("u1", "tok", 7);
|
|
expect(createMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({ data: expect.objectContaining({ tz: null }) }),
|
|
);
|
|
});
|
|
|
|
it("creates one row per distinct detected day from a luteal→follicular transition", async () => {
|
|
fetchCyclePhasesMock.mockResolvedValue([
|
|
{ day: "2026-06-08", phase: "luteal" },
|
|
{ day: "2026-06-09", phase: "follicular" },
|
|
]);
|
|
const created = await syncUserOuraCyclePhases("u1", "tok", 7);
|
|
expect(created).toBe(1);
|
|
expect(createMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
data: expect.objectContaining({ date: "2026-06-09", flow: "LIGHT" }),
|
|
}),
|
|
);
|
|
});
|
|
});
|