Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

8.4 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { EXIDX } from './exercises.js'
import { ALL_EQUIPMENT, eqAvailable } from './equipment.js'
import { buildStarterPlan, starterPlanDays, starterPlanOptions, starterRoutines } from './starter.js'

// The approved prescription, written out again rather than imported: a test that reads the
// same table as the code would pass no matter what that table said. [weekday, name, sets].
const APPROVED = {
  ppl: [
    [1, 'Push Day', [['0025', 4, 8], ['0047', 3, 10], ['0426', 3, 10], ['0334', 3, 12], ['0241', 3, 12], ['0251', 3, 10]]],
    [3, 'Pull Day', [['2330', 4, 10], ['0027', 4, 8], ['1323', 3, 10], ['0031', 3, 10], ['0313', 3, 12]]],
    [5, 'Leg Day', [['0043', 4, 8], ['0085', 3, 10], ['0739', 3, 12], ['0585', 3, 12], ['0586', 3, 12], ['0605', 4, 15]]],
  ],
  'upper-lower': [
    [1, 'Upper A', [['0025', 3, 8], ['2330', 3, 10], ['0047', 2, 10], ['1323', 2, 10], ['0334', 2, 12], ['0241', 2, 12], ['0031', 2, 12]]],
    [2, 'Lower A', [['0043', 3, 8], ['0085', 3, 8], ['0739', 2, 10], ['0586', 2, 12], ['0605', 3, 15]]],
    [4, 'Upper B', [['0047', 3, 8], ['0027', 3, 8], ['0426', 2, 10], ['2330', 2, 10], ['0334', 2, 12], ['0241', 2, 12], ['0313', 2, 12]]],
    [5, 'Lower B', [['0739', 3, 10], ['0085', 2, 10], ['0585', 2, 12], ['0586', 3, 12], ['0605', 3, 15]]],
  ],
  'full-body': [
    [1, 'Full Body A', [['0043', 3, 8], ['0025', 3, 8], ['2330', 3, 10], ['0586', 3, 12], ['0334', 2, 12], ['0031', 2, 12]]],
    [3, 'Full Body B', [['0085', 3, 8], ['0047', 3, 10], ['1323', 3, 10], ['0585', 3, 12], ['0334', 2, 12], ['0241', 2, 12]]],
    [5, 'Full Body C', [['0739', 3, 10], ['0025', 2, 10], ['0027', 3, 10], ['0426', 2, 10], ['0586', 3, 12], ['0605', 3, 15]]],
  ],
  '5x5': [
    [1, '5×5 A', [['0043', 5, 5], ['0025', 5, 5], ['0027', 5, 5]]],
    [3, '5×5 B', [['0085', 5, 5], ['0426', 5, 5], ['2330', 5, 5]]],
    [5, '5×5 C', [['0739', 5, 5], ['0047', 5, 5], ['1323', 5, 5]]],
  ],
}

const shape = r => r.ex.map(e => [e.id, e.sets, e.reps])

describe('starter plan catalog', () => {
  it('offers exactly the four plans, with the day count read off the schedule', () => {
    expect(starterPlanOptions()).toEqual([
      { id: 'ppl', days: 3 }, { id: 'upper-lower', days: 4 },
      { id: 'full-body', days: 3 }, { id: '5x5', days: 3 },
    ])
    for (const { id, days } of starterPlanOptions()) expect(starterPlanDays(id)).toHaveLength(days)
  })

  it('changes nothing for an unknown plan id', () => {
    expect(buildStarterPlan('nope')).toBeNull()
    expect(buildStarterPlan(undefined)).toBeNull()
    // A click handler wired straight to a loader would hand the event in as the plan.
    expect(buildStarterPlan({ type: 'click' })).toBeNull()
    expect(starterPlanDays('nope')).toBeNull()
  })
})

describe.each(Object.keys(APPROVED))('%s', planId => {
  const approved = APPROVED[planId]

  it('builds the approved exercises, sets and reps in order', () => {
    const { routines } = buildStarterPlan(planId)
    expect(routines.map(r => r.name)).toEqual(approved.map(([, name]) => name))
    routines.forEach((r, i) => expect(shape(r)).toEqual(approved[i][2]))
  })

  it('puts each routine on its approved weekday, by identity not position', () => {
    const { routines, schedule } = buildStarterPlan(planId)
    const nameOf = Object.fromEntries(routines.map(r => [r.id, r.name]))
    expect(schedule.map(({ day, routineId }) => [day, nameOf[routineId]]))
      .toEqual(approved.map(([day, name]) => [day, name]))
  })

  it('references only real exercises and starts every one at weight 0', () => {
    for (const r of buildStarterPlan(planId).routines) {
      for (const e of r.ex) {
        expect(EXIDX[e.id], e.id).toBeTruthy()
        expect(e.sets).toBeGreaterThan(0)
        expect(e.reps).toBeGreaterThan(0)
        expect(e.weight).toBe(0)
      }
    }
  })

  it('mints fresh ids and independent objects on every build', () => {
    const first = buildStarterPlan(planId)
    const second = buildStarterPlan(planId)
    const ids = first.routines.map(r => r.id)
    expect(new Set(ids).size).toBe(ids.length)
    expect(second.routines.some(r => ids.includes(r.id))).toBe(false)
    expect(first.routines[0].ex[0]).not.toBe(second.routines[0].ex[0])
    // and the static definition survives a caller mutating what it got back
    first.routines[0].ex[0].sets = 99
    expect(buildStarterPlan(planId).routines[0].ex[0].sets).toBe(approved[0][2][0][1])
  })
})

