curated2: !43
This commit is contained in:
commit
0e3b8fa74b
18 files changed
+366
-2
No files matched your search
@@ -0,0 +1,44 @@
|
||||
import { supersetUnits, unitOf } from './history.js'
|
||||
|
||||
function hasLoggedSet(entry) {
|
||||
return Array.isArray(entry?.sets) && entry.sets.some(set => set?.done === true)
|
||||
}
|
||||
|
||||
/**
|
||||
* Swap one exact active-workout occurrence.
|
||||
*
|
||||
* Unlogged occurrences are replaced in place. Logged results are never relabelled: after explicit
|
||||
* confirmation, the replacement is inserted beside the original. A logged group member also
|
||||
* requires an explicit choice to keep the replacement in the group or detach it after the group.
|
||||
*/
|
||||
export function swapActiveExercise(active, index, replacement, {
|
||||
loggedConfirmed = false,
|
||||
groupDisposition
|
||||
} = {}) {
|
||||
if (!active || !Array.isArray(active.entries) || !replacement || index < 0 || index >= active.entries.length) return null
|
||||
|
||||
const current = active.entries[index]
|
||||
if (!hasLoggedSet(current)) {
|
||||
const metadata = Object.fromEntries(Object.entries(current).filter(([key]) => (
|
||||
!['id', 'target', 'plan', 'sets', 'sg'].includes(key)
|
||||
)))
|
||||
active.entries[index] = { ...metadata, ...replacement, ...(current.sg ? { sg: current.sg } : {}) }
|
||||
active.cur = index
|
||||
return { inserted: false, index }
|
||||
}
|
||||
|
||||
if (!loggedConfirmed) return { needsConfirmation: true, grouped: !!current.sg, index }
|
||||
if (current.sg && !['keep', 'detach'].includes(groupDisposition)) {
|
||||
return { needsConfirmation: true, grouped: true, index }
|
||||
}
|
||||
|
||||
const unit = unitOf(supersetUnits(active.entries), index)
|
||||
const keepGroup = current.sg && groupDisposition === 'keep'
|
||||
const insertAt = keepGroup ? index + 1 : (unit.length > 1 ? unit.at(-1) + 1 : index + 1)
|
||||
active.entries.splice(insertAt, 0, {
|
||||
...replacement,
|
||||
...(keepGroup ? { sg: current.sg } : {})
|
||||
})
|
||||
active.cur = insertAt
|
||||
return { inserted: true, index: insertAt }
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { swapActiveExercise } from './active-exercise-swap.js'
|
||||
|
||||
const entry = (id, { sg, done = false, target, sets } = {}) => ({
|
||||
id,
|
||||
...(sg ? { sg } : {}),
|
||||
target: target || { mode: 'reps', sets: 1, reps: 5, weight: 40 },
|
||||
sets: sets || [{ w: 40, r: 5, done }]
|
||||
})
|
||||
|
||||
const replacement = () => entry('incline', {
|
||||
target: { mode: 'reps', sets: 2, reps: 8, weight: 32.5, note: 'Keep elbows tucked.', intensifier: { type: 'dropset', count: 1, pct: 20 } },
|
||||
sets: [{ w: 32.5, r: 8, done: false }, { w: 32.5, r: 8, done: false }]
|
||||
})
|
||||
|
||||
describe('safe active exercise swap', () => {
|
||||
it('replaces only the selected duplicate occurrence and preserves its group and replacement metadata', () => {
|
||||
const first = entry('bench', { sg: 'pair' })
|
||||
const selected = entry('bench', { sg: 'pair' })
|
||||
selected.occurrenceId = 'bench#2'
|
||||
selected.provenance = { routineIndex: 4 }
|
||||
const unrelated = entry('row')
|
||||
const active = { cur: 1, entries: [first, selected, unrelated] }
|
||||
const next = replacement()
|
||||
|
||||
expect(swapActiveExercise(active, 1, next)).toEqual({ inserted: false, index: 1 })
|
||||
expect(active.entries.map(value => value.id)).toEqual(['bench', 'incline', 'row'])
|
||||
expect(active.entries[0]).toBe(first)
|
||||
expect(active.entries[2]).toBe(unrelated)
|
||||
expect(active.entries[1]).toEqual({
|
||||
...next,
|
||||
occurrenceId: 'bench#2',
|
||||
provenance: { routineIndex: 4 },
|
||||
sg: 'pair'
|
||||
})
|
||||
expect(active.cur).toBe(1)
|
||||
})
|
||||
|
||||
it('fails closed before a logged standalone occurrence is explicitly confirmed', () => {
|
||||
const logged = entry('bench', { sets: [{ w: 42.5, r: 7, done: true, rir: 1 }] })
|
||||
const active = { cur: 0, entries: [logged, entry('row')] }
|
||||
|
||||
expect(swapActiveExercise(active, 0, replacement())).toEqual({ needsConfirmation: true, grouped: false, index: 0 })
|
||||
expect(active.entries).toEqual([logged, active.entries[1]])
|
||||
expect(active.cur).toBe(0)
|
||||
})
|
||||
|
||||
it('preserves a confirmed logged occurrence and inserts the replacement after it', () => {
|
||||
const logged = entry('bench', { sets: [{ w: 42.5, r: 7, done: true, rir: 1 }] })
|
||||
const unrelated = entry('row')
|
||||
const active = { cur: 0, entries: [logged, unrelated] }
|
||||
|
||||
expect(swapActiveExercise(active, 0, replacement(), { loggedConfirmed: true })).toEqual({ inserted: true, index: 1 })
|
||||
expect(active.entries[0]).toBe(logged)
|
||||
expect(active.entries[0].sets[0]).toEqual({ w: 42.5, r: 7, done: true, rir: 1 })
|
||||
expect(active.entries[1].id).toBe('incline')
|
||||
expect(active.entries[2]).toBe(unrelated)
|
||||
expect(active.cur).toBe(1)
|
||||
})
|
||||
|
||||
it('requires an explicit grouped disposition before changing a logged group member', () => {
|
||||
const active = { cur: 0, entries: [entry('bench', { sg: 'pair', done: true }), entry('row', { sg: 'pair' }), entry('curl')] }
|
||||
const before = [...active.entries]
|
||||
|
||||
expect(swapActiveExercise(active, 0, replacement(), { loggedConfirmed: true }))
|
||||
.toEqual({ needsConfirmation: true, grouped: true, index: 0 })
|
||||
expect(active.entries).toEqual(before)
|
||||
})
|
||||
|
||||
it.each([
|
||||
['keep', ['bench', 'incline', 'row', 'curl'], ['pair', 'pair', 'pair', undefined], 1],
|
||||
['detach', ['bench', 'row', 'incline', 'curl'], ['pair', 'pair', undefined, undefined], 2]
|
||||
])('%s confirmation preserves logged group data and unrelated entries', (groupDisposition, ids, groups, cursor) => {
|
||||
const logged = entry('bench', { sg: 'pair', sets: [{ w: 45, r: 6, done: true }] })
|
||||
const partner = entry('row', { sg: 'pair' })
|
||||
const unrelated = entry('curl')
|
||||
const active = { cur: 0, entries: [logged, partner, unrelated] }
|
||||
|
||||
expect(swapActiveExercise(active, 0, replacement(), { loggedConfirmed: true, groupDisposition }))
|
||||
.toEqual({ inserted: true, index: cursor })
|
||||
expect(active.entries.map(value => value.id)).toEqual(ids)
|
||||
expect(active.entries.map(value => value.sg)).toEqual(groups)
|
||||
expect(active.entries[0]).toBe(logged)
|
||||
expect(active.entries.includes(partner)).toBe(true)
|
||||
expect(active.entries.at(-1)).toBe(unrelated)
|
||||
expect(active.cur).toBe(cursor)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user