Merge remote-tracking branch 'pr/64'

This commit is contained in:
Duarte Santos committed 2026-08-11 22:30:09 +02:00
commit fe585097c8
15 files changed
+162 -16

No files matched your search

+54
View File
@@ -142,6 +142,60 @@ export function cleanupSg(ex) {
})
}
// Return the contiguous run around an entry that shares its superset id. A repeated id in a
// separated part of the list is deliberately not included: the display semantics are adjacent
// entries sharing one id, not every entry that happens to carry that id.
function contiguousSgGroup(items, idx) {
const sg = items[idx]?.sg
if (!sg) return [idx]
let first = idx
let last = idx
while (first > 0 && items[first - 1]?.sg === sg) first--
while (last + 1 < items.length && items[last + 1]?.sg === sg) last++
return Array.from({ length: last - first + 1 }, (_, i) => first + i)
}
function freshSg(items, first, second) {
const base = `sg-${Math.min(first, second)}-${Math.max(first, second)}`
let sg = base
let n = 2
while (items.some(e => e.sg === sg)) sg = `${base}-${n++}`
return sg
}
// Purely pair two adjacent entries. Existing contiguous groups on either side are merged, so
// pairing the end of one group with the start of another produces one display unit. A caller can
// provide a group id (useful when restoring a known id); otherwise an existing id is preferred,
// with a deterministic unused id for two previously ungrouped entries.
export function pairAdjacent(items, first, second, groupId) {
if (!Array.isArray(items)) throw new TypeError('Superset entries must be an array')
if (!Number.isInteger(first) || !Number.isInteger(second) || !items[first] || !items[second]) {
throw new RangeError('Superset entry indexes are invalid')
}
if (Math.abs(first - second) !== 1) throw new RangeError('Superset entries must be adjacent')
const next = items.map(e => ({ ...e }))
const left = Math.min(first, second)
const right = Math.max(first, second)
const group = groupId || next[left].sg || next[right].sg || freshSg(next, left, right)
const members = new Set([...contiguousSgGroup(next, left), ...contiguousSgGroup(next, right)])
members.forEach(i => { next[i].sg = group })
return next
}
// Remove one entry from its superset and clean any ids that no longer have an adjacent partner.
// This is pure so the active workout can replace its entries atomically through the store.
export function unpairSuperset(items, idx) {
if (!Array.isArray(items)) throw new TypeError('Superset entries must be an array')
if (!Number.isInteger(idx) || !items[idx]) throw new RangeError('Superset entry index is invalid')
const next = items.map(e => ({ ...e }))
delete next[idx].sg
next.forEach((e, i) => {
if (e.sg && !(next[i - 1]?.sg === e.sg || next[i + 1]?.sg === e.sg)) delete e.sg
})
return next
}
export function lastEntryFor(S, exId) {
for (let i = S.workouts.length - 1; i >= 0; i--) {
const en = S.workouts[i].entries.find(e => e.id === exId)
+44 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { modeOf, isTimed, fmtSec, setLabel, defaultConfig, buildSets, freestyleConfig, exLine, workoutVolume, effortOf, stepEffort, capEffort, isBw, isPerSide, sideReps, repStep, cascadeWeight, insertWarmupRow, removeRowAt } from './history.js'
import { modeOf, isTimed, fmtSec, setLabel, defaultConfig, buildSets, freestyleConfig, exLine, workoutVolume, effortOf, stepEffort, capEffort, isBw, isPerSide, sideReps, repStep, cascadeWeight, insertWarmupRow, removeRowAt, pairAdjacent, unpairSuperset, supersetUnits } from './history.js'
import { EXDB } from './exercises.js'
// Real ids out of the shipped catalogue, so the body-part fallback is exercised for real.
@@ -9,6 +9,49 @@ const CARDIO = EXDB.find(e => e.bp === 'cardio').id
const LIFT = EXDB.find(e => e.bp !== 'cardio' && e.eq !== 'body weight').id
const BW = EXDB.find(e => e.eq === 'body weight').id
describe('superset editing', () => {
it('pairs adjacent entries without mutating the source and keeps the display units contiguous', () => {
const entries = [{ id: 'a' }, { id: 'b' }, { id: 'c' }]
const paired = pairAdjacent(entries, 1, 2, 'sg-new')
expect(paired).toEqual([{ id: 'a' }, { id: 'b', sg: 'sg-new' }, { id: 'c', sg: 'sg-new' }])
expect(entries).toEqual([{ id: 'a' }, { id: 'b' }, { id: 'c' }])
expect(supersetUnits(paired)).toEqual([[0], [1, 2]])
})
it('merges both contiguous groups when their boundary entries are paired', () => {
const entries = [
{ id: 'a', sg: 'left' }, { id: 'b', sg: 'left' },
{ id: 'c', sg: 'right' }, { id: 'd', sg: 'right' }
]
const merged = pairAdjacent(entries, 1, 2)
expect(merged.map(e => e.sg)).toEqual(['left', 'left', 'left', 'left'])
expect(entries.map(e => e.sg)).toEqual(['left', 'left', 'right', 'right'])
})
it('unpairs one entry and removes sg values left without an adjacent partner', () => {
const entries = [
{ id: 'a', sg: 'group' }, { id: 'b', sg: 'group' }, { id: 'c', sg: 'group' },
{ id: 'd', sg: 'orphan' }
]
const unpaired = unpairSuperset(entries, 1)
expect(unpaired).toEqual([{ id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' }])
expect(entries.map(e => e.sg)).toEqual(['group', 'group', 'group', 'orphan'])
})
it('rejects a non-adjacent pairing request', () => {
const entries = [{ id: 'a' }, { id: 'b' }, { id: 'c' }]
expect(() => pairAdjacent(entries, 0, 2, 'sg-invalid')).toThrow(/adjacent/)
expect(entries).toEqual([{ id: 'a' }, { id: 'b' }, { id: 'c' }])
})
})
describe('modeOf', () => {
it('falls back to the body part when a plan has no mode — every existing plan keeps working', () => {
expect(modeOf({ id: CARDIO })).toBe('cardio')