fix: harden muscle balance stats semantics

This commit is contained in:
Space_Hermes committed 2026-08-27 18:34:06 +02:00
1 parent 5314b342ff
commit 194d62ace3
6 files changed
+130 -11

No files matched your search

+13 -2
View File
@@ -9,6 +9,7 @@
import { isWarmupRow } from './workout-model.js'
import { EXIDX, smOf } from './exercises.js'
import { todayISO, weekKey } from './format.js'
// The muscles a map can shade, in head-to-toe order — also the order of any list
// built from them, so "what am I neglecting" reads top-down like a body.
@@ -160,7 +161,7 @@ export function muscleGroupsOf(entry) {
? [...parts.primary, ...parts.secondary]
: explicit || [ex?.tg, ex?.mg, ...arrayOf(smOf(ex))]
const out = canonicalUnique(source)
if (!out.length && !useParts && explicit == null) {
if (!out.length && !useParts) {
canonicalUnique(Object.keys(BY_BODYPART[ex?.bp] || {})).forEach(slug => out.push(slug))
}
return out
@@ -259,6 +260,15 @@ export const loadOfWorkouts = (workouts, pick) =>
loadOf((workouts || []).flatMap(w =>
(w.entries || []).map(e => ({ id: e.id, ex: e.exercise || e, sets: (e.sets || []).filter(s => s.done && !isWarmupRow(s) && (!pick || pick(s))).length }))))
/** Workouts in one existing Muscle balance range, with time injected for deterministic tests. */
export function muscleBalanceWindow(workouts, win, now = Date.now(), today = todayISO()) {
return (workouts || []).filter(workout => win === 0
? true
: win === 7
? weekKey(workout.d) === weekKey(today)
: (workout.start || new Date(workout.d).getTime()) > now - win * 86400000)
}
/** Load a routine *would* produce, from its planned set counts. */
export const loadOfRoutine = routine =>
loadOf((routine?.ex || []).map(c => ({ id: c.id, ex: c, sets: c.sets || 1 })))
@@ -304,7 +314,8 @@ export function levelsOf(load, thresholds) {
/** Muscles sorted hardest-worked first; untrained ones last, in body order. */
export function rankOf(load) {
const worked = MUSCLES.filter(m => (load[m] || 0) > 0).sort((a, b) => load[b] - load[a])
const worked = MUSCLES.filter(m => (load[m] || 0) > 0)
.sort((a, b) => load[b] - load[a] || MUSCLES.indexOf(a) - MUSCLES.indexOf(b))
const missed = MUSCLES.filter(m => !(load[m] > 0))
return { worked, missed }
}
+39 -1
View File
@@ -1,6 +1,9 @@
import { describe, it, expect } from 'vitest'
import { EXIDX, EXDB, smOf } from './exercises.js'
import { MUSCLE_NAME, exerciseMuscleSnapshot, hasExplicitMuscleMetadata, loadOf, loadOfWorkouts, matchesMuscleGroups, muscleGroupsOf, musclesOf } from './muscles.js'
import {
MUSCLE_NAME, exerciseMuscleSnapshot, hasExplicitMuscleMetadata, levelsOf, loadOf,
loadOfWorkouts, matchesMuscleGroups, muscleBalanceWindow, muscleGroupsOf, musclesOf, rankOf
} from './muscles.js'
describe('multi-muscle exercise metadata', () => {
it('normalizes legacy primary/secondary fields and removes duplicate groups', () => {
@@ -20,6 +23,12 @@ describe('multi-muscle exercise metadata', () => {
expect(matchesMuscleGroups({ bp: 'back', muscleGroups: [] }, ['lower-back'])).toBe(true)
})
it('falls back consistently when explicit groups contain only unknown names', () => {
const ex = { bp: 'back', muscleGroups: ['not-a-drawable-name'] }
expect(muscleGroupsOf(ex)).toEqual(['upper-back', 'lower-back'])
expect(musclesOf(ex)).toEqual({ 'upper-back': 0.75, 'lower-back': 0.25 })
})
it('matches an exercise when any requested muscle group matches', () => {
const ex = { muscleGroups: ['chest', 'triceps'] }
expect(matchesMuscleGroups(ex, ['hamstring', 'triceps'])).toBe(true)
@@ -172,3 +181,32 @@ describe('warm-up boundary', () => {
}
})
})
describe('muscle balance windows and ranking', () => {
const now = new Date('2026-08-27T12:00:00').getTime()
const workouts = [
{ id: 'monday', d: '2026-08-24', start: new Date('2026-08-24T12:00:00').getTime() },
{ id: 'sunday', d: '2026-08-23', start: new Date('2026-08-23T12:00:00').getTime() },
{ id: 'boundary', d: '2026-07-28', start: now - 30 * 86400000 },
{ id: 'inside', d: '2026-07-29', start: now - 29 * 86400000 },
]
it('preserves calendar-week, strict trailing-day, and all-history semantics', () => {
expect(muscleBalanceWindow(workouts, 7, now, '2026-08-27').map(w => w.id)).toEqual(['monday'])
expect(muscleBalanceWindow(workouts, 30, now, '2026-08-27').map(w => w.id)).toEqual(['monday', 'sunday', 'inside'])
expect(muscleBalanceWindow(workouts, 0, now, '2026-08-27')).toEqual(workouts)
})
it('uses relative levels and canonical order to break load ties', () => {
const load = { chest: 2, deltoids: 2, biceps: 1 }
expect(rankOf(load).worked).toEqual(['deltoids', 'chest', 'biceps'])
expect(levelsOf(load)).toMatchObject({ deltoids: 4, chest: 4, biceps: 2, abs: 0 })
})
it('keeps catalogue precedence and deleted-custom snapshot weights', () => {
const known = { id: '0025', muscleGroups: ['quadriceps'], sets: [{ done: true }] }
const deleted = { id: 'deleted', muscleSnapshot: { muscleWeights: { chest: 1 } }, sets: [{ done: true }] }
expect(loadOfWorkouts([{ entries: [known] }])).toEqual({ chest: 1, triceps: 0.4, deltoids: 0.4, biceps: 0.4 })
expect(loadOfWorkouts([{ entries: [deleted] }])).toEqual({ chest: 1 })
})
})