fix(workout): continue after added exercise

This commit is contained in:
Space_Hermes committed 2026-08-27 09:15:40 +02:00
1 parent 5314b342ff
commit 82f0b84766
5 files changed
+200 -27

No files matched your search

+21
View File
@@ -2,6 +2,27 @@
// the stores makes the uneven-round and re-check rules explicit and directly testable.
const hasWork = (entries, idx) => !!entries[idx]?.sets?.some(set => !set.done)
// Return the first unfinished navigation unit after the current one, wrapping once so a user
// who completed units out of order is never offered workout completion while earlier work remains.
export function nextUnfinishedUnit(entries, units, fromIdx) {
if (!Array.isArray(entries) || !Array.isArray(units) || units.length === 0) return null
const current = units.findIndex(unit => unit.includes(fromIdx))
const ordered = current < 0
? units
: [...units.slice(current + 1), ...units.slice(0, current)]
return ordered.find(unit => unit.some(idx => hasWork(entries, idx))) || null
}
// The current exercise may be one member of a contiguous superset. Insert after that complete
// navigation unit; invalid/empty state safely falls back to the end of the entry list.
export function insertionIndexAfterCurrentUnit(units, currentIndex, entryCount) {
const length = Math.max(0, Number(entryCount) || 0)
if (!Array.isArray(units) || units.length === 0) return length
const unit = units.find(candidate => candidate.includes(currentIndex))
if (!unit?.length) return length
return Math.min(length, Math.max(...unit) + 1)
}
// A completion is new progress only when it takes this exercise beyond the largest number of
// simultaneously completed sets seen in this mounted session. Uncheck/re-check therefore does
// not repeat navigation or rest side effects, while completing an added set still can.
+21 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { setProgressHighWater, supersetFlowStep, restAfterSet, restOnRecheck } from './supersetFlow.js'
import { insertionIndexAfterCurrentUnit, nextUnfinishedUnit, setProgressHighWater, supersetFlowStep, restAfterSet, restOnRecheck } from './supersetFlow.js'
const entry = done => ({ sets: done.map(value => ({ done: value })) })
@@ -52,6 +52,26 @@ describe('supersetFlowStep', () => {
})
})
describe('active workout unit ordering', () => {
it('inserts after the whole current unit', () => {
expect(insertionIndexAfterCurrentUnit([[0, 1], [2]], 0, 3)).toBe(2)
expect(insertionIndexAfterCurrentUnit([[0], [1]], 1, 2)).toBe(2)
expect(insertionIndexAfterCurrentUnit([], 0, 0)).toBe(0)
})
it('finds the next unfinished unit, skips completed units, and wraps once', () => {
const entries = [entry([false]), entry([true]), entry([false]), entry([true])]
const units = [[0], [1], [2, 3]]
expect(nextUnfinishedUnit(entries, units, 0)).toEqual([2, 3])
expect(nextUnfinishedUnit(entries, units, 2)).toEqual([0])
})
it('returns null only when every unit is complete', () => {
const entries = [entry([true]), entry([true])]
expect(nextUnfinishedUnit(entries, [[0], [1]], 1)).toBeNull()
})
})
// Issue #3 has two halves. restAfterSet covers "no break after the LAST set of an exercise";
// this covers "after the first set, sometimes a break doesn't appear" — the uncheck/re-check
// that the high-water rule swallows.