From 03a5bd22fffbea16bd25d08c7ea2e1f5b20321bf Mon Sep 17 00:00:00 2001 From: Space_Hermes Date: Thu, 27 Aug 2026 19:34:08 +0200 Subject: [PATCH] fix: stop rest timer when removing active exercise --- frontend/src/views/Workout.jsx | 5 +- frontend/src/views/Workout.remove.test.jsx | 92 ++++++++++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/frontend/src/views/Workout.jsx b/frontend/src/views/Workout.jsx index d0a6279..97712f8 100644 --- a/frontend/src/views/Workout.jsx +++ b/frontend/src/views/Workout.jsx @@ -258,9 +258,10 @@ function ExerciseBlock({ entryIdx, compact, onToggle, onField, onAddSet, onRemov /* ---------- active workout ---------- */ export function removeActiveExercise(idx) { - // Clear the work callback before indexes can shift. This also protects a confirmation sheet - // that was opened first and confirmed after a timed hold started. + // Clear timer state and callbacks before indexes can shift. This also protects a confirmation + // sheet that was opened first and confirmed after a timed hold or rest countdown started. useUI.getState().stopWork() + useUI.getState().stopRest() useStore.getState().update(s => { if (!s.active || !Array.isArray(s.active.entries)) return if (idx < 0 || idx >= s.active.entries.length) return diff --git a/frontend/src/views/Workout.remove.test.jsx b/frontend/src/views/Workout.remove.test.jsx index 2b50bf8..526fbe0 100644 --- a/frontend/src/views/Workout.remove.test.jsx +++ b/frontend/src/views/Workout.remove.test.jsx @@ -21,6 +21,8 @@ const entry = (id, sg) => ({ let root let container +let sheetRoot +let sheetContainer function setActive(entries, cur = 0) { const S = clone(DEF) @@ -43,6 +45,18 @@ function removeButton() { return [...container.querySelectorAll('button')].find(button => button.textContent.includes('Remove exercise')) } +function renderTopSheet() { + if (sheetRoot) act(() => sheetRoot.unmount()) + if (sheetContainer) sheetContainer.remove() + const sheet = useUI.getState().sheets.at(-1) + expect(sheet).toBeTruthy() + sheetContainer = document.createElement('div') + document.body.appendChild(sheetContainer) + sheetRoot = createRoot(sheetContainer) + act(() => sheetRoot.render(sheet.render(() => useUI.getState().closeSheet(sheet.id)))) + return sheetContainer +} + beforeEach(() => { vi.useFakeTimers() localStorage.clear() @@ -52,9 +66,13 @@ beforeEach(() => { useStore.setState({ S: clone(DEF), user: null }) root = null container = null + sheetRoot = null + sheetContainer = null }) afterEach(() => { + if (sheetRoot) act(() => sheetRoot.unmount()) + if (sheetContainer) sheetContainer.remove() if (root) act(() => root.unmount()) if (container) container.remove() useUI.getState().stopRest() @@ -94,6 +112,58 @@ describe('active-session exercise removal', () => { expect(active.entries[0].sets[0].sec).toBeUndefined() }) + it('cancels a running rest countdown before removing the selected occurrence', () => { + setActive([entry('1001'), entry('1002')], 0) + useUI.getState().startRest(90) + expect(useUI.getState().timer).not.toBeNull() + + act(() => { removeActiveExercise(0) }) + + expect(useUI.getState().timer).toBeNull() + expect(useStore.getState().S.active.entries.map(e => e.id)).toEqual(['1002']) + }) + + it('does not mutate before confirmation, leaves state unchanged on Cancel, and removes on Confirm', () => { + renderWorkout([entry('1001'), entry('1002')]) + + act(() => removeButton().click()) + expect(useStore.getState().S.active.entries.map(e => e.id)).toEqual(['1001', '1002']) + + let dialog = renderTopSheet() + const cancel = [...dialog.querySelectorAll('button')].find(button => button.textContent === 'Cancel') + act(() => cancel.click()) + expect(useStore.getState().S.active.entries.map(e => e.id)).toEqual(['1001', '1002']) + + act(() => removeButton().click()) + dialog = renderTopSheet() + const confirm = [...dialog.querySelectorAll('button')].find(button => button.textContent === 'Remove') + act(() => confirm.click()) + expect(useStore.getState().S.active.entries.map(e => e.id)).toEqual(['1002']) + }) + + it('uses the selected superset member occurrence rather than its exercise id', () => { + renderWorkout([entry('1001', 'pair'), entry('1001', 'pair'), entry('1002')]) + useStore.getState().update(s => { + s.active.entries[0].target.marker = 'keep-first' + s.active.entries[1].target.marker = 'remove-second' + }, false) + + act(() => removeButton().click()) + const chooser = renderTopSheet() + const choices = [...chooser.querySelectorAll('.item')] + expect(choices).toHaveLength(2) + act(() => choices[1].click()) + + const dialog = renderTopSheet() + const confirm = [...dialog.querySelectorAll('button')].find(button => button.textContent === 'Remove') + act(() => confirm.click()) + + const active = useStore.getState().S.active + expect(active.entries.map(e => e.id)).toEqual(['1001', '1002']) + expect(active.entries[0].target.marker).toBe('keep-first') + expect(active.entries[0].sg).toBeUndefined() + }) + it('hides the remove control for an empty freestyle session', () => { renderWorkout([]) expect(removeButton()).toBeUndefined() @@ -143,4 +213,26 @@ describe('remove-exercise edge cases', () => { expect(A.entries.map(e => e.id)).toEqual(['b', 'c']) expect(A.entries[A.cur].id).toBe('c') }) + + it('persists the shortened active session for reload without changing completed history', () => { + const first = entry('same') + first.target.marker = 'keep-first' + const second = entry('same') + second.target.marker = 'remove-second' + setActive([first, second, entry('other')], 1) + const workouts = [{ id: 'completed', d: '2026-08-10', entries: [{ id: 'same', sets: [{ w: 42, r: 5, done: true }] }] }] + useStore.setState(state => ({ S: { ...state.S, workouts: clone(workouts) } })) + + act(() => { removeActiveExercise(1) }) + + const persisted = JSON.parse(localStorage.getItem('gym_state_v1')) + expect(persisted.workouts).toEqual(workouts) + expect(persisted.active.entries.map(e => e.id)).toEqual(['same', 'other']) + expect(persisted.active.entries[0].target.marker).toBe('keep-first') + expect(persisted.active.cur).toBe(1) + + useStore.setState({ S: Object.assign(clone(DEF), persisted), user: null }) + expect(useStore.getState().S.active.entries.map(e => e.id)).toEqual(['same', 'other']) + expect(useStore.getState().S.workouts).toEqual(workouts) + }) })