From e57eac027fd874b9fc17cbdd5f5a03101dddc347 Mon Sep 17 00:00:00 2001 From: "sebastian.goendoer" Date: Thu, 3 Sep 2026 21:40:11 +0200 Subject: [PATCH 1/2] fix: read fresh store value in set-stepper bump to prevent stale closure in supersets In a superset both ExerciseBlock instances subscribe to the same store slice. When one member's field is updated the store clone invalidates all entry references, causing both instances to re-render. The +/- button handlers were closing over the set object from the previous render, so the first tap wrote the correct value but the replacement closure made the next tap operate on stale data -- requiring two taps per visible increment instead of one. Fix: read the current field value directly from the store at call time instead of relying on the render-time snapshot. --- frontend/src/views/Workout.jsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx index a71bfc0..7206b16 100644 --- a/frontend/src/views/Workout.jsx +++ b/frontend/src/views/Workout.jsx @@ -139,8 +139,15 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov // The effort column walks its own scale — see stepEffort. Weight and reps step up from 0 // with no ceiling, as they always did. const bump = (s, i, col, dir) => { - if (col.eff) return onField(i, col.f, stepEffort(col.eff, s[col.f], dir)) - onField(i, col.f, Math.max(0, Math.round(((s[col.f] || 0) + dir * col.step) * 100) / 100)) + // Read the current value directly from the store rather than from the render-time snapshot. + // In a superset both ExerciseBlock instances share the same store subscription, so when one + // member's field changes the partner re-renders too, replacing the closure's `s` reference + // with a fresh clone before the next tap fires. Reading from the store avoids that stale- + // closure problem entirely and keeps every tap operating on the real current value. + 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)) + 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 // as every other +/- field in the app. From eb59240e2f04cc4f883a4e00b8879015c6a94637 Mon Sep 17 00:00:00 2001 From: Sebastian G Date: Fri, 4 Sep 2026 10:46:53 +0200 Subject: [PATCH 2/2] fix: superset RIR counter fixed --- frontend/src/views/Workout.stepper.test.jsx | 206 ++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 frontend/src/views/Workout.stepper.test.jsx diff --git a/frontend/src/views/Workout.stepper.test.jsx b/frontend/src/views/Workout.stepper.test.jsx new file mode 100644 index 0000000..4b849d6 --- /dev/null +++ b/frontend/src/views/Workout.stepper.test.jsx @@ -0,0 +1,206 @@ +import React, { act } from 'react' +import { createRoot } from 'react-dom/client' +import { parseHTML } from 'linkedom' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import Workout from './Workout.jsx' + +// This suite pins down the superset stepper regression: one tap on a +/- button must move the +// value by exactly one step. The bug was a stale closure — in a superset both ExerciseBlock +// instances share the same store subscription, so updating one member's field re-renders the +// partner and replaces the render-time `s` snapshot the button handler had closed over. The +// fix reads the current value from the store at call time. The mocked `update` below clones the +// whole state on every write (structuredClone), which is exactly what invalidates those +// snapshots, so this environment reproduces the original two-taps-per-increment behaviour. +const mocks = vi.hoisted(() => { + const state = { + S: null, + startRest: vi.fn(), + stopRest: vi.fn(), + stopWork: vi.fn(), + toast: vi.fn(), + } + state.storeSnapshot = () => ({ + S: state.S, + user: null, + update: mut => { + const next = structuredClone(state.S) + mut(next) + state.S = next + }, + }) + state.uiSnapshot = () => ({ + timer: null, + work: null, + startRest: state.startRest, + stopRest: state.stopRest, + stopWork: state.stopWork, + shiftRestOwner: vi.fn(), + startWork: vi.fn(), + toast: state.toast, + }) + return state +}) + +vi.mock('../store/useStore.js', () => { + const useStore = selector => selector(mocks.storeSnapshot()) + useStore.getState = mocks.storeSnapshot + return { useStore } +}) +vi.mock('../store/useUI.js', () => { + const useUI = selector => selector ? selector(mocks.uiSnapshot()) : mocks.uiSnapshot() + useUI.getState = mocks.uiSnapshot + return { useUI } +}) +vi.mock('react-router-dom', () => ({ useNavigate: () => () => {} })) +vi.mock('../sheets.jsx', () => ({ + startFlow: vi.fn(), + exercisePicker: vi.fn(), + exConfigSheet: vi.fn(), + exerciseDetailSheet: vi.fn(), + topWeightSheet: vi.fn(), + finishWorkout: vi.fn(), + workoutCompleteSheet: vi.fn(), + confirmSheet: vi.fn(), + swapActiveWorkoutExercise: vi.fn(), + barWeightSheet: vi.fn(), + exerciseNoteSheet: vi.fn(), + sessionNoteSheet: vi.fn(), +})) +vi.mock('../components/Media.jsx', () => ({ default: () => null })) +vi.mock('../lib/api.js', () => ({ + api: vi.fn(() => Promise.resolve({})), + IS_APPLE: false, IS_ANDROID: false, BIO: 'biometrics', +})) + +let dom +let root +let container + +function exercise(id, sets, extra = {}) { + return { + id, + target: { mode: 'reps', reps: 5, weight: 60, bodyweight: false }, + sets: sets.map(done => ({ w: 60, r: 5, done })), + ...extra, + } +} + +function workout(entries, cur = 0) { + return { + unit: 'kg', restSec: 90, sound: false, effort: 'none', gifSize: 'full', + workouts: [], exWeights: {}, routines: [], + active: { id: 'active', name: 'Test workout', start: Date.now(), cur, entries }, + } +} + +function installDom() { + const parsed = parseHTML('
') + dom = parsed.window + globalThis.window = dom + globalThis.document = dom.document + Object.defineProperty(globalThis, 'navigator', { configurable: true, value: dom.navigator }) + for (const key of ['HTMLElement', 'Node', 'Element', 'Event', 'Blob']) globalThis[key] = dom[key] + dom.Element.prototype.scrollIntoView = vi.fn() + globalThis.IS_REACT_ACT_ENVIRONMENT = true + container = document.getElementById('root') + root = createRoot(container) +} + +async function mount(entries, cur = 0) { + mocks.S = workout(entries, cur) + installDom() + await act(async () => { root.render(React.createElement(Workout)) }) +} + +async function unmount() { + if (!root) return + await act(async () => { root.unmount() }) + root = null + container = null + dom = null +} + +// The +/- controls live in `.stp` cells, one per editable column. For a plain reps set the +// order is [weight, reps]; this returns the Increase/Decrease button for a given column of a +// given set row inside a given exercise block (addressed by its data-exidx wrapper). +function stepperButton(exidx, setRow, col, direction) { + const block = container.querySelector(`[data-exidx="${exidx}"]`) + expect(block).toBeTruthy() + const row = block.querySelectorAll('.setrow')[setRow] + expect(row).toBeTruthy() + const cell = row.querySelectorAll('.stp')[col] + expect(cell).toBeTruthy() + return cell.querySelector(`button[aria-label="${direction}"]`) +} + +async function tap(button) { + expect(button).toBeTruthy() + await act(async () => { button.dispatchEvent(new dom.Event('click', { bubbles: true })) }) +} + +beforeEach(() => { + vi.clearAllMocks() +}) + +afterEach(async () => { + await unmount() +}) + +describe('superset stepper — one tap moves one step', () => { + // Weight steps by 2.5. A single tap on the first exercise of a superset must land on 62.5, + // not stay at 60 waiting for a second tap. + it('increments a superset member weight by one step per tap', async () => { + await mount([ + exercise('press', [false], { sg: 'group' }), + exercise('row', [false], { sg: 'group' }), + ]) + + await tap(stepperButton(0, 0, 0, 'Increase')) + + expect(mocks.S.active.entries[0].sets[0].w).toBe(62.5) + }) + + // Reps step by 1 and do not cascade, so they isolate the stepper from weight's carry-through. + // Three taps must reach 8 (5 -> 6 -> 7 -> 8); the stale closure made every tap after the + // first re-apply against last render's value, so the count would lag behind the taps. + it('advances reps by exactly one per tap across repeated taps', async () => { + await mount([ + exercise('press', [false], { sg: 'group' }), + exercise('row', [false], { sg: 'group' }), + ]) + + await tap(stepperButton(0, 0, 1, 'Increase')) + await tap(stepperButton(0, 0, 1, 'Increase')) + await tap(stepperButton(0, 0, 1, 'Increase')) + + expect(mocks.S.active.entries[0].sets[0].r).toBe(8) + }) + + // The partner exercise shares the subscription that gets replaced on every write. Tapping its + // stepper after the first exercise has already been bumped must still act on the partner's own + // current value, not a snapshot from before the clone. + it('steps the partner exercise correctly after the first member was changed', async () => { + await mount([ + exercise('press', [false], { sg: 'group' }), + exercise('row', [false], { sg: 'group' }), + ]) + + await tap(stepperButton(0, 0, 1, 'Increase')) + await tap(stepperButton(1, 0, 1, 'Increase')) + + expect(mocks.S.active.entries[0].sets[0].r).toBe(6) + expect(mocks.S.active.entries[1].sets[0].r).toBe(6) + }) + + // Decrement walks the same path and must not undershoot or need a double tap either. + it('decrements a superset member weight by one step per tap', async () => { + await mount([ + exercise('press', [false], { sg: 'group' }), + exercise('row', [false], { sg: 'group' }), + ]) + + await tap(stepperButton(0, 0, 0, 'Decrease')) + + expect(mocks.S.active.entries[0].sets[0].w).toBe(57.5) + }) +})