history: never seed a session from last time's warm-up rows

Found reviewing this release, and it is this release that makes it bite.

lastEntryFor returns every set that was `done`, warm-ups included, and four
callers then treat position 0 as "your first work set": buildSets seeds the
next session's rows from it, freestyleConfig sizes the config from its length,
and two views print it as "Last time".

Until now that was latent — warm-ups only existed if you pressed "Add warm-up
set", and that button was itself broken in a way that hid this: it handed you a
row at the full working weight, so seeding from it produced the right number by
accident. Fixing the ramp to a real 50% / 75%, and letting a routine plan
warm-ups into every session, turns the latent bug live: work set 1 gets seeded
at half your working weight, then next session at half of that. Your working
weight walks down on its own. A 3x5 also comes back as a 5-set exercise,
because two ramp rows are counted as sets you did.

So lastEntryFor now answers what every caller is actually asking — what you
lifted, not what you warmed up with. Warm-ups were already out of volume,
records and progression; this is the same rule one level up. An entry whose
only completed sets were warm-ups is no longer history at all, and the search
falls through to the session before it.

The regression test covers both seeding paths, planned and freestyle, since
freestyle takes the weight from last time directly and would drift fastest.
This commit is contained in:
Duarte Santos committed 2026-08-25 20:49:24 +02:00
1 parent bbf46e2aa2
commit 5ff6b7d0a8
3 files changed
+61 -5

No files matched your search

+9 -1
View File
@@ -209,10 +209,18 @@ export function unpairSuperset(items, idx) {
export function lastEntryFor(S, exId) {
for (let i = S.workouts.length - 1; i >= 0; i--) {
const en = S.workouts[i].entries.find(e => e.id === exId)
if (!en) continue
// Work sets only. Every caller asks the same question — "what did you actually lift last
// time" — to seed the next session's rows, to size a freestyle config, and to print "Last
// time" on the card. A warm-up answers none of them: seeding position 0 from a 50% ramp row
// walks the working weight DOWN a little every session, and counting the ramp rows makes a
// 3x5 come back as a 5-set exercise. Warm-ups are already excluded from volume, records and
// progression; this is the same rule one level up.
const done = en.sets.filter(s => s.done && !isWarmupRow(s))
// `target` is what the session prescribed; finished workouts carry it so labels and the
// progression engine can read a session back the way it was logged. Older workouts have
// none — modeOf() falls back to the body part for them, which is what they were.
if (en && en.sets.some(s => s.done)) return { d: S.workouts[i].d, sets: en.sets.filter(s => s.done), target: en.target || null }
if (done.length) return { d: S.workouts[i].d, sets: done, target: en.target || null }
}
return null
}
+13 -4
View File
@@ -2,24 +2,26 @@
// The note sheet is new JSX and nothing else mounts it, so a bad hook order, a missing import or
// a wrong store path would only surface on a real device. Render it through the real sheet stack
// and drive a save, so the wiring is checked and not just the shape of the module.
import { describe, expect, it, beforeEach } from 'vitest'
import { describe, expect, it, beforeEach, afterEach } from 'vitest'
import React, { act } from 'react'
import { createRoot } from 'react-dom/client'
// React only enables act() when it is told it is in a test environment.
globalThis.IS_REACT_ACT_ENVIRONMENT = true
import { useStore } from '../store/useStore.js'
import { useUI } from '../store/useUI.js'
import { exerciseNoteSheet } from '../sheets.jsx'
const activeWith = entry => ({ id: 'w1', d: '2026-08-25', start: 1, routineId: 'r1', name: 'Push', entries: [entry] })
// Roots have to be torn down between tests: the sheet subscribes to the store, so a root left
// mounted reacts to the next test's setState — outside act(), which React rightly complains about.
const mounted = []
function renderSheet() {
exerciseNoteSheet(0)
const sheet = useUI.getState().sheets.at(-1)
const host = document.createElement('div')
document.body.appendChild(host)
const root = createRoot(host)
mounted.push(root)
act(() => root.render(sheet.render(sheet_close(sheet))))
return host
}
@@ -33,6 +35,9 @@ const type = (el, value) => {
describe('exercise note sheet', () => {
beforeEach(() => {
// React only treats act() as real when told it is in a test environment, and vitest shares
// a worker across files — so set it per test rather than once at module scope.
globalThis.IS_REACT_ACT_ENVIRONMENT = true
useUI.setState({ sheets: [] })
useStore.setState(s => ({
S: { ...s.S, exNotes: {}, active: activeWith({ id: 'bench', sets: [] }) },
@@ -40,6 +45,10 @@ describe('exercise note sheet', () => {
document.body.innerHTML = ''
})
afterEach(() => {
act(() => { mounted.splice(0).forEach(r => r.unmount()) })
})
it('renders the session note, the pin and the standing note', () => {
const host = renderSheet()
const areas = host.querySelectorAll('textarea')
+39
View File
@@ -0,0 +1,39 @@
import { describe, expect, it } from 'vitest'
import { buildSets } from './history.js'
// A session that logged warm-ups, the way finish-workout persists it: warm-up rows and work
// rows in one `sets` array, all done.
const S = {
workouts: [{
d: '2026-08-24',
entries: [{
id: 'bench',
target: { id: 'bench', sets: 3, reps: 5, weight: 100 },
sets: [
{ w: 50, r: 5, done: true, phase: 'warmup', warmup: true },
{ w: 75, r: 5, done: true, phase: 'warmup', warmup: true },
{ w: 100, r: 5, done: true },
{ w: 100, r: 5, done: true },
{ w: 100, r: 5, done: true },
],
}],
}],
exWeights: {}, // no confirmed working weight for this exercise
routines: [],
}
describe('seeding the next session from one that had warm-ups', () => {
it('does not seed the work sets from the warm-up rows', () => {
const cfg = { id: 'bench', sets: 3, reps: 5, weight: 100 }
const rows = buildSets(S, cfg)
const work = rows.filter(r => r.phase !== 'warmup')
expect(work.map(r => r.w)).toEqual([100, 100, 100])
})
it('freestyle reproducing last time also skips the warm-ups', () => {
const cfg = { id: 'bench', sets: 3, reps: 5, weight: 100 }
const rows = buildSets(S, cfg, { preferLast: true })
const work = rows.filter(r => r.phase !== 'warmup')
expect(work.map(r => r.w)).toEqual([100, 100, 100])
})
})