-
Notifications
You must be signed in to change notification settings - Fork 1
GP-142 GP-143 GP-144: Harden training plan guardrails #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| import { canonicalizeMinimalTrainingPlanCreate } from "@repo/core/plan/canonicalization"; | ||
| import type { ProjectionChartPayload } from "@repo/core/plan/projectionTypes"; | ||
| import { withLegacyTrainingLoadAliases } from "@repo/core/plan/trainingLoadTimeline"; | ||
| import { | ||
| filterDateKeyedItemsToProjectionWindow, | ||
| resolveTrainingPlanProjectionWindow, | ||
| } from "@repo/core/plan/trainingPlanProjectionBudgets"; | ||
| import { | ||
| type AthleteTrainingSettings, | ||
| type AthleteTrainingSettingsFormInput, | ||
|
|
@@ -78,6 +82,7 @@ function toGoalTargets(goal: TrainingPlanSnapshot["profileGoals"][number]): Goal | |
| case "hr": | ||
| return [{ target_type: "hr_threshold", target_lthr_bpm: objective.value }]; | ||
| } | ||
| return []; | ||
| } | ||
| default: | ||
| return []; | ||
|
|
@@ -188,21 +193,35 @@ export function buildTrainingPreferencesLoadTimeline(input: { | |
| scheduledWindowStart?: string | null; | ||
| scheduledWindowEnd?: string | null; | ||
| }) { | ||
| const todayKey = toDateKey(new Date()); | ||
| const projectionWindow = resolveTrainingPlanProjectionWindow({ | ||
| anchorDate: todayKey, | ||
| requestedStartDate: input.scheduledWindowStart, | ||
| requestedEndDate: input.scheduledWindowEnd, | ||
| }); | ||
| const baselineTimeline = input.snapshot.insightTimeline?.timeline ?? []; | ||
| const baselineByDate = new Map(baselineTimeline.map((point) => [point.date, point])); | ||
| const previewByDate = new Map( | ||
| (input.projectionChart?.display_points ?? []).map((point) => [point.date, point]), | ||
| ); | ||
| const dateSource = baselineTimeline.length > 0 ? baselineTimeline : [...previewByDate.values()]; | ||
| const dates = new Set(dateSource.map((point) => point.date)); | ||
| const dateSource: Array<{ date: string }> = | ||
| baselineTimeline.length > 0 ? baselineTimeline : [...previewByDate.values()]; | ||
| const dates = new Set( | ||
| filterDateKeyedItemsToProjectionWindow(dateSource, { | ||
| window: projectionWindow, | ||
| getDate: (point) => point.date, | ||
| }).map((point) => point.date), | ||
| ); | ||
| const scheduledLoadAggregation = aggregateScheduledLoadByDate(input); | ||
| for (const date of scheduledLoadAggregation.dates) dates.add(date); | ||
| for (const date of scheduledLoadAggregation.dates) { | ||
| if (date >= projectionWindow.startDate && date <= projectionWindow.endDate) dates.add(date); | ||
| } | ||
|
|
||
| const hasCalendarScheduleForDate = (date: string) => | ||
| !!input.scheduledWindowStart && | ||
| !!input.scheduledWindowEnd && | ||
| date >= input.scheduledWindowStart && | ||
| date <= input.scheduledWindowEnd; | ||
| date >= projectionWindow.startDate && | ||
| date <= projectionWindow.endDate; | ||
|
Comment on lines
+223
to
+224
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a caller supplies Useful? React with 👍 / 👎. |
||
|
|
||
| return [...dates] | ||
| .sort((left, right) => left.localeCompare(right)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| evaluateTrainingPlanCreationReadiness, | ||
| validateTrainingPlanCreationInput, | ||
| } from "./trainingPlanCreationValidation"; | ||
|
|
||
| const publishedAccessiblePlan = { | ||
| id: "activity-plan-1", | ||
| accessible: true, | ||
| published: true, | ||
| }; | ||
|
|
||
| describe("trainingPlanCreationValidation", () => { | ||
| it("returns a deterministic readiness result with blockers and warnings", () => { | ||
| const readiness = evaluateTrainingPlanCreationReadiness({ | ||
| name: "", | ||
| anchorDateValid: true, | ||
| profileBirthDateValid: true, | ||
| planPreferencesValid: true, | ||
| preferences: { | ||
| weeklySessionCount: 6, | ||
| targetWeeklyHours: 1, | ||
| restDaysPerWeek: 2, | ||
| }, | ||
| sessions: [ | ||
| { | ||
| localId: "session-1", | ||
| offsetDays: 0, | ||
| activityPlan: publishedAccessiblePlan, | ||
| startTime: "09:00", | ||
| }, | ||
| ], | ||
| goals: [], | ||
| }); | ||
|
|
||
| expect(readiness.canSave).toBe(false); | ||
| expect(readiness.blockers.map((issue) => issue.code)).toEqual([ | ||
| "missing_plan_name", | ||
| "weekly_session_rest_day_conflict", | ||
| ]); | ||
| expect(readiness.warnings.map((issue) => issue.code)).toEqual([ | ||
| "weekly_hours_session_mismatch", | ||
| ]); | ||
| expect(readiness.issues.every((issue) => issue.severity)).toBe(true); | ||
| }); | ||
|
|
||
| it("keeps the legacy validation helper blocker-only", () => { | ||
| const issues = validateTrainingPlanCreationInput({ | ||
| name: "Base build", | ||
| anchorDateValid: true, | ||
| profileBirthDateValid: true, | ||
| planPreferencesValid: true, | ||
| preferences: { | ||
| weeklySessionCount: 4, | ||
| targetWeeklyHours: 1, | ||
| restDaysPerWeek: null, | ||
| }, | ||
| sessions: [ | ||
| { | ||
| localId: "session-1", | ||
| offsetDays: 0, | ||
| activityPlan: publishedAccessiblePlan, | ||
| }, | ||
| ], | ||
| goals: [], | ||
| }); | ||
|
|
||
| expect(issues).toEqual([]); | ||
| }); | ||
|
|
||
| it("allows a complete creation input", () => { | ||
| const readiness = evaluateTrainingPlanCreationReadiness({ | ||
| name: "Base build", | ||
| anchorDateValid: true, | ||
| profileBirthDateValid: true, | ||
| planPreferencesValid: true, | ||
| preferences: { | ||
| weeklySessionCount: 4, | ||
| targetWeeklyHours: 5, | ||
| restDaysPerWeek: 2, | ||
| }, | ||
| sessions: [ | ||
| { | ||
| localId: "session-1", | ||
| offsetDays: 0, | ||
| activityPlan: publishedAccessiblePlan, | ||
| startTime: "09:00", | ||
| }, | ||
| ], | ||
| goals: [{ localId: "goal-1", targetDateValid: true, targetOffsetDays: 28 }], | ||
| }); | ||
|
|
||
| expect(readiness).toMatchObject({ canSave: true, blockers: [], warnings: [] }); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this makes the backend commit route available, the create/edit builder starts saving through
createFromCreationConfig/updateFromCreationConfig, but the mapped backend input only carries the minimal goal/config data and notstate.detailsorstate.structure.sessions. The backend commit then persists the generatedexpandedPlan, so a user who typed a plan name/description or manually arranged sessions/activity plans can save a different generated plan instead of the one they built; keep the legacy payload route for manual-builder saves or include those builder fields in the backend contract before enabling these commit operations.Useful? React with 👍 / 👎.