diff --git a/frontend/src/lib/units.js b/frontend/src/lib/units.js index 3c22eed..41ae4d0 100644 --- a/frontend/src/lib/units.js +++ b/frontend/src/lib/units.js @@ -3,6 +3,7 @@ // can load: lb to the nearest 0.5, kg to the nearest 0.25 — enough that a value converted there // and back lands where it started for any plate-loadable number. import { isSideSet, syncSideAggregate } from './workout-model.js' +import { workoutVolume } from './history.js' const LB_PER_KG = 2.2046226218 @@ -13,6 +14,15 @@ export function convertWeight(value, from, to) { return Math.round(v / LB_PER_KG * 4) / 4 } +// Body weight is not loaded on a bar: the weigh-in sheet steps and stores it at 0.1, so plate +// rounding would move most weigh-ins on a kg → lb → kg round trip (78.6 → 173.5 → 78.75; QA C15). +// A tenth in either unit is fine enough that kg → lb → kg comes home for every 0.1-kg value. +export function convertBodyWeight(value, from, to) { + if (from === to || value == null || value === '' || !Number.isFinite(Number(value))) return value + const v = Number(value) + return Math.round((to === 'lb' ? v * LB_PER_KG : v / LB_PER_KG) * 10) / 10 +} + const convSet = (set, from, to) => { if (!set || typeof set !== 'object') return set const out = { ...set } @@ -47,13 +57,24 @@ export function convertStateUnit(S, to) { const from = S.unit || 'kg' if (from === to) return S const c = v => convertWeight(v, from, to) + const bw = v => convertBodyWeight(v, from, to) + // A session carries its body weight of the day and a cached total volume; History rows, the + // detail header, the month calendar and the heatmap tooltips read those rather than summing + // sets, so they must move with the sets or show kg totals under an lb label (QA C11). The + // volume is re-added from the converted sets, so it agrees with the set list to the number. + const convSession = s => { + const out = { ...s, entries: (s.entries || []).map(e => convEntry(e, from, to)) } + if (out.bw != null) out.bw = bw(out.bw) + if (Number.isFinite(out.vol)) out.vol = workoutVolume({ entries: out.entries.filter(e => Array.isArray(e?.sets)) }) + return out + } const out = { ...S, unit: to } - if (Array.isArray(S.bodyweight)) out.bodyweight = S.bodyweight.map(b => ({ ...b, w: c(b.w) })) - if (S.targetW != null) out.targetW = c(S.targetW) + if (Array.isArray(S.bodyweight)) out.bodyweight = S.bodyweight.map(b => ({ ...b, w: bw(b.w) })) + if (S.targetW != null) out.targetW = bw(S.targetW) if (S.exWeights) out.exWeights = Object.fromEntries(Object.entries(S.exWeights).map(([k, v]) => [k, v && typeof v === 'object' ? { ...v, w: c(v.w) } : c(v)])) if (S.barWeights) out.barWeights = Object.fromEntries(Object.entries(S.barWeights).map(([k, v]) => [k, c(v)])) if (Array.isArray(S.routines)) out.routines = S.routines.map(r => ({ ...r, ex: (r.ex || []).map(cfg => convTarget(cfg, from, to)) })) - if (Array.isArray(S.workouts)) out.workouts = S.workouts.map(w => ({ ...w, entries: (w.entries || []).map(e => convEntry(e, from, to)) })) - if (S.active) out.active = { ...S.active, entries: (S.active.entries || []).map(e => convEntry(e, from, to)) } + if (Array.isArray(S.workouts)) out.workouts = S.workouts.map(convSession) + if (S.active) out.active = convSession(S.active) return out } diff --git a/frontend/src/lib/units.test.js b/frontend/src/lib/units.test.js index cfdd404..6066d37 100644 --- a/frontend/src/lib/units.test.js +++ b/frontend/src/lib/units.test.js @@ -1,5 +1,6 @@ import { describe, it, expect } from 'vitest' import { convertWeight, convertStateUnit } from './units.js' +import { workoutVolume } from './history.js' describe('convertWeight', () => { it('rounds lb to a half and kg to a quarter', () => { @@ -32,8 +33,8 @@ describe('convertStateUnit', () => { it('converts every stored weight and keeps everything else', () => { const out = convertStateUnit(S, 'lb') expect(out.unit).toBe('lb') - expect(out.targetW).toBe(176.5) - expect(out.bodyweight[0]).toEqual({ d: '2026-01-01', w: 181.5, t: 1 }) + expect(out.targetW).toBe(176.4) // body weight keeps its 0.1, see below + expect(out.bodyweight[0]).toEqual({ d: '2026-01-01', w: 181.7, t: 1 }) expect(out.exWeights['0025'].w).toBe(176.5) expect(out.exWeights.legacy).toBe(220.5) expect(out.barWeights['0025']).toBe(44) @@ -49,4 +50,36 @@ describe('convertStateUnit', () => { it('is a no-op for the unit already in use', () => { expect(convertStateUnit(S, 'kg')).toBe(S) }) + // History rows, the detail header, the month calendar and the heatmap tooltips read the + // cached `vol` and `bw` off the saved workout rather than summing sets, so a conversion that + // skips them shows kg totals under an lb label (QA C11). + it('converts each workout\'s cached volume and session body weight, and the active session\'s (QA C11)', () => { + const drop = { id: '0025', sets: [{ w: 80, r: 5, done: true, type: 'dropset', drops: [{ w: 60, r: 5 }] }, { w: 40, r: 8, done: true, phase: 'warmup' }] } + const state = { ...S, + workouts: [{ id: 'w', vol: 700, bw: 82.4, entries: [drop] }, { id: 'x', entries: [] }], + active: { ...S.active, bw: 82.4 } } + const out = convertStateUnit(state, 'lb') + // 176.5 × 5 plus the 132.5 × 5 drop, warm-up left out — what the converted set list adds up to. + expect(out.workouts[0].vol).toBe(1545) + expect(workoutVolume(out.workouts[0])).toBe(1545) + expect(out.workouts[0].bw).toBe(181.7) + expect(out.active.bw).toBe(181.7) + // A workout that never had a cached volume does not grow one. + expect(out.workouts[1]).not.toHaveProperty('vol') + expect(state.workouts[0].vol).toBe(700) // the input is not mutated + }) + // Body weight is weighed in and edited at 0.1, not loaded on a bar: rounding it to plate steps + // moved 22 of 24 weigh-ins on a kg → lb → kg round trip (QA C15). The goal weight is one too. + it('keeps body weight at 0.1 resolution and brings a kg → lb → kg round trip home (QA C15)', () => { + const state = { unit: 'kg', targetW: 77, bodyweight: [78.6, 82.1, 82.2, 81.8, 81.4].map((w, i) => ({ d: `2026-01-0${i + 1}`, w, t: i })), + workouts: [{ id: 'w', bw: 78.6, entries: [] }], active: { id: 'a', bw: 78.6, entries: [] } } + const lb = convertStateUnit(state, 'lb') + expect(lb.bodyweight.map(b => b.w)).toEqual([173.3, 181, 181.2, 180.3, 179.5]) + expect(lb.targetW).toBe(169.8) + const back = convertStateUnit(lb, 'kg') + expect(back.bodyweight.map(b => b.w)).toEqual([78.6, 82.1, 82.2, 81.8, 81.4]) + expect(back.targetW).toBe(77) + expect(back.workouts[0].bw).toBe(78.6) + expect(back.active.bw).toBe(78.6) + }) })