diff --git a/frontend/src/lib/assisted.test.js b/frontend/src/lib/assisted.test.js index 252231e..1a000d5 100644 --- a/frontend/src/lib/assisted.test.js +++ b/frontend/src/lib/assisted.test.js @@ -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) + }) +}) diff --git a/frontend/src/lib/sync-merge.js b/frontend/src/lib/sync-merge.js index d1a4584..3f0ca79 100644 --- a/frontend/src/lib/sync-merge.js +++ b/frontend/src/lib/sync-merge.js @@ -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 } diff --git a/frontend/src/views/Stats.jsx b/frontend/src/views/Stats.jsx index 0543514..3af72bb 100644 --- a/frontend/src/views/Stats.jsx +++ b/frontend/src/views/Stats.jsx @@ -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 } } }) diff --git a/frontend/src/views/Workout.test.jsx b/frontend/src/views/Workout.test.jsx index f8c5828..452fcec 100644 --- a/frontend/src/views/Workout.test.jsx +++ b/frontend/src/views/Workout.test.jsx @@ -114,6 +114,8 @@ function workout(entries, cur = 0, overrides = {}) { } } +const frames = [] + function installDom() { const parsed = parseHTML('
') 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) {