From 5c3a9323b7411e2acad333d3de40a605973ef629 Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Sat, 19 Sep 2026 17:10:07 +0200 Subject: [PATCH] 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']) + }) +})