Skip to content

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.

Sync: an Undo wins a same-stamp tie on every device; tests run on set clocks (Node 22 CI)

Duarte Santos502abd983fcf
diff --git a/frontend/src/lib/sync-merge.js b/frontend/src/lib/sync-merge.js
index 9c9c9e04b..e6de6742f 100644
--- a/frontend/src/lib/sync-merge.js
+++ b/frontend/src/lib/sync-merge.js
@@ -247,10 +247,13 @@ const undoOf = (marks, k, at) => {
}
// Between two stamps of one field, `x` (marked `ux`) and `y` (marked `uy`): which side's value
// stays ('x', 'y' or '' on a tie) and the stamp it carries. See above.
+// An Undo and a change made on the removal can carry the same stamp when both clocks were lifted
+// from the removal's (stampChange: one past the highest stamp). The Undo wins that tie, whichever
+// copy is first: a first-wins tie kept a different value on each device, for good.
function undoPick(tx, ux, ty, uy) {
if (tx > ty) return ux && ty > Number(ux[0]) && ty < Number(ux[1]) ? ['y', tx + 1] : ['x', tx]
if (ty > tx) return uy && tx > Number(uy[0]) && tx < Number(uy[1]) ? ['x', ty + 1] : ['y', ty]
- return ['', tx]
+ return ux && !uy ? ['x', tx] : uy && !ux ? ['y', ty] : ['', tx]
}
// `marks` brought up to date with `stamps` after a change stamped `now`: a pending marker
// (`[base, del]`, set by the Undo itself) of a field this change stamped gets `now`; one whose field
@@ -674,16 +677,18 @@ export function mergeEdits(a, b) {
/**
* Into `out` (a merge built from `n`, the newer copy), every stamped field and plan day from the
- * copy that changed it last, removal included. A tie or a field nobody stamped stays as it is.
+ * copy that changed it last, removal included. A tie (but one an Undo wins, undoPick) or a field
+ * nobody stamped stays as it is.
*/
function applyEdits(out, n, o) {
const en = isMap(n.edited) ? n.edited : {}, eo = isMap(o.edited) ? o.edited : {}
const bumps = {}
for (const k of new Set([...Object.keys(en), ...Object.keys(eo)])) {
const tn = Number(en[k]) || 0, to = Number(eo[k]) || 0
- if (tn === to) continue
+ const un = undoOf(n.undone, k, tn), uo = undoOf(o.undone, k, to)
+ if (tn === to && !un === !uo) continue
// An Undo's marker can hand the field to the side whose stamp is older (see "Undo" above).
- const [side, at] = undoPick(tn, undoOf(n.undone, k, tn), to, undoOf(o.undone, k, to))
+ const [side, at] = undoPick(tn, un, to, uo)
if (at > Math.max(tn, to)) bumps[k] = at
const src = side === 'y' ? o : n
const dot = k.indexOf('.')
diff --git a/frontend/src/views/CoachChat.demo-failure.test.jsx b/frontend/src/views/CoachChat.demo-failure.test.jsx
index 4a1861647..2a176e3d7 100644
--- a/frontend/src/views/CoachChat.demo-failure.test.jsx
+++ b/frontend/src/views/CoachChat.demo-failure.test.jsx
@@ -4,6 +4,9 @@ import { parseHTML } from 'linkedom'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import CoachChat from './CoachChat.jsx'
import { CONSENT_VERSION } from '../lib/coach.js' // the consent a fixture grants is the current one, whatever its number
+// Loaded up front: coach-api.js imports it on first use, and a cold load (transform, file reads)
+// outlasted `flush` below on a fast runner (Node 22 on CI), so the first ask was not in yet.
+import '../lib/coach-demo.js'
// The demo Coach's failure state, end to end. Every other CoachChat test mocks lib/coach-api.js;
// this one runs the real thing with DEMO forced on, so the chain under test is exactly what the
diff --git a/frontend/src/views/undo-concurrent.test.jsx b/frontend/src/views/undo-concurrent.test.jsx
index 08eb711f3..2919d0efa 100644
--- a/frontend/src/views/undo-concurrent.test.jsx
+++ b/frontend/src/views/undo-concurrent.test.jsx
@@ -40,9 +40,13 @@ const shared = () => {
return change(base, s => { s.restSec = 100 }, T0 - 9_000)
}
const both = (x, y) => [mergeStates(x, y), mergeStates(y, x)]
+// The device's clock, set per step: with the real one, steps a fast machine runs in the same
+// millisecond got equal stamps (Node 22 on CI), and the order these tests are about was a tie.
+const T1 = Date.now()
+const clockAt = ms => { vi.useFakeTimers({ toFake: ['Date'] }); vi.setSystemTime(ms) }
beforeEach(() => { localStorage.clear(); useStore.setState({ S: shared(), user: null }) })
-afterEach(() => { localStorage.clear() })
+afterEach(() => { vi.useRealTimers(); localStorage.clear() })
describe('Undo of an exercise swiped out of a routine', () => {
it('a set count changed offline on another device before it saw the removal survives', () => {
@@ -62,12 +66,24 @@ describe('Undo of an exercise swiped out of a routine', () => {
for (const m of both(mergeStates(here, removed), other)) expect(m.routines[0].ex[2].sets).toBe(6)
})
it('a change made after the other device saw the removal still loses to the Undo', () => {
+ clockAt(T1)
removeRoutineExercise('a', 0)
const removed = clone(S())
- const other = change(removed, s => { s.routines[0].ex[1].sets = 9 }, Date.now()) // on the list without it
+ const other = change(removed, s => { s.routines[0].ex[1].sets = 9 }, T1 + 1000) // on the list without it
+ clockAt(T1 + 2000)
undo()
for (const m of both(S(), other)) expect(m.routines[0].ex.map(e => `${e.id}x${e.sets}`)).toEqual(['0025x3', '0198x3', '0586x3'])
})
+ it('a change on the removal stamped the same as the Undo loses to it on every device', () => {
+ // both clocks behind, so both lifted to one past the removal's stamp (stampChange)
+ clockAt(T1)
+ removeRoutineExercise('a', 0)
+ const removed = clone(S())
+ const other = change(removed, s => { s.routines[0].ex[1].sets = 9 }, T1 - 5000)
+ undo()
+ expect(S().routines[0]._ts).toBe(other.routines[0]._ts)
+ for (const m of both(S(), other)) expect(m.routines[0].ex.map(e => `${e.id}x${e.sets}`)).toEqual(['0025x3', '0198x3', '0586x3'])
+ })
it('an Undo after another edit of the routine puts the exercise back without claiming the rest', () => {
removeRoutineExercise('a', 0)
useStore.getState().update(s => { s.routines[0].ex[0].sets = 4 })
@@ -80,14 +96,18 @@ describe('Undo of an exercise swiped out of a routine', () => {
describe('Undo of a routine swiped out of the loop', () => {
const loop = s => s.queue?.ids?.join(',')
const setSeq = ids => useStore.getState().update(s => { saveRotation(s, ids, 'Loop') })
+ const reorder = s => { const ids = ['c', 'a', 'b']; s.rotation = { ...s.rotation, sequence: ids }; s.queue = { ...s.queue, ids } }
beforeEach(() => {
+ clockAt(T1)
useStore.getState().update(s => { s.scheduleMode = 'rotation'; saveRotation(s, ['a', 'b', 'c'], 'Loop') })
})
it('a reorder made offline on another device before it saw the removal survives', () => {
const base = clone(S())
- const other = change(base, s => { const ids = ['c', 'a', 'b']; s.rotation = { ...s.rotation, sequence: ids }; s.queue = { ...s.queue, ids } }, Date.now() - 1000)
+ const other = change(base, reorder, T1 + 1000)
+ clockAt(T1 + 2000)
expect(takeOutOfLoop('b', setSeq, ['a', 'b', 'c'], 'Loop')).toBe(true)
const removed = clone(S())
+ clockAt(T1 + 3000)
undo()
expect(loop(S())).toBe('a,b,c')
for (const m of both(S(), other)) {
@@ -96,6 +116,17 @@ describe('Undo of a routine swiped out of the loop', () => {
for (const n of both(m, removed)) expect(loop(n)).toBe('c,a,b')
}
})
+ it('a reorder on the removal stamped the same as the Undo loses to it on every device', () => {
+ clockAt(T1 + 2000)
+ expect(takeOutOfLoop('b', setSeq, ['a', 'b', 'c'], 'Loop')).toBe(true)
+ const other = change(clone(S()), reorder, T1) // clock behind: lifted past the removal
+ undo()
+ expect(S().edited.queue).toBe(other.edited.queue)
+ for (const m of both(S(), other)) {
+ expect(loop(m)).toBe('a,b,c')
+ expect(m.rotation.sequence.join(',')).toBe('a,b,c')
+ }
+ })
})
describe('Undo of a routine swiped away in Plan, with an older app (v1.3.9) on the account', () => {