Assistance machines: a sync no longer hands the help back, and Stats reads the best the same way (#232)

Two readers were still folding the kept load upwards, so on a signed-in profile the lighter
setting was saved and then overwritten on the next sync — the symptom the fix was supposed to
remove, one layer down.

`sync-merge` kept the larger `w` per exercise, on the stated grounds that the app only ever
raises it. That is no longer true: on an assistance machine it lowers it, so merging with a
device that still held 30 kg handed back the help the other one had just dropped. Both sides now
go through `beatsWeight`, which knows which way each exercise runs.

Stats tracked its per-exercise best with a plain maximum, so the curve's "best" line disagreed
with the exercise's own record. Weighted reps work now folds by the exercise; reps, duration and
speed are unchanged, since more is better in all three.

Also makes the list-view scroll test deterministic. It waited on the component's 0 ms fallback
and raced under a loaded full-suite run; the test DOM now carries a frame queue the test drains
itself, which is the requestAnimationFrame path a browser actually takes.
This commit is contained in:
Duarte Santos committed 2026-09-20 20:27:24 +02:00
1 parent 542c3f493a
commit 6aa43e7a06
4 files changed
+40 -7

No files matched your search

+13
View File
@@ -91,3 +91,16 @@ describe('no one-rep max for an assistance machine', () => {
expect(bestSetOf({ id: PLAIN, sets: [set(100, 5)] })).not.toBe(null)
})
})
describe('a sync does not hand the help back', () => {
it('keeps the smaller assistance and the larger ordinary load', async () => {
const { mergeStates } = await import('./sync-merge.js')
const phone = { _ts: 2000, exWeights: { [ASSISTED]: { w: 20, d: '2026-09-08' }, [PLAIN]: { w: 100, d: '2026-09-08' } } }
const server = { _ts: 1000, exWeights: { [ASSISTED]: { w: 30, d: '2026-09-01' }, [PLAIN]: { w: 90, d: '2026-09-01' } } }
expect(mergeStates(phone, server).exWeights[ASSISTED].w).toBe(20)
expect(mergeStates(phone, server).exWeights[PLAIN].w).toBe(100)
// and the other way round, so it does not depend on which copy is newer
expect(mergeStates(server, phone).exWeights[ASSISTED].w).toBe(20)
expect(mergeStates(server, phone).exWeights[PLAIN].w).toBe(100)
})
})
+12 -3
View File
@@ -12,8 +12,9 @@
* of an id that both have; workouts sorted by day and start like every other writer
* - bodyweight: union by day, the later-edited (`t`) entry of a day that both have
* - favEx: ordered set union, the newer copy first
* - exWeights: union by exercise, the larger `w` (the app itself only ever raises it — a PR
* logged on the other device must not be forgotten); exNotes, barWeights: key union
* - exWeights: union by exercise, the better `w` for that exercise — larger for an ordinary
* lift, smaller on an assistance machine (a PR logged on the other device must not be
* forgotten, whichever way it runs); exNotes, barWeights: key union
* - `_ts`: the later of the two; `_rev` dropped (the server sets it); `active` left to the caller
*
* Known limit: with no record of what each side deleted, an entry removed on one device inside
@@ -21,6 +22,8 @@
* on resume and every push is conditional), and a resurrected entry beats a lost one. Tombstones
* would close it.
*/
import { beatsWeight } from './exercises.js'
const clone = o => JSON.parse(JSON.stringify(o))
const list = v => (Array.isArray(v) ? v : [])
@@ -58,10 +61,16 @@ export function mergeBodyweight(a = [], b = []) {
return [...byDay.values()].sort((x, y) => (x.d < y.d ? -1 : 1))
}
// The kept load per exercise. "The larger one wins" held while the app only ever raised it —
// but an assistance machine progresses downwards, so there the smaller number is the newer,
// harder setting and taking the larger would hand back the help the other device just dropped
// (issue #232). `beatsWeight` knows which way round each exercise runs; the date breaks a tie
// on an exercise where both sides moved in the same direction.
function mergeExWeights(n = {}, o = {}) {
const out = { ...(o || {}), ...(n || {}) }
for (const k of Object.keys(o || {})) {
if (n && n[k] && o[k] && (o[k].w || 0) > (n[k].w || 0)) out[k] = o[k]
if (!(n && n[k] && o[k])) continue
if (beatsWeight(k, o[k].w || 0, n[k].w || 0)) out[k] = o[k]
}
return out
}
+5 -2
View File
@@ -1,7 +1,7 @@
import { useEffect, useMemo, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { useStore } from '../store/useStore.js'
import { EXIDX, matchExercise } from '../lib/exercises.js'
import { EXIDX, matchExercise, betterWeight } from '../lib/exercises.js'
import { lastBW, streakWeeks, setLabel, modeOf, effortOf, metricModeForEntry, metricRowsForEntry, bestWeightForEntry } from '../lib/history.js'
import { fmtNum, fmtDate, fmtVol, todayISO, weekStartOf } from '../lib/format.js'
import { t, exerciseNameFor, getLang } from '../lib/i18n.js'
@@ -393,7 +393,10 @@ export default function Stats() {
: Math.max(0, ...doneSets.map(metric))
if (mx > 0) {
exPts.push({ t: w.start, y: mx, d: w.d, sets: doneSets, target: en.target })
if (mx > exBest) exBest = mx
// Weighted work on an assistance machine reads the other way: the smallest load is the
// best (issue #232). Reps, duration and speed are always "more is better".
const better = curMode === 'reps' && !repsOnly ? betterWeight(curEx, exBest || mx, mx) : Math.max(exBest, mx)
exBest = exBest > 0 ? better : mx
}
}
})
+10 -2
View File
@@ -114,6 +114,8 @@ function workout(entries, cur = 0, overrides = {}) {
}
}
const frames = []
function installDom() {
const parsed = parseHTML('<!doctype html><html><body><div id="root"></div></body></html>')
dom = parsed.window
@@ -124,6 +126,9 @@ function installDom() {
dom.Element.prototype.scrollIntoView = vi.fn(function (options) {
mocks.scrollCalls.push({ node: this, options })
})
frames.length = 0
dom.requestAnimationFrame = cb => frames.push(cb)
dom.cancelAnimationFrame = id => { const i = frames.indexOf(id); if (i >= 0) frames.splice(i, 1) }
// linkedom has no layout; the sticky workout header reports the height a test gives it.
Object.defineProperty(dom.HTMLElement.prototype, 'offsetHeight', {
configurable: true, get() { return this.classList.contains('whdr') ? mocks.headerHeight : 0 },
@@ -192,9 +197,12 @@ async function addExerciseThroughSheets(ex = { id: 'added-exercise' }, cfg = { m
await act(async () => { configCall[2](cfg) })
}
// The list's scroll-to-current waits for the next frame (linkedom has no rAF, so a 0 ms timer).
// The list's scroll-to-current waits for the next frame. linkedom has no requestAnimationFrame,
// and leaning on the component's 0 ms fallback made this race under a loaded full-suite run — so
// the DOM gets a frame queue the test drains itself, which is also the path a browser takes.
async function flushFrame() {
await act(async () => { await new Promise(resolve => setTimeout(resolve, 0)) })
const due = frames.splice(0)
await act(async () => { due.forEach(cb => cb(0)) })
}
async function rerenderAt(cur) {