Merge !84: fix: use exercise weight step for workout controls

# Conflicts:
#	frontend/src/views/Workout.jsx
This commit is contained in:
Duarte Santos committed 2026-09-04 16:53:51 +02:00
commit b301b8c81d
4 files changed
+99 -14

No files matched your search

+18 -7
View File
@@ -61,6 +61,11 @@ export function defaultIncrement(exId, unit) {
if (unit === 'lb') return heavy ? 10 : 5
return heavy ? 5 : 2.5
}
// Resolve the load step for reps-mode weight controls and progression. Timed exercises use
// `inc` for seconds, so their optional weight column must not call this helper.
export function weightIncrement(cfg, unit) {
return cfg && cfg.inc > 0 ? cfg.inc : defaultIncrement(cfg?.id, unit)
}
export const DEFAULT_SEC_INCREMENT = 5
// Where adding another set of push-ups stops being progress and starts being a way to spend
// an evening. Past this the honest advice is load or a harder variation (issue #33).
@@ -76,18 +81,22 @@ export function policyFor(cfg, routine, mode) {
}
const round1 = v => Math.round(v * 10) / 10
// Snap to a loadable multiple of the step.
function snap(v, step) {
// Snap to a loadable multiple of the step. Manual weight controls use this same normalization
// so fractional increments produce the same number as automatic progression.
export function snapWeight(v, step) {
if (!(step > 0)) return round1(v)
return round1(Math.round(v / step) * step)
}
export function stepWeight(value, step, direction) {
return Math.max(0, snapWeight((Number(value) || 0) + direction * step, step))
}
// Back off by DELOAD_FACTOR, landing on something you can actually load. Rounding to the
// nearest step keeps the cut close to the intended 10 %, but on small weights the nearest
// step can be the weight you started from — so a deload that did not actually reduce
// anything takes one step down instead. Never goes below a single step.
function deloadTo(cur, step) {
let next = snap(cur * DELOAD_FACTOR, step)
if (next >= cur) next = snap(cur - step, step)
let next = snapWeight(cur * DELOAD_FACTOR, step)
if (next >= cur) next = snapWeight(cur - step, step)
return Math.max(step, next)
}
@@ -167,7 +176,9 @@ export function nextPrescription(S, cfg, routine) {
const mode = modeOf(cfg)
const policy = policyFor(cfg, routine, mode)
const unit = S.unit || 'kg'
const inc = cfg.inc > 0 ? cfg.inc : (mode === 'time' ? DEFAULT_SEC_INCREMENT : defaultIncrement(cfg.id, unit))
const inc = mode === 'time'
? (cfg.inc > 0 ? cfg.inc : DEFAULT_SEC_INCREMENT)
: weightIncrement(cfg, unit)
if (policy === 'off') return { policy, kind: 'off' }
const sessions = sessionsFor(S, cfg.id, cfg).filter(s => s.mode === mode)
@@ -218,7 +229,7 @@ export function nextPrescription(S, cfg, routine) {
const range = normalizeRepRange(cfg.reps || last.goal || 10, cfg.repsMin, repStep(cfg))
const top = range.reps
const bottom = range.repsMin
if (last.ok) return { policy, kind: 'up', weight: snap(w + inc, inc), reps: bottom, why: ['Top of the rep range in every set — {0} {1} more, back to {2} reps.', inc, unit, bottom] }
if (last.ok) return { policy, kind: 'up', weight: snapWeight(w + inc, inc), reps: bottom, why: ['Top of the rep range in every set — {0} {1} more, back to {2} reps.', inc, unit, bottom] }
if (stalls >= deloadAt) {
const dw = deloadTo(w, inc)
return { policy, kind: 'deload', weight: dw, reps: bottom, why: ['Stalled {0} sessions — deload to {1} {2}.', stalls, dw, unit] }
@@ -234,7 +245,7 @@ export function nextPrescription(S, cfg, routine) {
const dbl = policy === 'greyskull' && last.goal > 0 && last.amrap >= last.goal * 2
const step = dbl ? inc * 2 : inc
return {
policy, kind: 'up', weight: snap(w + step, inc),
policy, kind: 'up', weight: snapWeight(w + step, inc),
why: dbl
? ['Last set hit {0} reps — twice the target, so take a double jump of {1} {2}.', last.amrap, step, unit]
: ['Every rep last time — {0} {1} more.', step, unit]
+9 -1
View File
@@ -1,7 +1,7 @@
import { describe, it, expect } from 'vitest'
import {
readSession, sessionsFor, stallCount, nextPrescription, applyPrescription,
policyFor, defaultIncrement, POLICIES_FOR, DELOAD_AFTER, MAX_BW_SETS
policyFor, defaultIncrement, weightIncrement, POLICIES_FOR, DELOAD_AFTER, MAX_BW_SETS
} from './progression.js'
import { EXDB } from './exercises.js'
@@ -103,6 +103,14 @@ describe('defaultIncrement', () => {
})
})
describe('weightIncrement', () => {
it('uses a positive exercise override and otherwise the exercise/unit default', () => {
expect(weightIncrement({ id: LIFT, inc: 1 }, 'kg')).toBe(1)
expect(weightIncrement({ id: LIFT }, 'kg')).toBe(2.5)
expect(weightIncrement({ id: HEAVY, inc: 0 }, 'kg')).toBe(5)
})
})
describe('linear progression', () => {
const cfg = { id: LIFT, sets: 3, reps: 5, weight: 60, prog: 'linear' }
+8 -6
View File
@@ -14,7 +14,7 @@ import Media from '../components/Media.jsx'
import { startFlow, exercisePicker, exConfigSheet, exerciseDetailSheet, topWeightSheet, finishWorkout, workoutCompleteSheet, confirmSheet, exerciseNoteSheet, sessionNoteSheet, swapActiveWorkoutExercise, barWeightSheet } from '../sheets.jsx'
import Icon from '../components/Icon.jsx'
import { Button, Check, NumberField } from '../components/ui.jsx'
import { nextPrescription, applyPrescription, defaultIncrement } from '../lib/progression.js'
import { nextPrescription, applyPrescription, defaultIncrement, weightIncrement, stepWeight } from '../lib/progression.js'
import { progressionGuidance } from '../lib/progression-copy.js'
import { glyphOf } from '../lib/glyphs.js'
import { isWarmupRow, isDropSet, isRestPauseSet, dropsOf, clustersOf, addDrop, addCluster, removeDropAt, removeClusterAt, setDropAt, setClusterAt, nextDropWeight, nextBurstReps } from '../lib/workout-model.js'
@@ -120,7 +120,8 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
const cfg = { ...(entry.target || {}), id: entry.id }
const bw = !cardio && isBw(cfg)
const added = bw && entry.sets.some(s => s.w > 0)
const loadCol = { f: 'w', step: 2.5, dec: true, hd: bw ? t('Added ({0})', S.unit) : t('Weight ({0})', S.unit) }
const loadStep = mode === 'reps' ? weightIncrement(cfg, S.unit) : 2.5
const loadCol = { f: 'w', step: loadStep, dec: true, hd: bw ? t('Added ({0})', S.unit) : t('Weight ({0})', S.unit) }
// The reps column is the total in every mode, unilateral included — the stepper walks in
// twos there so the number you land on is one you can actually split evenly.
const repCol = { f: 'r', step: repStep(cfg), dec: false, hd: t('Reps') }
@@ -147,6 +148,7 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
const fresh = useStore.getState().S.active?.entries[entryIdx]?.sets[i]
const cur = fresh ? fresh[col.f] : s[col.f]
if (col.eff) return onField(i, col.f, stepEffort(col.eff, cur, dir))
if (mode === 'reps' && col.f === 'w') return onField(i, col.f, stepWeight(cur, col.step, dir))
onField(i, col.f, Math.max(0, Math.round(((cur || 0) + dir * col.step) * 100) / 100))
}
// Uses the shared stepper markup so a set row picks up the same control styling
@@ -162,11 +164,11 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
)
// A smaller stepper for a drop's weight/reps or a burst's reps — editing what the plan (or a
// live "+ Drop"/"+ Burst" tap) already put on the row, not typing into a fresh field.
const miniStepper = (value, step, dec, onChange) => (
const miniStepper = (value, step, dec, onChange, snapWeightStep = false) => (
<div className="stp mini">
<button aria-label="Decrease" onClick={() => onChange(Math.max(0, Math.round(((value || 0) - step) * 100) / 100))}><Icon name="minus" /></button>
<button aria-label="Decrease" onClick={() => onChange(snapWeightStep ? stepWeight(value, step, -1) : Math.max(0, Math.round(((value || 0) - step) * 100) / 100))}><Icon name="minus" /></button>
<span className="val"><NumberField decimal={dec} value={value ?? ''} onChange={onChange} /></span>
<button aria-label="Increase" onClick={() => onChange(Math.max(0, Math.round(((value || 0) + step) * 100) / 100))}><Icon name="plus" /></button>
<button aria-label="Increase" onClick={() => onChange(snapWeightStep ? stepWeight(value, step, 1) : Math.max(0, Math.round(((value || 0) + step) * 100) / 100))}><Icon name="plus" /></button>
</div>
)
return <>
@@ -259,7 +261,7 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov
{dropsOf(s).map((d, di) => (
<div className="subrow" key={'d' + di}>
<span className="subn">{t('Drop {0}', di + 1)}</span>
{miniStepper(d.w, 2.5, true, v => setDropField(i, di, 'w', v))}
{miniStepper(d.w, loadStep, true, v => setDropField(i, di, 'w', v), true)}
{miniStepper(d.r, 1, false, v => setDropField(i, di, 'r', v))}
<button className="iconbtn" aria-label={t('Remove drop')} onClick={() => removeDrop(i, di)}><Icon name="xmark" /></button>
</div>
+64
View File
@@ -3,6 +3,7 @@ import { createRoot } from 'react-dom/client'
import { parseHTML } from 'linkedom'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import Workout from './Workout.jsx'
import { nextPrescription } from '../lib/progression.js'
const mocks = vi.hoisted(() => {
const state = {
@@ -401,6 +402,69 @@ describe('Workout add exercise flow', () => {
})
})
describe('active workout weight controls', () => {
const press = async (label, selector) => {
const control = container.querySelector(selector)
const button = control?.querySelector(`button[aria-label="${label}"]`)
expect(button).toBeTruthy()
await act(async () => { button.dispatchEvent(new dom.Event('click', { bubbles: true })) })
await rerender()
}
it('uses the configured reps weight step for manual increases and decreases, with the default fallback', async () => {
await mount([exercise('plain-bench', [false], {
target: { mode: 'reps', reps: 5, weight: 60, bodyweight: false, inc: 1 },
})])
await press('Increase', '.setrow .stp.w')
expect(mocks.S.active.entries[0].sets[0].w).toBe(61)
await press('Decrease', '.setrow .stp.w')
expect(mocks.S.active.entries[0].sets[0].w).toBe(60)
await unmount()
await mount([exercise('plain-bench', [false])])
await press('Increase', '.setrow .stp.w')
expect(mocks.S.active.entries[0].sets[0].w).toBe(62.5)
})
it('matches automatic progression rounding for a fractional configured step', async () => {
const target = { mode: 'reps', sets: 1, reps: 5, weight: 60, bodyweight: false, inc: 1.25 }
const automatic = nextPrescription({
unit: 'kg',
workouts: [{ d: '2026-08-30', entries: [{ id: 'plain-bench', target, sets: [{ w: 60, r: 5, done: true }] }] }],
}, { id: 'plain-bench', ...target })
await mount([exercise('plain-bench', [false], { target, sets: [{ w: 60, r: 5, done: false }] })])
await press('Increase', '.setrow .stp.w')
expect(automatic.weight).toBe(61.3)
expect(mocks.S.active.entries[0].sets[0].w).toBe(automatic.weight)
})
it('uses the configured reps weight step for drop-set weight controls', async () => {
await mount([exercise('plain-bench', [false], {
target: { mode: 'reps', reps: 5, weight: 60, bodyweight: false, inc: 1 },
sets: [{ w: 60, r: 5, done: false, type: 'dropset', drops: [{ w: 50, r: 5 }] }],
})])
await press('Increase', '.subrow .stp')
expect(mocks.S.active.entries[0].sets[0].drops[0].w).toBe(51)
})
it('keeps timed seconds and optional timed weight on their existing steps', async () => {
await mount([exercise('timed-plank', [false], {
target: { mode: 'time', sec: 30, weight: 60, bodyweight: false, inc: 1 },
sets: [{ sec: 30, w: 60, done: false }],
})])
await press('Increase', '.setrow .stp.w')
expect(mocks.S.active.entries[0].sets[0].sec).toBe(35)
await press('Increase', '.setrow .stp.r')
expect(mocks.S.active.entries[0].sets[0].w).toBe(62.5)
})
})
describe('Workout discard timer lifecycle', () => {
it('preserves active timers while discard is awaiting confirmation', async () => {
const timer = { left: 30, total: 90, endsAt: Date.now() + 30_000 }