diff --git a/frontend/src/lib/plan-share.js b/frontend/src/lib/plan-share.js index 06a3bd5..7c82757 100644 --- a/frontend/src/lib/plan-share.js +++ b/frontend/src/lib/plan-share.js @@ -9,7 +9,7 @@ // a page break — each exercise, and each routine that fits, stays in one place. import { EXIDX, isBodyweightEq } from './exercises.js' -import { modeOf, fmtSec, isBw, isPerSide, sideReps } from './history.js' +import { modeOf, fmtSec, isBw, isPerSide, sideReps, MAX_PLANNED_WARMUPS } from './history.js' import { uid, todayISO, DAYN, fmtNum, exCount } from './format.js' import { t, exerciseNameFor } from './i18n-core.js' @@ -46,10 +46,36 @@ function cleanEx(e) { if (e.repsMax != null) o.repsMax = e.repsMax if (e.sg) o.sg = e.sg if (e.note) o.note = e.note - if (e.warmupSets) o.warmupSets = e.warmupSets + const warm = cleanWarmupSets(e.warmupSets) + if (warm) o.warmupSets = warm + // Drop-sets and rest-pause are part of how the exercise is prescribed, not a logging detail. + // Without this a shared "3x5 with a double drop" arrived at the other end as a plain 3x5, + // silently — parsePlan's `dropped` counter only tracks exercises it cannot resolve at all. + const intens = cleanIntensifier(e.intensifier) + if (intens) o.intensifier = intens return o } +/** Clamped the same way buildSets clamps it on the way out — the stepper showed a hand-edited + * plan file's "999" verbatim, because the clamp only happened when the rows were built. */ +function cleanWarmupSets(v) { + const n = Math.round(Number(v)) || 0 + return n > 0 ? Math.min(MAX_PLANNED_WARMUPS, n) : 0 +} + +/** Keep the floors the config sheet and applyIntensifierPlan already enforce, and nothing else: + * a plan file is someone else's data, so anything unrecognised is dropped rather than trusted. */ +function cleanIntensifier(x) { + const type = x && x.type + if (type === 'dropset') { + return { type, count: Math.max(1, Math.round(Number(x.count)) || 1), pct: Math.max(5, Math.round(Number(x.pct)) || 20) } + } + if (type === 'restpause') { + return { type, totalReps: Math.max(1, Math.round(Number(x.totalReps)) || 1), restSec: Math.max(5, Math.round(Number(x.restSec)) || 15) } + } + return null +} + /** Build the shareable bundle: every routine, the week schedule, referenced customs. */ export function buildPlanBundle(S, name) { const routines = (S.routines || []).map(r => ({ @@ -87,6 +113,13 @@ export function parsePlan(raw) { const ok = !!e && (known.has(e.id) || !!EXIDX[e.id]) if (!ok) dropped++ return ok + }).map(e => { + // The exercises pass through as written, so the two fields that carry numbers into the + // planner get the same clamps on the way in that they get on the way out. + const warm = cleanWarmupSets(e.warmupSets) + const intens = cleanIntensifier(e.intensifier) + const { warmupSets, intensifier, ...rest } = e + return { ...rest, ...(warm ? { warmupSets: warm } : {}), ...(intens ? { intensifier: intens } : {}) } }) })) return { diff --git a/frontend/src/lib/plan-share.test.js b/frontend/src/lib/plan-share.test.js new file mode 100644 index 0000000..ccb5bf8 --- /dev/null +++ b/frontend/src/lib/plan-share.test.js @@ -0,0 +1,47 @@ +import { describe, expect, it } from 'vitest' +import { buildPlanBundle, parsePlan } from './plan-share.js' + +// There was no test file for plan sharing at all, which is how a whole prescription field +// went missing without anyone noticing. +const stateWith = ex => ({ + routines: [{ id: 'r1', name: 'Push', ex: [{ id: '0025', sets: 3, reps: 5, weight: 100, ...ex }] }], + week: {}, customEx: [], +}) +const roundTrip = ex => parsePlan(JSON.stringify(buildPlanBundle(stateWith(ex), 'Plan'))).routines[0].ex[0] + +describe('what survives a shared plan', () => { + it('carries a drop-set prescription', () => { + expect(roundTrip({ intensifier: { type: 'dropset', count: 2, pct: 20 } }).intensifier) + .toEqual({ type: 'dropset', count: 2, pct: 20 }) + }) + + it('carries a rest-pause prescription', () => { + expect(roundTrip({ intensifier: { type: 'restpause', totalReps: 12, restSec: 15 } }).intensifier) + .toEqual({ type: 'restpause', totalReps: 12, restSec: 15 }) + }) + + it('carries planned warm-ups', () => { + expect(roundTrip({ warmupSets: 3 }).warmupSets).toBe(3) + }) + + it('drops an intensifier it does not recognise rather than passing it on', () => { + expect(roundTrip({ intensifier: { type: 'nonsense', count: 3 } }).intensifier).toBeUndefined() + }) + + it('clamps a hand-edited warm-up count instead of showing it verbatim', () => { + const bundle = { opengym_plan: 1, name: 'x', routines: [{ id: 'r', name: 'R', ex: [{ id: '0025', sets: 3, reps: 5, warmupSets: 999 }] }], week: {}, customEx: [] } + expect(parsePlan(bundle).routines[0].ex[0].warmupSets).toBe(5) + }) + + // The floors are the config sheet's own (count >= 1, pct >= 5); a value that is present but + // out of range is pulled up to the floor, while a missing one falls back to the default. + it('clamps out-of-range intensifier numbers to the floors the app enforces', () => { + const bundle = { opengym_plan: 1, name: 'x', routines: [{ id: 'r', name: 'R', ex: [{ id: '0025', sets: 3, reps: 5, intensifier: { type: 'dropset', count: 0, pct: -5 } }] }], week: {}, customEx: [] } + expect(parsePlan(bundle).routines[0].ex[0].intensifier).toEqual({ type: 'dropset', count: 1, pct: 5 }) + }) + + it('falls back to the default drop percentage when the file omits it', () => { + const bundle = { opengym_plan: 1, name: 'x', routines: [{ id: 'r', name: 'R', ex: [{ id: '0025', sets: 3, reps: 5, intensifier: { type: 'dropset' } }] }], week: {}, customEx: [] } + expect(parsePlan(bundle).routines[0].ex[0].intensifier).toEqual({ type: 'dropset', count: 1, pct: 20 }) + }) +})