diff --git a/frontend/src/lib/demoSeed.js b/frontend/src/lib/demoSeed.js index f8f2372..57a9daf 100644 --- a/frontend/src/lib/demoSeed.js +++ b/frontend/src/lib/demoSeed.js @@ -2,6 +2,7 @@ // out of the bundle self-hosters ship. import { isoOf, uid } from './format.js' import { starterRoutines } from './starter.js' +import { modeOf } from './history.js' // Starting weight and weekly increment per exercise of the starter plan (kg). // Chest dips are body-weight only here, so they log reps at 0 added weight. @@ -14,6 +15,30 @@ const WEEKS = 12 // how much history to fabricate const BW_FROM = 82.4, BW_TO = 78.3 // body-weight trend across those weeks const TARGET_W = 77 +// --- Effort ----------------------------------------------------------------------------- +// The demo has to show the effort stats, not just the volume ones, so the history carries +// ratings. Flat ratings would draw a flat trend and prove nothing, so this fabricates the +// shape the charts exist to make visible: a block grinding toward failure, a deload jumping +// back off it, another block going a little deeper than the first. +const DELOAD_WEEK = 5 +// Reps left in the tank the block is aiming for, by week. +const weekTarget = wk => + wk === DELOAD_WEEK ? 4.5 + : wk < DELOAD_WEEK ? 2.8 - wk * 0.3 + : 2.6 - (wk - DELOAD_WEEK - 1) * 0.26 +// Leg day is trained further from failure than the upper body — deliberate, so the muscle +// map's "hard sets" mode shows a different picture from its all-sets mode. +const EASY = new Set(['0043', '0085', '0739', '0585', '0586']) +// One exercise nobody ever rates: partial coverage is the normal case (rating is optional and +// off by default), and it shows the per-exercise Effort toggle correctly staying away. +const NEVER_RATED = '0605' +const UNRATED = 0.1 // …plus this share of the remaining sets, at random +// The first weeks are logged in RPE, as if they came out of another app before the profile +// switched to RIR. A set is never rewritten (see history.js), so the stats have to average a +// mixed history as one series — the demo should be showing that, not hiding it. +const RPE_UNTIL = 3 +const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v)) + // Deterministic PRNG — the demo should look the same on every visit and in screenshots. function rng(seed) { let a = seed >>> 0 @@ -26,9 +51,14 @@ function rng(seed) { } const round = (w, step) => Math.round(w / step) * step const at = (date, h, m) => { const d = new Date(date); d.setHours(h, m, 0, 0); return d.getTime() } +// The Monday of a date. The effort trend is plotted per calendar week, so the training block +// has to run on calendar weeks too — a deload counted off the first day of the history would +// straddle two points and average itself away in both. +const monday = date => { const d = new Date(date); d.setDate(d.getDate() - ((d.getDay() + 6) % 7)); d.setHours(12, 0, 0, 0); return +d } // A full example profile: 12 weeks of Mon/Wed/Fri sessions on the starter plan, with linear -// progression, the odd missed session, and twice-weekly weigh-ins trending toward the goal. +// progression, the odd missed session, twice-weekly weigh-ins trending toward the goal, and +// per-set effort ratings on most (not all) of it. export function buildDemoState() { const rnd = rng(20260723) const [push, pull, legs] = starterRoutines() @@ -61,15 +91,35 @@ export function buildDemoState() { if (iso === isoOf(today) && nowH < 18) continue // leave today's session to try out, unless it's already evening const prs = [] - const entries = routine.ex.map(cfg => { + const blockWk = Math.round((monday(day) - monday(start)) / (7 * 86400000)) + const rir0 = weekTarget(blockWk) + const scale = blockWk < RPE_UNTIL ? 'rpe' : 'rir' + const entries = routine.ex.map((cfg, exIdx) => { const [base, inc] = PROG[cfg.id] || [20, 0.5] const step = base >= 40 ? 2.5 : 1.25 - const w = base ? Math.max(step, round(base + inc * weekIdx, step)) : 0 + // The deload pulls the weight back too — effort dropping on its own would look like the + // same session suddenly got easy. + const back = blockWk === DELOAD_WEEK ? 0.88 : 1 + const w = base ? Math.max(step, round((base + inc * weekIdx) * back, step)) : 0 + const rateable = modeOf(cfg) === 'reps' && cfg.id !== NEVER_RATED const sets = [] for (let i = 0; i < cfg.sets; i++) { // last set is where reps usually start slipping const drop = i === cfg.sets - 1 && rnd() < 0.55 ? (rnd() < 0.4 ? 2 : 1) : 0 - sets.push({ w, r: Math.max(4, cfg.reps - drop), done: true }) + const s = { w, r: Math.max(4, cfg.reps - drop), done: true } + const rir = clamp(round(rir0 + + (cfg.sets - 1 - i) * 0.6 // a first set sits further from failure than a last + - exIdx * 0.12 // …and fatigue accumulates across the session + + (EASY.has(cfg.id) ? 1.2 : 0) + - (drop ? 0.5 : 0) // reps slipping is the set that ran out of room + + (rnd() - 0.5), 0.5), 0, 6) + if (rateable && rnd() > UNRATED) { + // RPE's floor of 6 is a convention about which sets are worth rating, so an easy + // set logged in RPE genuinely loses the distance it was from failure. + if (scale === 'rpe') s.rpe = clamp(10 - rir, 6, 10) + else s.rir = rir + } + sets.push(s) } if (w > (best[cfg.id] || 0)) { best[cfg.id] = w; prs.push(cfg.id) } exWeights[cfg.id] = { w: Math.max(w, exWeights[cfg.id]?.w || 0), d: iso } @@ -103,6 +153,9 @@ export function buildDemoState() { week: { 1: push.id, 3: pull.id, 5: legs.id }, dayPlan, workouts, bodyweight, exWeights, - targetW: TARGET_W + targetW: TARGET_W, + // The history is rated, so the demo turns the column on and the stats get a scale to + // label their aggregates with instead of guessing one (see displayScale). + effort: 'rir' } } diff --git a/frontend/src/lib/demoSeed.test.js b/frontend/src/lib/demoSeed.test.js new file mode 100644 index 0000000..03315c3 --- /dev/null +++ b/frontend/src/lib/demoSeed.test.js @@ -0,0 +1,109 @@ +// The demo build is the only openGym most people ever see, so its seeded history has to +// exercise the stats it is there to show off — including the effort card, which renders as +// dashes on a history that is rated too thinly or not at all. +import { describe, it, expect } from 'vitest' +import { buildDemoState } from './demoSeed.js' +import { + effortSummary, effortWeeks, effortHistogram, hasEffort, displayScale, avgRir, + rirOf, isHardSet, MIN_RATED, HARD_RIR +} from './effort.js' +import { effortOf, modeOf } from './history.js' + +const S = buildDemoState() +const eachSet = fn => S.workouts.forEach(w => w.entries.forEach(e => e.sets.forEach(s => fn(s, w, e)))) +const sum = effortSummary(S, 0) // 0 = the whole history + +describe('demo seed — effort', () => { + it('rates enough of the history to clear every guard in the effort stats', () => { + expect(hasEffort(S)).toBe(true) + expect(sum.done).toBeGreaterThan(400) + expect(sum.rated).toBeGreaterThan(MIN_RATED * 20) + expect(sum.avg).not.toBeNull() + expect(sum.hardPct).not.toBeNull() + // Somewhere between "everything is a grinder" and "nothing ever gets close". + expect(sum.hardPct).toBeGreaterThan(0.3) + expect(sum.hardPct).toBeLessThan(0.9) + }) + + it('leaves coverage partial, because that is the normal case the UI states a denominator for', () => { + const cov = sum.rated / sum.done + expect(cov).toBeGreaterThan(0.7) + expect(cov).toBeLessThan(0.95) + }) + + it('never rates a set that has no reps to leave in the tank', () => { + eachSet((s, w, e) => { + if (rirOf(s) != null) expect(modeOf({ ...(e.target || {}), id: e.id })).toBe('reps') + }) + }) + + it('leaves one exercise unrated throughout, so the per-exercise Effort toggle is absent for it', () => { + const rated = {} + eachSet((s, w, e) => { rated[e.id] = (rated[e.id] || 0) + (rirOf(s) != null ? 1 : 0) }) + const ids = Object.keys(rated) + expect(ids.filter(id => rated[id] === 0)).toEqual(['0605']) + // …while the rest carry enough rated sessions for a curve of their own (needs 3). + ids.filter(id => id !== '0605').forEach(id => { + const sessions = S.workouts.filter(w => { + const en = w.entries.find(e => e.id === id) + return en && avgRir(en.sets.filter(s => s.done)) != null + }) + expect(sessions.length).toBeGreaterThanOrEqual(3) + }) + }) + + it('labels the aggregates in the scale the profile logs', () => { + expect(effortOf(S)).toBe('rir') + expect(displayScale(S)).toBe('rir') + // The oldest block is written in RPE, as if imported — the stats have to average the mix. + let rir = 0, rpe = 0 + eachSet(s => { if (s.rir != null) rir++; else if (s.rpe != null) rpe++ }) + expect(rir).toBeGreaterThan(0) + expect(rpe).toBeGreaterThan(0) + }) + + it('draws a weekly trend with a point for every week of the history', () => { + const wks = effortWeeks(S, 0) + expect(wks.length).toBeGreaterThanOrEqual(10) + wks.forEach(w => { expect(w.n).toBeGreaterThanOrEqual(2); expect(w.sets).toBeGreaterThanOrEqual(w.n) }) + // Sorted oldest first, one point per calendar week. + expect(wks.map(w => w.t)).toEqual([...wks.map(w => w.t)].sort((a, b) => a - b)) + expect(new Set(wks.map(w => w.t)).size).toBe(wks.length) + }) + + it('makes the deload visible as a genuinely easier week', () => { + const wks = effortWeeks(S, 0) + const easiest = wks.reduce((a, b) => (b.rir > a.rir ? b : a)) + const rest = wks.filter(w => w !== easiest) + const restAvg = rest.reduce((a, w) => a + w.rir, 0) / rest.length + expect(easiest.rir - restAvg).toBeGreaterThan(1) // a step, not noise + // and the blocks around it grind toward failure rather than sitting flat + const hardest = wks.reduce((a, b) => (b.rir < a.rir ? b : a)) + expect(easiest.rir - hardest.rir).toBeGreaterThan(1.5) + expect(hardest.t).toBeGreaterThan(easiest.t) // the deepest week comes after it + }) + + it('spreads across the scale instead of piling onto one bucket', () => { + const hist = effortHistogram(S, 0) + expect(hist.reduce((n, b) => n + b.n, 0)).toBe(sum.rated) + expect(hist.filter(b => b.pct > 0.05).length).toBeGreaterThan(2) + expect(Math.max(...hist.map(b => b.pct))).toBeLessThan(0.6) + // both ends occupied: sets taken to (or near) failure and sets left well short of it + expect(hist[0].n + hist[1].n).toBeGreaterThan(0) + expect(hist[hist.length - 1].n).toBeGreaterThan(0) + }) + + it('has hard sets to filter the muscle map by', () => { + let hard = 0 + eachSet(s => { if (isHardSet(s)) hard++ }) + expect(hard).toBe(sum.hard) + expect(hard).toBeGreaterThan(50) + eachSet(s => { if (isHardSet(s)) expect(rirOf(s)).toBeLessThanOrEqual(HARD_RIR) }) + }) + + it('is deterministic — two builds produce the same ratings', () => { + const b = buildDemoState() + const flat = st => st.workouts.map(w => w.entries.map(e => e.sets.map(s => `${s.w}x${s.r}/${s.rir ?? ''}/${s.rpe ?? ''}`).join(',')).join('|')).join(';') + expect(flat(b)).toBe(flat(S)) + }) +})