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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Follow-up to #209: a finished workout keeps only the loads, not the mark that kept a typed load from being cascaded over, on a row or on either side
Duarte Santos50b9e2ff2364
diff --git a/frontend/src/lib/finish-workout.js b/frontend/src/lib/finish-workout.jsindex eb46ffd2d..7f479f1d3 100644--- a/frontend/src/lib/finish-workout.js+++ b/frontend/src/lib/finish-workout.js@@ -1,22 +1,39 @@// The persisted boundary for a finished session. Keep this pure so compatibility tests can// exercise the exact shape the UI writes without mounting React or mutating store state.import { bestWeightForEntry } from './history.js'-import { hasCompletedWork } from './workout-model.js'+import { hasCompletedWork, isSideSet } from './workout-model.js'++// planSec is live-session bookkeeping: a hold displaced before it finished puts its plan aside so+// the row still knows what it is asking for (Workout.startTimed). A finished session keeps only+// what was logged, so the key never reaches S.workouts — an unfinished row goes back to recording+// its plan, exactly as it did before any of this.+//+// weightOrigin is the same kind of bookkeeping (#209): it marks a load typed by hand so a later+// edit of an earlier set does not cascade over it. Once the session is over there is nothing left+// to cascade, and every load in history is simply the one logged. Kept, it would ride into the+// saved workout, the server copy and every backup as a field nothing reads — on the row and on+// each side of a per-side row, where the side keeps its own.+//+// Rows that carry neither are passed through by reference, so an ordinary session is the shape+// it always was.+function finishedRow(set) {+ if (!set) return set+ const sideMarked = isSideSet(set) && (set.sides.L.weightOrigin != null || set.sides.R.weightOrigin != null)+ if (set.planSec == null && set.weightOrigin == null && !sideMarked) return set+ const { planSec, weightOrigin, ...rest } = set+ if (sideMarked) {+ const side = ({ weightOrigin: _, ...kept }) => kept+ rest.sides = { ...rest.sides, L: side(rest.sides.L), R: side(rest.sides.R) }+ }+ return planSec == null || rest.done ? rest : { ...rest, sec: planSec }+}export function buildCompletedWorkout(active, { end = Date.now(), prs = [], snapshotFor } = {}) {const entries = (active?.entries || []).map(entry => {const completed = {id: entry.id,- // planSec is live-session bookkeeping: a hold displaced before it finished puts its plan- // aside so the row still knows what it is asking for (Workout.startTimed). A finished- // session keeps only what was logged, so the key never reaches S.workouts — an unfinished- // row goes back to recording its plan, exactly as it did before any of this. Rows without- // it are passed through by reference, so an ordinary session is the shape it always was.- sets: (entry.sets || []).map(set => {- if (!set || set.planSec == null) return set- const { planSec, ...rest } = set- return rest.done ? rest : { ...rest, sec: planSec }- }),+ // Only what was logged — the live-session bookkeeping on a row stays behind (finishedRow).+ sets: (entry.sets || []).map(finishedRow),topW: bestWeightForEntry(entry) || null,target: entry.target || null,// Which routine this entry came from, and whether it counts for progression. Writtendiff --git a/frontend/src/lib/finish-workout.test.js b/frontend/src/lib/finish-workout.test.jsindex 18dc93767..112bc4e60 100644--- a/frontend/src/lib/finish-workout.test.js+++ b/frontend/src/lib/finish-workout.test.js@@ -1,5 +1,8 @@import { describe, expect, it } from 'vitest'import { buildCompletedWorkout } from './finish-workout.js'+import { cascadeWeight } from './history.js'+import { buildSessionEntries } from './session-start.js'+import { makeSideSet, setSideField, toggleSide, WEIGHT_ORIGIN_MANUAL } from './workout-model.js'describe('completed workout boundary', () => {it('builds the same legacy-shaped record doFinishWorkout stores and keeps it visible', () => {@@ -40,6 +43,42 @@ describe('completed workout boundary', () => {expect(JSON.stringify(sets)).not.toContain('planSec')})+ // weightOrigin marks a load typed by hand during the session so a later edit of an earlier set+ // does not cascade over it (#209). History has nothing left to cascade: the saved rows carry+ // only the loads, and the plan the session was built from (`planned`) is kept as it was.+ it('never stores weightOrigin, on a row or on either side, and keeps the plan stamps', () => {+ const planned = { sets: 3, reps: 5, weight: 100 }+ let rows = [{ w: 100, r: 5, done: true }, { w: 100, r: 5, done: false }, { w: 100, r: 5, done: false }]+ rows[2] = { ...rows[2], w: 95, weightOrigin: WEIGHT_ORIGIN_MANUAL }+ rows[1] = { ...rows[1], w: 105, weightOrigin: WEIGHT_ORIGIN_MANUAL }+ rows = cascadeWeight(rows, 1, 105)+ expect(rows[2]).toMatchObject({ w: 95, weightOrigin: WEIGHT_ORIGIN_MANUAL }) // a manual row stays put+ rows = rows.map(row => ({ ...row, done: true }))+ let side = setSideField(makeSideSet({ w: 20, r: 16 }), 'L', 'w', 22.5)+ side.sides.L.weightOrigin = WEIGHT_ORIGIN_MANUAL+ side = toggleSide(toggleSide(side, 'L'), 'R')++ const active = {+ id: 'a', d: '2026-09-18', start: 1000, routineIds: ['r'],+ entries: [+ { id: '0025', rid: 'r', target: { ...planned }, planned, sets: rows },+ { id: 'lunge', rid: 'r', target: { sets: 1, reps: 16, side: true }, sets: [side] },+ ],+ }+ const saved = buildCompletedWorkout(active, { end: 2000 })+ expect(JSON.stringify(saved)).not.toContain('weightOrigin')+ expect(saved.entries[0].sets.map(s => s.w)).toEqual([100, 105, 95])+ expect(saved.entries[0].planned).toEqual(planned)+ expect(saved.entries[1].sets[0].sides).toMatchObject({ L: { w: 22.5, done: true }, R: { w: 20, done: true } })+ // The live rows keep their marks until the session is over.+ expect(active.entries[0].sets[2].weightOrigin).toBe(WEIGHT_ORIGIN_MANUAL)++ // The next session of the plan reads the loads back and carries no mark of its own.+ const st = { unit: 'kg', exWeights: {}, routines: [{ id: 'r', ex: [{ id: '0025', ...planned }] }], workouts: [saved] }+ const next = buildSessionEntries(st, st.routines[0])[0]+ expect(JSON.stringify(next.sets)).not.toContain('weightOrigin')+ })+it('leaves the rows it has nothing to strip exactly as they are', () => {const rows = [{ done: true, w: 60, r: 8 }]const active = { id: 'a', d: '2026-09-18', start: 1000, entries: [{ id: '0025', sets: rows }] }