Fix critical bug: cycle phase boundaries now scale with cycle length

CRITICAL BUG FIX:
- Phase boundaries were hardcoded for 31-day cycle, breaking correct
  phase calculations for users with different cycle lengths (28, 35, etc.)
- Added getPhaseBoundaries(cycleLength) function in cycle.ts
- Updated getPhase() to accept cycleLength parameter (default 31)
- Updated all callers (API routes, components) to pass cycleLength
- Added 13 new tests for phase boundaries with 28, 31, and 35-day cycles

ICS IMPROVEMENTS:
- Fixed emojis to match calendar.md spec: 🩸🌱🌸🌙🌑
- Added CATEGORIES field for calendar app colors per spec:
  MENSTRUAL=Red, FOLLICULAR=Green, OVULATION=Pink,
  EARLY_LUTEAL=Yellow, LATE_LUTEAL=Orange
- Added 5 new tests for CATEGORIES

Updated IMPLEMENTATION_PLAN.md with discovered issues and test counts.

825 tests passing (up from 807)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-11 22:39:09 +00:00
parent 58f6c5605a
commit a977934c23
15 changed files with 337 additions and 148 deletions

View File

@@ -8,7 +8,6 @@ import {
getPhase,
getPhaseConfig,
getPhaseLimit,
PHASE_CONFIGS,
} from "@/lib/cycle";
import { getDecisionWithOverrides } from "@/lib/decision-engine";
import { logger } from "@/lib/logger";
@@ -47,21 +46,25 @@ export const GET = withAuth(async (_request, user) => {
user.cycleLength,
new Date(),
);
const phase = getPhase(cycleDay);
const phase = getPhase(cycleDay, user.cycleLength);
const phaseConfig = getPhaseConfig(phase);
const phaseLimit = getPhaseLimit(phase);
// Calculate days until next phase
const currentPhaseIndex = PHASE_CONFIGS.findIndex((c) => c.name === phase);
const nextPhaseIndex = (currentPhaseIndex + 1) % PHASE_CONFIGS.length;
const nextPhaseStartDay = PHASE_CONFIGS[nextPhaseIndex].days[0];
// Calculate days until next phase using dynamic boundaries
// Phase boundaries: MENSTRUAL 1-3, FOLLICULAR 4-(cl-16), OVULATION (cl-15)-(cl-14),
// EARLY_LUTEAL (cl-13)-(cl-7), LATE_LUTEAL (cl-6)-cl
let daysUntilNextPhase: number;
if (nextPhaseIndex === 0) {
// Currently in LATE_LUTEAL, next phase is MENSTRUAL (start of new cycle)
if (phase === "LATE_LUTEAL") {
daysUntilNextPhase = user.cycleLength - cycleDay + 1;
} else if (phase === "MENSTRUAL") {
daysUntilNextPhase = 4 - cycleDay;
} else if (phase === "FOLLICULAR") {
daysUntilNextPhase = user.cycleLength - 15 - cycleDay;
} else if (phase === "OVULATION") {
daysUntilNextPhase = user.cycleLength - 13 - cycleDay;
} else {
daysUntilNextPhase = nextPhaseStartDay - cycleDay;
// EARLY_LUTEAL
daysUntilNextPhase = user.cycleLength - 6 - cycleDay;
}
// Try to fetch today's DailyLog for biometrics