import { describe, it, expect } from 'vitest'; import { prisma } from '../../src/lib/prisma.js'; import { buildApp, createEquipment, createExercise, createUserWithToken, rand, seedPlan, seedSession, } from '../helpers.js'; describe('GET /me/stats — statystyki osobiste', () => { it('rejects /me/stats without a token with 401', async () => { const app = buildApp(); const res = await app.get('/api/v1/me/stats'); expect(res.status).toBe(401); expect(res.body.error.code).toBe('UNAUTHORIZED'); }); it('rejects an invalid range with 400', async () => { const app = buildApp(); const { token } = await createUserWithToken(); const res = await app.get('/api/v1/me/stats?range=7').set('Authorization', `Bearer ${token}`); expect(res.status).toBe(400); }); it('returns zeroed stats for a user without sessions', async () => { const app = buildApp(); const { token } = await createUserWithToken(); const res = await app.get('/api/v1/me/stats').set('Authorization', `Bearer ${token}`); expect(res.status).toBe(200); expect(res.body.rangeDays).toBe(90); expect(res.body.sessions).toMatchObject({ count: 0, sets: 0, avgMinutes: null }); expect(res.body.topGym).toBeNull(); expect(res.body.topEquipment).toBeNull(); expect(res.body.records).toEqual([]); expect(res.body.weeklyTonnage).toHaveLength(12); expect(res.body.plans).toEqual({ completed: 0, abandoned: 0, daysCompleted: 0, daysSkipped: 0 }); }); it('aggregates favorites, muscle split, tonnage and records from seed', async () => { const app = buildApp(); const { user, token } = await createUserWithToken(); const suffix = rand(); const gym = await prisma.gym.create({ data: { name: `Stats Gym ${suffix}` } }); const squat = await createExercise({ slug: `sq-${suffix}`, name: `Przysiad ${suffix}` }); await prisma.exercise.update({ where: { id: squat.id }, data: { primaryMuscle: 'legs' } }); const bench = await createExercise({ slug: `bn-${suffix}`, name: `Wyciskanie ${suffix}` }); const rackEq = await createEquipment(`Rack ${suffix}`); await prisma.exercise.update({ where: { id: squat.id }, data: { equipment: { connect: { id: rackEq.id } } } }); const plan = await seedPlan({ userId: user.id, status: 'ACTIVE', weeks: [ { index: 0, days: [ { dayOfWeek: 1, date: new Date('2026-08-03T00:00:00.000Z'), status: 'COMPLETED', exercises: [ { exerciseId: squat.id, order: 0, sets: [{ index: 0, reps: 5, loadKg: 100 }] }, ], }, { dayOfWeek: 3, date: new Date('2026-08-05T00:00:00.000Z'), status: 'COMPLETED', exercises: [ { exerciseId: bench.id, order: 0, sets: [{ index: 0, reps: 10, loadKg: 60 }] }, ], }, { dayOfWeek: 5, date: new Date('2026-08-07T00:00:00.000Z'), status: 'SKIPPED' }, ], }, ], }); await prisma.trainingPlan.update({ where: { id: plan.id }, data: { gymId: gym.id } }); const days = plan.weeks[0].days; await seedSession(days[0].id, { status: 'COMPLETED', startedAt: new Date('2026-08-03T17:00:00.000Z'), exercises: [ { exerciseId: squat.id, planExerciseId: days[0].exercises[0].id, order: 0, sets: [{ planSetId: days[0].exercises[0].sets[0].id, reps: 5 }], }, ], }); await seedSession(days[1].id, { status: 'COMPLETED', startedAt: new Date('2026-08-05T17:00:00.000Z'), exercises: [ { exerciseId: bench.id, planExerciseId: days[1].exercises[0].id, order: 0, sets: [{ planSetId: days[1].exercises[0].sets[0].id, reps: 10 }], }, ], }); // zakres „all” (sesje z 2026-08 są poza 90/30/365 względem dziś) — pełna historia const res = await app .get('/api/v1/me/stats?range=all') .set('Authorization', `Bearer ${token}`); expect(res.status).toBe(200); const b = res.body; expect(b.rangeDays).toBeNull(); expect(b.sessions).toMatchObject({ count: 2, sets: 2, reps: 15 }); expect(b.tonnageKg).toBe(100 * 5 + 60 * 10); expect(b.tonnageAllTimeKg).toBe(1100); expect(b.topGym).toMatchObject({ gymId: gym.id, name: gym.name, sessions: 2 }); expect(b.topExercise).toMatchObject({ name: squat.name, sessions: 1, sets: 1 }); expect(b.topEquipment).toMatchObject({ equipmentId: rackEq.id, name: rackEq.name, sets: 1 }); expect(b.muscles).toHaveLength(2); expect(b.weekday.reduce((n: number, d: { sessions: number }) => n + d.sessions, 0)).toBe(2); expect(b.weeklyTonnage.reduce((n: number, w: { volumeKg: number }) => n + w.volumeKg, 0)).toBe(0); // sesje sprzed >12 tyg. // rekord: Przysiad 100×(1+5/30)=116.7 > Wyciskanie 60×(1+10/30)=80 expect(b.records[0]).toMatchObject({ exerciseId: squat.id, oneRm: 116.7 }); expect(b.plans).toMatchObject({ completed: 0, abandoned: 0, daysCompleted: 2, daysSkipped: 1 }); }); it('range filter excludes older sessions from cards', async () => { const app = buildApp(); const { user, token } = await createUserWithToken(); // masa ciała — seria bez planowanego obciążenia = trening z masą ciała await prisma.bodyMeasurement.create({ data: { userId: user.id, value: 80, date: new Date('2026-08-01T00:00:00.000Z') }, }); const ex = await createExercise(); const plan = await seedPlan({ userId: user.id, weeks: [{ index: 0, days: [{ dayOfWeek: 1, date: new Date('2026-08-03T00:00:00.000Z') }] }], }); const day = plan.weeks[0].days[0]; await seedSession(day.id, { status: 'COMPLETED', startedAt: new Date('2026-08-03T17:00:00.000Z'), exercises: [{ exerciseId: ex.id, order: 0, sets: [{ reps: 8, index: 0 }] }], }); const all = await app.get('/api/v1/me/stats?range=all').set('Authorization', `Bearer ${token}`); expect(all.body.sessions.count).toBe(1); const d30 = await app .get('/api/v1/me/stats?range=30') .set('Authorization', `Bearer ${token}`); expect(d30.body.rangeDays).toBe(30); expect(d30.body.sessions.count).toBe(0); // tonaż all-time i rekordy zostają niezależnie od zakresu expect(d30.body.tonnageAllTimeKg).toBe(all.body.tonnageAllTimeKg); expect(d30.body.records).toHaveLength(1); }); });