workout: optional per-exercise rest for normal sets

An exercise can now carry its own restSec: the workout timer prefers it
over the global default, a superset rests the longest resolved rest in
the group after each round, and the field travels with shared plans —
normalised on import like every other prescribed number. An explicit
restSec still times its rest when the global timer is turned off; only
the default is silenced. Closes #10.
This commit is contained in:
Alexby committed 2026-08-26 16:03:27 +02:00
1 parent 5314b342ff
commit 0b4a224c95
20 files changed
+195 -15

No files matched your search

+15 -3
View File
@@ -44,6 +44,10 @@ function cleanEx(e) {
if (e.inc > 0) o.inc = e.inc
if (e.repsMin != null) o.repsMin = e.repsMin
if (e.repsMax != null) o.repsMax = e.repsMax
// The exercise's own rest (issue #10) is part of how it is prescribed, so it travels too —
// only when set, so a plan that never asked for one leaves the recipient's own default
// timer in charge. parsePlan and mergePlan carry it through by spread.
if (e.restSec > 0) o.restSec = e.restSec
if (e.sg) o.sg = e.sg
if (e.note) o.note = e.note
const warm = cleanWarmupSets(e.warmupSets)
@@ -63,6 +67,13 @@ function cleanWarmupSets(v) {
return n > 0 ? Math.min(MAX_PLANNED_WARMUPS, n) : 0
}
/** A positive whole number of seconds or nothing — the same gate cleanEx applies on the way
* out, so a hand-edited plan file can't hand the rest timer a string or a negative. */
function cleanRestSec(v) {
const n = Math.round(Number(v)) || 0
return n > 0 ? n : 0
}
/** Keep the floors the config sheet and applyIntensifierPlan already enforce, and nothing else:
* a plan file is someone else's data, so anything unrecognised is dropped rather than trusted. */
function cleanIntensifier(x) {
@@ -114,12 +125,13 @@ export function parsePlan(raw) {
if (!ok) dropped++
return ok
}).map(e => {
// The exercises pass through as written, so the two fields that carry numbers into the
// The exercises pass through as written, so the fields that carry numbers into the
// planner get the same clamps on the way in that they get on the way out.
const warm = cleanWarmupSets(e.warmupSets)
const intens = cleanIntensifier(e.intensifier)
const { warmupSets, intensifier, ...rest } = e
return { ...rest, ...(warm ? { warmupSets: warm } : {}), ...(intens ? { intensifier: intens } : {}) }
const rest = cleanRestSec(e.restSec)
const { warmupSets, intensifier, restSec, ...passthrough } = e
return { ...passthrough, ...(warm ? { warmupSets: warm } : {}), ...(intens ? { intensifier: intens } : {}), ...(rest ? { restSec: rest } : {}) }
})
}))
return {
+31 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { buildPlanBundle, parsePlan } from './plan-share.js'
import { buildPlanBundle, mergePlan, parsePlan } from './plan-share.js'
// There was no test file for plan sharing at all, which is how a whole prescription field
// went missing without anyone noticing.
@@ -24,6 +24,26 @@ describe('what survives a shared plan', () => {
expect(roundTrip({ warmupSets: 3 }).warmupSets).toBe(3)
})
// Issue #10: the rest an exercise prescribes is part of the prescription. A shared 5x5 whose
// rests arrive as the recipient's 60 s default is a different session than the one written.
it('carries a per-exercise rest', () => {
expect(roundTrip({ restSec: 180 }).restSec).toBe(180)
})
// The absence has to survive too: writing a 0 would pin the recipient's timer to "off"
// instead of letting the exercise keep inheriting whatever their own default is.
it('leaves an exercise that set no rest free of the field', () => {
expect('restSec' in roundTrip({})).toBe(false)
expect('restSec' in roundTrip({ restSec: 0 })).toBe(false)
})
it('carries the rest onto the routine mergePlan adds', () => {
const bundle = parsePlan(JSON.stringify(buildPlanBundle(stateWith({ restSec: 180 }), 'Plan')))
const s = { routines: [], customEx: [], week: {} }
mergePlan(s, bundle, { schedule: false })
expect(s.routines[0].ex[0].restSec).toBe(180)
})
it('drops an intensifier it does not recognise rather than passing it on', () => {
expect(roundTrip({ intensifier: { type: 'nonsense', count: 3 } }).intensifier).toBeUndefined()
})
@@ -33,6 +53,16 @@ describe('what survives a shared plan', () => {
expect(parsePlan(bundle).routines[0].ex[0].warmupSets).toBe(5)
})
// A plan file is someone else's data: a rest that arrives as a string would reach the timer's
// arithmetic as one, and a negative or garbage one has no meaning to keep.
it('normalises a hand-edited rest to a positive whole number or drops it', () => {
const withRest = restSec => ({ opengym_plan: 1, name: 'x', routines: [{ id: 'r', name: 'R', ex: [{ id: '0025', sets: 3, reps: 5, restSec }] }], week: {}, customEx: [] })
expect(parsePlan(withRest('120')).routines[0].ex[0].restSec).toBe(120)
expect(parsePlan(withRest(90.6)).routines[0].ex[0].restSec).toBe(91)
expect('restSec' in parsePlan(withRest(-30)).routines[0].ex[0]).toBe(false)
expect('restSec' in parsePlan(withRest('abc')).routines[0].ex[0]).toBe(false)
})
// The floors are the config sheet's own (count >= 1, pct >= 5); a value that is present but
// out of range is pulled up to the floor, while a missing one falls back to the default.
it('clamps out-of-range intensifier numbers to the floors the app enforces', () => {
+1 -1
View File
@@ -27,7 +27,7 @@ describe('Brazilian Portuguese locale', () => {
.sort(byCodeUnit)
const fingerprint = createHash('sha256').update(JSON.stringify(inherited)).digest('hex')
expect(Object.keys(PT_BR_OVERRIDES)).toHaveLength(291)
expect(Object.keys(PT_BR_OVERRIDES)).toHaveLength(292)
expect(inherited).toHaveLength(449)
// If this fails, review the changed keys and wording before accepting a new hash. From
// frontend/: node scripts/pt-br-inheritance-fingerprint.mjs --list
+26
View File
@@ -42,6 +42,32 @@ export function restOnRecheck({ timerRunning, unitDone, lastUnit }) {
return !timerRunning && restAfterSet({ unitDone, lastUnit })
}
/**
* How long the rest after a completed set should run, in seconds.
*
* An exercise may carry its own `restSec` in its target (issue #10) — a heavy triple and a set
* of curls do not want the same break. One that carries none inherits `defaultRestSec`, the
* global rest timer, which is what every routine did before the field existed.
*
* `unit` is the superset group the set belongs to, as entry indices — a plain exercise is a
* group of one. A group rests once, after the round, so it takes the LONGEST rest any of its
* members asked for: the shortest would send you back to the bar before the member that needs
* the most recovery is ready.
*
* `defaultRestSec` of 0 is the rest timer turned off (v1.2.11). That silences the members that
* have no rest of their own, but an exercise that explicitly asks for one still gets it — the
* setting is a default, and this field overrides the default.
*/
export function restSecFor(entries, unit, defaultRestSec) {
const fallback = defaultRestSec > 0 ? defaultRestSec : 0
const idxs = Array.isArray(unit) && unit.length ? unit : []
if (!idxs.length) return fallback
return idxs.reduce((longest, idx) => {
const own = entries?.[idx]?.target?.restSec
return Math.max(longest, own > 0 ? own : fallback)
}, 0)
}
// Decide where a newly completed superset set goes next. Spent members are skipped, including
// across the wrap. A round ends when no later member in display order has work left; this makes
// the last *active* member the boundary rather than blindly using the group's last array index.
+46 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { setProgressHighWater, supersetFlowStep, restAfterSet, restOnRecheck } from './supersetFlow.js'
import { setProgressHighWater, supersetFlowStep, restAfterSet, restOnRecheck, restSecFor } from './supersetFlow.js'
const entry = done => ({ sets: done.map(value => ({ done: value })) })
@@ -72,3 +72,48 @@ describe('rest on a re-check', () => {
expect(restOnRecheck({ timerRunning: false, unitDone: true, lastUnit: false })).toBe(true)
})
})
// Issue #10: a routine can give an exercise its own rest, and the global timer stops being the
// only answer. The resolution is the whole feature — the UI just writes the number down.
describe('restSecFor', () => {
// 0: no rest of its own, 1: 180 s, 2: 45 s — a heavy pull, a light accessory, a plain one.
const entries = [
{ id: 'a', target: { mode: 'reps' } },
{ id: 'b', target: { mode: 'reps', restSec: 180 } },
{ id: 'c', target: { mode: 'reps', restSec: 45 } },
]
it('prefers the exercise’s own rest over the global default', () => {
expect(restSecFor(entries, [1], 90)).toBe(180)
expect(restSecFor(entries, [2], 90)).toBe(45)
})
it('falls back to the global default when the exercise sets none', () => {
expect(restSecFor(entries, [0], 90)).toBe(90)
})
it('gives a superset the longest rest its members asked for', () => {
// Not the member that closed the round, and not the shortest: the group rests once, and
// the 180 s exercise is the one still recovering when the 45 s one is ready to go again.
expect(restSecFor(entries, [1, 2], 90)).toBe(180)
// A member with no rest of its own pulls in the global, which can be the longest of all.
expect(restSecFor(entries, [0, 2], 90)).toBe(90)
})
it('honours an explicit rest even with the global timer switched off', () => {
expect(restSecFor(entries, [1], 0)).toBe(180)
expect(restSecFor(entries, [0, 1], 0)).toBe(180)
})
it('stays off when the timer is off and nothing asked for a rest', () => {
expect(restSecFor(entries, [0], 0)).toBe(0)
expect(restSecFor(entries, [0, 0], 0)).toBe(0)
})
it('survives a missing unit or entry rather than timing NaN', () => {
expect(restSecFor(entries, null, 90)).toBe(90)
expect(restSecFor(entries, [], 90)).toBe(90)
expect(restSecFor(entries, [7], 90)).toBe(90)
expect(restSecFor(undefined, [0], undefined)).toBe(0)
})
})