warm-up carve-outs (effort ratio, muscle map), cascade/warmup row helpers with tests, dead guard removed, total+work set counts

This commit is contained in:
root committed 2026-08-07 15:16:19 +02:00
1 parent 9cc645c755
commit 86ac937d5d
17 files changed
+118 -25

No files matched your search

+1 -1
View File
@@ -44,7 +44,7 @@ export const scaleName = kind => EFFORT[kind].hd
function eachDoneSet(S, fn) {
;(S.workouts || []).forEach(w =>
(w.entries || []).forEach(e =>
(e.sets || []).forEach(s => { if (s.done) fn(s, w, e) })))
(e.sets || []).forEach(s => { if (s.done && !s.warmup) fn(s, w, e) })))
}
// A window in days, counted back from now. 0 = everything, which is also what an empty
+46
View File
@@ -270,3 +270,49 @@ export function streakWeeks(S) {
}
return streak
}
/**
* Cascade a weight change forward: following sets of the same warm-up flag that are still
* undone take the new value (null deletes the key). Done sets are never rewritten.
*/
export function cascadeWeight(rows, from, value) {
const warm = !!rows[from]?.warmup
const next = rows.slice()
for (let j = from + 1; j < next.length; j++) {
if (!!next[j].warmup === warm && !next[j].done) {
if (value == null) delete next[j].w
else next[j].w = value
}
}
return next
}
/** Insert a warm-up row before the first work row, copying the preceding warm-up's values. */
export function insertWarmupRow(rows, mode, target) {
const firstWork = rows.findIndex(x => !x.warmup)
const at = firstWork === -1 ? rows.length : firstWork
const l = rows[at - 1] || rows[rows.length - 1]
const warm = mode === 'cardio'
? { min: l ? l.min : (target.min || 20), speed: l ? l.speed : (target.speed || 8), done: false, warmup: true }
: mode === 'time'
? { sec: l ? l.sec : (target.sec || 45), w: l ? (l.w || 0) : (target.weight || 0), done: false, warmup: true }
: { w: l ? l.w : 0, r: l ? l.r : target.reps, done: false, warmup: true }
const next = rows.slice()
next.splice(at, 0, warm)
return next
}
/** Remove the row at `i`, never emptying the entry below one row. */
export function removeRowAt(rows, i) {
if (rows.length <= 1) return rows.slice()
const next = rows.slice()
next.splice(i, 1)
return next
}
/** Completed non-warm-up sets across a workout's entries. */
export function workSetsDone(w) {
return (w?.entries || []).reduce(
(n, e) => n + (e.sets || []).filter(s => s.done && !s.warmup).length, 0,
)
}
+52 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { modeOf, isTimed, fmtSec, setLabel, defaultConfig, buildSets, freestyleConfig, exLine, workoutVolume, effortOf, stepEffort, capEffort, isBw, isPerSide, sideReps, repStep } from './history.js'
import { modeOf, isTimed, fmtSec, setLabel, defaultConfig, buildSets, freestyleConfig, exLine, workoutVolume, effortOf, stepEffort, capEffort, isBw, isPerSide, sideReps, repStep, cascadeWeight, insertWarmupRow, removeRowAt } from './history.js'
import { EXDB } from './exercises.js'
// Real ids out of the shipped catalogue, so the body-part fallback is exercised for real.
@@ -478,3 +478,54 @@ describe('workoutVolume', () => {
expect(workoutVolume(w)).toBe(0)
})
})
describe('session row helpers', () => {
it('cascadeWeight propagates to same-flag undone rows and never rewrites done sets', () => {
const rows = [
{ warmup: true, w: 20, done: true },
{ warmup: true, w: 20, done: false },
{ w: 60, done: true },
{ w: 60, done: false },
{ w: 60, done: false },
]
const next = cascadeWeight(rows, 2, 62.5)
expect(next[2].w).toBe(60) // done set untouched
expect(next[3].w).toBe(62.5) // same flag (work), undone
expect(next[4].w).toBe(62.5) // same flag (work), undone
expect(next[1].w).toBe(20) // different flag (warm-up) untouched
})
it('cascadeWeight deleting the weight removes the key from following undone rows only', () => {
const rows = [
{ w: 60, done: true },
{ w: 60, done: false },
{ w: 60, done: false },
]
const next = cascadeWeight(rows, 0, null)
expect(next[0].w).toBe(60) // done set untouched
expect('w' in next[1]).toBe(false)
expect('w' in next[2]).toBe(false)
})
it('insertWarmupRow inserts before the first work row and copies the last warm-up values', () => {
const rows = [
{ warmup: true, w: 20, r: 8, done: true },
{ warmup: true, w: 30, r: 8, done: false },
{ w: 60, r: 8, done: false },
]
const next = insertWarmupRow(rows, 'reps', { reps: 8 })
expect(next.length).toBe(4)
expect(next[2].warmup).toBe(true)
expect(next[2].w).toBe(30) // copies the preceding warm-up
expect(next[3].w).toBe(60) // work row still after the warm-up block
})
it('removeRowAt never empties an entry below one row', () => {
expect(removeRowAt([{ w: 60 }], 0).length).toBe(1)
const rows = [{ w: 60 }, { w: 70 }]
const next = removeRowAt(rows, 0)
expect(next.length).toBe(1)
expect(next[0].w).toBe(70)
})
})
+2 -2
View File
@@ -108,7 +108,7 @@ export function loadOf(items) {
*/
export const loadOfWorkouts = (workouts, pick) =>
loadOf((workouts || []).flatMap(w =>
(w.entries || []).map(e => ({ id: e.id, sets: (e.sets || []).filter(s => s.done && (!pick || pick(s))).length }))))
(w.entries || []).map(e => ({ id: e.id, sets: (e.sets || []).filter(s => s.done && !s.warmup && (!pick || pick(s))).length }))))
/** Load a routine *would* produce, from its planned set counts. */
export const loadOfRoutine = routine =>
@@ -116,7 +116,7 @@ export const loadOfRoutine = routine =>
/** Load for a workout still in progress — the sets ticked so far. */
export const loadOfActive = active =>
loadOf((active?.entries || []).map(e => ({ id: e.id, sets: (e.sets || []).filter(s => s.done).length })))
loadOf((active?.entries || []).map(e => ({ id: e.id, sets: (e.sets || []).filter(s => s.done && !s.warmup).length })))
/**
* Shade buckets 0–4 per muscle.