warm-up round 3: applyPrescription/readSession never touch warm-up rows; per-phase set numbering; regression tests

This commit is contained in:
root committed 2026-08-07 15:19:11 +02:00
1 parent 86ac937d5d
commit 01072ef5e3
3 files changed
+69 -8

No files matched your search

+13 -7
View File
@@ -101,7 +101,9 @@ function deloadTo(cur, step) {
export function readSession(entry, fallback) {
const target = (entry && entry.target) || fallback || {}
const mode = modeOf({ ...target, id: entry && entry.id })
const sets = (entry && entry.sets) || []
// Warm-up rows are prep, not the session: one filtered read beats guarding every consumer
// below (an undone warm-up otherwise poisons `ok` forever and its reps drag `low`/`count`).
const sets = ((entry && entry.sets) || []).filter(s => !s.warmup)
const planned = target.sets || sets.length
const enough = sets.length >= planned
@@ -110,7 +112,7 @@ export function readSession(entry, fallback) {
const held = sets.map(s => (s.done ? (s.sec || 0) : 0))
return {
mode, goal, held,
weight: Math.max(0, ...sets.filter(s => s.done && !s.warmup).map(s => s.w || 0)),
weight: Math.max(0, ...sets.filter(s => s.done).map(s => s.w || 0)),
best: Math.max(0, ...held),
ok: goal > 0 && enough && held.length > 0 && held.every(h => h >= goal)
}
@@ -119,7 +121,7 @@ export function readSession(entry, fallback) {
const reps = sets.map(s => (s.done ? (s.r || 0) : 0))
return {
mode, goal, reps,
weight: Math.max(0, ...sets.filter(s => s.done && !s.warmup).map(s => s.w || 0)),
weight: Math.max(0, ...sets.filter(s => s.done).map(s => s.w || 0)),
count: reps.length, // the dimension bodyweight work grows (#33)
low: reps.length ? Math.min(...reps) : 0,
amrap: reps.length ? reps[reps.length - 1] : 0, // Greyskull's final set
@@ -250,7 +252,10 @@ export function nextPrescription(S, cfg, routine) {
export function applyPrescription(sets, p) {
if (!p || p.kind === 'off' || p.kind === 'first') return sets
const out = sets.map(s => {
if (s.done && !s.warmup) return s
// Never rewrite a logged set, and never rewrite a warm-up: the prescription speaks to
// the work rows only (a ticked warm-up falling through here would be the data-loss the
// cascade fix removed, two files over).
if (s.done || s.warmup) return s
const o = { ...s }
if (p.weight != null) o.w = p.weight
if (p.reps != null) o.r = p.reps
@@ -260,9 +265,10 @@ export function applyPrescription(sets, p) {
// A policy that decided on a set count gets to grow the list — bodyweight progression adds
// a set where a barbell would have added a plate. Only ever upwards, and only by copying a
// row that is already there: a session in progress must not lose a set it has logged.
if (p.sets > out.length) {
const seed = out[out.length - 1]
while (out.length < p.sets) out.push({ ...seed, done: false })
const workRows = out.filter(s => !s.warmup)
if (p.sets > workRows.length) {
const seed = workRows[workRows.length - 1] || out[out.length - 1]
while (out.filter(s => !s.warmup).length < p.sets) out.push({ ...seed, done: false })
}
return out
}
+53
View File
@@ -450,3 +450,56 @@ describe('applyPrescription', () => {
expect(applyPrescription(sets, { kind: 'up', weight: 60, sets: 1 })).toHaveLength(sets.length)
})
})
describe('warm-up rows in session reads (round 3)', () => {
it('readSession ignores warm-up rows for reps, count, low and ok', () => {
// An undone warm-up (r 0) must not poison `ok` forever; its lighter reps must not
// drag `low`/`count` - the warm-up is prep, the session is the work rows.
const s = readSession({ id: LIFT, target: { sets: 2, reps: 5, mode: 'reps' }, sets: [
{ w: 20, r: 8, done: true, warmup: true },
{ w: 60, r: 5, done: true },
{ w: 60, r: 6, done: true },
] })
expect(s.count).toBe(2)
expect(s.low).toBe(5)
expect(s.reps).toEqual([5, 6])
expect(s.ok).toBe(true)
})
it('readSession keeps an undone warm-up out of held/ok in time mode', () => {
const s = readSession({ id: LIFT, target: { sets: 2, sec: 45, mode: 'time' }, sets: [
{ sec: 45, done: true, warmup: true },
{ sec: 45, done: true },
{ sec: 30, done: true },
] })
expect(s.held).toEqual([45, 30])
expect(s.ok).toBe(false) // the 30s work row is the miss, not the warm-up
})
})
describe('applyPrescription never touches warm-up rows (round 3)', () => {
it('leaves a done warm-up exactly as logged', () => {
const sets = [
{ w: 20, r: 8, done: true, warmup: true },
{ w: 60, r: 5, done: true },
{ w: 60, r: 5, done: false },
]
const out = applyPrescription(sets, { kind: 'up', weight: 62.5, reps: 5 })
expect(out[0]).toEqual({ w: 20, r: 8, done: true, warmup: true })
expect(out[1]).toEqual({ w: 60, r: 5, done: true })
expect(out[2]).toEqual({ w: 62.5, r: 5, done: false })
})
it('grows the work rows, not the warm-up rows, when the policy adds sets', () => {
const sets = [
{ w: 20, r: 8, done: true, warmup: true },
{ w: 60, r: 5, done: true },
{ w: 60, r: 5, done: false },
]
const out = applyPrescription(sets, { kind: 'up', weight: 62.5, reps: 5, sets: 4 })
expect(out.filter(s => !s.warmup)).toHaveLength(4) // 2 existing + 2 grown
expect(out.filter(s => s.warmup)).toHaveLength(1) // warm-up untouched
expect(out[0]).toEqual({ w: 20, r: 8, done: true, warmup: true })
})
})