From f9715d76d35dc3401bce52fe0de741ce00b25849 Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Sat, 19 Sep 2026 17:08:09 +0200 Subject: [PATCH 1/3] Import: hyphenated names get the same body part as their spaced spelling (QA C28) bpFromName matched its rules against the lower-cased name only, while the rules are written with spaces: 'Stiff-Legged Deadlift' fell through to the deadlift rule (back) and 'Chest-Supported Row' hit 'chest' before 'row'. The matcher's wordsOf already treats both spellings as one exercise, so the fallback now reads '-', '_' and '/' as spaces before the rules run and agrees with it. --- frontend/src/lib/import-csv.js | 8 +++++++- frontend/src/lib/import-name-bodypart.test.js | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/frontend/src/lib/import-csv.js b/frontend/src/lib/import-csv.js index f15e6df..104ad9b 100644 --- a/frontend/src/lib/import-csv.js +++ b/frontend/src/lib/import-csv.js @@ -286,7 +286,13 @@ const NAME_BP = [ [/\bneck\b/, 'neck'], [/\bgrip\b/, 'lower arms'], ] -export const bpFromName = name => (NAME_BP.find(([re]) => re.test(name)) || [])[1] || null +// Hyphens, underscores and slashes read as spaces first: "Stiff-Legged Deadlift" and +// "Chest-Supported Row" are the same names the rules above spell with a space, and the +// matcher (wordsOf) already treats the two spellings as one exercise — the body part has to agree. +export const bpFromName = name => { + const n = String(name || '').toLowerCase().replace(/[-_/]+/g, ' ').replace(/\s+/g, ' ').trim() + return (NAME_BP.find(([re]) => re.test(n)) || [])[1] || null +} // Categories the exporters use -> the dataset's body parts, for exercises we invent. const CATEGORY_BP = { diff --git a/frontend/src/lib/import-name-bodypart.test.js b/frontend/src/lib/import-name-bodypart.test.js index 976731b..0191f7c 100644 --- a/frontend/src/lib/import-name-bodypart.test.js +++ b/frontend/src/lib/import-name-bodypart.test.js @@ -27,6 +27,19 @@ describe('body part from an exercise name', () => { expect(bp('Deadlift')).toBe('back') }) + // The rules are written with spaces, but people hyphenate these names as often as not (QA C28). + // The matcher already treats "chest-supported row" and "chest supported row" as one exercise; + // the body-part fallback has to agree with it, or the two spellings land on different maps. + it('reads the hyphenated, underscored and slashed spellings like the spaced ones', () => { + expect(bp('Stiff-Legged Deadlift')).toBe('upper legs') + expect(bp('Straight-Leg Deadlift')).toBe('upper legs') + expect(bp('Stiff_Leg Deadlift')).toBe('upper legs') + expect(bp('Chest-Supported Row')).toBe('back') + expect(bp('Chest-Supported T-Bar Row')).toBe('back') + expect(bp('Chest/Supported Row')).toBe('back') + expect(bp('Sit-Up')).toBe('waist') + }) + it('still reads a bare grip exercise as forearms, and the rest as before', () => { expect(bp('Grip Trainer')).toBe('lower arms') expect(bp('Back Squat')).toBe('back') // unchanged: "back" is matched before "squat" From e20201a87071b4a2f2f09bdcce0dec56d0080116 Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Sat, 19 Sep 2026 17:08:51 +0200 Subject: [PATCH 2/3] Import: the stored volume leaves warm-up sets out, like a workout finished in the app (QA C14) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both importers summed every set into w.vol, while a workout finished in the app writes workoutVolume(w), which skips warm-ups — the config sheet promises they stay out of volume, records and progression. The inflated number is stored with the workout and read straight back by History, the heatmap and the month totals. Filter with isWarmupRow, as the topW line right above it already does. --- frontend/src/lib/import-csv.js | 4 +++- frontend/src/lib/import-csv.test.js | 21 +++++++++++++++++++++ frontend/src/lib/import-hevy.js | 3 ++- frontend/src/lib/import-hevy.test.js | 3 +++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/frontend/src/lib/import-csv.js b/frontend/src/lib/import-csv.js index 104ad9b..7317813 100644 --- a/frontend/src/lib/import-csv.js +++ b/frontend/src/lib/import-csv.js @@ -498,7 +498,9 @@ export function parseWorkoutCSV(text, { unit = 'kg' } = {}) { id: 'iw' + uid(), d, start, end: end > start ? end : start, routineId: null, name: day.name || 'Imported', entries, prs: [], } - w.vol = entries.reduce((a, e) => a + e.sets.reduce((b, s) => b + (s.w || 0) * (s.r || 0), 0), 0) + // Work sets only, like `workoutVolume` for a workout finished in the app: warm-ups are + // promised to stay out of the volume, and this number is stored with the workout for good. + w.vol = entries.reduce((a, e) => a + e.sets.reduce((b, s) => b + (isWarmupRow(s) ? 0 : (s.w || 0) * (s.r || 0)), 0), 0) return w }) diff --git a/frontend/src/lib/import-csv.test.js b/frontend/src/lib/import-csv.test.js index 6f392f6..1a21a30 100644 --- a/frontend/src/lib/import-csv.test.js +++ b/frontend/src/lib/import-csv.test.js @@ -1,5 +1,6 @@ import { describe, expect, it } from 'vitest' import { parseWorkoutCSV } from './import-csv.js' +import { workoutVolume } from './history.js' const CSV = [ 'Date,Exercise,Weight,Reps,Set Type', @@ -50,3 +51,23 @@ describe('gravl export', () => { expect(cardio.entries[0].sets).toEqual([{ min: 0.4, speed: 0, done: true }]) }) }) + +// A finished workout stores `vol = workoutVolume(w)`, which leaves warm-ups out; an imported one +// wrote the sum of every set, so the History row, the heatmap and the month totals read a number +// the app's own arithmetic never produces — and it stays wrong forever (QA C14). +const HEVY_WARMUPS = [ + 'title,start_time,end_time,description,exercise_title,superset_id,exercise_notes,set_index,set_type,weight_kg,reps,distance_km,duration_seconds,rpe', + '"QA","03 Mar 2025, 18:00","03 Mar 2025, 19:00","","Bench Press (Barbell)",,"",0,warmup,20,10,,,', + '"QA","03 Mar 2025, 18:00","03 Mar 2025, 19:00","","Bench Press (Barbell)",,"",1,normal,60,5,,,', + '"QA","03 Mar 2025, 18:00","03 Mar 2025, 19:00","","Bench Press (Barbell)",,"",2,normal,60,5,,,', + '"QA","03 Mar 2025, 18:00","03 Mar 2025, 19:00","","Squat (Barbell)",,"",0,warmup,40,5,,,', + '"QA","03 Mar 2025, 18:00","03 Mar 2025, 19:00","","Squat (Barbell)",,"",1,normal,100,5,,,', +].join('\n') + +describe('imported volume', () => { + it('stores the work-set volume, the same number workoutVolume computes', () => { + const [w] = parseWorkoutCSV(HEVY_WARMUPS, { unit: 'kg' }).workouts + expect(w.vol).toBe(1100) + expect(w.vol).toBe(workoutVolume(w)) + }) +}) diff --git a/frontend/src/lib/import-hevy.js b/frontend/src/lib/import-hevy.js index b66c871..bed02b3 100644 --- a/frontend/src/lib/import-hevy.js +++ b/frontend/src/lib/import-hevy.js @@ -267,7 +267,8 @@ export function parseHevyWorkouts(workouts, templates, { unit = 'kg' } = {}) { id: 'iw' + uid(), d, start: startMs, end: endMs > startMs ? endMs : startMs, routineId: null, name: day.name || 'Imported', entries, prs: [], } - workout.vol = entries.reduce((a, e) => a + e.sets.reduce((b, s) => b + (s.w || 0) * (s.r || 0), 0), 0) + // Work sets only, the number `workoutVolume` gives a workout finished in the app; it is stored for good. + workout.vol = entries.reduce((a, e) => a + e.sets.reduce((b, s) => b + (isWarmupRow(s) ? 0 : (s.w || 0) * (s.r || 0)), 0), 0) return workout }) diff --git a/frontend/src/lib/import-hevy.test.js b/frontend/src/lib/import-hevy.test.js index 7d7bc39..b395ea7 100644 --- a/frontend/src/lib/import-hevy.test.js +++ b/frontend/src/lib/import-hevy.test.js @@ -124,6 +124,9 @@ describe('parseHevyWorkouts', () => { expect(pulldown.topW).toBe(52) expect(parsed.warmups).toBe(1) expect(parsed.rpeSets).toBe(1) + // The stored volume leaves the warm-up out, like a workout finished in the app (QA C14): + // 52×15 + 52×10 + 20×12 = 1,540, not 1,915. + expect(parsed.workouts[0].vol).toBe(1540) const custom = entries.find(e => String(e.id).startsWith('im')) expect(custom).toBeTruthy() From 5c3a9323b7411e2acad333d3de40a605973ef629 Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Sat, 19 Sep 2026 17:10:07 +0200 Subject: [PATCH 3/3] Import: a second export reuses the custom exercises the first one created (QA C12) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parseWorkoutCSV invents a fresh 'im…' id for every name it cannot match, and mergeImport appended those to S.customEx without looking at what was already there — so importing Hevy, then a later Hevy export, left two 'Grip Trainer' entries with one workout of history each (Hevy + Strong + FitNotes grew 25 → 76 customs, 24 of them duplicated names). mergeImport now matches parsed customs against S.customEx by lower-cased, whitespace-collapsed name (an earlier import or one made by hand), rewrites the fresh days' entry ids through that map and only pushes the names with no match — the same shape mergeHevyRoutines and mergePlan already use. Covers the Hevy API path too, which merges through here. --- frontend/src/lib/import-csv.js | 15 +++++++++++- frontend/src/lib/import-csv.test.js | 36 ++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/frontend/src/lib/import-csv.js b/frontend/src/lib/import-csv.js index 7317813..114529e 100644 --- a/frontend/src/lib/import-csv.js +++ b/frontend/src/lib/import-csv.js @@ -597,10 +597,23 @@ export function mergeImport(S, parsed) { return { added: fresh.length, skipped: parsed.bodyweight.length - fresh.length } } const have = new Set(S.workouts.map(w => w.d)) + // Every parse invents fresh ids for the names it cannot match, and a later export of the same + // account names those exercises again. So a custom exercise is looked up by name among the ones + // already here — from an earlier import or made by hand — and the new days are pointed at it, + // the way mergeHevyRoutines and mergePlan do; otherwise the Library lists "Grip Trainer" twice, + // each with half the history. Only a name with no match becomes a new exercise. + S.customEx = S.customEx || [] + const nameKey = n => String(n || '').toLowerCase().replace(/\s+/g, ' ').trim() + const exIdMap = {} + parsed.customEx.forEach(c => { + const same = S.customEx.find(x => x.id !== c.id && nameKey(x.n) === nameKey(c.n)) + if (same) exIdMap[c.id] = same.id + }) const fresh = parsed.workouts.filter(w => !have.has(w.d)) + .map(w => ({ ...w, entries: w.entries.map(e => (exIdMap[e.id] ? { ...e, id: exIdMap[e.id] } : e)) })) const used = new Set(fresh.flatMap(w => w.entries.map(e => e.id))) const customs = parsed.customEx.filter(c => used.has(c.id) && !EXIDX[c.id]) - S.customEx = [...(S.customEx || []), ...customs] + S.customEx = [...S.customEx, ...customs] S.workouts = [...S.workouts, ...fresh].sort((a, b) => (a.d < b.d ? -1 : 1)) // seed the weight suggestions from the newest imported set of each lift fresh.forEach(w => w.entries.forEach(e => { diff --git a/frontend/src/lib/import-csv.test.js b/frontend/src/lib/import-csv.test.js index 1a21a30..a7fd749 100644 --- a/frontend/src/lib/import-csv.test.js +++ b/frontend/src/lib/import-csv.test.js @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { parseWorkoutCSV } from './import-csv.js' +import { parseWorkoutCSV, mergeImport } from './import-csv.js' import { workoutVolume } from './history.js' const CSV = [ @@ -71,3 +71,37 @@ describe('imported volume', () => { expect(w.vol).toBe(workoutVolume(w)) }) }) + +// Two exports of the same account, the second one a week later. Each parse invents fresh ids for +// the names it cannot match, so the merge has to find the custom exercise the first import created +// — otherwise the Library lists "Zorb Roller" twice with half the history each (QA C12). +const hevyDay = (title, day, name, w, r) => + `"${title}","${day} Mar 2025, 18:00","${day} Mar 2025, 19:00","","${name}",,"",0,normal,${w},${r},,,` +const HEVY_HEAD = HEVY_WARMUPS.split('\n')[0] +const EXPORT_A = [HEVY_HEAD, hevyDay('Day A', '02', 'Zorb Roller', 49, 8), hevyDay('Day A', '02', 'Bench Press (Barbell)', 80, 5)].join('\n') +const EXPORT_B = [HEVY_HEAD, hevyDay('Day A', '02', 'Zorb Roller', 49, 8), hevyDay('Day B', '09', 'Zorb Roller', 30, 15)].join('\n') + +describe('mergeImport and custom exercises', () => { + const fresh = () => ({ workouts: [], customEx: [], exWeights: {}, bodyweight: [] }) + + it('reuses the custom exercise an earlier import created for the same name', () => { + const S = fresh() + mergeImport(S, parseWorkoutCSV(EXPORT_A, { unit: 'kg' })) + const [zorb] = S.customEx + expect(zorb.n).toBe('zorb roller') + + expect(mergeImport(S, parseWorkoutCSV(EXPORT_B, { unit: 'kg' }))).toEqual({ added: 1, skipped: 1 }) + expect(S.customEx).toHaveLength(1) + // The new day points at the existing exercise, so its history is one line, not two. + expect(S.workouts.map(w => w.entries[0].id)).toEqual([zorb.id, zorb.id]) + expect(S.exWeights[zorb.id]).toEqual({ w: 30, d: '2025-03-09' }) + }) + + it('matches the name regardless of case and spacing, including one the user made by hand', () => { + const S = fresh() + S.customEx.push({ id: 'mine', n: 'Zorb Roller ', bp: 'waist', custom: true }) + mergeImport(S, parseWorkoutCSV(EXPORT_A, { unit: 'kg' })) + expect(S.customEx).toHaveLength(1) + expect(S.workouts[0].entries.map(e => e.id)).toEqual(['mine', '0025']) + }) +})