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
fix(exercises): a drawing id on its way in is stored as the exercise it draws
Duarte Santosfe780d2c2db1
diff --git a/frontend/src/lib/coach.js b/frontend/src/lib/coach.jsindex cecb865f2..b9dcb1331 100644--- a/frontend/src/lib/coach.js+++ b/frontend/src/lib/coach.js@@ -12,7 +12,7 @@// The screens that call it arrive in PR 4. Nothing in this file needs one to be tested, which// is the point of it being separate from them.-import { EXIDX } from './exercises.js'+import { EXIDX, canonicalExId } from './exercises.js'import { modeOf, isBw, isPerSide, cleanupSg } from './history.js'import { uid, todayISO, DAYN } from './format.js'import { mergePlan } from './plan-share.js'@@ -425,7 +425,8 @@ const CHANGE_APPLY = {'add-exercise': (s, c) => {const r = need(findRoutine(s, c.target.routineId))const a = c.after || {}- const e = { id: a.id, sets: a.sets || 3, mode: a.mode || 'reps' }+ // A drawing's id the Coach may have picked up names the exercise it draws.+ const e = { id: canonicalExId(a.id), sets: a.sets || 3, mode: a.mode || 'reps' }if (e.mode === 'cardio') { e.min = a.min || 20; e.speed = a.speed || 8 }else if (e.mode === 'time') e.sec = a.sec || 45else e.reps = a.reps || 10@@ -460,7 +461,7 @@ const CHANGE_APPLY = {// app halve a rep count that was never per-side, so an explicit flag is dropped and the new// exercise goes back to whatever the catalogue says about it.const { bodyweight, side, ...keep } = old- r.ex[i] = { ...keep, id: a.id, ...(a.sets ? { sets: a.sets } : {}), ...(a.reps ? { reps: a.reps } : {}), ...(a.weight > 0 ? { weight: a.weight } : {}) }+ r.ex[i] = { ...keep, id: canonicalExId(a.id), ...(a.sets ? { sets: a.sets } : {}), ...(a.reps ? { reps: a.reps } : {}), ...(a.weight > 0 ? { weight: a.weight } : {}) }},sets: (s, c) => { need(findExIn(s, c)).sets = c.after },reps: (s, c) => { need(findExIn(s, c)).reps = c.after },@@ -510,7 +511,7 @@ const CHANGE_APPLY = {id: uid(), name: a.name, emoji: a.emoji || '🏋️',...(POLICIES.includes(a.prog) ? { prog: a.prog } : {}),ex: a.ex.map(e => ({- id: e.id, sets: e.sets || 3, mode: e.mode || 'reps',+ id: canonicalExId(e.id), sets: e.sets || 3, mode: e.mode || 'reps',...(e.mode === 'time' ? { sec: e.sec || 45 } : { reps: e.reps || 10 }),...(Number.isInteger(e.repsMax) ? { repsMax: e.repsMax } : {}),...(e.bodyweight != null ? { bodyweight: !!e.bodyweight } : {}),diff --git a/frontend/src/lib/exercise-alias-ids.test.js b/frontend/src/lib/exercise-alias-ids.test.jsnew file mode 100644index 000000000..8c5c26314--- /dev/null+++ b/frontend/src/lib/exercise-alias-ids.test.js@@ -0,0 +1,75 @@+/* The id of a drawing that is no exercise of its own (the female figure of a male exercise,+ EXALIAS) looks up the exercise it draws, and a plan file, an import or the Coach may carry one.+ On the way in it becomes the exercise's own id, so the app goes on storing exercise ids only;+ ids already in state are never rewritten. */+import { afterEach, describe, expect, it } from 'vitest'+import { EXALIAS } from './exercises-data.js'+import { EXIDX, canonicalExId } from './exercises.js'+import { mergePlan, parsePlan } from './plan-share.js'+import { applyChangeSet, CONSENT_VERSION } from './coach.js'+import { HEVY_ID_MAP, parseHevyWorkouts } from './import-hevy.js'++const [ALIAS, TO] = Object.entries(EXALIAS).find(([id, to]) => EXIDX[to] && !Object.keys(EXIDX).includes(id))+const state = () => ({+ unit: 'kg', lang: 'en', customEx: [], workouts: [], bodyweight: [], exWeights: {}, dayPlan: {}, week: {},+ routines: [{ id: 'r1', name: 'Old', ex: [{ id: ALIAS, sets: 3, reps: 10, mode: 'reps' }, { id: '0001', sets: 3, reps: 10, mode: 'reps' }] }],+ coach: { consent: { agreedAt: '2026-07-01T00:00:00Z', version: CONSENT_VERSION }, log: [], snapshots: [], cadence: 'off' },+})+const plan = ex => JSON.stringify({ opengym_plan: 1, unit: 'kg', routines: [{ id: 'p1', name: 'Shared', ex }], customEx: [] })++describe('canonicalExId', () => {+ it('turns a drawing id into its exercise and leaves everything else alone', () => {+ expect(EXIDX[ALIAS]).toBe(EXIDX[TO])+ expect(canonicalExId(ALIAS)).toBe(TO)+ expect(canonicalExId(TO)).toBe(TO)+ expect(canonicalExId('k3x9custom')).toBe('k3x9custom')+ expect(canonicalExId(undefined)).toBe(undefined)+ expect(canonicalExId('constructor')).toBe('constructor')+ })+})++describe('a drawing id on its way in', () => {+ it('a plan file: parsed and merged as the exercise, the routines already there untouched', () => {+ const s = state()+ const bundle = parsePlan(plan([{ id: ALIAS, sets: 3, reps: 8, mode: 'reps' }]))+ expect(bundle.routines[0].ex[0].id).toBe(TO)+ mergePlan(s, { ...bundle, routines: [{ ...bundle.routines[0], ex: [{ id: ALIAS, sets: 3 }] }] })+ expect(s.routines[1].ex[0].id).toBe(TO)+ expect(s.routines[0].ex[0].id).toBe(ALIAS)+ })++ it('a plan file whose own custom exercise happens to carry such an id keeps it as a custom one', () => {+ const raw = JSON.stringify({ opengym_plan: 1, unit: 'kg', routines: [{ id: 'p1', name: 'Shared', ex: [{ id: ALIAS, sets: 3 }] }], customEx: [{ id: ALIAS, n: 'my thing', bp: 'chest' }] })+ const bundle = parsePlan(raw)+ expect(bundle.routines[0].ex[0].id).toBe(ALIAS)+ const s = state()+ mergePlan(s, bundle)+ const added = s.routines[1].ex[0].id+ expect(s.customEx.map(c => c.id)).toEqual([added])+ })++ it('the Coach: an added, swapped-in or new-routine exercise', () => {+ const s = state()+ applyChangeSet(s, {+ id: 'p', kind: 'review', changes: [+ { id: 'a', type: 'add-exercise', target: { routineId: 'r1' }, after: { id: ALIAS, sets: 3, reps: 8 } },+ { id: 'b', type: 'swap-exercise', target: { routineId: 'r1', exId: '0001' }, after: { id: ALIAS } },+ { id: 'c', type: 'add-routine', target: {}, after: { name: 'New', ex: [{ id: ALIAS, sets: 3, reps: 8 }] } },+ ]+ }, ['a', 'b', 'c'])+ expect(s.routines[0].ex.map(e => e.id)).toEqual([ALIAS, TO, TO])+ expect(s.routines[1].ex[0].id).toBe(TO)+ })++ describe('an import', () => {+ afterEach(() => { delete HEVY_ID_MAP.TESTALIAS })+ it('a Hevy template pinned to a drawing id lands on the exercise', () => {+ HEVY_ID_MAP.TESTALIAS = ALIAS+ const parsed = parseHevyWorkouts([{+ id: 'w', title: 'W', start_time: '2026-09-01T10:00:00+00:00', end_time: '2026-09-01T11:00:00+00:00',+ exercises: [{ title: 'x', exercise_template_id: 'TESTALIAS', sets: [{ type: 'normal', weight_kg: 40, reps: 8 }] }],+ }], [])+ expect(parsed.workouts[0].entries[0].id).toBe(TO)+ })+ })+})diff --git a/frontend/src/lib/exercises.js b/frontend/src/lib/exercises.jsindex 5e8522da0..dcf8dc85d 100644--- a/frontend/src/lib/exercises.js+++ b/frontend/src/lib/exercises.js@@ -46,6 +46,10 @@ CATALOGUE.forEach(e => { EXIDX[e.id] = e })for (const [id, to] of Object.entries(EXALIAS)) {if (EXIDX[to]) Object.defineProperty(EXIDX, id, { value: EXIDX[to], enumerable: false, configurable: true })}+// The exercise a drawing's id stands for; any other id as it is. For ids on their way in (a plan+// file, an import, the Coach's output), so the app goes on storing only exercise ids. An id+// already in state is left alone: rewriting it would move history behind the user's back.+export const canonicalExId = id => (typeof id === 'string' && Object.hasOwn(EXALIAS, id) && EXIDX[EXALIAS[id]] ? EXALIAS[id] : id)export const BODYPARTS = [...new Set(CATALOGUE.map(e => e.bp))].sort()// Equipment options present in a given list of exercises, most common first (issue #6).diff --git a/frontend/src/lib/import-csv.js b/frontend/src/lib/import-csv.jsindex 9d75f396c..0246b7ac7 100644--- a/frontend/src/lib/import-csv.js+++ b/frontend/src/lib/import-csv.js@@ -18,7 +18,7 @@// body-weight records are interesting here. parseBodyweight() scans for those without// building a DOM.-import { EXDB, EXIDX, isCardio as isCardioEx } from './exercises.js'+import { EXDB, EXIDX, canonicalExId, isCardio as isCardioEx } from './exercises.js'import { uid } from './format.js'import { isWarmupRow } from './workout-model.js'import { HEVY_TITLE_MAP } from './hevy-id-map.js'@@ -542,7 +542,7 @@ export function parseWorkoutCSV(text, { unit = 'kg', customEx = [] } = {}) {if (own) prior.set(key, own)// Hevy CSV: prefer the generated English-title map (same table as the API import).// Localized titles still fall through to the word-bag matcher.- id = own ? own.id : (source === 'Hevy' ? matchHevyTitle(name) : null) || matchExercise(name)+ id = own ? own.id : canonicalExId((source === 'Hevy' ? matchHevyTitle(name) : null) || matchExercise(name))resolved.set(key, id)}if (id) matched++diff --git a/frontend/src/lib/import-hevy.js b/frontend/src/lib/import-hevy.jsindex e537fd376..8df1b4150 100644--- a/frontend/src/lib/import-hevy.js+++ b/frontend/src/lib/import-hevy.js@@ -5,7 +5,7 @@// with `node scripts/build-hevy-id-map.mjs`. The API key never leaves this module's// callers: nothing here stores it.-import { EXIDX } from './exercises.js'+import { EXIDX, canonicalExId } from './exercises.js'import { uid } from './format.js'import { isWarmupRow } from './workout-model.js'import { HEVY_ID_MAP, HEVY_TITLE_MAP } from './hevy-id-map.js'@@ -175,7 +175,7 @@ function makeResolver(templates, customEx) {const t = templateId ? byId.get(templateId) : nullconst own = priorCustom(customEx, importId('im', 'Hevy|' + key), t?.title || fallbackTitle)if (own) return own.id- const pinned = templateId && HEVY_ID_MAP[templateId]+ const pinned = templateId && canonicalExId(HEVY_ID_MAP[templateId])if (pinned && EXIDX[pinned]) {matchedIds.add(pinned)return pinneddiff --git a/frontend/src/lib/plan-share.js b/frontend/src/lib/plan-share.jsindex 823de75b9..ecb40573d 100644--- a/frontend/src/lib/plan-share.js+++ b/frontend/src/lib/plan-share.js@@ -8,7 +8,7 @@// 2. A clean, printable page (Save as PDF) where a single exercise never splits across// a page break — each exercise, and each routine that fits, stays in one place.-import { EXIDX, isBodyweightEq } from './exercises.js'+import { EXIDX, canonicalExId, isBodyweightEq } from './exercises.js'import { cleanUrl } from './media-refs.js'import { modeOf, exLine, MAX_PLANNED_WARMUPS } from './history.js'import { deriveSessionName } from './session-merge.js'@@ -231,7 +231,8 @@ export function parsePlan(raw, destinationUnit = 'kg') {return ok}).map(e => {// The exercises pass through as written, so the fields that carry numbers into the- // planner get the same clamps on the way in that they get on the way out.+ // planner get the same clamps on the way in that they get on the way out. A drawing's id+ // (the female figure of an exercise) becomes the exercise's own.const warm = cleanWarmupSets(e.warmupSets)const intens = cleanIntensifier(e.intensifier)const rest = cleanRestSec(e.restSec)@@ -240,6 +241,7 @@ export function parsePlan(raw, destinationUnit = 'kg') {const pyramidRest = pyramid.length ? normalizePyramidRest(e.pyramidRest, pyramid.length) : []const pyramidWeight = pyramid.length ? normalizePyramidWeight(e.pyramidWeight, pyramid.length) : []const { warmupSets, intensifier, restSec, warmupRestSec, pyramid: _pyramid, pyramidRest: _pyramidRest, pyramidWeight: _pyramidWeight, ...passthrough } = e+ if (!known.has(passthrough.id)) passthrough.id = canonicalExId(passthrough.id)return convertedExercise({ ...passthrough, ...(pyramid.length ? { pyramid } : {}), ...(pyramidRest.length ? { pyramidRest } : {}), ...(pyramidWeight.length ? { pyramidWeight } : {}), ...(warm ? { warmupSets: warm } : {}), ...(intens ? { intensifier: intens } : {}), ...(rest ? { restSec: rest } : {}), ...(warmRest ? { warmupRestSec: warmRest } : {}) }, sourceUnit || destination, destination)})}))@@ -293,7 +295,7 @@ export function mergePlan(s, bundle, { schedule } = {}) {emoji: r.emoji,...(r.prog ? { prog: r.prog } : {}),...(r.excludeFromProgression === true ? { excludeFromProgression: true } : {}),- ex: (r.ex || []).map(e => ({ ...e, id: exIdMap[e.id] || e.id }))+ ex: (r.ex || []).map(e => ({ ...e, id: exIdMap[e.id] || canonicalExId(e.id) }))})})if (schedule) {