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.

Sign-in: "Add them" no longer carries a guest's removals or reset into the account

Duarte Santos7e86aa48ec5a
diff --git a/frontend/src/lib/sync-merge.js b/frontend/src/lib/sync-merge.js
index 11843fcce..636467216 100644
--- a/frontend/src/lib/sync-merge.js
+++ b/frontend/src/lib/sync-merge.js
@@ -55,7 +55,7 @@
* clock runs behind, an entry with no date at all (sinceReset). Only a reset stamped before
* `resetIds` existed falls back to judging by time. The reset copy's settings and plan win
* whatever the `_ts`. `resetAt` only ever moves forward: the merge keeps the later one (with
- * its ids; the union of both for the same reset), with `prefer` too, and so do a replace in
+ * its ids; the union of both for the same reset; with `prefer`, the preferred side's), and so do a replace in
* the store and the server's PUT — a restored backup that carried no stamp would otherwise be
* taken for a copy older than the reset and wiped again. Before, a device with an unsent
* change got the 409, merged, and its union brought the whole wiped profile back. Not with
@@ -63,7 +63,8 @@
* account's history.
*
* After all of these, removals: an entry either copy records in `deleted` (stampDeletions) as
- * removed after it was last edited is left out, whatever the other copy still holds. Without it a
+ * removed after it was last edited is left out, whatever the other copy still holds. With
+ * `prefer` the reset stamp and the removals are the preferred side's only. Without it a
* device that came back online after any time away brought back every workout, routine, custom
* exercise, weigh-in or favourite the other device had removed meanwhile.
*/
@@ -501,9 +502,14 @@ export function mergeStates(a0, b0, { prefer } = {}) {
if (ra > rb) { b = sinceReset(b, ra, a.resetIds); side = 'a' }
else if (rb > ra) { a = sinceReset(a, rb, b.resetIds); side = 'b' }
}
- // The reset stamp only moves forward, with the names it wiped — with `prefer` too.
- const resetAt = Math.max(ra, rb)
- const resetIds = ra === rb ? (a0.resetIds || b0.resetIds ? mergeResetIds(a0.resetIds, b0.resetIds) : null)
+ // The reset stamp only moves forward, with the names it wiped. With `prefer` it is the
+ // preferred side's: a reset made on a guest copy, or on a phone in local mode, wiped that copy,
+ // not the account it signs in to — carried over, it made every other device of the account drop
+ // its unsent settings and plan as if the account had been reset (a backup import keeps the
+ // stamp of the copy it replaces on its own, keepReset).
+ const resetAt = prefer ? (prefer === 'a' ? ra : rb) : Math.max(ra, rb)
+ const resetIds = prefer ? (prefer === 'a' ? a0 : b0).resetIds || null
+ : ra === rb ? (a0.resetIds || b0.resetIds ? mergeResetIds(a0.resetIds, b0.resetIds) : null)
: (ra > rb ? a0 : b0).resetIds || null
// One unit before anything is compared.
let lead = null
@@ -584,7 +590,10 @@ export function mergeStates(a0, b0, { prefer } = {}) {
// What either device removed stays removed (the `deleted` section above). A workout taken out
// this way leaves its exercises' kept loads to be read again, as an edit of it would: from the
// merged history and from the deleting copy's own, which already let go of what it held.
- const deleted = mergeDeletions(a.deleted, b.deleted)
+ // With `prefer` only the preferred side's removals count: a guest's are about the guest's own
+ // entries, and keyed by day (weigh-ins) or exercise (favourites) they also named the account's
+ // weigh-in of that day and its favourite, which the guest never had.
+ const deleted = prefer ? mergeDeletions(prefer === 'a' ? a.deleted : b.deleted, null) : mergeDeletions(a.deleted, b.deleted)
for (const w of applyDeletions(out, deleted)) {
const k = String(workoutKey(w))
const by = [n, o].filter(S => Number(S.deleted?.workouts?.[k]) > 0).map(S => S.exWeights)
diff --git a/frontend/src/lib/sync-merge.test.js b/frontend/src/lib/sync-merge.test.js
index 9216b11f6..688f6e625 100644
--- a/frontend/src/lib/sync-merge.test.js
+++ b/frontend/src/lib/sync-merge.test.js
@@ -618,9 +618,12 @@ describe('a reset names what it wiped', () => {
expect(ids(m.customEx)).toEqual(['c-unstamped'])
})
- it('the stamp only moves forward: with prefer, and with a copy that carries none', () => {
+ it('the stamp only moves forward: kept by a backup merged over it (keepReset), and with a copy that carries none', () => {
const backup = base({ _ts: 5, workouts: [w('restored', 4)] })
- const m = mergeStates(backup, reset, { prefer: 'a' })
+ // with prefer the merge itself keeps the preferred side's stamp (a guest's reset is not the
+ // account's); the import puts the replaced copy's back with keepReset
+ expect(mergeStates(backup, reset, { prefer: 'a' }).resetAt).toBeUndefined()
+ const m = keepReset(reset, mergeStates(backup, reset, { prefer: 'a' }))
expect(m.resetAt).toBe(R)
expect(m.resetIds).toEqual(reset.resetIds)
expect(ids(m.workouts)).toEqual(['restored'])
diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js
index 7ca9cf9c1..233ee36a4 100644
--- a/frontend/src/store/useStore.js
+++ b/frontend/src/store/useStore.js
@@ -1121,7 +1121,9 @@ export const useStore = create((set, get) => {
const next = Object.assign(clone(DEF), backup)
if (!mergeWith?.state || !get().user) { get().replaceState(next, !!get().user); return }
const others = mergeWith.local ? mergeStates(get().S, mergeWith.state) : mergeWith.state
- const merged = keepReset(get().S, Object.assign(clone(DEF), mergeStates(next, others, { prefer: 'a' })))
+ // The merge keeps the backup's reset stamp (prefer); a later one this copy or the server's
+ // holds stays, as it does on a replace.
+ const merged = keepReset(others, keepReset(get().S, Object.assign(clone(DEF), mergeStates(next, others, { prefer: 'a' }))))
merged.active = next.active || null
persist(merged, true)
if (mergeWith.rev != null) writeSync(mergeWith.rev, 0)
diff --git a/frontend/src/store/useStore.signin-safety.test.jsx b/frontend/src/store/useStore.signin-safety.test.jsx
new file mode 100644
index 000000000..ea834bcea
--- /dev/null
+++ b/frontend/src/store/useStore.signin-safety.test.jsx
@@ -0,0 +1,70 @@
+// @vitest-environment happy-dom
+
+/* Signing in with "Add them" merges a guest's (or a local-mode phone's) copy into the account.
+ What that copy recorded about itself must not be applied to the account: its removals are
+ keyed by day (weigh-ins) and exercise (favourites), and named the account's own entries; its
+ "Reset everything" stamp made every other device of the account drop its unsent settings and
+ plan as if the account had been reset. (QA 2026-10-06.) */
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+vi.mock('../lib/api.js', () => ({ api: vi.fn(), setRemoteAuth: vi.fn() }))
+vi.mock('./useUI.js', () => ({ useUI: { getState: () => ({ toast: vi.fn() }) } }))
+
+import { api } from '../lib/api.js'
+import { DEF, useStore } from './useStore.js'
+import { mergeStates, stampEdits } from '../lib/sync-merge.js'
+
+const clone = v => JSON.parse(JSON.stringify(v))
+const puts = () => api.mock.calls.filter(([, o]) => o?.method === 'PUT').map(([, o]) => JSON.parse(o.body))
+const up = f => useStore.getState().update(f)
+
+beforeEach(() => { localStorage.clear(); api.mockReset(); useStore.setState({ S: { ...clone(DEF), _ts: 1 }, user: null, ready: true }) })
+afterEach(() => { localStorage.clear() })
+
+describe('sign-in "Add them" keeps the account\'s own entries and history', () => {
+ it('a guest\'s removed weigh-in and unstarred favourite do not delete the account\'s', async () => {
+ const t0 = Date.now() - 30 * 86400e3
+ up(s => { s.bodyweight = [{ d: '2026-09-01', w: 8.2, t: Date.now() }] })
+ up(s => { s.bodyweight = [] })
+ up(s => { s.favEx = ['0025'] })
+ up(s => { s.favEx = [] })
+ up(s => { s.workouts = [{ id: 'gw1', d: '2026-10-05', start: Date.now() - 3600e3, end: Date.now(), entries: [] }] })
+ const server = { ...clone(DEF), _ts: t0, workouts: [], favEx: ['0025', '0047'],
+ bodyweight: [{ d: '2026-08-31', w: 82, t: t0 }, { d: '2026-09-01', w: 81.8, t: t0 }, { d: '2026-09-02', w: 81.5, t: t0 }], _rev: 7 }
+ api.mockImplementation(async (p, o) => (o?.method === 'PUT' ? { ok: true, rev: 8 } : { state: clone(server), rev: 7 }))
+ useStore.getState().setUser({ id: 'u1', name: 'x' }, { adopt: true })
+ await useStore.getState().adoptProfile(async () => true)
+ const S = useStore.getState().S
+ expect(S.bodyweight.map(b => b.d)).toEqual(['2026-08-31', '2026-09-01', '2026-09-02'])
+ expect(S.favEx).toContain('0025')
+ const put = puts().at(-1)
+ expect(put.state.bodyweight.map(b => b.d)).toEqual(['2026-08-31', '2026-09-01', '2026-09-02'])
+ expect(put.state.favEx).toContain('0025')
+ expect(S.workouts.map(w => w.id)).toContain('gw1')
+ })
+
+ it('a guest\'s "Reset everything" does not become the account\'s reset', async () => {
+ const t0 = Date.now() - 10 * 86400e3
+ up(s => { s.favEx = ['0025']; s.exNotes = { '0025': 'test' }; s.workouts = [{ id: 'gw0', d: '2026-09-30', start: 1, entries: [] }] })
+ await useStore.getState().resetEverything()
+ up(s => { s.workouts = [{ id: 'gw1', d: '2026-10-05', start: Date.now() - 3600e3, end: Date.now(), entries: [] }] })
+ const server = { ...clone(DEF), _ts: t0, workouts: [{ id: 'sw1', d: '2026-09-20', start: t0, end: t0 + 1, entries: [] }],
+ favEx: ['0025'], exNotes: { '0025': 'seat 4, pin 7' }, week: { 1: 'r1' }, routines: [{ id: 'r1', name: 'A', ex: [], _ts: t0 }], _rev: 7 }
+ api.mockImplementation(async (p, o) => (o?.method === 'PUT' ? { ok: true, rev: 8 } : { state: clone(server), rev: 7 }))
+ useStore.getState().setUser({ id: 'u1', name: 'x' }, { adopt: true })
+ await useStore.getState().adoptProfile(async () => true)
+ const put = puts().at(-1)
+ expect(put.state.resetAt).toBeUndefined()
+ expect(put.state.resetIds).toBeUndefined()
+ // device B of the account with offline plan, note and setting changes merges the pushed copy
+ const prevB = clone(server)
+ const B = clone(prevB)
+ B.week = { 1: 'r1', 3: 'r1' }; B.exNotes = { '0025': 'seat 5 now', '0047': 'grip wide' }; B.restSec = 150
+ stampEdits(prevB, B, Date.now()); B._ts = Date.now()
+ const merged = mergeStates(B, { ...put.state, _rev: 8 })
+ expect(merged.week).toEqual({ 1: 'r1', 3: 'r1' })
+ expect(merged.restSec).toBe(150)
+ expect(merged.exNotes['0025']).toBe('seat 5 now')
+ expect(merged.favEx).toContain('0025')
+ })
+})