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.

Follow-up to #211: a routine saved from a planned session takes the plan's sets and reps and its routine's rule, and none of the session's own stamps, so its first session continues instead of restarting

Duarte Santos0ccd5e029330
diff --git a/frontend/src/lib/session-routines.js b/frontend/src/lib/session-routines.js
index 79e3e54f9..62d96c77a 100644
--- a/frontend/src/lib/session-routines.js
+++ b/frontend/src/lib/session-routines.js
@@ -1,5 +1,5 @@
import { uid } from './format.js'
-import { modeOf, defaultConfig, isPerSide } from './history.js'
+import { modeOf, defaultConfig, isPerSide, entryRoutineId } from './history.js'
import { isSideSet, isWarmupRow } from './workout-model.js'
// A saved workout is evidence of what was logged, not a live routine. Copy only its flat
@@ -61,7 +61,7 @@ function perSideWeight(row) {
return weights.length ? Math.max(...weights) : null
}
-function copiedEntry(entry) {
+function copiedEntry(entry, source) {
const rows = Array.isArray(entry?.sets) ? entry.sets : []
const work = rows.filter(row => !isWarmupRow(row))
// A saved entry can retain planned rows after an early finish. Match history.js lastEntryFor:
@@ -85,22 +85,57 @@ function copiedEntry(entry) {
if (isPerSide(cfg) && row == null) cfg.reps = evenTotal(cfg.reps) ?? cfg.reps
}
+ // A planned entry says what its routine asked for (`planned`, session-start.js), and the plan
+ // owns a planned session's sets and reps. Today's target is the prescription and the rows are
+ // what was done with it: a double-progression aim inside the range, a set the bodyweight
+ // ceiling added, a bonus set, or reps carried over from last time. Copied as the new plan,
+ // each of those reads as an edited plan on the copy's first session (nextPrescription):
+ // "Plan changed", a range narrowed to the reps of one day, the climb started again. The copy
+ // takes the plan's sets and reps instead, so it continues from where the source routine
+ // stands. The weight is still the one lifted — history decides it either way.
+ const planned = entry?.planned && typeof entry.planned === 'object' ? entry.planned : null
+ if (planned) {
+ cfg.sets = Math.max(1, positive(planned.sets) || 1)
+ if (mode === 'reps' && positive(planned.reps) != null) {
+ cfg.reps = planned.reps
+ // A plan with no range has no bottom to keep, whatever the target carried.
+ if (positive(planned.repsMin) != null) cfg.repsMin = planned.repsMin
+ else delete cfg.repsMin
+ }
+ if (mode === 'time' && positive(planned.sec) != null) cfg.sec = planned.sec
+ }
+ // A rule set on the source routine rather than on the exercise decides how the plan is read:
+ // a range is a double-progression aim there and a flat target under the default. A copy
+ // without it would open 3 × 8–12 at 12. Each exercise keeps its own routine's rule, since a
+ // combined session can hold routines that progress differently.
+ if (!cfg.prog && typeof source?.prog === 'string' && source.prog) cfg.prog = source.prog
+
// buildCompletedWorkout carries setup.sg inside target, while older records may carry it on
// the entry. Keep either form, but never copy session-only ownership fields such as rid.
const sg = scalarGroup(entry?.sg ?? target.sg)
if (sg) cfg.sg = sg
else delete cfg.sg
+ // What a session stamps on its entries describes that session, not a plan. None of it belongs
+ // in a routine: a copied `planned` or `rid` would tell the next session it had been built from
+ // another plan or another routine.
+ for (const key of SESSION_ONLY) delete cfg[key]
return cfg
}
-export function routineFromSession(session, name = session?.name) {
- const ex = groupIds(sessionEntries(session).filter(entry => entry?.sets?.length).map(copiedEntry))
+const SESSION_ONLY = ['planned', 'plan', 'carried', 'rid', 'noProg', 'muscleSnapshot']
+
+// `routines` are the ones the session was built from, when they still exist: the rule each copied
+// exercise is read under comes from there (see copiedEntry).
+export function routineFromSession(session, name = session?.name, routines = []) {
+ const byId = new Map((Array.isArray(routines) ? routines : []).filter(r => r?.id != null).map(r => [r.id, r]))
+ const ex = groupIds(sessionEntries(session).filter(entry => entry?.sets?.length)
+ .map(entry => copiedEntry(entry, byId.get(entryRoutineId(session, entry)))))
if (!ex.length) throw new Error('no exercises')
return { id: uid(), name: String(name || 'Workout').trim() || 'Workout', ex }
}
export function saveSessionAsRoutine(state, session, name) {
- const routine = routineFromSession(session, name)
+ const routine = routineFromSession(session, name, state.routines)
state.routines.push(routine)
return routine.id
}
diff --git a/frontend/src/lib/session-routines.test.js b/frontend/src/lib/session-routines.test.js
index 8818a9509..6f3ed19ad 100644
--- a/frontend/src/lib/session-routines.test.js
+++ b/frontend/src/lib/session-routines.test.js
@@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest'
import { routineFromSession, saveSessionAsRoutine } from './session-routines.js'
import { buildCompletedWorkout } from './finish-workout.js'
import { buildSessionEntries } from './session-start.js'
-import { makeSideSet, setSideField, toggleSide } from './workout-model.js'
+import { makeSideSet, setSideField, toggleSide, isWarmupRow } from './workout-model.js'
const row = (patch = {}) => ({ w: 40, r: 8, done: true, ...patch })
const entry = (id, target, sets, extra = {}) => ({ id, target, sets, ...extra })
@@ -135,3 +135,76 @@ describe('saveSessionAsRoutine', () => {
expect(session).toEqual(before)
})
})
+
+// A copy made from a planned session is a routine like any other: the plan owns its sets and reps
+// (#275), and its first session continues from the logged one rather than restarting as an
+// edited plan. Each case trains the source routine the way the app does (buildSessionEntries →
+// buildCompletedWorkout), copies the saved workout and opens the copy.
+describe('a routine saved from a planned session', () => {
+ const BENCH = '0025' // barbell bench press — loaded, 2.5 kg step
+ const PUSHUP = '0662' // push-up — body weight
+ const work = e => e.sets.filter(s => !isWarmupRow(s))
+ const stateOf = routines => ({ unit: 'kg', exWeights: {}, routines: structuredClone(routines), workouts: [], week: {}, dayPlan: {} })
+ const train = (st, routine, typed = {}) => {
+ const entries = buildSessionEntries(st, routine).map(e => ({ ...e, rid: routine.id,
+ sets: e.sets.map(s => ({ ...s, ...(isWarmupRow(s) ? {} : typed), done: true })) }))
+ const n = st.workouts.length + 1
+ const saved = buildCompletedWorkout({ id: 'w' + n, d: `2026-09-${String(n).padStart(2, '0')}`, start: n * 1000, routineIds: [routine.id], name: routine.name, entries }, { end: n * 1000 + 1 })
+ st.workouts.push(saved)
+ return saved
+ }
+ const open = (st, routine) => buildSessionEntries(st, routine)[0]
+
+ it('keeps a double-progression range and its rule, not the reps of the day', () => {
+ const source = { id: 'A', name: 'Push', prog: 'double', ex: [{ id: BENCH, sets: 3, reps: 12, repsMin: 8, weight: 60 }] }
+ const st = stateOf([source])
+ const saved = train(st, source, { r: 9 })
+ const id = saveSessionAsRoutine(st, saved)
+ const copy = st.routines.find(r => r.id === id)
+ expect(copy.ex[0]).toMatchObject({ sets: 3, reps: 12, repsMin: 8, weight: 60, prog: 'double' })
+ // The copy picks up where the source stands: the same aim the source itself opens at next.
+ const next = open(st, copy)
+ expect(next.plan.why[0]).not.toBe('Plan changed — starting from your new target.')
+ expect(work(next).map(s => [s.w, s.r])).toEqual(work(open(st, source)).map(s => [s.w, s.r]))
+ })
+
+ it('takes the planned set count, not a set the bodyweight ceiling added or a bonus set', () => {
+ const source = { id: 'B', name: 'Body', ex: [{ id: PUSHUP, sets: 2, reps: 10, repsMax: 10, weight: 0, bodyweight: true }] }
+ const st = stateOf([source])
+ train(st, source, { r: 10 })
+ const saved = train(st, source, { r: 10 }) // the ceiling added a third set
+ expect(work(saved.entries[0])).toHaveLength(3)
+ const copy = routineFromSession(saved, 'Copy', st.routines)
+ expect(copy.ex[0]).toMatchObject({ sets: 2, reps: 10 })
+
+ const bonus = { ...saved, entries: [{ ...saved.entries[0], sets: [...saved.entries[0].sets, { w: 0, r: 12, done: true }] }] }
+ expect(routineFromSession(bonus, 'Copy', st.routines).ex[0].sets).toBe(2)
+ })
+
+ it('takes the planned reps when the session started from last time\'s', () => {
+ const source = { id: 'C', name: 'Legs', ex: [{ id: BENCH, sets: 2, reps: 5, weight: 100 }] }
+ const st = { ...stateOf([source]), startFrom: 'last' }
+ train(st, source, { r: 8 })
+ const saved = train(st, source) // opened at last time's 8, not the plan's 5
+ expect(work(saved.entries[0]).map(s => s.r)).toEqual([8, 8])
+ expect(routineFromSession(saved, 'Copy', st.routines).ex[0].reps).toBe(5)
+ })
+
+ it('carries none of the session\'s own stamps into the plan', () => {
+ const source = { id: 'D', name: 'Pull', ex: [{ id: BENCH, sets: 2, reps: 8, weight: 50 }] }
+ const st = stateOf([source])
+ const saved = train(st, source)
+ saved.entries[0].noProg = true
+ saved.entries[0].target = { ...saved.entries[0].target, planned: { sets: 9 }, rid: 'D', carried: true }
+ const cfg = routineFromSession(saved, 'Copy', st.routines).ex[0]
+ for (const key of ['planned', 'plan', 'carried', 'rid', 'noProg', 'muscleSnapshot']) expect(cfg).not.toHaveProperty(key)
+ expect(cfg).toMatchObject({ id: BENCH, sets: 2, reps: 8 })
+ })
+
+ it('leaves the rule to the default when the source routine is gone', () => {
+ const source = { id: 'E', name: 'Gone', prog: 'double', ex: [{ id: BENCH, sets: 3, reps: 12, repsMin: 8, weight: 60 }] }
+ const st = stateOf([source])
+ const saved = train(st, source, { r: 9 })
+ expect(routineFromSession(saved, 'Copy', []).ex[0]).not.toHaveProperty('prog')
+ })
+})