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
Import: two workouts on the same day stay two (#505)
Duarte Santos9ec0008b0bee
diff --git a/frontend/src/lib/import-csv.js b/frontend/src/lib/import-csv.jsindex ff8b4ef13..193927977 100644--- a/frontend/src/lib/import-csv.js+++ b/frontend/src/lib/import-csv.js@@ -495,7 +495,7 @@ export function parseWorkoutCSV(text, { unit = 'kg', customEx = [] } = {}) {const resolved = new Map() // exercise name -> dataset id | null, resolved onceconst prior = new Map() // exercise name -> the user's own custom exercise for it- const byDate = new Map()+ const byDate = new Map() // session key -> the workout being built (see `sessionKey` below)const created = new Map()const unmatched = new Set()let sets = 0, skipped = 0, matched = 0, warmups = 0, rpeSets = 0, rirSets = 0@@ -595,10 +595,16 @@ export function parseWorkoutCSV(text, { unit = 'kg', customEx = [] } = {}) {else if (rpe != null) { set.rpe = rpe; rpeSets++ }}- let day = byDate.get(when.d)+ // Strong and Hevy write the workout's start on every row, so two sessions on one day are+ // told apart by it (and by the name) and stay two workouts (#505). FitNotes has no times, and+ // a generic file may stamp each set with its own, so everything else stays one per day.+ const sessionKey = (source === 'Strong' || source === 'Hevy') && when.t != null+ ? when.d + '|' + when.t + '|' + (cell(r, 'workoutName') || '')+ : when.d+ let day = byDate.get(sessionKey)if (!day) {- day = { ex: new Map(), name: cell(r, 'workoutName') || '', start: when.t, end: null, note: '', notes: new Map(), sg: new Map(), groups: new Map() }- byDate.set(when.d, day)+ day = { d: when.d, ex: new Map(), name: cell(r, 'workoutName') || '', start: when.t, end: null, note: '', notes: new Map(), sg: new Map(), groups: new Map() }+ byDate.set(sessionKey, day)}if (!day.name) day.name = cell(r, 'workoutName') || ''if (!day.note) day.note = cell(r, 'workoutNote')@@ -621,7 +627,7 @@ export function parseWorkoutCSV(text, { unit = 'kg', customEx = [] } = {}) {}const ss = cell(r, 'superset')if (ss && !day.sg.has(id)) {- if (!day.groups.has(ss)) day.groups.set(ss, importId('is', when.d + '|' + ss))+ if (!day.groups.has(ss)) day.groups.set(ss, importId('is', sessionKey + '|' + ss))day.sg.set(id, day.groups.get(ss))}sets++@@ -645,9 +651,11 @@ export function parseWorkoutCSV(text, { unit = 'kg', customEx = [] } = {}) {}const converted = (!!fileUnit && fileUnit !== unit) || mixedUnits- const dates = [...byDate.keys()].sort()- const workouts = dates.map(d => {- const day = byDate.get(d)+ // By day, and within a day by start, the order the history keeps (sync-merge byDayStart).+ const days = [...byDate.values()].sort((a, b) => (a.d === b.d ? (a.start ?? 0) - (b.start ?? 0) : a.d < b.d ? -1 : 1))+ const dates = days.map(day => day.d)+ const workouts = days.map(day => {+ const d = day.dconst entries = [...day.ex.entries()].map(([id, ss]) => {const conv2 = ss.map(({ u, ...s }) => (s.w !== undefined ? { ...s, w: convRow({ ...s, u }) } : s))const mx = Math.max(0, ...conv2.filter(s => !isWarmupRow(s)).map(s => s.w || 0))@@ -769,7 +777,11 @@ export function parseImport(text, opts) {/* --------------------------------------------------------------- merge ---- */-/** Merge into state. Existing days win — importing twice never duplicates a workout. */+/**+ * Merge into state. Existing days win — importing twice never duplicates a workout. A day the+ * file has two sessions on brings both (they are checked against the state before the merge,+ * not against each other); a day already in the history brings neither.+ */export function mergeImport(S, parsed) {if (parsed.kind === 'bodyweight') {const have = new Set(S.bodyweight.map(b => b.d))@@ -796,7 +808,7 @@ export function mergeImport(S, parsed) {const known = new Set(S.customEx.map(c => c?.id))const customs = parsed.customEx.filter(c => used.has(c.id) && !EXIDX[c.id] && !known.has(c.id))S.customEx = [...S.customEx, ...customs]- S.workouts = [...S.workouts, ...fresh].sort((a, b) => (a.d < b.d ? -1 : 1))+ S.workouts = [...S.workouts, ...fresh].sort((a, b) => (a.d === b.d ? (a.start || 0) - (b.start || 0) : a.d < b.d ? -1 : 1))// seed the weight suggestions from the newest imported set of each liftfresh.forEach(w => w.entries.forEach(e => {const mx = Math.max(0, ...e.sets.map(s => s.w || 0), e.topW || 0)diff --git a/frontend/src/lib/import-csv.strong.test.js b/frontend/src/lib/import-csv.strong.test.jsindex 85d1d3057..1a2ee567e 100644--- a/frontend/src/lib/import-csv.strong.test.js+++ b/frontend/src/lib/import-csv.strong.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'// Rows from a real Strong export (QA sample). Strong writes the workout's length in Duration// ("1h 5m", "52m") on every row and a set's own time in Seconds; the session note is@@ -71,3 +71,64 @@ describe('a Hevy export with a superset', () => {expect(w.entries[0].sets).toEqual([{ sec: 45, w: 0, done: true }])})})++// GitHub #505: a morning and an evening session on one day were filed as one workout, because+// the rows were grouped by calendar day only. Strong and Hevy write each workout's start on+// every row, so the two stay two; FitNotes has no times and stays one workout per day.+describe('two sessions on one day', () => {+ const TWO_STRONG = [+ 'Date;Workout Name;Duration;Exercise Name;Set Order;Weight;Reps;Distance;Seconds;Notes;Workout Notes;RPE',+ '2026-10-05 07:00:00;Morning Push;45m;Bench Press (Barbell);1;80;5;0;0;;;',+ '2026-10-05 07:00:00;Morning Push;45m;Bench Press (Barbell);2;80;5;0;0;;;',+ '2026-10-05 18:30:00;Evening Legs;1h 0m;Squat (Barbell);1;100;5;0;0;;;',+ '2026-10-05 18:30:00;Evening Legs;1h 0m;Squat (Barbell);2;100;5;0;0;;;',+ ].join('\n')++ it('Strong: keeps a morning and an evening workout apart', () => {+ const parsed = parseWorkoutCSV(TWO_STRONG, { unit: 'kg' })+ expect(parsed.workouts).toHaveLength(2)+ const [am, pm] = parsed.workouts+ expect([am.d, pm.d]).toEqual(['2026-10-05', '2026-10-05'])+ expect([am.name, pm.name]).toEqual(['Morning Push', 'Evening Legs'])+ expect(new Date(am.start).getHours()).toBe(7)+ expect(minutes(am)).toBe(45)+ expect(new Date(pm.start).getHours()).toBe(18)+ expect(minutes(pm)).toBe(60)+ expect(am.entries.map(e => e.sets.length)).toEqual([2])+ expect(pm.entries.map(e => e.sets.length)).toEqual([2])+ expect(am.id).not.toBe(pm.id)+ expect([parsed.from, parsed.to]).toEqual(['2026-10-05', '2026-10-05'])+ })++ it('Strong: the evening session listed first still comes out after the morning one', () => {+ const [head, ...rows] = TWO_STRONG.split('\n')+ const parsed = parseWorkoutCSV([head, ...rows.reverse()].join('\n'), { unit: 'kg' })+ expect(parsed.workouts.map(w => w.name)).toEqual(['Morning Push', 'Evening Legs'])+ })++ it('Hevy CSV: two workouts on one day stay two', () => {+ const pm = HEVY.split('\n').slice(1).map(l => l.replace('"Push"', '"Arms"').replace('18:00', '20:00').replace('19:10', '21:00'))+ const parsed = parseWorkoutCSV([HEVY, ...pm].join('\n'), { unit: 'kg' })+ expect(parsed.workouts.map(w => w.name)).toEqual(['Push', 'Arms'])+ expect(parsed.workouts.map(w => w.d)).toEqual(['2026-02-03', '2026-02-03'])+ expect(parsed.workouts.map(minutes)).toEqual([70, 60])+ })++ it('both come in once, and importing the file again adds nothing', () => {+ const S = { workouts: [], customEx: [], exWeights: {}, bodyweight: [] }+ expect(mergeImport(S, parseWorkoutCSV(TWO_STRONG, { unit: 'kg' }))).toEqual({ added: 2, skipped: 0 })+ expect(mergeImport(S, parseWorkoutCSV(TWO_STRONG, { unit: 'kg' }))).toEqual({ added: 0, skipped: 2 })+ expect(S.workouts.map(w => w.name)).toEqual(['Morning Push', 'Evening Legs'])+ })++ it('FitNotes, which writes no times, stays one workout per day', () => {+ const parsed = parseWorkoutCSV([+ 'Date,Exercise,Category,Weight (kg),Reps',+ '2026-10-05,Flat Barbell Bench Press,Chest,80,5',+ '2026-10-05,Barbell Squat,Legs,100,5',+ ].join('\n'), { unit: 'kg' })+ expect(parsed.source).toBe('FitNotes')+ expect(parsed.workouts).toHaveLength(1)+ expect(parsed.workouts[0].entries).toHaveLength(2)+ })+})diff --git a/frontend/src/lib/import-hevy.js b/frontend/src/lib/import-hevy.jsindex fbbd63f1d..4278b5246 100644--- a/frontend/src/lib/import-hevy.js+++ b/frontend/src/lib/import-hevy.js@@ -222,7 +222,7 @@ const toProfileWeight = (wKg, unit) => {*/export function parseHevyWorkouts(workouts, templates, { unit = 'kg', customEx = [] } = {}) {const R = makeResolver(templates, customEx)- const byDate = new Map()+ const byDate = new Map() // Hevy workout -> the session being builtlet sets = 0, skipped = 0, matched = 0, warmups = 0, rpeSets = 0for (const w of workouts || []) {@@ -232,10 +232,12 @@ export function parseHevyWorkouts(workouts, templates, { unit = 'kg', customEx =// The history list renders the name as it is stored, so only text may become one.const title = typeof w.title === 'string' ? w.title : ''- let day = byDate.get(start.d)+ // One workout per Hevy workout, not per day: a morning and an evening session stay two (#505).+ const key = w.id != null ? 'id|' + w.id : start.d + '|' + start.t + '|' + title+ let day = byDate.get(key)if (!day) {- day = { ex: new Map(), name: title, start: start.t, end: end?.t ?? null }- byDate.set(start.d, day)+ day = { d: start.d, ex: new Map(), name: title, start: start.t, end: end?.t ?? null }+ byDate.set(key, day)} else if (!day.name && title) day.name = title// Past midnight the end's clock restarts; count it from the start day's midnight instead.if (end?.t != null) day.end = end.t + daysBetween(start.d, end.d) * 86400000@@ -279,11 +281,13 @@ export function parseHevyWorkouts(workouts, templates, { unit = 'kg', customEx =}}- // A day on which nothing measurable landed is not a workout — and imported, it would block the- // real one from a later export, since existing days win in mergeImport.- const dates = [...byDate.keys()].filter(d => byDate.get(d).ex.size).sort()- const outWorkouts = dates.map(d => {- const day = byDate.get(d)+ // A session in which nothing measurable landed is not a workout — and imported, it would block+ // the real one from a later export, since existing days win in mergeImport.+ const days = [...byDate.values()].filter(day => day.ex.size)+ .sort((a, b) => (a.d === b.d ? (a.start ?? 0) - (b.start ?? 0) : a.d < b.d ? -1 : 1))+ const dates = days.map(day => day.d)+ const outWorkouts = days.map(day => {+ const d = day.dconst entries = [...day.ex.entries()].map(([id, ss]) => {const mx = Math.max(0, ...ss.filter(s => !isWarmupRow(s)).map(s => s.w || 0))return { id, sets: ss, topW: mx || null }diff --git a/frontend/src/lib/import-hevy.test.js b/frontend/src/lib/import-hevy.test.jsindex 50f245a2b..7cda9ae1e 100644--- a/frontend/src/lib/import-hevy.test.js+++ b/frontend/src/lib/import-hevy.test.js@@ -142,7 +142,8 @@ describe('parseHevyWorkouts', () => {expect(parsed.converted).toBe(true)})- it('merges two Hevy sessions on the same local day', () => {+ // Two Hevy workouts on one day used to be merged into one (GitHub #505).+ it('keeps two Hevy sessions on the same local day apart', () => {const a = { ...WORKOUT, id: 'a', title: 'AM', exercises: [WORKOUT.exercises[0]] }const b = {...WORKOUT, id: 'b', title: 'PM',@@ -151,8 +152,14 @@ describe('parseHevyWorkouts', () => {exercises: [WORKOUT.exercises[1]],}const parsed = parseHevyWorkouts([a, b], TEMPLATES, { unit: 'kg' })- expect(parsed.workouts).toHaveLength(1)- expect(parsed.workouts[0].entries.length).toBeGreaterThanOrEqual(2)+ expect(parsed.workouts).toHaveLength(2)+ expect(parsed.workouts.map(w => w.name)).toEqual(['AM', 'PM'])+ expect(parsed.workouts[0].id).not.toBe(parsed.workouts[1].id)+ // Both arrive once; the same payload again adds nothing.+ const S = { workouts: [], customEx: [], exWeights: {}, bodyweight: [] }+ expect(mergeImport(S, parsed).added).toBe(2)+ expect(mergeImport(S, parsed).added).toBe(0)+ expect(S.workouts).toHaveLength(2)})})