describe('starterRoutines (the demo build entry point)', () => {
  it('still returns push, pull and legs unchanged', () => {
    const routines = starterRoutines()
    expect(routines.map(r => r.name)).toEqual(['Push Day', 'Pull Day', 'Leg Day'])
    routines.forEach((r, i) => expect(shape(r)).toEqual(APPROVED.ppl[i][2]))
    expect(routines.map(r => r.emoji)).toEqual(['barbell', 'pullup', 'legs'])
  })

  it('mints fresh ids on every invocation', () => {
    const first = starterRoutines().map(r => r.id)
    const second = starterRoutines().map(r => r.id)
    expect(new Set([...first, ...second]).size).toBe(6)
  })
})

describe('fitting a starter plan to equipment', () => {
  const ids = plan => plan.routines.flatMap(r => r.ex.map(e => e.id))

  it('changes nothing when the equipment already covers the plan', () => {
    const all = ALL_EQUIPMENT
    const fitted = buildStarterPlan('ppl', all)
    expect(fitted.routines.map(shape)).toEqual(APPROVED.ppl.map(([, , list]) => list))
    expect(fitted.fit).toEqual({ swapped: 0, dropped: 0 })
  })

  it('has no fit report when no equipment is given', () => {
    expect(buildStarterPlan('ppl').fit).toBeUndefined()
  })

  it('swaps only what the equipment cannot do, keeping sets and reps', () => {
    const fitted = buildStarterPlan('ppl', ['dumbbell'])
    const push = fitted.routines[0]
    // dumbbell exercises stay put; the barbell bench and the cable pushdown are swapped
    expect(push.ex.map(e => e.id)).toContain('0426')
    expect(push.ex.map(e => e.id)).toContain('0334')
    expect(push.ex[0].id).not.toBe('0025')
    expect(push.ex[0].sets).toBe(4)
    expect(push.ex[0].reps).toBe(8)
    expect(fitted.fit.swapped).toBeGreaterThan(0)
  })

  it('only ever uses exercises the equipment allows (body weight is always allowed)', () => {
    for (const eq of [['dumbbell'], ['barbell', 'dumbbell'], ['band'], []]) {
      for (const { id } of starterPlanOptions()) {
        const fitted = buildStarterPlan(id, eq)
        for (const exId of ids(fitted)) expect(eqAvailable(eq, EXIDX[exId])).toBe(true)
      }
    }
  })

  it('never repeats an exercise inside one routine', () => {
    for (const eq of [['dumbbell'], ['band'], []]) {
      for (const { id } of starterPlanOptions()) {
        for (const r of buildStarterPlan(id, eq).routines) {
          const own = r.ex.map(e => e.id)
          expect(new Set(own).size).toBe(own.length)
        }
      }
    }
  })

  it('never stands in with a stretch, a hold or a drill that needs a ball nobody ticked', () => {
    const bad = /stretch|isometric|squeeze|yoga|toe touch|exercise ball|bosu/i
    for (const eq of [['dumbbell'], ['dumbbell'], ['band'], []]) {
      for (const { id } of starterPlanOptions()) {
        for (const exId of ids(buildStarterPlan(id, eq))) expect(EXIDX[exId].n).not.toMatch(bad)
      }
    }
  })

  it('prefers a loaded exercise over a body-weight drill for a loaded lift', () => {
    // leg extension has no dumbbell twin, so the nearest same-muscle options include plain
    // body-weight drills; a loaded leg exercise still wins
    const legs = buildStarterPlan('ppl', ['dumbbell']).routines.find(r => r.name === 'Leg Day')
    const swappedIn = legs.ex[3]
    expect(swappedIn.id).not.toBe('0585')
    expect(EXIDX[swappedIn.id].eq).toBe('dumbbell')
    expect(EXIDX[swappedIn.id].bp).toBe('upper legs')
  })

  it('swaps a barbell bench press for chest work with the same kit, not a push-up', () => {
    const push = buildStarterPlan('ppl', ['dumbbell']).routines[0]
    expect(EXIDX[push.ex[0].id].eq).toBe('dumbbell')
    expect(EXIDX[push.ex[0].id].bp).toBe('chest')
  })

  it('is deterministic', () => {
    const a = buildStarterPlan('upper-lower', ['dumbbell']).routines.map(shape)
    const b = buildStarterPlan('upper-lower', ['dumbbell']).routines.map(shape)
    expect(a).toEqual(b)
  })

  it('keeps every scheduled day pointing at a routine that exists', () => {
    const fitted = buildStarterPlan('5x5', [])
    const have = new Set(fitted.routines.map(r => r.id))
    for (const { routineId } of fitted.schedule) expect(have.has(routineId)).toBe(true)
  })
})