import { describe, it, expect } from 'vitest'; import { aggregateUserStats, type RawStatsSession, type StatsExerciseRaw, } from '../../src/services/userStats.js'; // Deterministyczna „dziś": czwartek 2026-10-29 (ISO tydzień od pon 2026-10-26). const NOW = new Date('2026-10-29T12:00:00'); // Oczekiwania dni/godzin liczone tymi samymi metodami lokalnymi co serwis — // bez hardcode'ów zależnych od strefy czasowej maszyny testowej. const dowOf = (iso: string) => { const d = new Date(iso); return (d.getDay() + 6) % 7; }; const hourOf = (iso: string) => new Date(iso).getHours(); const isoWeekStart = (at: Date) => { const w = new Date(at.getFullYear(), at.getMonth(), at.getDate() - ((at.getDay() + 6) % 7)); return w.toISOString(); }; const eq = (id: string, name: string, nameEn: string | null = null) => ({ id, name, nameEn }); const ex = ( exerciseId: string, name: string, muscle: string, sets: StatsExerciseRaw['sets'], equipment: StatsExerciseRaw['equipment'] = [], nameEn: string | null = null, ): StatsExerciseRaw => ({ exerciseId, name, nameEn, muscle, equipment, sets }); const session = ( startedAt: string, exercises: StatsExerciseRaw[], opts: { endedAt?: string | null; gym?: { id: string; name: string } | null; bodyKg?: number | null; } = {}, ): RawStatsSession => ({ startedAt, endedAt: opts.endedAt ?? null, gym: opts.gym ?? null, bodyKg: opts.bodyKg ?? null, exercises, }); describe('aggregateUserStats', () => { it('returns zeroed result for empty history', () => { const s = aggregateUserStats([], { rangeDays: 90, now: NOW }); expect(s.sessions).toEqual({ count: 0, sets: 0, reps: 0, avgMinutes: null }); expect(s.tonnageKg).toBe(0); expect(s.tonnageAllTimeKg).toBe(0); expect(s.topGym).toBeNull(); expect(s.topEquipment).toBeNull(); expect(s.topExercise).toBeNull(); expect(s.muscles).toEqual([]); expect(s.weekday).toHaveLength(7); expect(s.heatmap).toEqual([]); expect(s.weeklyTonnage).toHaveLength(12); expect(s.records).toEqual([]); }); it('picks favorite gym / equipment / exercise by usage count', () => { const bench = eq('eq-bench', 'Ławka', 'Bench'); const s = aggregateUserStats( [ session( '2026-10-26T12:00:00Z', [ex('x1', 'Wyciskanie', 'chest', [{ reps: 10, loadKg: 60 }], [bench])], { gym: { id: 'g1', name: 'Siłownia A' } }, ), session( '2026-10-27T12:00:00Z', [ex('x1', 'Wyciskanie', 'chest', [{ reps: 8, loadKg: 70 }], [bench])], { gym: { id: 'g1', name: 'Siłownia A' } }, ), session( '2026-10-28T12:00:00Z', [ex('x2', 'Przysiad', 'legs', [{ reps: 5, loadKg: 100 }], [eq('eq-rack', 'Rack')])], { gym: { id: 'g2', name: 'Siłownia B' } }, ), ], { rangeDays: 30, now: NOW }, ); expect(s.topGym).toMatchObject({ gymId: 'g1', name: 'Siłownia A', sessions: 2 }); expect(s.topEquipment).toMatchObject({ equipmentId: 'eq-bench', name: 'Ławka', nameEn: 'Bench', sets: 2, }); expect(s.topExercise).toMatchObject({ exerciseId: 'x1', sessions: 2, sets: 2 }); expect(s.sessions).toMatchObject({ count: 3, sets: 3, reps: 23 }); // 60×10 + 70×8 + 100×5 = 1660 expect(s.tonnageKg).toBe(1660); expect(s.muscles).toEqual([ { muscle: 'chest', sets: 2, volumeKg: 1160 }, { muscle: 'legs', sets: 1, volumeKg: 500 }, ]); }); it('breaks ties alphabetically (pl collation)', () => { const s = aggregateUserStats( [ session( '2026-10-26T12:00:00Z', [ex('x1', 'Zakwasy', 'core', [{ reps: 5, loadKg: 10 }], [eq('e1', 'Żuraw')])], { gym: { id: 'g1', name: 'Atom' } }, ), session( '2026-10-27T12:00:00Z', [ex('x2', 'Mostki', 'chest', [{ reps: 5, loadKg: 10 }], [eq('e2', 'Atlas')])], { gym: { id: 'g2', name: 'Bron' } }, ), ], { rangeDays: 30, now: NOW }, ); expect(s.topGym?.name).toBe('Atom'); expect(s.topEquipment?.name).toBe('Atlas'); }); it('range excludes old sessions from cards but keeps all-time tonnage and records', () => { const s = aggregateUserStats( [ session( '2026-01-10T12:00:00Z', [ex('x1', 'Przysiad', 'legs', [{ reps: 10, loadKg: 80 }])], { gym: { id: 'g1', name: 'Stara' } }, ), session( '2026-10-26T12:00:00Z', [ex('x2', 'Uginanie', 'biceps', [{ reps: 12, loadKg: 20 }])], { gym: { id: 'g2', name: 'Nowa' } }, ), ], { rangeDays: 30, now: NOW }, ); expect(s.sessions.count).toBe(1); expect(s.tonnageKg).toBe(240); expect(s.tonnageAllTimeKg).toBe(240 + 800); expect(s.topGym?.name).toBe('Nowa'); // rekord liczony z całej historii: 80 × (1 + 10/30) ≈ 106.7 > 20 × 1.4 = 28 expect(s.records[0]).toMatchObject({ exerciseId: 'x1', oneRm: 106.7, date: '2026-01-10T12:00:00Z', }); expect(s.records).toHaveLength(2); }); it('uses bodyKg for sets without planned load; unmeasurable sets stay out of tonnage', () => { const s = aggregateUserStats( [ session( '2026-10-26T12:00:00Z', [ ex('x1', 'Pompki', 'chest', [{ reps: 10, loadKg: null }]), ex('x2', 'Plank', 'core', [{ reps: null, loadKg: null }]), ], { bodyKg: 80 }, ), ], { rangeDays: 30, now: NOW }, ); expect(s.tonnageKg).toBe(800); expect(s.sessions.sets).toBe(1); // seria bez powt. nie liczona (plank poza statystykami) expect(s.muscles).toEqual([{ muscle: 'chest', sets: 1, volumeKg: 800 }]); }); it('weekday, heatmap and avgMinutes follow session start/end', () => { const monNoon = '2026-10-26T12:00:00Z'; const thuNoon = '2026-10-29T12:00:00Z'; const s = aggregateUserStats( [session(monNoon, [], { endedAt: '2026-10-26T13:00:00Z' }), session(thuNoon, [])], { rangeDays: 30, now: NOW }, ); expect(s.weekday[dowOf(monNoon)].sessions).toBe(1); expect(s.weekday[dowOf(thuNoon)].sessions).toBe(1); expect(s.weekday.reduce((n, d) => n + d.sessions, 0)).toBe(2); expect(s.heatmap).toEqual([ { day: dowOf(thuNoon), hour: hourOf(thuNoon), count: 1 }, { day: dowOf(monNoon), hour: hourOf(monNoon), count: 1 }, ].sort((a, b) => a.day - b.day || a.hour - b.hour)); // avgMinutes tylko z sesji z endedAt expect(s.sessions.avgMinutes).toBe(60); }); it('weeklyTonnage buckets into last 12 ISO weeks with current week last', () => { const s = aggregateUserStats( [ session('2026-10-26T12:00:00Z', [ex('x1', 'Przysiad', 'legs', [{ reps: 10, loadKg: 50 }])]), session('2026-01-05T12:00:00Z', [ex('x1', 'Przysiad', 'legs', [{ reps: 10, loadKg: 999 }])]), ], { rangeDays: null, now: NOW }, ); expect(s.weeklyTonnage[11]).toEqual({ weekStart: isoWeekStart(NOW), volumeKg: 500 }); expect(s.weeklyTonnage.slice(0, 11).every((w) => w.volumeKg === 0)).toBe(true); }); it('records keep max Epley per exercise with the record date, top 5 sorted', () => { const sessions = [ session('2026-10-20T12:00:00Z', [ex('x1', 'Przysiad', 'legs', [{ reps: 10, loadKg: 80 }])]), session('2026-10-21T12:00:00Z', [ex('x1', 'Przysiad', 'legs', [{ reps: 5, loadKg: 100 }])]), ]; for (let i = 2; i <= 6; i++) { sessions.push( session(`2026-10-2${i}T12:00:00Z`, [ ex(`x${i}`, `Ćwiczenie ${i}`, 'core', [{ reps: 10, loadKg: i }]), ]), ); } const s = aggregateUserStats(sessions, { rangeDays: null, now: NOW }); expect(s.records).toHaveLength(5); expect(s.records[0]).toMatchObject({ exerciseId: 'x1', oneRm: 116.7, // max(106.7, 100×(1+5/30)=116.67) date: '2026-10-21T12:00:00Z', }); }); });