import { describe, expect, it } from 'vitest' import { exerciseDefinitions, initialRoutine } from './data/index.js' import { blankRow, equipmentLabels, externalKg, loadColumns, measurementColumns, newPerformance, performanceError, performanceTotals, preparePerformanceForSave } from './performanceModel.js' const def = (id) => exerciseDefinitions.find((d) => d.id === id) const prescription = (id) => Object.values(initialRoutine.days).flatMap((day) => [...day.morning.exercises, ...day.afternoon.exercises]).find((e) => e.exerciseId === id).prescription const make = (id) => newPerformance('2026-11-03', 'morning', def(id), prescription(id)) const expectedEquipment = { 'pull-up': ['weighted-vest'], 'ez-bar-bent-over-row': ['ez-curl-bar'], 'single-arm-dumbbell-row': ['dumbbells'], 'suitcase-carry': ['dumbbells'], 'weighted-deep-push-up': ['weighted-vest'], 'dumbbell-floor-fly': ['dumbbells'], 'push-up-plus': ['weighted-vest'], 'feet-elevated-weighted-push-up': ['weighted-vest'], 'bulgarian-split-squat': ['weighted-vest', 'dumbbells'], 'single-leg-romanian-deadlift': ['weighted-vest', 'dumbbells'], squat: ['weighted-vest', 'dumbbells'], 'single-leg-calf-raise': ['weighted-vest', 'dumbbells'], 'front-plank': ['weighted-vest'], 'chin-up': ['weighted-vest'], 'hanging-knee-raise': ['bodyweight'], 'finger-hang': ['weighted-vest'], 'single-arm-preacher-curl': ['dumbbells'], 'hammer-curl': ['dumbbells'], 'single-arm-shoulder-press': ['dumbbells'], 'leaning-single-arm-lateral-raise': ['dumbbells'], 'bent-over-rear-delt-fly': ['dumbbells'], 'lying-dumbbell-triceps-extension': ['dumbbells'], 'single-arm-triceps-extension': ['dumbbells'], 'side-plank-hip-raise': ['weighted-vest'], 'deep-squat-hold': ['bodyweight'], 'ninety-ninety-hip-hold': ['bodyweight'], 'bird-dog-hold': ['bodyweight'], 'single-leg-balance': ['bodyweight'], 'prone-swimmer-iso': ['bodyweight'], 'daily-hang': ['bodyweight'], } describe('metadata and actual performance', () => { it.each(exerciseDefinitions)('audits equipment for $id without a numeric load in definitions', (d) => { expect(d.equipment).toEqual(expectedEquipment[d.id]) expect(d.equipment.every((e) => e in equipmentLabels)).toBe(true) expect(measurementColumns(d).length).toBeGreaterThan(0) expect(d).not.toHaveProperty('load') expect(performanceError(make(d.id))).toBeNull() if (d.equipment[0] === 'bodyweight') expect(loadColumns(d)).toEqual([]) }) it('supports straight-bar totals and distance without adding seed exercises', () => { const d = { ...def('ez-bar-bent-over-row'), equipment: ['straight-bar'], measurement: 'distance' } expect(loadColumns(d).map((c) => c.key)).toEqual(['straightBarTotalKg']) expect(measurementColumns(d).map((c) => c.key)).toEqual(['metres']) expect(externalKg({ load: { straightBarTotalKg: 25 } })).toBe(25) }) it('requires measurements/load for completion, permits drafts, and rejects invalid fields', () => { const entry = make('front-plank') entry.rows[0].completed = true expect(performanceError(entry)).toMatch(/seconds/) entry.rows[0].values.seconds = 45 expect(performanceError(entry)).toMatch(/vest/) entry.rows[0].load.vestKg = 0 expect(performanceError(entry)).toBeNull() entry.rows[0].values.reps = 2 expect(performanceError(entry)).toMatch(/invalid fields/) }) it('sums variable per-minute resistance and keeps target and actuals independent', () => { const entry = make('pull-up') expect(entry.rows).toHaveLength(10) expect(entry.rows[0].load.vestKg).toBeNull() entry.rows[0] = { values: { reps: 4 }, load: { vestKg: 6 }, completed: true } entry.rows[1] = { values: { reps: 3 }, load: { vestKg: 10 }, completed: true } expect(performanceTotals(entry)).toMatchObject({ reps: 7, completed: 2, volume: 54 }) }) it('records independent side values and dumbbell count without counting handles', () => { const entry = make('bulgarian-split-squat') entry.rows[0] = { values: { leftReps: 10, rightReps: 8 }, load: { vestKg: 5, dumbbellKgEach: 12, dumbbellCount: 2 }, completed: true } expect(externalKg(entry.rows[0])).toBe(29) expect(performanceTotals(entry).volume).toBe(522) expect(loadColumns(def('weighted-deep-push-up')).map((c) => c.key)).toEqual(['vestKg']) const next = blankRow(entry.definition, entry.rows[0]) expect(next.values).toEqual({ leftReps: null, rightReps: null }) expect(next.load).toEqual(entry.rows[0].load) expect(next.completed).toBe(false) }) it('separates cumulative hanging time from continuous success and incomplete drafts', () => { const entry = make('daily-hang') entry.rows = [60, 75, 150].map((seconds, i) => ({ values: { seconds }, load: {}, completed: i < 2 })) expect(performanceTotals(entry)).toMatchObject({ longest: 75, seconds: 135, completed: 2 }) }) }) it('rejects impossible dates and excludes invalid completed drafts from achieved totals', () => { const record = make('daily-hang') record.rows[0].completed = true expect(performanceTotals(record)).toMatchObject({ completed: 0, longest: 0, seconds: 0 }) record.date = '2026-02-30' expect(performanceError(record)).toMatch(/Invalid performance/) }) it('rejects unsupported exercise formats and malformed protocol metadata', () => { const record = make('daily-hang') record.format = 'sets' expect(performanceError(record)).toMatch(/Unsupported exercise format/) record.definition.protocols = 'attempts' expect(performanceError(record)).toMatch(/Invalid performance/) }) it('fixes floor-fly count at two and calculates paired resistance from kg each', () => { const record = make('dumbbell-floor-fly') record.rows[0] = { values: { reps: 12 }, load: { dumbbellKgEach: 8, dumbbellCount: 2 }, completed: true } expect(performanceError(record)).toBeNull() expect(performanceTotals(record).volume).toBe(192) record.rows[0].load.dumbbellCount = 1 expect(performanceError(record)).toMatch(/dumbbells used/) }) it('derives completion on explicit save while retaining drafts and rejecting invalid actuals', () => { const original = make('front-plank') original.rows[0] = { values: { seconds: 0 }, load: { vestKg: 0 }, completed: false } let prepared = preparePerformanceForSave(original) expect(prepared.rows[0].completed).toBe(true) expect(original.rows[0].completed).toBe(false) original.rows[0].values.seconds = null prepared = preparePerformanceForSave(original) expect(prepared.rows[0].completed).toBe(false) expect(performanceError(prepared)).toBeNull() original.rows[0].load.vestKg = -1 expect(performanceError(preparePerformanceForSave(original))).toMatch(/vest/) })