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
Data safety: switching quality or figure never touches the profile
Duarte Santos3c91fc09e1f5
diff --git a/frontend/src/lib/anim-data-safety.test.js b/frontend/src/lib/anim-data-safety.test.jsnew file mode 100644index 000000000..3541de6c3--- /dev/null+++ b/frontend/src/lib/anim-data-safety.test.js@@ -0,0 +1,122 @@+// @vitest-environment happy-dom+import { beforeEach, describe, expect, it, vi } from 'vitest'++/* DATA SAFETY. The animation quality and the figure only ever change which picture shows: the+ profile, its workouts, routines and history are never touched, and every exercise anything+ refers to keeps a picture at every quality and on both figures. Run against a synthetic profile+ in the shape of real ones (fixtures/anim-safety-state.json: v1.3 ids, a drawing id stored as an+ exercise id, twins drawn on both figures, a female exercise with a male twin, custom exercises+ with and without a photo, one deleted on another device, an id no catalogue knows). The quality+ choice lives in this device's localStorage under one key and nowhere else. */+vi.mock('./mobile.js', () => ({ MOBILE: true }))+vi.mock('@capacitor/core', () => {+ const impl = {+ available: async () => ({ available: true }),+ status: async () => ({ state: 'ready', bytes: 1 }),+ localPath: async ({ packId }) => ({ path: '/packs/' + packId }),+ download: async () => {}, remove: async () => {}, addListener: () => ({ remove() {} }),+ }+ return {+ registerPlugin: () => new Proxy({}, { get: (_, k) => impl[k] || (() => new Promise(() => {})) }),+ Capacitor: { convertFileSrc: p => 'app://files' + p },+ }+})++import FIXTURE from './fixtures/anim-safety-state.json' with { type: 'json' }+import { EXIDX, exOr, isCustomEx, registerCustom, gifSrc, imgSrc, figureOf, canonicalExId } from './exercises.js'+import { PACK_QUALITIES, packId, clipSources, packDrawingId } from './anim-quality.js'+import * as packs from './anim-packs.js'++const CHOICES = ['auto', 'still', 'builtin', ...PACK_QUALITIES]+const FIGS = ['male', 'female']+const deepFreeze = o => { if (o && typeof o === 'object' && !Object.isFrozen(o)) { Object.freeze(o); Object.values(o).forEach(deepFreeze) } return o }+const all = fig => Object.fromEntries(PACK_QUALITIES.map(px => [packId(px, fig), `app://files/packs/${packId(px, fig)}`]))+const INSTALLED = { none: () => ({}), all, half: fig => ({ [packId(360, fig)]: `app://files/packs/${packId(360, fig)}` }), other: fig => all(fig === 'male' ? 'female' : 'male') }++/** Every exercise id the profile refers to anywhere. */+export function referencedIds(S) {+ const ids = new Set()+ for (const w of S.workouts || []) for (const e of w.entries || []) ids.add(e.id)+ for (const w of S.workouts || []) for (const id of w.prs || []) ids.add(id)+ for (const r of S.routines || []) for (const e of r.ex || []) ids.add(e.id)+ for (const k of Object.keys(S.exWeights || {})) ids.add(k)+ for (const id of S.favEx || []) ids.add(id)+ for (const k of Object.keys(S.exNotes || {})) ids.add(k)+ return [...ids]+}++/** What the media for one id is, at one choice and figure: the same split Media.jsx makes. */+function pictureFor(id, { choice, figure, installed }) {+ const ex = exOr(id)+ if (isCustomEx(ex)) return { kind: 'custom', ex }+ if (ex.missing) return { kind: 'missing', ex }+ return { kind: 'catalogue', ex, stillSrc: imgSrc(ex, figure), ...clipSources({ ex, figure, choice, device: { shortSide: 390 }, installed, builtin: gifSrc(ex, figure) }) }+}++export function assertEveryPictureResolves(S) {+ registerCustom(S.customEx)+ const counts = { catalogue: 0, custom: 0, missing: 0 }+ for (const id of referencedIds(S)) {+ for (const choice of CHOICES) for (const figure of FIGS) for (const [name, inst] of Object.entries(INSTALLED)) {+ const p = pictureFor(id, { choice, figure, installed: inst(figure) })+ const where = `${id} ${choice} ${figure} ${name}`+ if (p.kind === 'catalogue') {+ expect(p.stillSrc, where).toMatch(/^exercise-media\/still\/\d{4,5}\.webp$/)+ if (choice === 'still') { expect(p.sources, where).toEqual([]); continue }+ expect(p.sources.length, where).toBeGreaterThan(0)+ expect(p.sources.at(-1), where).toBe(gifSrc(p.ex, figure))+ // Never another figure's pack, never a drawing the figure does not show.+ for (const src of p.sources.slice(0, -1)) {+ expect(src, where).toContain(`-${figure}/`)+ expect(src.endsWith('/' + packDrawingId(p.ex, figure) + '.webp'), where).toBe(true)+ }+ } else if (p.kind === 'custom') {+ // Custom media keep their own path (components/CustomMedia.jsx): never a pack, never the catalogue's.+ expect(p.ex.custom === true || S.customEx.some(c => c.id === id), where).toBe(true)+ } else {+ expect(p.ex.missing, where).toBe(true)+ }+ if (choice === 'auto' && figure === 'male' && name === 'none') counts[p.kind]+++ }+ }+ return counts+}++beforeEach(() => { localStorage.clear(); packs._resetAnimPacks() })++describe('every exercise the profile refers to has a picture, at every quality and on both figures', () => {+ it('the synthetic profile: catalogue, v1.3, alias, twins, custom with and without media, deleted, unknown', () => {+ const counts = assertEveryPictureResolves(structuredClone(FIXTURE))+ expect(counts).toEqual({ catalogue: 5, custom: 2, missing: 2 })+ })+ it('a drawing id stored as an exercise id still reads as its exercise, and is left as it is', () => {+ expect(EXIDX['2476']).toBe(EXIDX['0001'])+ expect(canonicalExId('2476')).toBe('0001')+ expect(FIXTURE.workouts[1].entries[0].id).toBe('2476')+ })+})++describe('switching quality and figure never touches the profile', () => {+ it('the state is deep-equal before and after, and only the device key is written', async () => {+ const S = deepFreeze(structuredClone(FIXTURE))+ const before = JSON.stringify(S)+ const keysBefore = Object.keys(localStorage)+ await packs.refresh()+ for (const choice of CHOICES) {+ packs.writePrefs({ choice })+ for (const figure of FIGS) {+ // what a figure switch reads: S.exFigure, never written by any of this+ figureOf({ ...S, exFigure: figure })+ await packs.ensureWanted(figure, { shortSide: 1024 })+ for (const id of referencedIds(S)) pictureFor(id, { choice, figure, installed: packs.installedBases() })+ }+ }+ packs.writePrefs({ wifiOnly: false })+ expect(JSON.stringify(S)).toBe(before)+ expect(Object.keys(localStorage).filter(k => !keysBefore.includes(k))).toEqual([packs.PREFS_KEY])+ expect(JSON.parse(localStorage.getItem(packs.PREFS_KEY))).toEqual({ choice: 1080, wifiOnly: false })+ })+ it('nothing about the quality is kept in the profile that syncs', () => {+ expect(JSON.stringify(FIXTURE)).not.toMatch(/gym_anim|quality|anim-\d+/)+ })+})diff --git a/frontend/src/lib/fixtures/anim-safety-state.json b/frontend/src/lib/fixtures/anim-safety-state.jsonnew file mode 100644index 000000000..08b935c1f--- /dev/null+++ b/frontend/src/lib/fixtures/anim-safety-state.json@@ -0,0 +1,30 @@+{+ "_comment": "Synthetic profile in the shape of real ones, for anim-data-safety.test.js: v1.3 ids, a drawing id stored as an exercise id (an alias), exercises drawn on both figures and once, a female exercise with a male twin, custom exercises with and without a photo, one deleted on another device, an id no catalogue knows.",+ "_rev": 41, "_ts": 1760300000000, "unit": "kg", "body": "female", "exFigure": "female", "gifSize": "full",+ "customEx": [+ { "id": "cu_grip", "n": "grip trainer", "bp": "lower arms", "eq": "custom", "custom": true, "tg": "", "desc": "",+ "media": { "kind": "image", "hash": "a3f1c2d4e5b60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f9", "mime": "image/jpeg", "size": 48213, "width": 640, "height": 480, "at": 1759000000000 }, "_ts": 1759000000000 },+ { "id": "cu_plain", "n": "farmer walk (park)", "bp": "full body", "eq": "custom", "tg": "", "_ts": 1758000000000 }+ ],+ "routines": [+ { "id": "r_a", "name": "Upper A", "emoji": "💪", "ex": [ { "id": "0025", "sets": 3, "reps": 8 }, { "id": "0001", "sets": 3, "reps": 15 }, { "id": "cu_grip", "sets": 2, "reps": 20 } ] },+ { "id": "r_b", "name": "Lower B", "ex": [ { "id": "0980", "sets": 3, "reps": 10 }, { "id": "5965", "sets": 2, "reps": 30 }, { "id": "2476", "sets": 3, "reps": 12 } ] }+ ],+ "week": { "1": ["r_a"], "3": ["r_b"], "5": ["r_a", "r_b"] },+ "workouts": [+ { "id": "w1", "d": "2025-07-21", "start": 1753100000000, "end": 1753103600000, "name": "Upper A", "routineId": "r_a",+ "entries": [ { "id": "0025", "sets": [ { "w": 60, "r": 8, "done": true }, { "w": 62.5, "r": 8, "done": true } ] },+ { "id": "0001", "sets": [ { "w": 0, "r": 15, "done": true } ] },+ { "id": "cu_grip", "sets": [ { "w": 0, "r": 20, "done": true } ] } ] },+ { "id": "w2", "d": "2025-09-03", "start": 1756900000000, "routineId": "r_b",+ "entries": [ { "id": "2476", "sets": [ { "w": 0, "r": 12, "done": true } ] }, { "id": "cu_gone", "sets": [ { "w": 20, "r": 10, "done": true } ] },+ { "id": "zz_not_a_catalogue_id", "sets": [ { "w": 5, "r": 5 } ] }, { "id": "0980", "sets": [ { "w": 40, "r": 10, "done": true } ] } ],+ "prs": ["0980"] },+ { "id": "w3", "d": "2026-10-01", "start": 1759300000000, "entries": [ { "id": "5965", "sets": [ { "t": 30, "done": true } ] }, { "id": "cu_plain", "sets": [ { "w": 32, "r": 1, "done": true } ] } ] }+ ],+ "exWeights": { "0025": 62.5, "0980": 40, "2476": 0, "cu_grip": 0, "zz_not_a_catalogue_id": 5 },+ "favEx": ["0025", "2476", "cu_grip"],+ "exNotes": { "0025": { "text": "elbows in", "_ts": 1 } },+ "bodyweight": [ { "d": "2026-10-01", "w": 63.4 } ],+ "dayNotes": { "2026-10-02": { "tag": "sick", "_ts": 2 } }+}