Files
FitOS/api/test/integration/exercise-progress.test.ts
2026-09-04 19:06:19 +02:00

165 lines
5.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect, beforeAll } from 'vitest';
import { prisma } from '../../src/lib/prisma.js';
import { buildApp, createExercise, createUserWithToken, rand, seedPlan, seedSession } from '../helpers.js';
async function createMembership(userId: string, gymId: string, roles: string[]) {
return prisma.gymMembership.create({
data: { userId, gymId, roles: roles as never },
});
}
describe('exercise progress — per-exercise 1RM + volume', () => {
let app: ReturnType<typeof buildApp>;
let trainer: { id: string; token: string };
let trainee: { id: string; token: string };
let stranger: { id: string; token: string };
let gymId: string;
let squatId: string;
beforeAll(async () => {
app = buildApp();
const t = await createUserWithToken({ firstName: 'Tomasz' });
const tr = await createUserWithToken({ firstName: 'Anna' });
const s = await createUserWithToken({ firstName: 'Piotr' });
const gym = await prisma.gym.create({ data: { name: `Prog Gym ${rand()}` } });
gymId = gym.id;
await createMembership(t.user.id, gymId, ['TRAINER']);
await createMembership(tr.user.id, gymId, ['MEMBER']);
await createMembership(s.user.id, gymId, ['MEMBER']);
trainer = { id: t.user.id, token: t.token };
trainee = { id: tr.user.id, token: tr.token };
stranger = { id: s.user.id, token: s.token };
// ACTIVE relation.
const invite = await app
.post('/api/v1/trainer/invitations')
.set('Authorization', `Bearer ${trainer.token}`)
.send({ userId: trainee.id });
expect(invite.status).toBe(201);
const accept = await app
.post(`/api/v1/trainer/invitations/${invite.body.coaching.id}/accept`)
.set('Authorization', `Bearer ${trainee.token}`);
expect(accept.status).toBe(200);
// Trainee: one exercise across two COMPLETED sessions + one IN_PROGRESS (ignored).
const squat = await createExercise({ name: 'Przysiady e2e' });
squatId = squat.id;
const plan = await seedPlan({
userId: trainee.id,
weeks: [
{
index: 0,
days: [
{
dayOfWeek: 1,
exercises: [
{
exerciseId: squat.id,
order: 0,
sets: [
{ index: 0, reps: 10, loadKg: 60 },
{ index: 1, reps: 8, loadKg: 70 },
],
},
],
},
],
},
],
});
const day = plan.weeks[0].days[0];
const s1 = await seedSession(day.id, {
status: 'COMPLETED',
startedAt: new Date('2026-08-01T07:00:00.000Z'),
exercises: [
{
exerciseId: squat.id,
planExerciseId: day.exercises[0].id,
order: 0,
sets: [
{ planSetId: day.exercises[0].sets[0].id, reps: 10, rir: 2 },
{ planSetId: day.exercises[0].sets[1].id, reps: 8, rir: 1 },
],
},
],
});
const s2 = await seedSession(day.id, {
status: 'COMPLETED',
startedAt: new Date('2026-08-08T07:00:00.000Z'),
exercises: [
{
exerciseId: squat.id,
planExerciseId: day.exercises[0].id,
order: 0,
sets: [{ planSetId: day.exercises[0].sets[0].id, reps: 10, rir: 2 }],
},
],
});
const s3 = await seedSession(day.id, {
status: 'IN_PROGRESS',
startedAt: new Date('2026-08-15T07:00:00.000Z'),
exercises: [
{ exerciseId: squat.id, planExerciseId: day.exercises[0].id, order: 0, sets: [{ planSetId: day.exercises[0].sets[0].id, reps: 12 }] },
],
});
expect([s1.id, s2.id, s3.id].length).toBe(3);
});
describe('GET /me/exercise-progress', () => {
it('returns per-exercise points with 1RM and volume (COMPLETED only)', async () => {
const res = await app.get('/api/v1/me/exercise-progress').set('Authorization', `Bearer ${trainee.token}`);
expect(res.status).toBe(200);
expect(res.body.exercises).toHaveLength(1);
const e = res.body.exercises[0];
expect(e).toMatchObject({ exerciseId: squatId, name: 'Przysiady e2e', muscle: 'chest' });
expect(e.points).toHaveLength(2);
expect(e.points[0]).toMatchObject({
date: '2026-08-01T07:00:00.000Z',
oneRm: 88.7, // max(60×(1+10/30)=80, 70×(1+8/30)≈88.7)
volumeKg: 60 * 10 + 70 * 8, // 1160
reps: 18,
sets: 2,
});
expect(e.points[1]).toMatchObject({
date: '2026-08-08T07:00:00.000Z',
oneRm: 80,
volumeKg: 600,
reps: 10,
sets: 1,
});
});
it('returns an empty list for a user without sessions', async () => {
const res = await app.get('/api/v1/me/exercise-progress').set('Authorization', `Bearer ${stranger.token}`);
expect(res.status).toBe(200);
expect(res.body.exercises).toEqual([]);
});
});
describe('GET /trainer/trainees/:userId/exercise-progress', () => {
it('returns the trainee progress for the trainer', async () => {
const res = await app
.get(`/api/v1/trainer/trainees/${trainee.id}/exercise-progress`)
.set('Authorization', `Bearer ${trainer.token}`);
expect(res.status).toBe(200);
expect(res.body.exercises).toHaveLength(1);
expect(res.body.exercises[0].exerciseId).toBe(squatId);
expect(res.body.exercises[0].points).toHaveLength(2);
});
it('rejects a non-trainee with 404', async () => {
const res = await app
.get(`/api/v1/trainer/trainees/${stranger.id}/exercise-progress`)
.set('Authorization', `Bearer ${trainer.token}`);
expect(res.status).toBe(404);
});
it('rejects a user without the TRAINER role with 403', async () => {
const res = await app
.get(`/api/v1/trainer/trainees/${trainee.id}/exercise-progress`)
.set('Authorization', `Bearer ${trainee.token}`);
expect(res.status).toBe(403);
});
});
});