diff --git a/frontend/src/lib/import-csv.js b/frontend/src/lib/import-csv.js index f15e6df..114529e 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 = { @@ -492,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 }) @@ -589,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 6f392f6..a7fd749 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 { parseWorkoutCSV, mergeImport } from './import-csv.js' +import { workoutVolume } from './history.js' const CSV = [ 'Date,Exercise,Weight,Reps,Set Type', @@ -50,3 +51,57 @@ 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)) + }) +}) + +// 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']) + }) +}) 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() 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"