243 lines
7.4 KiB
TypeScript
243 lines
7.4 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { buildApp, createUser, createExercise, loginViaApi, seedPlan, seedSession, PASSWORD } from '../helpers.js';
|
|
|
|
function localMidnight(offsetDays: number) {
|
|
const now = new Date();
|
|
return new Date(now.getFullYear(), now.getMonth(), now.getDate() + offsetDays);
|
|
}
|
|
|
|
describe('dashboard routes', () => {
|
|
it('returns zeros for a user without an active plan', async () => {
|
|
const app = buildApp();
|
|
const { user } = await createUser();
|
|
const token = (await loginViaApi(user.email, PASSWORD)).token;
|
|
const res = await app.get('/api/v1/dashboard').set('Authorization', `Bearer ${token}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toEqual({
|
|
killstreak: 0,
|
|
nextTraining: null,
|
|
progress: null,
|
|
extra: null,
|
|
missing: null,
|
|
activePlan: null,
|
|
});
|
|
});
|
|
|
|
it('returns killstreak, next training and progress for an active plan', async () => {
|
|
const app = buildApp();
|
|
const { user } = await createUser();
|
|
const token = (await loginViaApi(user.email, PASSWORD)).token;
|
|
const exercise = await createExercise();
|
|
|
|
const plan = await seedPlan({
|
|
userId: user.id,
|
|
title: 'Dashboard plan',
|
|
status: 'ACTIVE',
|
|
startDate: localMidnight(-7),
|
|
weeks: [
|
|
{
|
|
index: 0,
|
|
days: [
|
|
{
|
|
dayOfWeek: 1,
|
|
date: localMidnight(-1),
|
|
status: 'COMPLETED',
|
|
exercises: [
|
|
{
|
|
exerciseId: exercise.id,
|
|
order: 0,
|
|
sets: [
|
|
{ index: 0, reps: 10 },
|
|
{ index: 1, reps: 8 },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
dayOfWeek: 3,
|
|
date: localMidnight(1),
|
|
exercises: [{ exerciseId: exercise.id, order: 0, sets: [{ index: 0, reps: 10 }] }],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|
|
const doneDay = plan.weeks[0].days[0];
|
|
const planExercise = doneDay.exercises[0];
|
|
const planSets = planExercise.sets;
|
|
await seedSession(doneDay.id, {
|
|
status: 'COMPLETED',
|
|
exercises: [
|
|
{
|
|
exerciseId: exercise.id,
|
|
planExerciseId: planExercise.id,
|
|
order: 0,
|
|
// both planned sets done + one additional set beyond the plan (planSetId = null)
|
|
sets: [
|
|
...planSets.map((s) => ({ planSetId: s.id, reps: 10 })),
|
|
{ planSetId: null, reps: 12 },
|
|
],
|
|
},
|
|
// extra exercise added beyond the plan (no planExerciseId)
|
|
{
|
|
exerciseId: exercise.id,
|
|
planExerciseId: null,
|
|
order: 1,
|
|
sets: [{ reps: 12 }, { reps: 10 }],
|
|
},
|
|
],
|
|
});
|
|
|
|
const res = await app.get('/api/v1/dashboard').set('Authorization', `Bearer ${token}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.killstreak).toBe(1);
|
|
expect(res.body.activePlan).toMatchObject({ id: plan.id, title: 'Dashboard plan' });
|
|
expect(res.body.progress).toEqual({ completed: 1, total: 2, percent: 50 });
|
|
expect(res.body.nextTraining).toMatchObject({ dayId: plan.weeks[0].days[1].id });
|
|
expect(res.body.nextTraining.exercises).toHaveLength(1);
|
|
// 1 additional set of the planned exercise + 2 sets of the added exercise
|
|
expect(res.body.extra).toEqual({ sets: 3 });
|
|
expect(res.body.missing).toEqual({ sets: 0 });
|
|
});
|
|
|
|
it('itemizes missing planned sets and ignores removed/hidden/non-completed extras', async () => {
|
|
const app = buildApp();
|
|
const { user } = await createUser();
|
|
const token = (await loginViaApi(user.email, PASSWORD)).token;
|
|
const exercise = await createExercise();
|
|
|
|
const plan = await seedPlan({
|
|
userId: user.id,
|
|
title: 'Missing plan',
|
|
status: 'ACTIVE',
|
|
startDate: localMidnight(-7),
|
|
weeks: [
|
|
{
|
|
index: 0,
|
|
days: [
|
|
{
|
|
dayOfWeek: 1,
|
|
date: localMidnight(-1),
|
|
status: 'COMPLETED',
|
|
exercises: [
|
|
{
|
|
exerciseId: exercise.id,
|
|
order: 0,
|
|
sets: [
|
|
{ index: 0, reps: 10 },
|
|
{ index: 1, reps: 8 },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
dayOfWeek: 3,
|
|
date: localMidnight(1),
|
|
exercises: [{ exerciseId: exercise.id, order: 0, sets: [{ index: 0, reps: 10 }] }],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|
|
const doneDay = plan.weeks[0].days[0];
|
|
const plannedDay = plan.weeks[0].days[1];
|
|
const planExercise = doneDay.exercises[0];
|
|
// Only the first planned set is done -> 1 missing set.
|
|
// Removed + hidden extras are not counted.
|
|
await seedSession(doneDay.id, {
|
|
status: 'COMPLETED',
|
|
exercises: [
|
|
{
|
|
exerciseId: exercise.id,
|
|
planExerciseId: planExercise.id,
|
|
order: 0,
|
|
sets: [{ planSetId: planExercise.sets[0].id, reps: 10 }],
|
|
},
|
|
{
|
|
exerciseId: exercise.id,
|
|
planExerciseId: null,
|
|
order: 1,
|
|
removed: true,
|
|
sets: [{ reps: 10 }],
|
|
},
|
|
{
|
|
exerciseId: exercise.id,
|
|
planExerciseId: null,
|
|
order: 2,
|
|
hidden: true,
|
|
sets: [{ reps: 10 }],
|
|
},
|
|
],
|
|
});
|
|
// Extra in an IN_PROGRESS session is not counted yet.
|
|
await seedSession(plannedDay.id, {
|
|
status: 'IN_PROGRESS',
|
|
exercises: [
|
|
{ exerciseId: exercise.id, planExerciseId: null, order: 0, sets: [{ reps: 10 }] },
|
|
],
|
|
});
|
|
|
|
const res = await app.get('/api/v1/dashboard').set('Authorization', `Bearer ${token}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.extra).toEqual({ sets: 0 });
|
|
expect(res.body.missing).toEqual({ sets: 1 });
|
|
});
|
|
|
|
it('resets killstreak after a partially completed day', async () => {
|
|
const app = buildApp();
|
|
const { user } = await createUser();
|
|
const token = (await loginViaApi(user.email, PASSWORD)).token;
|
|
const exercise = await createExercise();
|
|
|
|
const plan = await seedPlan({
|
|
userId: user.id,
|
|
status: 'ACTIVE',
|
|
weeks: [
|
|
{
|
|
index: 0,
|
|
days: [
|
|
{
|
|
dayOfWeek: 1,
|
|
date: localMidnight(-2),
|
|
status: 'COMPLETED',
|
|
exercises: [
|
|
{
|
|
exerciseId: exercise.id,
|
|
order: 0,
|
|
sets: [
|
|
{ index: 0, reps: 10 },
|
|
{ index: 1, reps: 8 },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|
|
const doneDay = plan.weeks[0].days[0];
|
|
const planExercise = doneDay.exercises[0];
|
|
// Only the first plan set is done -> partially completed day
|
|
await seedSession(doneDay.id, {
|
|
status: 'COMPLETED',
|
|
exercises: [
|
|
{
|
|
exerciseId: exercise.id,
|
|
planExerciseId: planExercise.id,
|
|
order: 0,
|
|
sets: [{ planSetId: planExercise.sets[0].id, reps: 10 }],
|
|
},
|
|
],
|
|
});
|
|
|
|
const res = await app.get('/api/v1/dashboard').set('Authorization', `Bearer ${token}`);
|
|
expect(res.body.killstreak).toBe(0);
|
|
});
|
|
|
|
it('rejects /dashboard without a token with 401', async () => {
|
|
const app = buildApp();
|
|
const res = await app.get('/api/v1/dashboard');
|
|
expect(res.status).toBe(401);
|
|
});
|
|
});
|