diff --git a/frontend/src/sheets.combine-ui.test.jsx b/frontend/src/sheets.combine-ui.test.jsx new file mode 100644 index 0000000..fba38e0 --- /dev/null +++ b/frontend/src/sheets.combine-ui.test.jsx @@ -0,0 +1,111 @@ +// @vitest-environment happy-dom +import React, { act } from 'react' +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { createRoot } from 'react-dom/client' +import { useStore } from './store/useStore.js' +import { useUI } from './store/useUI.js' +import { addRoutineToSessionSheet, workoutDetailSheet } from './sheets.jsx' +import { EXDB } from './lib/exercises.js' + +const clone = v => JSON.parse(JSON.stringify(v)) +const ids = EXDB.filter(e => e.bp !== 'cardio').slice(0, 4).map(e => e.id) +const mounted = [] + +function renderTop() { + const sheet = useUI.getState().sheets.at(-1) + const host = document.createElement('div') + document.body.appendChild(host) + const root = createRoot(host) + mounted.push(root) + act(() => root.render(sheet.render(() => useUI.getState().closeSheet(sheet.id)))) + return host +} +const rowFor = (host, name) => [...host.querySelectorAll('.item')].find(el => el.querySelector('.tt')?.textContent === name) + +beforeEach(() => { + globalThis.IS_REACT_ACT_ENVIRONMENT = true + useUI.setState({ sheets: [], toastMsg: '' }) + document.body.innerHTML = '' +}) +afterEach(() => { act(() => { mounted.splice(0).forEach(r => r.unmount()) }) }) + +describe('Add routine mid-session sheet', () => { + const setup = () => { + const S = clone(useStore.getState().S) + S.routines = [ + { id: 'strength', name: 'Strength', emoji: '🏋️', prog: 'off', ex: [{ id: ids[0], sets: 3, reps: 5, weight: 60 }] }, + { id: 'core', name: 'Core', emoji: '🧘', prog: 'off', ex: [{ id: ids[1], sets: 3, reps: 12, weight: 0 }, { id: ids[2], sets: 3, reps: 10, weight: 0 }] }, + { id: 'empty', name: 'Empty', emoji: '📝', ex: [] }, + ] + S.workouts = [] + S.active = { + id: 'a', d: '2026-09-06', start: 1, routineIds: ['strength'], name: 'Strength', cur: 0, + entries: [{ id: ids[0], rid: 'strength', target: {}, sets: [{ w: 60, r: 5, done: true }] }], + workoutView: 'cards', + } + useStore.setState({ S, user: null }) + } + + it('appends the picked routine’s entries with rid, updates routineIds + name, toasts', () => { + setup() + addRoutineToSessionSheet() + const host = renderTop() + act(() => { rowFor(host, 'Core').click() }) + + const a = useStore.getState().S.active + expect(a.routineIds).toEqual(['strength', 'core']) + expect(a.name).toBe('Strength + Core') + expect(a.entries.map(e => e.rid)).toEqual(['strength', 'core', 'core']) + expect(a.cur).toBe(0) // current unit is left where it was + expect(useUI.getState().toastMsg).toContain('Core added') + }) + + it('disables a routine already in the session and one with no exercises', () => { + setup() + addRoutineToSessionSheet() + const host = renderTop() + expect(rowFor(host, 'Strength').className).toContain('disabled') + expect(rowFor(host, 'Strength').textContent).toContain('already added') + expect(rowFor(host, 'Empty').className).toContain('disabled') + expect(rowFor(host, 'Empty').textContent).toContain('no exercises') + }) +}) + +describe('WorkoutDetail — per-routine grouping', () => { + const combined = { + id: 'w', d: '2026-09-06', start: 1, end: 2, name: 'Strength + Core', vol: 500, + routineIds: ['strength', 'core'], prs: [], + entries: [ + { id: ids[0], rid: 'strength', target: { reps: 5 }, sets: [{ w: 60, r: 5, done: true }, { w: 60, r: 5, done: true }] }, + { id: ids[1], rid: 'core', target: { reps: 12 }, sets: [{ w: 0, r: 12, done: true }] }, + ], + } + const legacy = { + id: 'w2', d: '2026-09-06', start: 1, end: 2, name: 'Push', vol: 100, routineIds: ['strength'], prs: [], + entries: [{ id: ids[0], target: { reps: 5 }, sets: [{ w: 100, r: 5, done: true }] }], + } + + beforeEach(() => { + const S = clone(useStore.getState().S) + S.routines = [ + { id: 'strength', name: 'Strength', emoji: '🏋️', ex: [] }, + { id: 'core', name: 'Core', emoji: '🧘', ex: [] }, + ] + useStore.setState({ S, user: null }) + }) + + it('groups a combined workout into per-routine sections with a sets/volume subheader', () => { + const host = (workoutDetailSheet(combined), renderTop()) + expect(host.textContent).toContain('Strength') + expect(host.textContent).toContain('Core') + expect(host.textContent).toMatch(/2 sets/) + expect(host.textContent).toMatch(/1 sets|1 set/) + }) + + it('renders a legacy single-routine workout flat — no routine subheader', () => { + const host = (workoutDetailSheet(legacy), renderTop()) + // the only routine name that could appear is the header

