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: with storage full, the marker never runs ahead of the saved copy
Duarte Santos17c9113fb3ca
diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.jsindex 73041180a..747665372 100644--- a/frontend/src/store/useStore.js+++ b/frontend/src/store/useStore.js@@ -325,15 +325,22 @@ export const useStore = create((set, get) => {if (!m) { m = { base: readStoredSync(), owed: storedOwed() }; meta.set(S, m) }return m}+ // The saved copy is not the one in memory: its last write was refused (storage full). The+ // marker and the dirty flag then must not describe the copy in memory, or a restart loads the+ // older saved copy as one in step with a newer revision, calls it synced, and its next push+ // (a conditional one that matches) deletes from the server everything it received meanwhile.+ // Until a write lands again the marker is left out (a restart merges, its first-sync path) and+ // the copy stays owed.+ let storeLag = falseconst saveMarker = base => {try {- if (!base) { localStorage.removeItem(SYNC_KEY); return }+ if (!base || storeLag) { localStorage.removeItem(SYNC_KEY); return }const v = JSON.stringify(base)// Unchanged, it is not written again: other tabs react to this key moving (see below).if (localStorage.getItem(SYNC_KEY) !== v) localStorage.setItem(SYNC_KEY, v)} catch { /* storage refused — the copy in memory still knows */ }}- const saveOwed = owed => { try { if (owed) localStorage.setItem(DIRTY_KEY, '1'); else localStorage.removeItem(DIRTY_KEY) } catch { /* as above */ } }+ const saveOwed = owed => { try { if (owed || storeLag) localStorage.setItem(DIRTY_KEY, '1'); else localStorage.removeItem(DIRTY_KEY) } catch { /* as above */ } }// `wid`: the write id of the server's document at that revision (api/server.js PUT /api/data):// the same `rev` after the server went back in time names a different document.const writeSync = (rev, ts, wid) => {@@ -497,8 +504,11 @@ export const useStore = create((set, get) => {seenWid = widseenOwner = readOwner()toldNoRoom = false+ if (storeLag) { storeLag = false; saveMarker(base) } // the saved copy is this one again} catch (e) {saved = false+ storeLag = true+ try { localStorage.removeItem(SYNC_KEY) } catch { /* nothing to take back */ }if (!toldNoRoom) {toldNoRoom = trueimport('./useUI.js')diff --git a/frontend/src/store/useStore.quota.test.jsx b/frontend/src/store/useStore.quota.test.jsxnew file mode 100644index 000000000..efd4ad715--- /dev/null+++ b/frontend/src/store/useStore.quota.test.jsx@@ -0,0 +1,74 @@+// @vitest-environment happy-dom++/* Storage full: the copy can no longer be saved, but the marker and the dirty flag (small keys)+ still could. A pull adopted another device's workouts in memory and moved the marker to their+ revision; after a restart the older saved copy loaded as one in step with that revision, said+ "synced", and its next push (a matching conditional one) deleted those workouts from the+ server and from the other device. (QA 2026-10-06, also in v1.3.9.) */+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'++const h = vi.hoisted(() => ({ api: null }))+vi.mock('../lib/api.js', () => ({ api: (...a) => h.api(...a), setRemoteAuth: () => {} }))+vi.mock('./useUI.js', () => ({ useUI: { getState: () => ({ toast: () => {} }) } }))++const clone = v => JSON.parse(JSON.stringify(v))+const w = (id, d = '2026-09-01') => ({ id, d, start: 1, end: 2, entries: [] })+const ids = st => (st?.workouts || []).map(x => x.id).sort()+const httpError = (status, data) => Object.assign(new Error('HTTP ' + status), { status, data })++let srv+function server(doc) {+ srv = { doc: clone(doc) }+ h.api = async (path, opts = {}) => {+ const method = opts.method || 'GET'+ const body = opts.body ? JSON.parse(opts.body) : undefined+ if (method === 'GET' && path === '/api/data/rev') return { rev: srv.doc._rev }+ if (method === 'GET' && path === '/api/data') return { state: clone(srv.doc), rev: srv.doc._rev }+ if (method === 'PUT') {+ if (body.baseRev != null && body.baseRev !== srv.doc._rev) throw httpError(409, { error: 'conflict', rev: srv.doc._rev, state: clone(srv.doc) })+ srv.doc = { ...body.state, _rev: srv.doc._rev + 1 }+ return { ok: true, rev: srv.doc._rev }+ }+ throw new Error('unexpected ' + path)+ }+}+async function openApp() {+ vi.resetModules()+ const m = await import('./useStore.js')+ m.useStore.setState({ user: { id: 'u1' }, ready: true })+ return m+}++let full = false+const realSet = Storage.prototype.setItem+// (spied before the first write: happy-dom keeps the method a storage first used)+vi.spyOn(Storage.prototype, 'setItem').mockImplementation(function (k, v) {+ if (full && k === 'gym_state_v1') throw new DOMException('quota', 'QuotaExceededError')+ return realSet.call(this, k, v)+})+beforeEach(() => {+ localStorage.clear(); full = false+ localStorage.setItem('gym_owner', 'u1')+})+afterEach(() => { localStorage.clear() })++describe('a full storage never lets the marker run ahead of the saved copy', () => {+ it('a restart after a pull that could not be saved merges, and the other device\'s workouts stay', async () => {+ let m = await openApp()+ const DEF = m.DEF+ server({ ...clone(DEF), _ts: 100, workouts: [w('w1')], _rev: 1 })+ await m.useStore.getState().pullState() // in step at rev 1, saved+ full = true+ srv.doc = { ...clone(srv.doc), _ts: 200, workouts: [w('w1'), w('b1', '2026-09-02'), w('b2', '2026-09-03')], _rev: 2 }+ await m.useStore.getState().pullState() // adopted in memory, not saved+ expect(ids(m.useStore.getState().S)).toEqual(['b1', 'b2', 'w1'])+ // restart, still full+ m = await openApp()+ expect(ids(m.useStore.getState().S)).toEqual(['w1']) // the older saved copy+ await m.useStore.getState().pullState()+ m.useStore.getState().update(s => { s.restSec = 45 })+ await m.useStore.getState().pushState()+ expect(ids(srv.doc)).toEqual(['b1', 'b2', 'w1'])+ expect(srv.doc.restSec).toBe(45)+ })+})