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-out: a sign-in still held (its server read failed) no longer wipes the guest's workouts

Duarte Santosa9c59369a80d
diff --git a/frontend/src/store/useStore.hold-signout.test.jsx b/frontend/src/store/useStore.hold-signout.test.jsx
new file mode 100644
index 000000000..21a459c9b
--- /dev/null
+++ b/frontend/src/store/useStore.hold-signout.test.jsx
@@ -0,0 +1,52 @@
+// @vitest-environment happy-dom
+
+/* A sign-in whose adoption could not read the server (a 502, a dropped connection) holds sync. A
+ sign-out in that state counted nothing as owed and wiped the guest's workouts and whatever was
+ logged during the hold, with nothing stashed. (QA 2026-10-06, also in v1.3.9.) */
+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(), stopRest: vi.fn(), abandonWork: vi.fn() }) } }))
+vi.mock('../sheets.jsx', () => ({ askAddDeviceData: async () => true }))
+
+import { api } from '../lib/api.js'
+import { DEF, useStore } from './useStore.js'
+
+const clone = v => JSON.parse(JSON.stringify(v))
+const w = (id, d = '2026-09-01') => ({ id, d, start: 1, end: 2, entries: [] })
+const ids = xs => (xs || []).map(x => x.id).sort()
+const badGateway = () => Object.assign(new Error('HTTP 502'), { status: 502 })
+
+beforeEach(() => { localStorage.clear(); api.mockReset(); useStore.setState({ S: { ...clone(DEF), _ts: 1 }, user: null, ready: true }) })
+afterEach(() => { localStorage.clear(); useStore.setState({ S: clone(DEF), user: null, ready: false }) })
+
+describe('signing out while a sign-in is held', () => {
+ it('asks first, keeps the guest\'s and the held copy\'s workouts, and adds them back on the next sign-in', async () => {
+ useStore.getState().update(s => { s.workouts = [w('g1'), w('g2', '2026-09-02')] })
+ const U = { id: 'u1', name: 'Olga' }
+ useStore.getState().setUser(U, { adopt: true })
+ api.mockRejectedValueOnce(badGateway())
+ await expect(useStore.getState().adoptProfile(async () => true)).rejects.toThrow()
+ useStore.getState().update(s => { s.workouts.push(w('h1', '2026-09-03')) }) // trains during the hold
+
+ expect(useStore.getState().unsyncedChanges().owed).toBe(true)
+ api.mockResolvedValue({ ok: true })
+ const asked = await useStore.getState().signOut()
+ expect(asked.owed).toBe(true)
+ expect(ids(useStore.getState().S.workouts)).toEqual(['g1', 'g2', 'h1']) // nothing wiped yet
+ const forced = await useStore.getState().signOut({ force: true })
+ expect(forced.stashed).toBe(true)
+ const stash = Object.values(JSON.parse(localStorage.getItem('gym_stash')))[0]
+ expect(ids(stash.state.workouts)).toEqual(['g1', 'g2', 'h1'])
+
+ // back as the same account: the server's profile, with the kept workouts added to it
+ const server = { ...clone(DEF), _ts: 50, restSec: 70, workouts: [w('w1', '2026-08-01')], _rev: 3 }
+ api.mockReset()
+ api.mockImplementation(async (p, o) => (o?.method === 'PUT' ? { ok: true, rev: 4 } : { state: clone(server), rev: 3 }))
+ useStore.getState().setUser(U, { adopt: true })
+ await useStore.getState().adoptProfile(async () => false)
+ const S = useStore.getState().S
+ expect(ids(S.workouts)).toEqual(['g1', 'g2', 'h1', 'w1'])
+ expect(S.restSec).toBe(70) // the account's settings
+ })
+})
diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js
index 747665372..34525bcaa 100644
--- a/frontend/src/store/useStore.js
+++ b/frontend/src/store/useStore.js
@@ -354,7 +354,10 @@ export const useStore = create((set, get) => {
// the debounce or in flight, a change made while boot was pulling, or a copy changed since the
// revision it descends from.
const localChanged = (S = get().S) => { const { base } = metaOf(S); return !!base && (S._ts || 0) > (base.ts || 0) }
- const owes = () => metaOf().owed || pushPending || pushTm !== null || !!pushing || localChanged()
+ // A sign-in whose question is still open (adoptHold, e.g. its GET failed) holds a copy that has
+ // reached no server yet: with any data in it, it is owed, so a sign-out asks and keeps it aside
+ // instead of wiping the guest's history and what was logged during the hold.
+ const owes = () => metaOf().owed || pushPending || pushTm !== null || !!pushing || localChanged() || (adoptHold && hasData(get().S))
/* The connection as the screens show it (components/SyncBanner.jsx, Settings). The flags are set
where each outcome is known; `status` is derived from them here, once, so no screen has to
@@ -872,7 +875,10 @@ export const useStore = create((set, get) => {
const prev = all[key]?.state
const state = prev ? mergeStates(get().S, prev) : clone(get().S)
state.active = get().S.active || prev?.active || null
- all[key] = { server: server || null, uid: user.id, name: user.name || '', at: Date.now(), state }
+ // Kept while a sign-in's question was open: a copy that was never this account's, added back
+ // like an answered "Add them" (applyStash), the account's settings and history first.
+ const adopt = adoptHold || !!all[key]?.adopt
+ all[key] = { server: server || null, uid: user.id, name: user.name || '', at: Date.now(), state, ...(adopt ? { adopt: true } : {}) }
return writeStashes(all)
}
// A workout running here is never on the server (a push leaves `active` out), so a sign-out
@@ -915,7 +921,7 @@ export const useStore = create((set, get) => {
if (!keys.length) return
const S = get().S
let merged = S
- for (const k of keys) merged = mergeStates(merged, all[k].state)
+ for (const k of keys) merged = all[k].adopt ? mergeStates(merged, all[k].state, { prefer: 'a' }) : mergeStates(merged, all[k].state)
merged = Object.assign(clone(DEF), merged)
merged.active = S.active || keys.map(k => all[k].state?.active).find(Boolean) || null
persist(merged, false)