From 1224b05d347e401673b0daf8dfc6abdd15654081 Mon Sep 17 00:00:00 2001 From: Space_Hermes Date: Sun, 30 Aug 2026 21:09:38 +0200 Subject: [PATCH] fix(workout): make progression guidance actionable --- frontend/src/views/Workout.jsx | 53 +++++++++++++++++------------ frontend/src/views/Workout.test.jsx | 41 ++++++++++++++++++++++ 2 files changed, 73 insertions(+), 21 deletions(-) diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx index fafab26..8ebd552 100644 --- a/frontend/src/views/Workout.jsx +++ b/frontend/src/views/Workout.jsx @@ -441,28 +441,39 @@ function ActiveWorkout() { const state = useStore.getState().S const entry = state.active?.entries?.[idx] if (!entry) return + const activeId = state.active.id + const entryId = entry.id const routine = state.routines.find(r => r.id === state.active.routineId) - exConfigSheet(exOr(entry.id), entry.target, cfg => update(s => { - const activeEntry = s.active?.entries?.[idx] - if (!activeEntry) return - const full = { ...cfg, id: activeEntry.id } - const activeRoutine = s.routines.find(r => r.id === s.active.routineId) - const step = defaultIncrement(activeEntry.id, s.unit) - // A config without a set count keeps the rows the session already has. - if (!(full.sets > 0)) full.sets = activeEntry.sets.filter(x => !isWarmupRow(x)).length || 1 - const plan = nextPrescription(s, full, activeRoutine) - // The sheet edits sets, reps, weight and warm-ups as well as the rule — so the rows are - // rebuilt from the new config the way the session was, and only what you already logged - // is kept in place (done warm-ups first, then done work sets, then the fresh remainder). - const fresh = applyIntensifierPlan(applyPrescription(buildSets(s, full, { step }), plan, step), full) - const doneWarm = activeEntry.sets.filter(x => x.done && isWarmupRow(x)) - const doneWork = activeEntry.sets.filter(x => x.done && !isWarmupRow(x)) - const freshWarm = fresh.filter(isWarmupRow) - const freshWork = fresh.filter(x => !isWarmupRow(x)) - activeEntry.target = { ...cfg } - activeEntry.plan = plan - activeEntry.sets = [...doneWarm, ...freshWarm.slice(doneWarm.length), ...doneWork, ...freshWork.slice(doneWork.length)] - }), null, routine) + exConfigSheet(exOr(entryId), entry.target, cfg => { + // Store updates clone the state tree. If this exact object is no longer at the captured + // index, the list changed while the sheet was open; an id check alone cannot distinguish + // duplicate occurrences of the same exercise, so fail closed before cloning again. + const current = useStore.getState().S + if (current.active?.id !== activeId || current.active.entries?.[idx] !== entry) return + update(s => { + const activeEntry = s.active?.id === activeId ? s.active.entries?.[idx] : null + // The sheet may outlive its workout or entry. Never apply its result to whatever later + // happens to occupy the same index. + if (!activeEntry || activeEntry.id !== entryId) return + const full = { ...cfg, id: activeEntry.id } + const activeRoutine = s.routines.find(r => r.id === s.active.routineId) + const step = defaultIncrement(activeEntry.id, s.unit) + // A config without a set count keeps the rows the session already has. + if (!(full.sets > 0)) full.sets = activeEntry.sets.filter(x => !isWarmupRow(x)).length || 1 + const plan = nextPrescription(s, full, activeRoutine) + // The sheet edits sets, reps, weight and warm-ups as well as the rule — so the rows are + // rebuilt from the new config the way the session was, and only what you already logged + // is kept in place (done warm-ups first, then done work sets, then the fresh remainder). + const fresh = applyIntensifierPlan(applyPrescription(buildSets(s, full, { step }), plan, step), full) + const doneWarm = activeEntry.sets.filter(x => x.done && isWarmupRow(x)) + const doneWork = activeEntry.sets.filter(x => x.done && !isWarmupRow(x)) + const freshWarm = fresh.filter(isWarmupRow) + const freshWork = fresh.filter(x => !isWarmupRow(x)) + activeEntry.target = { ...cfg } + activeEntry.plan = plan + activeEntry.sets = [...doneWarm, ...freshWarm.slice(doneWarm.length), ...doneWork, ...freshWork.slice(doneWork.length)] + }) + }, null, routine) } // Remove a whole exercise from the session. The confirmation always asks first; in a diff --git a/frontend/src/views/Workout.test.jsx b/frontend/src/views/Workout.test.jsx index 72c0082..488eff4 100644 --- a/frontend/src/views/Workout.test.jsx +++ b/frontend/src/views/Workout.test.jsx @@ -477,6 +477,47 @@ describe('progression guidance', () => { expect(mocks.S.active.cur).toBe(0) }) + it('does not save into a different duplicate occurrence after the entry list shifts', async () => { + const plan = { + policy: 'linear', kind: 'up', weight: 62.5, + why: ['Every rep last time — {0} {1} more.', 2.5, 'kg'], + } + const first = exercise('plain-bench', [false], { plan, target: { mode: 'reps', reps: 5, weight: 60, marker: 'first' } }) + const second = exercise('plain-bench', [false], { plan, target: { mode: 'reps', reps: 8, weight: 80, marker: 'second' } }) + const third = exercise('plain-bench', [false], { plan, target: { mode: 'reps', reps: 10, weight: 100, marker: 'third' } }) + await mount([first, second, third], 1) + await pressProgression() + const save = mocks.exConfigSheet.mock.calls[0][2] + + mocks.S.active.entries.splice(0, 1) + await act(async () => { save({ ...second.target, prog: 'double', repsMin: 6 }) }) + + expect(mocks.S.active.entries.map(entry => entry.target.marker)).toEqual(['second', 'third']) + expect(mocks.S.active.entries[0].target.prog).toBeUndefined() + expect(mocks.S.active.entries[1].target.prog).toBeUndefined() + }) + + it('does not save through a sheet left open from a replaced workout', async () => { + const plan = { + policy: 'linear', kind: 'up', weight: 62.5, + why: ['Every rep last time — {0} {1} more.', 2.5, 'kg'], + } + const original = exercise('plain-bench', [false], { plan }) + await mount([original]) + await pressProgression() + const save = mocks.exConfigSheet.mock.calls[0][2] + const replacement = exercise('plain-bench', [false], { + plan, + target: { mode: 'reps', reps: 10, weight: 100, marker: 'replacement' }, + }) + mocks.S.active = { ...mocks.S.active, id: 'replacement-workout', entries: [replacement] } + + await act(async () => { save({ ...original.target, prog: 'double', repsMin: 6 }) }) + + expect(mocks.S.active.entries[0].target).toEqual(replacement.target) + expect(mocks.S.active.entries[0].target.prog).toBeUndefined() + }) + it('leaves the active entry unchanged when progression settings are cancelled', async () => { const entry = exercise('plain-bench', [true, false], { plan: {