Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Sync: an entry edited on two devices keeps both edits, field by field
Duarte Santos7e77a8ff246e
diff --git a/frontend/src/lib/sync-fuzz.test.js b/frontend/src/lib/sync-fuzz.test.jsindex e1d9fa9de..a28c87dc9 100644--- a/frontend/src/lib/sync-fuzz.test.js+++ b/frontend/src/lib/sync-fuzz.test.js@@ -130,9 +130,9 @@ function run(seed, opts = {}) {}// ---- user operations ---------------------------------------------------------------------- const canonW = (w, unit = 'kg') => { const x = clone(w); delete x._ts; delete x.media; for (const e of x.entries || []) for (const st of e.sets || []) st.w = unit === 'lb' ? st.w / 2.2046226218 : st.w; return x }+ const canonW = (w, unit = 'kg') => { const x = clone(w); delete x._ts; delete x._f; delete x.media; delete x.note; for (const e of x.entries || []) for (const st of e.sets || []) st.w = unit === 'lb' ? st.w / 2.2046226218 : st.w; return x }const sameW = (a, b) => { const ws = [], wt = []; const sa = JSON.stringify(a, (k, v) => (k === 'w' && typeof v === 'number' ? (ws.push(v), 0) : v)); const sb = JSON.stringify(b, (k, v) => (k === 'w' && typeof v === 'number' ? (wt.push(v), 0) : v)); return sa === sb && ws.every((x, i) => Math.abs(x - wt[i]) < 0.4) }- const canonR = x => { const y = clone(x); delete y._ts; return y }+ const canonR = x => { const y = clone(x); delete y._ts; delete y._f; return y }const ops = {addWorkout(r) {const id = uid('w')@@ -146,6 +146,7 @@ function run(seed, opts = {}) {const id = pick(W).idlet recupdate(r, (S, now) => { const i = S.workouts.findIndex(x => x.id === id); rec = S.workouts[i]; rec.entries[0].sets.push({ w: 20 + ri(100), r: 1 + ri(10) }); stampWorkout(rec, now) })+ if (truth.get('w:' + id)?.val === DEL) note('wn:' + id, rec.note ?? DEL, r.i)note('w:' + id, canonW(rec, r.S.unit), r.i)return `editWorkout ${id}`},@@ -154,30 +155,34 @@ function run(seed, opts = {}) {const id = pick(W).id; const h = uid('h')let recupdate(r, (S, now) => { rec = S.workouts.find(x => x.id === id); rec.media = [...(rec.media || []), { hash: h, kind: 'img' }]; stampWorkout(rec, now) })- note('m:' + id + ':' + h, true, r.i); note('w:' + id, canonW(rec, r.S.unit), r.i)+ note('m:' + id + ':' + h, true, r.i)+ // an edit keeps the workout (also after a delete elsewhere); its sets are whatever was set last+ { const was = truth.get('w:' + id); if (was?.val === DEL) note('wn:' + id, rec.note ?? DEL, r.i); note('w:' + id, was && was.val !== DEL ? was.val : canonW(rec, r.S.unit), r.i) }return `addMedia ${id} ${h}`},deleteWorkout(r) {const W = r.S.workouts; if (!W.length) return nullconst id = pick(W).idupdate(r, S => { S.workouts = S.workouts.filter(x => x.id !== id) })- note('w:' + id, DEL, r.i)+ note('w:' + id, DEL, r.i); note('wn:' + id, DEL, r.i)return `deleteWorkout ${id}`},addRoutine(r) {const id = uid('r'); const x = { id, name: 'R' + id, ex: [{ id: pick(EXS), sets: 3 }] }- update(r, S => { S.routines.push(x) }); note('r:' + id, canonR(x), r.i); return `addRoutine ${id}`+ update(r, S => { S.routines.push(x) }); note('r:' + id, clone(x.ex), r.i); note('rn:' + id, x.name, r.i); return `addRoutine ${id}`},editRoutine(r) {if (!r.S.routines.length) return nullconst id = pick(r.S.routines).id; let recupdate(r, S => { rec = S.routines.find(x => x.id === id); rec.ex.push({ id: pick(EXS), sets: 1 + ri(5) }) })- note('r:' + id, canonR(rec), r.i); return `editRoutine ${id}`+ note('r:' + id, clone(rec.ex), r.i)+ if (truth.get('rn:' + id)?.val === DEL) note('rn:' + id, rec.name, r.i)+ return `editRoutine ${id}`},deleteRoutine(r) {if (!r.S.routines.length) return nullconst id = pick(r.S.routines).id- update(r, S => { S.routines = S.routines.filter(x => x.id !== id) }); note('r:' + id, DEL, r.i); return `deleteRoutine ${id}`+ update(r, S => { S.routines = S.routines.filter(x => x.id !== id) }); note('r:' + id, DEL, r.i); note('rn:' + id, DEL, r.i); return `deleteRoutine ${id}`},reorderRoutines(r) {if (r.S.routines.length < 2) return null@@ -185,18 +190,20 @@ function run(seed, opts = {}) {},addCustom(r) {const id = uid('c'); const x = { id, name: 'C' + id, custom: true, eq: '' }- update(r, S => { S.customEx.push(x) }); note('c:' + id, canonR(x), r.i); return `addCustom ${id}`+ update(r, S => { S.customEx.push(x) }); note('c:' + id, x.name, r.i); return `addCustom ${id}`},editCustom(r) {if (!r.S.customEx.length) return nullconst id = pick(r.S.customEx).id; let recupdate(r, S => { rec = S.customEx.find(x => x.id === id); rec.name = 'C' + id + '-' + ri(1000) })- note('c:' + id, canonR(rec), r.i); return `editCustom ${id}`+ note('c:' + id, rec.name, r.i)+ if (truth.get('cm:' + id)?.val === DEL && rec.media) note('cm:' + id, rec.media.hash, r.i)+ return `editCustom ${id}`},deleteCustom(r) {if (!r.S.customEx.length) return nullconst id = pick(r.S.customEx).id- update(r, S => { S.customEx = S.customEx.filter(x => x.id !== id) }); note('c:' + id, DEL, r.i); return `deleteCustom ${id}`+ update(r, S => { S.customEx = S.customEx.filter(x => x.id !== id) }); note('c:' + id, DEL, r.i); note('cm:' + id, DEL, r.i); return `deleteCustom ${id}`},weighIn(r) {const d = pick(DAYS); const w = 60 + ri(40)@@ -241,7 +248,41 @@ function run(seed, opts = {}) {deleteCard(r) {if (!r.S.gymCards.length) return nullconst id = pick(r.S.gymCards).id- update(r, S => { S.gymCards = S.gymCards.filter(x => x.id !== id) }); note('g:' + id, DEL, r.i); return `deleteCard ${id}`+ update(r, S => { S.gymCards = S.gymCards.filter(x => x.id !== id) }); note('g:' + id, DEL, r.i); note('gl:' + id, DEL, r.i); return `deleteCard ${id}`+ },+ // Second fields of the same entries: edited on one device while another edits the first+ // field, both edits must survive (field-level merge).+ noteWorkout(r) {+ const W = r.S.workouts; if (!W.length) return null+ const id = pick(W).id; const v = 'n' + ri(1000)+ let rec+ update(r, (S, now) => { rec = S.workouts.find(x => x.id === id); rec.note = v; stampWorkout(rec, now) })+ note('wn:' + id, v, r.i)+ { const was = truth.get('w:' + id); note('w:' + id, was && was.val !== DEL ? was.val : canonW(rec, r.S.unit), r.i) }+ return `noteWorkout ${id}`+ },+ renameRoutine(r) {+ if (!r.S.routines.length) return null+ const id = pick(r.S.routines).id; const v = 'R' + id + '-' + ri(1000); let rec+ update(r, S => { rec = S.routines.find(x => x.id === id); rec.name = v })+ note('rn:' + id, v, r.i)+ { const was = truth.get('r:' + id); note('r:' + id, was && was.val !== DEL ? was.val : clone(rec.ex), r.i) }+ return `renameRoutine ${id}`+ },+ customMedia(r) {+ if (!r.S.customEx.length) return null+ const id = pick(r.S.customEx).id; const h = uid('ch'); let rec+ update(r, S => { rec = S.customEx.find(x => x.id === id); rec.media = { hash: h } })+ note('cm:' + id, h, r.i)+ { const was = truth.get('c:' + id); note('c:' + id, was && was.val !== DEL ? was.val : rec.name, r.i) }+ return `customMedia ${id}`+ },+ editCard(r) {+ if (!r.S.gymCards.length) return null+ const id = pick(r.S.gymCards).id; const v = 'L' + ri(1000)+ update(r, S => { S.gymCards.find(x => x.id === id).label = v })+ note('gl:' + id, v, r.i); note('g:' + id, true, r.i)+ return `editCard ${id}`},}const resets = []@@ -319,14 +360,23 @@ function run(seed, opts = {}) {let gotif (kind === 'w') { const w = (D.workouts || []).find(x => x.id === a); got = w ? canonW(w, D.unit) : DEL; if (got !== DEL && val !== DEL && sameW(got, val)) continue }else if (kind === 'm') { const w = (D.workouts || []).find(x => x.id === a); const wTruth = truth.get('w:' + a); if (!w || wTruth?.val === DEL || everDeleted.has('w:' + a)) continue; got = (w.media || []).some(m => m.hash === b) ? true : DEL }- else if (kind === 'r') { const x = (D.routines || []).find(x => x.id === a); got = x ? canonR(x) : DEL }- else if (kind === 'c') { const x = (D.customEx || []).find(x => x.id === a); got = x ? canonR(x) : DEL }+ else if (kind === 'r') { const x = (D.routines || []).find(x => x.id === a); got = x ? x.ex : DEL }+ else if (kind === 'rn') { const x = (D.routines || []).find(x => x.id === a); if (!x && truth.get('r:' + a)?.val !== DEL) continue; got = x ? x.name : DEL }+ else if (kind === 'c') { const x = (D.customEx || []).find(x => x.id === a); got = x ? x.name : DEL }+ else if (kind === 'cm') { const x = (D.customEx || []).find(x => x.id === a); if (!x || truth.get('c:' + a)?.val === DEL) continue; got = x.media?.hash ?? DEL }+ else if (kind === 'wn') { const x = (D.workouts || []).find(x => x.id === a); if (!x || truth.get('w:' + a)?.val === DEL) continue; got = x.note ?? DEL }+ else if (kind === 'gl') { const x = (D.gymCards || []).find(x => x.id === a); if (!x || truth.get('g:' + a)?.val === DEL) continue; got = x.label ?? DEL }else if (kind === 'bw') { const x = (D.bodyweight || []).find(x => x.d === a); got = x ? { w: D.unit === 'lb' ? x.w / 2.2046226218 : x.w } : DEL; if (got !== DEL && val !== DEL && Math.abs(got.w - val.w) < 0.2) continue }else if (kind === 'fav') got = (D.favEx || []).includes(a) ? true : DELelse if (kind === 'set') got = a in D ? D[a] : DELelse if (kind === 'week') got = D.week && a in D.week ? D.week[a] : DELelse if (kind === 'note') got = D.exNotes && a in D.exNotes ? D.exNotes[a] : DELelse if (kind === 'g') got = (D.gymCards || []).some(x => x.id === a) ? true : DEL+ // An entry deleted on one device and edited on another that had not seen the delete comes+ // back with the editing device's version; which fields of the deleted versions survive in it+ // is not a last-change question, so only its presence is checked then.+ const baseKey = { w: 'w:', wn: 'w:', r: 'r:', rn: 'r:', c: 'c:', cm: 'c:', g: 'g:', gl: 'g:' }[kind]+ if (baseKey && everDeleted.has(baseKey + a) && (['wn', 'rn', 'cm', 'gl'].includes(kind) || (val !== DEL && got !== DEL))) continueif (JSON.stringify(got) !== JSON.stringify(val)) problems.push(`${key}: want ${JSON.stringify(val)} (r${by} @T+${t - 1_700_000_000_000}) got ${JSON.stringify(got)}`)}// ids unique@@ -357,7 +407,7 @@ describe('sync fuzz: clocks up to a day apart still converge, with nothing dupliif (hard.length) bad.push(`seed ${s}: ${hard.join('; ')}`)}expect(bad.slice(0, 5)).toEqual([])- })+ }, 120000)})describe('sync fuzz: every device converges on every last change', () => {@@ -369,6 +419,6 @@ describe('sync fuzz: every device converges on every last change', () => {if (problems.length) bad.push(`seed ${s}: ${problems.slice(0, 3).join('; ')}`)}expect(bad.slice(0, 5)).toEqual([])- })+ }, 120000)}})diff --git a/frontend/src/lib/sync-merge.js b/frontend/src/lib/sync-merge.jsindex 8c1bd98d1..0ccc47e36 100644--- a/frontend/src/lib/sync-merge.js+++ b/frontend/src/lib/sync-merge.js@@ -11,7 +11,10 @@* the copy that changed it last by its own stamp in `edited` (stampEdits; `week`, `dayPlan`,* exNotes and barWeights per day or per exercise), from the copy with the newer `_ts` when* neither side stamped it or on a tie- * - equipProfiles, gymCards: union by id, the newer copy's version of an id that both have+ * - equipProfiles, gymCards: union by id; of an id that both have, field by field as below+ * - workouts, routines, customEx, equipProfiles, gymCards: of an id that both have, each field+ * from the side that changed it last by its own stamp (`_f`, stampEntry / mergeEntry), the+ * version edited last for every field neither side stamped* - customEx: union by id; of an id that both have, the version edited last by its own `_ts`* (stampCustomEx), the newer copy's on a tie — a photo or link added on one device must not* be lost to the other's copy just because that one logged a set since@@ -182,6 +185,55 @@ export function stampWorkout(w, now = Date.now()) {const stampOf = v => (v && typeof v === 'object' ? Number(v._ts) || 0 : 0)const isMap = v => !!v && typeof v === 'object' && !Array.isArray(v)+// ---- Entries merged field by field ----------------------------------------------------------+//+// A workout, routine, custom exercise, equipment profile or gym card edited on two devices kept+// the version edited last as a whole: a set corrected from 100 to 110 on one device was reverted+// by a note added on the other, a routine's 3 sets turned 5 lost to a rename, a custom exercise's+// photo to a rename. Each edit now records, per field, when it changed it (`_f`, stampEntry), and+// the merge takes every field from the side that changed it last (mergeEntry). A field neither+// side stamped follows the version edited last, as before.+const ENTRY_META = new Set(['id', '_ts', '_f'])+const fieldTime = (x, k) => Number(x?._f?.[k]) || 0+const sameJSON = (x, y) => JSON.stringify(x) === JSON.stringify(y)++/**+ * Stamps `x`, the new version of an entry whose previous version is `old`, as edited at `now`:+ * its `_ts`, and in `_f` every field that differs from `old`. A new entry gets its `_ts` only.+ * Mutates and returns `x`.+ */+export function stampEntry(old, x, now) {+ if (!x || typeof x !== 'object') return x+ x._ts = now+ if (!old || typeof old !== 'object') return x+ const f = isMap(old._f) ? { ...old._f } : {}+ for (const k of new Set([...Object.keys(old), ...Object.keys(x)])) {+ if (ENTRY_META.has(k)) continue+ if ((k in old) !== (k in x) || !sameJSON(old[k], x[k])) f[k] = now+ }+ if (Object.keys(f).length) x._f = f; else delete x._f+ return x+}++/**+ * Two versions of one entry: the version edited last (`_ts`; `a` on a tie), with every field the+ * other side changed later (`_f`) taken from that side, removal included. Returns a new object.+ */+export function mergeEntry(a, b) {+ const [n, o] = (Number(b?._ts) || 0) > (Number(a?._ts) || 0) ? [b, a] : [a, b]+ const out = clone(n)+ for (const k of new Set([...Object.keys(n), ...Object.keys(o)])) {+ if (ENTRY_META.has(k)) continue+ if (fieldTime(o, k) > fieldTime(n, k)) { if (k in o) out[k] = clone(o[k]); else delete out[k] }+ }+ const f = { ...(isMap(o._f) ? o._f : {}) }+ for (const [k, v] of Object.entries(isMap(n._f) ? n._f : {})) if (!((Number(f[k]) || 0) > (Number(v) || 0))) f[k] = v+ if (Object.keys(f).length) out._f = f; else delete out._f+ const ts = Math.max(Number(a?._ts) || 0, Number(b?._ts) || 0)+ if (ts) out._ts = ts+ return out+}+/*** A settings map whose entries carry their own edit time (`{ …, _ts }`), such as the Structural* Balance overrides or the plate-loading choices: every key of either side, and of a key both have, the entry set last — the@@ -317,10 +369,11 @@ const DEL_LISTS = {gymCards: x => x?.id, equipProfiles: x => x?.id, favEx: x => x,}// When an entry was last edited, to hold against a removal. Entries with no time of their own-// (cards, profiles, favourites) count as older than any removal.+// (favourites, and cards and profiles saved before they were stamped) count as older than any+// removal.const DEL_TIME = {workouts: workoutTime, routines: x => Number(x?._ts) || 0, customEx: x => Number(x?._ts) || 0,- bodyweight: e => Number(e?.t) || 0,+ bodyweight: e => Number(e?.t) || 0, gymCards: x => Number(x?._ts) || 0, equipProfiles: x => Number(x?._ts) || 0,}// Per field, the most stamps kept; past it the oldest go first.export const DELETED_MAX = 5000@@ -539,9 +592,13 @@ export function mergeStates(a0, b0, { prefer } = {}) {const key = workoutKey(w)const alt = mine.has(key) ? other.get(key) : nullif (!alt) return w- const [kept, lost, by] = (alt._ts || 0) > (w._ts || 0) ? [clone(alt), w, o] : [w, alt, n]- if ((kept._ts || 0) > (lost._ts || 0) && JSON.stringify(kept.entries) !== JSON.stringify(lost.entries)) {- for (const e of [...list(kept.entries), ...list(lost.entries)]) {+ // Field by field (mergeEntry): the sets from the side that edited them last, the note from+ // the side that edited it last.+ const kept = mergeEntry(w, alt)+ const [last, first, byLast, byFirst] = (alt._ts || 0) > (w._ts || 0) ? [alt, w, o, n] : [w, alt, n, o]+ if ((last._ts || 0) !== (first._ts || 0) && JSON.stringify(last.entries) !== JSON.stringify(first.entries)) {+ const by = JSON.stringify(kept.entries) === JSON.stringify(first.entries) ? byFirst : byLast+ for (const e of [...list(kept.entries), ...list(w.entries), ...list(alt.entries)]) {if (e?.id == null) continueif (!editedBy.has(e.id)) editedBy.set(e.id, new Set())editedBy.get(e.id).add(by.exWeights)@@ -570,23 +627,18 @@ export function mergeStates(a0, b0, { prefer } = {}) {// version dropped a plan edit made on one device whenever the other had since logged a set or// flipped a setting — its whole copy was newer, its version of that routine was not. `prefer`// (sign-in) keeps the preferred side's plan as it is.- if (!prefer && out.routines) {- const other = new Map(list(o.routines).filter(r => r?.id != null).map(r => [r.id, r]))- out.routines = out.routines.map(r => {- const alt = r?.id != null && other.get(r.id)- return alt && (alt._ts || 0) > (r._ts || 0) ? clone(alt) : r- })- }- // A custom exercise edited on both sides keeps the version edited last, the same rule and for- // the same reason. The merge is whole-entry: a device that later renames an exercise whose- // media it never saw change brings its old media back (the old file outlives the grace period- // on the server, so nothing breaks, it is only the older picture).- if (!prefer && out.customEx) {- const other = new Map(list(o.customEx).filter(c => c?.id != null).map(c => [c.id, c]))- out.customEx = out.customEx.map(c => {- const alt = c?.id != null && other.get(c.id)- return alt && (alt._ts || 0) > (c._ts || 0) ? clone(alt) : c- })+ // Field by field (mergeEntry): a routine's sets changed on one device and its name on the other+ // are both kept. The same for a custom exercise (a photo added on one device survives a rename+ // on the other), an equipment profile and a gym card.+ if (!prefer) {+ for (const f of ['routines', 'customEx', 'equipProfiles', 'gymCards']) {+ if (!out[f]) continue+ const other = new Map(list(o[f]).filter(x => x?.id != null).map(x => [x.id, x]))+ out[f] = out[f].map(x => {+ const alt = x?.id != null && other.get(x.id)+ return alt ? mergeEntry(x, alt) : x+ })+ }}out.bodyweight = mergeBodyweight(n.bodyweight, o.bodyweight).map(clone)if (list(n.favEx).length || list(o.favEx).length) out.favEx = [...new Set([...list(n.favEx), ...list(o.favEx)])]@@ -734,7 +786,7 @@ export function stampChange(prev, next, wall = Date.now()) {for (const w of list(next.workouts)) {if (!w || typeof w !== 'object' || w.id == null || w._ts == null) continueconst old = before.get(w.id)- if (old && w._ts !== old._ts) w._ts = now+ if (old && w._ts !== old._ts) stampEntry(old, w, now)}// Settings maps whose entries carry their own stamp (mergeStampedMap): an entry the change// re-stamped takes the change's time.@@ -745,12 +797,14 @@ export function stampChange(prev, next, wall = Date.now()) {}stampRoutines(prev?.routines, next.routines, now)stampCustomEx(prev?.customEx, next.customEx, now)+ stampEntries(prev?.equipProfiles, next.equipProfiles, now)+ stampEntries(prev?.gymCards, next.gymCards, now)stampDeletions(prev, next, now)stampEdits(prev, next, now)return now}-const sameRoutine = (a, b) => JSON.stringify({ ...a, _ts: 0 }) === JSON.stringify({ ...b, _ts: 0 })+const sameRoutine = (a, b) => JSON.stringify({ ...a, _ts: 0, _f: 0 }) === JSON.stringify({ ...b, _ts: 0, _f: 0 })/*** Stamps `_ts` on every routine of `next` that is new or differs from its version in `prev` — the@@ -762,12 +816,12 @@ export function stampRoutines(prev = [], next = [], now = Date.now()) {for (const r of list(next)) {if (!r || r.id == null) continueconst old = before.get(r.id)- if (!old || (old !== r && !sameRoutine(old, r))) r._ts = now+ if (!old || (old !== r && !sameRoutine(old, r))) stampEntry(old, r, now)}return next}-const sameEntry = (a, b) => JSON.stringify({ ...a, _ts: 0 }) === JSON.stringify({ ...b, _ts: 0 })+const sameEntry = (a, b) => JSON.stringify({ ...a, _ts: 0, _f: 0 }) === JSON.stringify({ ...b, _ts: 0, _f: 0 })/*** Stamps `_ts` on every custom exercise of `next` that is new or differs from its version in@@ -780,11 +834,14 @@ export function stampCustomEx(prev = [], next = [], now = Date.now()) {for (const c of list(next)) {if (!c || typeof c !== 'object' || c.id == null) continueconst old = before.get(c.id)- if (!old || (old !== c && !sameEntry(old, c))) c._ts = now+ if (!old || (old !== c && !sameEntry(old, c))) stampEntry(old, c, now)}return next}+/** stampCustomEx for any list of entries with ids: equipment profiles, gym cards. */+export function stampEntries(prev = [], next = [], now = Date.now()) { return stampCustomEx(prev, next, now) }+// What `local` holds that `server` does not: the workouts and weigh-ins a device logged while it// was signed out, and the custom exercises they use. Sign-in asks about these before the server's// profile replaces the local copy; zero of each means there is nothing to ask about.diff --git a/frontend/src/lib/sync-merge.test.js b/frontend/src/lib/sync-merge.test.jsindex 00ad7f11c..52b07c0a3 100644--- a/frontend/src/lib/sync-merge.test.js+++ b/frontend/src/lib/sync-merge.test.js@@ -804,6 +804,56 @@ describe('a change is stamped after everything the copy it was made on carries (})})++describe('an entry edited on two devices keeps both edits, field by field', () => {+ const change = (prev, wall, mut) => { const n = clone(prev); mut(n); n._ts = stampChange(prev, n, wall); return n }+ const S0 = base({ _ts: 100,+ workouts: [{ id: 'w1', d: '2026-09-30', start: 1, end: 2, entries: [{ id: 'bench', sets: [{ w: 100, r: 5 }] }] }],+ routines: [{ id: 'r1', name: 'Push', ex: [{ id: 'bench', sets: 3 }], _ts: 50 }],+ customEx: [{ id: 'c1', n: 'Landmine press', custom: true, _ts: 50 }],+ equipProfiles: [{ id: 'eq1', name: 'Home', equipment: ['dumbbell'] }],+ gymCards: [{ id: 'g1', name: 'FitX', code: '' }] })++ it('a corrected set and a note, a plan change and a rename, a photo and a rename all survive', () => {+ const A = change(S0, 1000, S => {+ S.workouts[0].entries[0].sets[0].w = 110; stampWorkout(S.workouts[0], 1000)+ S.routines[0].ex[0].sets = 5+ S.customEx[0].media = { hash: 'abc', kind: 'img' }+ S.equipProfiles[0].equipment.push('barbell')+ S.gymCards[0].code = '4006381333931'+ })+ const B = change(S0, 2000, S => {+ S.workouts[0].note = 'shoulder hurt'; stampWorkout(S.workouts[0], 2000)+ S.routines[0].name = 'Push heavy'+ S.customEx[0].n = 'Landmine press (1 arm)'+ S.gymCards[0].name = 'FitX Mitte'+ S.bodyweight = [{ d: '2026-10-06', w: 80, t: 2000 }]+ })+ for (const m of [mergeStates(A, B), mergeStates(B, A)]) {+ expect(m.workouts[0].entries[0].sets[0].w).toBe(110)+ expect(m.workouts[0].note).toBe('shoulder hurt')+ expect(m.routines[0]).toMatchObject({ name: 'Push heavy', ex: [{ id: 'bench', sets: 5 }] })+ expect(m.customEx[0]).toMatchObject({ n: 'Landmine press (1 arm)', media: { hash: 'abc' } })+ expect(m.equipProfiles[0].equipment).toEqual(['dumbbell', 'barbell'])+ expect(m.gymCards[0]).toMatchObject({ name: 'FitX Mitte', code: '4006381333931' })+ }+ })++ it('the same field edited on both: the later edit wins, a removal included', () => {+ const A = change(S0, 1000, S => { S.workouts[0].note = 'first'; stampWorkout(S.workouts[0], 1000) })+ const B = change(A, 2000, S => { delete S.workouts[0].note; stampWorkout(S.workouts[0], 2000) })+ const A2 = change(A, 3000, S => { S.bodyweight = [{ d: '2026-10-06', w: 80, t: 3000 }] })+ expect(mergeStates(A2, B).workouts[0].note).toBeUndefined()+ expect(mergeStates(B, A2).workouts[0].note).toBeUndefined()+ })++ it('a card or profile edited after a delete elsewhere is kept, as other entries are', () => {+ const del = change(S0, 1000, S => { S.gymCards = [] })+ const ed = change(S0, 2000, S => { S.gymCards[0].code = '1' })+ expect(ids(mergeStates(del, ed).gymCards)).toEqual(['g1'])+ })+})+describe('settings and plan days keep the change made last', () => {// B, offline, sets Wednesday and the rest timer; A logs a weigh-in later and flips the sound.const start = () => base({ _ts: 100, week: { 1: ['r1'] }, restSec: 90, sound: true, exNotes: { '0025': 'seat 4' } })