diff --git a/frontend/src/lib/plan-share.js b/frontend/src/lib/plan-share.js index 6089047..affc02f 100644 --- a/frontend/src/lib/plan-share.js +++ b/frontend/src/lib/plan-share.js @@ -14,6 +14,7 @@ import { deriveSessionName } from './session-merge.js' import { uid, todayISO, DAYN, weekOrder, weekStartOf, fmtNum, exCount } from './format.js' import { t, exerciseNameFor } from './i18n-core.js' import { convertWeight } from './units.js' +import { MUSCLES, inMuscleOrder } from './muscles.js' const PLAN_FMT = 1 const WEEK_DAYS = [1, 2, 3, 4, 5, 6, 0] // every getDay() index; only the reader's own @@ -133,6 +134,32 @@ function cleanIntensifier(x) { return null } +// The muscles the map can draw, plus the one label the form gives a cardio exercise. +const CUSTOM_MUSCLES = new Set([...MUSCLES, 'cardiovascular system']) +const muscleList = v => inMuscleOrder([...new Set((Array.isArray(v) ? v : []).filter(m => CUSTOM_MUSCLES.has(m)))]) + +/** A custom exercise with its own metadata — equipment, muscles, description — in the shape + * CustomExForm writes, minus the recipient-side `custom`/`sm` fields mergePlan adds. The bundle + * used to carry only {id, n, bp}, so the exercise arrived with an empty equipment tag, no muscle + * credit, and (stored without `custom: true`) no way to edit or delete it (QA C7). The same gate + * runs on the way in as on the way out, since a plan file is someone else's data: only muscles the + * map can draw, a secondary never repeating a primary, the way the form itself enforces. Fields + * that are empty stay absent, so a file written before this change reads the same as one after. */ +function cleanCustom(c) { + const o = { id: c.id, n: c.n, bp: c.bp } + if (c.desc) o.desc = c.desc + if (typeof c.eq === 'string' && c.eq) o.eq = c.eq + const prim = muscleList(c.primaries) + const sm = muscleList(c.secondaries).filter(m => !prim.includes(m)) + // `tg` is the legacy single primary; the form keeps it equal to the first primary. + const tg = prim[0] || (CUSTOM_MUSCLES.has(c.tg) ? c.tg : '') + if (tg) o.tg = tg + if (prim.length) o.primaries = prim + if (sm.length) o.secondaries = sm + if (prim.length || sm.length) o.muscleGroups = [...prim, ...sm] + return o +} + /** Build the shareable bundle: every routine, the week schedule, referenced customs. */ export function buildPlanBundle(S, name) { const unit = planUnit(S.unit == null ? 'kg' : S.unit) @@ -146,7 +173,7 @@ export function buildPlanBundle(S, name) { const usedIds = new Set(routines.flatMap(r => r.ex.map(e => e.id))) const customEx = (S.customEx || []) .filter(c => usedIds.has(c.id)) - .map(c => ({ id: c.id, n: c.n, bp: c.bp, ...(c.desc ? { desc: c.desc } : {}) })) + .map(cleanCustom) // A weekday can hold several routines (merge order preserved). `[].concat` normalises a // legacy scalar id to a one-element list, so a bundle written before this change and one // written after are read the same way at the other end. @@ -226,7 +253,10 @@ export function mergePlan(s, bundle, { schedule } = {}) { if (same) { exIdMap[c.id] = same.id; return } const nid = uid() exIdMap[c.id] = nid - s.customEx.push({ id: nid, n: c.n, bp: c.bp, ...(c.desc ? { desc: c.desc } : {}) }) + // Stored exactly as the form would have created it — `custom: true` is what lets the recipient + // edit or delete it, and `sm` mirrors the secondaries the way the form writes them. + const clean = cleanCustom(c) + s.customEx.push({ ...clean, id: nid, ...(clean.secondaries ? { sm: clean.secondaries } : {}), custom: true }) }) const ridMap = {} source.routines.forEach(r => { diff --git a/frontend/src/lib/plan-share.test.js b/frontend/src/lib/plan-share.test.js index 73f3388..a114faf 100644 --- a/frontend/src/lib/plan-share.test.js +++ b/frontend/src/lib/plan-share.test.js @@ -195,3 +195,64 @@ describe('week schedule as a routine-id list', () => { expect(parsePlan({ opengym_plan: 1, routines: [], customEx: [], week: { 1: ['a'], 2: [], 4: 'b' } }).scheduledDays).toBe(2) }) }) + +// ---- custom exercises travel whole (QA C7) ---- +// The bundle used to carry only {id, n, bp} and mergePlan stored exactly that: the recipient's copy +// showed an empty equipment tag, no muscle credit, and — without `custom: true` — no Edit or Delete, +// so a wrong import could only be fixed by hand-editing localStorage. +describe('custom exercises in a shared plan', () => { + // The shape CustomExForm writes, in the map's order. + const landmine = { + id: 'c1', n: 'QA Landmine Row', bp: 'back', desc: 'Bar in the corner', tg: 'upper-back', sm: ['biceps'], + muscleGroups: ['upper-back', 'biceps'], primaries: ['upper-back'], secondaries: ['biceps'], eq: 'barbell', custom: true, + } + const source = { + routines: [{ id: 'r', name: 'Back day', ex: [{ id: 'c1', sets: 3, reps: 8, weight: 40 }] }], + week: {}, customEx: [landmine], + } + + it('exports equipment, muscles and description with the custom exercise', () => { + expect(buildPlanBundle(source, 'Plan').customEx[0]).toEqual({ + id: 'c1', n: 'QA Landmine Row', bp: 'back', desc: 'Bar in the corner', eq: 'barbell', tg: 'upper-back', + primaries: ['upper-back'], secondaries: ['biceps'], muscleGroups: ['upper-back', 'biceps'], + }) + }) + + it('stores the imported custom exercise the way the form would have created it', () => { + const parsed = parsePlan(JSON.stringify(buildPlanBundle(source, 'Plan'))) + const target = { routines: [], week: {}, customEx: [] } + mergePlan(target, parsed) + const stored = target.customEx[0] + expect(stored).toMatchObject({ ...landmine, id: stored.id }) + expect(stored.id).not.toBe('c1') + expect(target.routines[0].ex[0].id).toBe(stored.id) + }) + + // A file written before the metadata travelled has only a name and a body part; it still imports, + // and the recipient can now add the equipment and muscles themselves. + it('keeps importing an old name-plus-body-part file, editable at the other end', () => { + const legacy = { opengym_plan: 1, routines: [{ id: 'r', name: 'R', ex: [{ id: 'x', sets: 3, reps: 5 }] }], customEx: [{ id: 'x', n: 'Old one', bp: 'legs' }] } + const target = { routines: [], week: {}, customEx: [] } + mergePlan(target, parsePlan(legacy)) + expect(target.customEx[0]).toEqual({ id: target.customEx[0].id, n: 'Old one', bp: 'legs', custom: true }) + expect(target.routines[0].ex[0].id).toBe(target.customEx[0].id) + }) + + // A plan file is someone else's data: only muscles the map can draw are kept, and a muscle listed + // as both primary and secondary counts once, as the form itself enforces. + it('drops muscles it cannot draw and a secondary that repeats a primary', () => { + const bundle = { opengym_plan: 1, routines: [], customEx: [{ id: 'x', n: 'Odd', bp: 'back', eq: 'barbell', primaries: ['upper-back', 'wings'], secondaries: ['upper-back', 'biceps', 7] }] } + const target = { routines: [], week: {}, customEx: [] } + mergePlan(target, parsePlan(bundle)) + expect(target.customEx[0]).toMatchObject({ primaries: ['upper-back'], secondaries: ['biceps'], muscleGroups: ['upper-back', 'biceps'], tg: 'upper-back', sm: ['biceps'] }) + }) + + it('still reuses a custom the recipient already has under the same name and body part', () => { + const parsed = parsePlan(JSON.stringify(buildPlanBundle(source, 'Plan'))) + const mine = { id: 'mine', n: 'qa landmine row', bp: 'back', eq: 'landmine', custom: true } + const target = { routines: [], week: {}, customEx: [mine] } + mergePlan(target, parsed) + expect(target.customEx).toEqual([mine]) + expect(target.routines[0].ex[0].id).toBe('mine') + }) +})