Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Merge merge request !42 (workout flow after adding an exercise mid-session)

Duarte Santos358d39590a71
diff --cc frontend/src/views/Workout.test.jsx
index 18ac7bfb1,60fa2881e..e094180ae
--- a/frontend/src/views/Workout.test.jsx
+++ b/frontend/src/views/Workout.test.jsx
@@@ -7,29 -7,29 +7,37 @@@ import Workout from './Workout.jsx
const mocks = vi.hoisted(() => {
const state = {
S: null,
+ timer: null,
+ work: null,
startRest: vi.fn(),
- stopRest: vi.fn(),
+ stopRest: null,
+ stopWork: null,
+ confirmSheet: vi.fn(),
topWeightSheet: vi.fn(),
+ workoutCompleteSheet: vi.fn(),
+ exercisePicker: vi.fn(),
+ exConfigSheet: vi.fn(),
+ toast: vi.fn(),
}
+ state.stopRest = vi.fn(() => { state.timer = null })
+ state.stopWork = vi.fn(() => { state.work = null })
state.storeSnapshot = () => ({
S: state.S,
user: null,
- update: mut => mut(state.S),
+ update: mut => {
+ const next = structuredClone(state.S)
+ mut(next)
+ state.S = next
+ },
})
state.uiSnapshot = () => ({
- work: null,
+ timer: state.timer,
+ work: state.work,
startRest: state.startRest,
stopRest: state.stopRest,
+ stopWork: state.stopWork,
startWork: vi.fn(),
- toast: vi.fn(),
+ toast: state.toast,
})
return state
})
@@@ -52,8 -52,8 +60,8 @@@ vi.mock('../sheets.jsx', () => (
exerciseDetailSheet: vi.fn(),
topWeightSheet: mocks.topWeightSheet,
finishWorkout: vi.fn(),
- workoutCompleteSheet: vi.fn(),
+ workoutCompleteSheet: mocks.workoutCompleteSheet,
- confirmSheet: vi.fn(),
+ confirmSheet: mocks.confirmSheet,
// Both note sheets belong here even though the tests never open one: Workout.jsx reads
// sessionNoteSheet during render, so a missing export is a render crash, not a no-op.
exerciseNoteSheet: vi.fn(),
@@@ -120,16 -122,27 +130,35 @@@ async function toggleSet(index)
await act(async () => { checkbox.dispatchEvent(new dom.Event('click', { bubbles: true })) })
}
+async function requestDiscard() {
+ const button = container.querySelector('button[aria-label="Discard"]')
+ expect(button).toBeTruthy()
+ await act(async () => { button.dispatchEvent(new dom.Event('click', { bubbles: true })) })
+}
+
+ async function rerender() {
+ await act(async () => { root.render(React.createElement(Workout)) })
+ }
+
+ async function addExerciseThroughSheets(ex = { id: 'added-exercise' }, cfg = { mode: 'reps', sets: 1, reps: 5, weight: 0 }) {
+ const addButton = [...container.querySelectorAll('button')]
+ .find(button => button.textContent.trim() === 'Add exercise')
+ expect(addButton).toBeTruthy()
+ await act(async () => { addButton.dispatchEvent(new dom.Event('click', { bubbles: true })) })
+
+ const pickerCall = mocks.exercisePicker.mock.calls.at(-1)
+ expect(pickerCall?.[0]).toEqual(expect.any(Function))
+ await act(async () => { pickerCall[0](ex) })
+
+ const configCall = mocks.exConfigSheet.mock.calls.at(-1)
+ expect(configCall?.[2]).toEqual(expect.any(Function))
+ await act(async () => { configCall[2](cfg) })
+ }
+
beforeEach(() => {
vi.clearAllMocks()
+ mocks.timer = null
+ mocks.work = null
})
afterEach(async () => {
@@@ -169,42 -261,110 +277,144 @@@ describe('Workout set completion flow'
expect(mocks.S.active.cur).toBe(1)
expect(mocks.startRest).toHaveBeenCalledWith(90)
})
+
+ it('skips completed intervening units and selects the next unfinished superset as one unit', async () => {
+ await mount([
+ exercise('current-hold', [false], { asked: true, target: { mode: 'time', sec: 30, weight: 0 } }),
+ exercise('already-done', [true], { asked: true }),
+ exercise('pending-a', [false], { sg: 'pending-group' }),
+ exercise('pending-b', [false], { sg: 'pending-group' }),
+ ])
+
+ await toggleSet(0)
+
+ expect(mocks.S.active.cur).toBe(2)
+ expect(mocks.workoutCompleteSheet).not.toHaveBeenCalled()
+ expect(mocks.toast).toHaveBeenCalledWith('Hold logged')
+ expect(mocks.startRest).toHaveBeenCalledWith(90)
+ })
+
+ it('wraps to earlier unfinished work instead of showing a false completion prompt', async () => {
+ await mount([
+ exercise('pending-earlier', [false], { asked: true }),
+ exercise('current-hold', [false], { asked: true, target: { mode: 'time', sec: 30, weight: 0 } }),
+ ], 1)
+
+ await toggleSet(0)
+
+ expect(mocks.S.active.cur).toBe(0)
+ expect(mocks.workoutCompleteSheet).not.toHaveBeenCalled()
+ expect(mocks.startRest).toHaveBeenCalledWith(90)
+ })
+
+ it('keeps top-weight confirmation in control without declaring completion while work remains', async () => {
+ await mount([
+ exercise('current-loaded', [false]),
+ exercise('pending', [false], { asked: true }),
+ ])
+
+ await toggleSet(0)
+
+ expect(mocks.topWeightSheet).toHaveBeenCalledWith(0)
+ expect(mocks.workoutCompleteSheet).not.toHaveBeenCalled()
+ expect(mocks.S.active.cur).toBe(0)
+ })
+
+ it('shows workout completion only when no unfinished unit remains', async () => {
+ await mount([
+ exercise('already-done', [true], { asked: true }),
+ exercise('final-hold', [false], { asked: true, target: { mode: 'time', sec: 30, weight: 0 } }),
+ ], 1)
+
+ await toggleSet(0)
+
+ expect(mocks.workoutCompleteSheet).toHaveBeenCalledOnce()
+ expect(mocks.startRest).not.toHaveBeenCalled()
+ })
+ })
+
+ describe('Workout add exercise flow', () => {
+ it.each([
+ ['freestyle', {}],
+ ['planned', {
+ active: { routineId: 'routine-1' },
+ routines: [{ id: 'routine-1', ex: [] }],
+ }],
+ ])('inserts after the current unit and continues to pending work in a %s session', async (_label, overrides) => {
+ await mount([
+ exercise('current', [true], { asked: true }),
+ exercise('pending', [false], { asked: true }),
+ ], 0, overrides)
+
+ await addExerciseThroughSheets(
+ { id: 'inserted' },
+ { mode: 'time', sets: 1, sec: 30, weight: 0 },
+ )
+
+ expect(mocks.S.active.entries.map(entry => entry.id)).toEqual(['current', 'inserted', 'pending'])
+ expect(mocks.S.active.cur).toBe(1)
+
+ await rerender()
+ await toggleSet(0)
+
+ expect(mocks.S.active.entries[1].sets[0].done).toBe(true)
+ expect(mocks.S.active.cur).toBe(2)
+ expect(mocks.workoutCompleteSheet).not.toHaveBeenCalled()
+ })
+
+ it('inserts after the complete current superset without splitting the group', async () => {
+ await mount([
+ exercise('current-a', [true], { sg: 'current-group', asked: true }),
+ exercise('current-b', [true], { sg: 'current-group', asked: true }),
+ exercise('pending', [false], { asked: true }),
+ ])
+
+ await addExerciseThroughSheets({ id: 'inserted' })
+
+ expect(mocks.S.active.entries.map(entry => entry.id)).toEqual([
+ 'current-a', 'current-b', 'inserted', 'pending',
+ ])
+ expect(mocks.S.active.entries.slice(0, 2).map(entry => entry.sg)).toEqual([
+ 'current-group', 'current-group',
+ ])
+ expect(mocks.S.active.cur).toBe(2)
+ })
})
+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 }
+ const work = { left: 20, total: 45, endsAt: Date.now() + 20_000, label: 'Plank' }
+ mocks.timer = timer
+ mocks.work = work
+ await mount([exercise('timed-plank', [false])])
+
+ await requestDiscard()
+
+ expect(mocks.confirmSheet).toHaveBeenCalledOnce()
+ expect(mocks.timer).toBe(timer)
+ expect(mocks.work).toBe(work)
+ expect(mocks.stopRest).not.toHaveBeenCalled()
+ expect(mocks.stopWork).not.toHaveBeenCalled()
+ expect(mocks.S.active).not.toBeNull()
+ })
+
+ it('clears rest and work timers only after discard is confirmed', async () => {
+ mocks.timer = { left: 30, total: 90, endsAt: Date.now() + 30_000 }
+ mocks.work = { left: 20, total: 45, endsAt: Date.now() + 20_000, label: 'Plank' }
+ await mount([exercise('timed-plank', [false])])
+ await requestDiscard()
+
+ await act(async () => { mocks.confirmSheet.mock.calls[0][0].onConfirm() })
+
+ expect(mocks.S.active).toBeNull()
+ expect(mocks.timer).toBeNull()
+ expect(mocks.work).toBeNull()
+ expect(mocks.stopRest).toHaveBeenCalledOnce()
+ expect(mocks.stopWork).toHaveBeenCalledOnce()
+ })
+})
+
describe('superset flow survives an exercise being removed mid-session', () => {
// removeActiveExercise splices A.entries, shifting every index above the removal down.
// The high-water marks are index-keyed, so without re-baselining the shifted exercise