Merge !92: fix: capture working weight automatically
This commit is contained in:
commit
f00400206d
6 files changed
+131
-51
No files matched your search
@@ -1,11 +1,13 @@
|
||||
// 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'
|
||||
|
||||
export function buildCompletedWorkout(active, { end = Date.now(), prs = [], snapshotFor } = {}) {
|
||||
const entries = (active?.entries || []).map(entry => {
|
||||
const completed = {
|
||||
id: entry.id,
|
||||
sets: entry.sets,
|
||||
topW: entry.topW || null,
|
||||
topW: bestWeightForEntry(entry) || null,
|
||||
target: entry.target || null,
|
||||
}
|
||||
const snapshot = typeof snapshotFor === 'function' ? snapshotFor(entry) : null
|
||||
|
||||
@@ -15,6 +15,36 @@ describe('completed workout boundary', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('derives topW from the highest completed non-warm-up work set, not stale entry data', () => {
|
||||
const active = {
|
||||
id: 'active-1', d: '2026-08-08', start: 1000,
|
||||
entries: [{
|
||||
id: '0025', topW: 80, target: { mode: 'reps', sets: 2, reps: 8 },
|
||||
sets: [
|
||||
{ phase: 'warmup', done: true, w: 120, r: 8 },
|
||||
{ done: true, w: 75, r: 8 },
|
||||
{ done: true, w: 85, r: 7 },
|
||||
{ done: false, w: 100, r: 8 },
|
||||
],
|
||||
}],
|
||||
}
|
||||
|
||||
expect(buildCompletedWorkout(active).entries[0].topW).toBe(85)
|
||||
expect(buildCompletedWorkout({
|
||||
...active,
|
||||
entries: [{ ...active.entries[0], topW: 120 }],
|
||||
}).entries[0].topW).toBe(85)
|
||||
})
|
||||
|
||||
it('keeps a legacy topW when old completed rows have no usable weight', () => {
|
||||
const active = {
|
||||
id: 'active-1', d: '2026-08-08', start: 1000,
|
||||
entries: [{ id: '0025', topW: 60, sets: [{ done: true, r: 8 }] }],
|
||||
}
|
||||
|
||||
expect(buildCompletedWorkout(active).entries[0].topW).toBe(60)
|
||||
})
|
||||
|
||||
it('persists progression exclusion only for a marked session', () => {
|
||||
const active = {
|
||||
id: 'active-1', d: '2026-08-08', start: 1000, routineId: 'routine-1', name: 'Deload', bw: 80,
|
||||
|
||||
+14
-10
@@ -626,20 +626,24 @@ export function bestWeightForEntry(entry = {}) {
|
||||
? entry.sets.filter(s => phaseForSet(s) === 'work')
|
||||
: []
|
||||
const repsRows = metricRowsForEntry(entry, 'reps')
|
||||
if (!repsRows.length) {
|
||||
return workRows.reduce((best, set) => {
|
||||
if (set?.done !== true || isWarmupRow(set)) return best
|
||||
const weight = Number(set.w)
|
||||
return Number.isFinite(weight) && weight > best ? weight : best
|
||||
}, 0)
|
||||
}
|
||||
|
||||
// Reps rows are the authoritative load metric for a mixed entry. Otherwise use every
|
||||
// completed work row (timed holds can carry an added load too).
|
||||
const completedRows = repsRows.length
|
||||
? repsRows
|
||||
: workRows.filter(set => set?.done === true && !isWarmupRow(set))
|
||||
let best = 0
|
||||
repsRows.forEach(set => {
|
||||
let hasUsableWeight = false
|
||||
completedRows.forEach(set => {
|
||||
const weight = Number(set?.w)
|
||||
if (Number.isFinite(weight) && weight > best) best = weight
|
||||
if (!Number.isFinite(weight)) return
|
||||
hasUsableWeight = true
|
||||
if (weight > best) best = weight
|
||||
})
|
||||
|
||||
// A real completed row, including an explicit zero for an unloaded bodyweight set, always
|
||||
// wins. A manual topW is only useful for old records whose rows did not carry a usable load.
|
||||
if (hasUsableWeight) return best
|
||||
|
||||
const parentMode = modeForSet({}, target)
|
||||
const hasNonRepsWorkRow = workRows.some(set => modeForSet(set, target) !== 'reps')
|
||||
const hasWarmupRow = Array.isArray(entry.sets) && entry.sets.some(isWarmupRow)
|
||||
|
||||
Reference in new issue
Block a user