(w.name = "Push"); there is + // no "Strength" subheader row + expect(host.querySelectorAll('.row.between').length).toBe(0) + }) +}) diff --git a/frontend/src/sheets.jsx b/frontend/src/sheets.jsx index 8af095f..a04e587 100644 --- a/frontend/src/sheets.jsx +++ b/frontend/src/sheets.jsx @@ -1539,8 +1539,9 @@ export const dayOverrideSheet = iso => ui().openSheet(close => s.S) - // A weekday holds a routine-id list; this single-pick sheet sets the day to exactly one - // routine (or rest). The inline per-day management on the Plan screen is what appends. + // A weekday holds a routine-id list; this single-pick sheet sets an empty day to exactly one + // routine (or rest). The inline + Add routine on the Plan screen is what appends to a + // populated day. const cur = [].concat(st.week[day] || []) const set = v => { update(s => { if (v) s.week[day] = [v]; else delete s.week[day] }); close() } return <> @@ -1556,6 +1557,29 @@ function DayAssign({ day, close }) { } export const dayAssignSheet = day => ui().openSheet(close => ) +// + Add routine on a populated weekday: single-pick, appends to the day's list. A routine +// already on that day is disabled; picking one closes the sheet. +function DayAddRoutine({ day, close }) { + const st = useStore(s => s.S) + const on = new Set([].concat(st.week[day] || [])) + const add = id => { update(s => { s.week[day] = [...[].concat(s.week[day] || []), id] }); close() } + return <> +

{t('Add routine')}

+
+ {st.routines.map(r => { + const already = on.has(r.id) + return
add(r.id))}> + +
{r.name}
{exCount(r.ex.length)}
+ {already ? {t('already added')} : } +
+ })} +
+ +} +export const dayAddRoutineSheet = day => ui().openSheet(close => ) + /* ============================ workout detail ============================ */ function WorkoutDetail({ w, close }) { const noteRef = useRef(null) @@ -1587,20 +1611,44 @@ function WorkoutDetail({ w, close }) { if (text) rec.note = text; else delete rec.note }) }, []) + // A combined session's entries carry a `rid`; group them into per-routine sections in merge + // order. A legacy single-routine workout (one routineIds, or no rid anywhere) renders flat. + const entryRow = (e, i) => { + const ex = EXIDX[e.id] + return
+ {ex && } +
{ex ? exerciseNameFor(ex) : (e.n || e.id)} {w.prs && w.prs.includes(e.id) && PR}
+
{e.sets.filter(s => s.done).map(s => setLabel(e.id, s, e.target)).join(' · ') || t('no sets')}
+ {e.note &&
+ {e.notePin && }{e.note} +
}
+
+ } + const groups = [] + w.entries.forEach((e, i) => { + const key = e.rid || '__none' + let g = groups.find(x => x.key === key) + if (!g) { g = { key, rid: e.rid || null, items: [] }; groups.push(g) } + g.items.push([e, i]) + }) + const grouped = groups.length > 1 || (groups[0] && groups[0].rid && (w.routineIds || []).length > 1) return <>

{w.name}

{[fmtDate(w.d, true), ...durPart(w.end - w.start), fmtVol(w.vol, st.unit), ...(w.bw ? [fmtNum(w.bw) + ' ' + st.unit] : [])].join(' · ')}
- {w.entries.map((e, i) => { - const ex = EXIDX[e.id] - return
- {ex && } -
{ex ? exerciseNameFor(ex) : (e.n || e.id)} {w.prs && w.prs.includes(e.id) && PR}
-
{e.sets.filter(s => s.done).map(s => setLabel(e.id, s, e.target)).join(' · ') || t('no sets')}
- {e.note &&
- {e.notePin && }{e.note} -
}
+ {grouped ? groups.map(g => { + const r = g.rid ? st.routines.find(x => x.id === g.rid) : null + const setN = g.items.reduce((n, [e]) => n + e.sets.filter(s => s.done && !isWarmupRow(s)).length, 0) + const vol = workoutVolume({ entries: g.items.map(([e]) => e) }) + return
+
+
+ {r && }{r ? r.name : t('Freestyle')} +
+
{t('{0} sets', setN)} · {fmtVol(vol, st.unit)}
+
+ {g.items.map(([e, i]) => entryRow(e, i))}
- })} + }) : w.entries.map((e, i) => entryRow(e, i))}
{t('Session note')}