|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { sessionNoProg, setSessionNoProg, setEntryNoProg, joinSessionNoProg } from './session-noprog.js' |
| 3 | +import { buildCompletedWorkout } from './finish-workout.js' |
| 4 | +import { entryExcluded, lastEntryFor } from './history.js' |
| 5 | + |
| 6 | +// "Don't count for progression" for the whole workout (Discord, asierlama: an injury day). |
| 7 | +const entry = (id, extra = {}) => ({ id, target: { sets: 1, reps: 5, weight: 60 }, sets: [{ w: 60, r: 5, done: true }], ...extra }) |
| 8 | +const session = entries => ({ id: 'a', d: '2026-09-24', start: 0, routineIds: ['main'], name: 'Main', entries }) |
| 9 | +const deloadOwns = e => e.rid === 'deload' |
| 10 | + |
| 11 | +describe('setSessionNoProg', () => { |
| 12 | + it('on: every entry is kept out and the session says it is out as a whole', () => { |
| 13 | + const active = session([entry('0025'), entry('0027', { noProg: true })]) |
| 14 | + setSessionNoProg(active, true) |
| 15 | + expect(sessionNoProg(active)).toBe(true) |
| 16 | + expect(active.entries.map(e => e.noProg)).toEqual([true, true]) |
| 17 | + }) |
| 18 | + |
| 19 | + it('off: every entry counts again, apart from the ones a deload routine keeps out', () => { |
| 20 | + const active = session([entry('0025', { rid: 'main' }), entry('0027', { rid: 'deload', noProg: true })]) |
| 21 | + setSessionNoProg(active, true, deloadOwns) |
| 22 | + setSessionNoProg(active, false, deloadOwns) |
| 23 | + expect(sessionNoProg(active)).toBe(false) |
| 24 | + expect('noProg' in active).toBe(false) |
| 25 | + expect(active.entries.map(e => e.noProg)).toEqual([undefined, true]) |
| 26 | + }) |
| 27 | + |
| 28 | + it('does nothing without a session', () => { |
| 29 | + expect(() => setSessionNoProg(null, true)).not.toThrow() |
| 30 | + expect(sessionNoProg(null)).toBe(false) |
| 31 | + }) |
| 32 | +}) |
| 33 | + |
| 34 | +describe('setEntryNoProg', () => { |
| 35 | + it('keeps one exercise out without making it the whole session', () => { |
| 36 | + const active = session([entry('0025')]) |
| 37 | + setEntryNoProg(active, 0, true) |
| 38 | + expect(active.entries[0].noProg).toBe(true) |
| 39 | + expect(sessionNoProg(active)).toBe(false) |
| 40 | + }) |
| 41 | + |
| 42 | + it('counting one exercise again ends the whole-session choice, the others stay out', () => { |
| 43 | + const active = session([entry('0025'), entry('0027')]) |
| 44 | + setSessionNoProg(active, true) |
| 45 | + setEntryNoProg(active, 1, false) |
| 46 | + expect(sessionNoProg(active)).toBe(false) |
| 47 | + expect(active.entries.map(e => e.noProg)).toEqual([true, undefined]) |
| 48 | + }) |
| 49 | + |
| 50 | + it('ignores an index with no entry', () => { |
| 51 | + const active = session([entry('0025')]) |
| 52 | + setSessionNoProg(active, true) |
| 53 | + setEntryNoProg(active, 5, false) |
| 54 | + expect(sessionNoProg(active)).toBe(true) |
| 55 | + }) |
| 56 | +}) |
| 57 | + |
| 58 | +describe('joinSessionNoProg', () => { |
| 59 | + it('an entry added to a session kept out as a whole is kept out too', () => { |
| 60 | + const active = session([entry('0025')]) |
| 61 | + setSessionNoProg(active, true) |
| 62 | + expect(joinSessionNoProg(active, entry('0043')).noProg).toBe(true) |
| 63 | + }) |
| 64 | + |
| 65 | + it('an exercise kept out by hand is not passed on to one added after it', () => { |
| 66 | + const active = session([entry('0025')]) |
| 67 | + setEntryNoProg(active, 0, true) |
| 68 | + const added = entry('0043') |
| 69 | + expect(joinSessionNoProg(active, added)).toBe(added) |
| 70 | + expect(added.noProg).toBeUndefined() |
| 71 | + }) |
| 72 | +}) |
| 73 | + |
| 74 | +// The saved workout is built from the entries, so it reads exactly like one whose exercises were |
| 75 | +// each kept out by hand: the whole-workout mirror, every entry excluded, no session-only field. |
| 76 | +describe('a session kept out as a whole, saved', () => { |
| 77 | + it('writes excludeFromProgression and noProg on every entry, and the history reads past it', () => { |
| 78 | + const active = session([entry('0025', { rid: 'main' }), entry('0027', { rid: 'main' })]) |
| 79 | + setSessionNoProg(active, true) |
| 80 | + active.entries.push(joinSessionNoProg(active, entry('0043', { rid: 'main' }))) |
| 81 | + const saved = buildCompletedWorkout(active, { end: 1 }) |
| 82 | + expect(saved.excludeFromProgression).toBe(true) |
| 83 | + expect(saved.entries.every(e => e.noProg === true && entryExcluded(saved, e))).toBe(true) |
| 84 | + expect(saved).not.toHaveProperty('noProg') |
| 85 | + |
| 86 | + const earlier = { id: 'w0', d: '2026-09-20', routineIds: ['main'], entries: [entry('0025', { rid: 'main', sets: [{ w: 100, r: 5, done: true }] })] } |
| 87 | + const S = { workouts: [earlier, saved], routines: [], exWeights: {} } |
| 88 | + expect(lastEntryFor(S, '0025', 'main').d).toBe('2026-09-20') |
| 89 | + }) |
| 90 | + |
| 91 | + it('switched off again before the finish, it saves as an ordinary counting workout', () => { |
| 92 | + const active = session([entry('0025'), entry('0027')]) |
| 93 | + setSessionNoProg(active, true) |
| 94 | + setSessionNoProg(active, false) |
| 95 | + const saved = buildCompletedWorkout(active, { end: 1 }) |
| 96 | + expect(saved).not.toHaveProperty('excludeFromProgression') |
| 97 | + expect(saved.entries.some(e => 'noProg' in e)).toBe(false) |
| 98 | + }) |
| 99 | +}) |
0 commit comments