From 19f09dcccc5e23b650774de93bfcdcecf6a70a89 Mon Sep 17 00:00:00 2001 From: Sebastian G Date: Mon, 17 Aug 2026 16:33:49 +0200 Subject: [PATCH 1/3] feat: add copy routine button to RoutineEdit view --- frontend/src/lib/routines.js | 12 +++++ frontend/src/lib/routines.test.js | 79 ++++++++++++++++++++++++++++++ frontend/src/views/RoutineEdit.jsx | 7 +++ 3 files changed, 98 insertions(+) create mode 100644 frontend/src/lib/routines.js create mode 100644 frontend/src/lib/routines.test.js diff --git a/frontend/src/lib/routines.js b/frontend/src/lib/routines.js new file mode 100644 index 0000000..7613b30 --- /dev/null +++ b/frontend/src/lib/routines.js @@ -0,0 +1,12 @@ +import { uid } from './format.js' + +/** + * Create a deep copy of a routine with a new id and a "(Copy)" suffix. + * All exercises and their configuration are preserved independently. + */ +export function copyRoutine(routine, suffix = 'Copy') { + const copy = structuredClone(routine) + copy.id = uid() + copy.name = routine.name + ' (' + suffix + ')' + return copy +} diff --git a/frontend/src/lib/routines.test.js b/frontend/src/lib/routines.test.js new file mode 100644 index 0000000..8c6912b --- /dev/null +++ b/frontend/src/lib/routines.test.js @@ -0,0 +1,79 @@ +import { describe, it, expect } from 'vitest' +import { copyRoutine } from './routines.js' + +const routine = { + id: 'r1', + name: 'Push Day', + emoji: 'arm', + prog: 'linear', + ex: [ + { id: 'ex1', sets: 3, reps: 10, weight: 60, sg: 'sg1' }, + { id: 'ex2', sets: 4, reps: 8, weight: 80, sg: 'sg1' }, + { id: 'ex3', sets: 3, reps: 12, weight: 20 } + ] +} + +describe('copyRoutine', () => { + it('returns a new routine with a different id', () => { + const copy = copyRoutine(routine) + expect(copy.id).not.toBe(routine.id) + expect(copy.id).toBeTruthy() + }) + + it('appends "(Copy)" to the name by default', () => { + const copy = copyRoutine(routine) + expect(copy.name).toBe('Push Day (Copy)') + }) + + it('uses a custom suffix when provided', () => { + const copy = copyRoutine(routine, 'Kopie') + expect(copy.name).toBe('Push Day (Kopie)') + }) + + it('deep-copies all exercises with their configuration', () => { + const copy = copyRoutine(routine) + expect(copy.ex).toHaveLength(3) + expect(copy.ex[0]).toEqual(routine.ex[0]) + expect(copy.ex[1]).toEqual(routine.ex[1]) + expect(copy.ex[2]).toEqual(routine.ex[2]) + }) + + it('preserves exercise ids (references to the global library)', () => { + const copy = copyRoutine(routine) + expect(copy.ex[0].id).toBe('ex1') + expect(copy.ex[1].id).toBe('ex2') + expect(copy.ex[2].id).toBe('ex3') + }) + + it('preserves superset groupings', () => { + const copy = copyRoutine(routine) + expect(copy.ex[0].sg).toBe('sg1') + expect(copy.ex[1].sg).toBe('sg1') + expect(copy.ex[2].sg).toBeUndefined() + }) + + it('preserves the emoji and progression settings', () => { + const copy = copyRoutine(routine) + expect(copy.emoji).toBe('arm') + expect(copy.prog).toBe('linear') + }) + + it('produces an independent copy — mutating it does not affect the original', () => { + const copy = copyRoutine(routine) + copy.name = 'Modified' + copy.ex[0].weight = 999 + copy.ex.push({ id: 'ex4', sets: 5, reps: 5 }) + + expect(routine.name).toBe('Push Day') + expect(routine.ex[0].weight).toBe(60) + expect(routine.ex).toHaveLength(3) + }) + + it('handles a routine with no exercises', () => { + const empty = { id: 'r2', name: 'Empty', emoji: 'dumbbell', ex: [] } + const copy = copyRoutine(empty) + expect(copy.ex).toEqual([]) + expect(copy.name).toBe('Empty (Copy)') + expect(copy.id).not.toBe('r2') + }) +}) diff --git a/frontend/src/views/RoutineEdit.jsx b/frontend/src/views/RoutineEdit.jsx index 701b062..bedef96 100644 --- a/frontend/src/views/RoutineEdit.jsx +++ b/frontend/src/views/RoutineEdit.jsx @@ -11,6 +11,7 @@ import { glyphPicker, exercisePicker, exConfigSheet, confirmSheet } from '../she import Icon from '../components/Icon.jsx' import { glyphOf } from '../lib/glyphs.js' import { Button, Row, SelectRow, Switch } from '../components/ui.jsx' +import { copyRoutine } from '../lib/routines.js' import { POLICIES_FOR, POLICY_NAME, POLICY_DESC } from '../lib/progression.js' import BodyMap from '../components/BodyMap.jsx' import { loadOfRoutine, rankOf, MUSCLE_NAME } from '../lib/muscles.js' @@ -425,6 +426,12 @@ export default function RoutineEdit() {
{t('Tap the link button on an exercise to superset it with the one above — you’ll do them back-to-back.')}
+ +