395 lines
14 KiB
TypeScript
395 lines
14 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest';
|
|
import { abandonSession, addExercise, addSet, completeSession, deleteSet, getActiveSession, startSession } from '../../src/services/sessions.js';
|
|
|
|
function mockPrisma() {
|
|
return {
|
|
planDay: { findUnique: vi.fn(), update: vi.fn() },
|
|
trainingSession: { findUnique: vi.fn(), findFirst: vi.fn(), create: vi.fn(), update: vi.fn() },
|
|
sessionExercise: {
|
|
findUnique: vi.fn(),
|
|
findFirst: vi.fn(),
|
|
findMany: vi.fn(),
|
|
create: vi.fn(),
|
|
createMany: vi.fn(),
|
|
update: vi.fn(),
|
|
aggregate: vi.fn(),
|
|
},
|
|
sessionSet: {
|
|
findUnique: vi.fn(),
|
|
findMany: vi.fn(),
|
|
create: vi.fn(),
|
|
createMany: vi.fn(),
|
|
update: vi.fn(),
|
|
aggregate: vi.fn(),
|
|
},
|
|
exercise: { findUnique: vi.fn() },
|
|
$transaction: vi.fn(),
|
|
};
|
|
}
|
|
|
|
const ownedDay = {
|
|
id: 'd1',
|
|
status: 'PLANNED',
|
|
week: { plan: { userId: 'u1' } },
|
|
exercises: [
|
|
{ id: 'pe1', exerciseId: 'pz1', order: 0, sets: [{ id: 'ps1', index: 0 }] },
|
|
{ id: 'pe2', exerciseId: 'pz2', order: 1, sets: [] },
|
|
],
|
|
};
|
|
|
|
describe('startSession', () => {
|
|
it('404 when the day belongs to another user', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.planDay.findUnique.mockResolvedValue({
|
|
...ownedDay,
|
|
week: { plan: { userId: 'other' } },
|
|
});
|
|
await expect(startSession(prisma, 'd1', 'u1')).rejects.toMatchObject({
|
|
status: 404,
|
|
message: 'Day not found',
|
|
});
|
|
});
|
|
|
|
it('400 when the day is not in planned state', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.planDay.findUnique.mockResolvedValue({ ...ownedDay, status: 'COMPLETED' });
|
|
await expect(startSession(prisma, 'd1', 'u1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Day is not in planned state',
|
|
});
|
|
});
|
|
|
|
it('409 when the user already has an active session', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.planDay.findUnique.mockResolvedValue(ownedDay);
|
|
prisma.trainingSession.findFirst.mockResolvedValue({ id: 's-active' });
|
|
await expect(startSession(prisma, 'd1', 'u1')).rejects.toMatchObject({
|
|
status: 409,
|
|
message: 'You already have an active session',
|
|
});
|
|
});
|
|
|
|
it('creates a session with exercises and planned sets', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.planDay.findUnique.mockResolvedValue(ownedDay);
|
|
prisma.trainingSession.findFirst.mockResolvedValue(null);
|
|
prisma.trainingSession.create.mockResolvedValue({ id: 's1', status: 'IN_PROGRESS' });
|
|
prisma.sessionExercise.createMany.mockResolvedValue({ count: 2 });
|
|
prisma.sessionExercise.findFirst.mockResolvedValue({ id: 'se1' });
|
|
prisma.sessionSet.createMany.mockResolvedValue({ count: 1 });
|
|
|
|
const result = await startSession(prisma, 'd1', 'u1');
|
|
|
|
expect(result).toEqual({ sessionId: 's1', status: 'IN_PROGRESS' });
|
|
expect(prisma.sessionExercise.createMany).toHaveBeenCalledWith({
|
|
data: [
|
|
{ sessionId: 's1', exerciseId: 'pz1', planExerciseId: 'pe1', order: 0 },
|
|
{ sessionId: 's1', exerciseId: 'pz2', planExerciseId: 'pe2', order: 1 },
|
|
],
|
|
});
|
|
expect(prisma.sessionSet.createMany).toHaveBeenCalledWith({
|
|
data: [{ exerciseId: 'se1', planSetId: 'ps1', index: 0 }],
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('getActiveSession', () => {
|
|
it('returns the active session with day and plan context', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.trainingSession.findFirst.mockResolvedValue({
|
|
id: 's1',
|
|
dayId: 'd1',
|
|
startedAt: '2026-08-24T07:00:00.000Z',
|
|
day: { id: 'd1', date: '2026-08-24', dayOfWeek: 1, week: { plan: { title: 'Plan A' } } },
|
|
});
|
|
await expect(getActiveSession(prisma, 'u1')).resolves.toEqual({
|
|
session: {
|
|
id: 's1',
|
|
dayId: 'd1',
|
|
dayDate: '2026-08-24',
|
|
dayOfWeek: 1,
|
|
planTitle: 'Plan A',
|
|
startedAt: '2026-08-24T07:00:00.000Z',
|
|
},
|
|
});
|
|
expect(prisma.trainingSession.findFirst).toHaveBeenCalledWith(
|
|
expect.objectContaining({ where: expect.objectContaining({ status: 'IN_PROGRESS' }) }),
|
|
);
|
|
});
|
|
|
|
it('returns a null session when none is active', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.trainingSession.findFirst.mockResolvedValue(null);
|
|
await expect(getActiveSession(prisma, 'u1')).resolves.toEqual({ session: null });
|
|
});
|
|
});
|
|
|
|
describe('completeSession', () => {
|
|
const ownedInProgress = {
|
|
id: 's1',
|
|
status: 'IN_PROGRESS',
|
|
dayId: 'd1',
|
|
day: { week: { plan: { userId: 'u1' } } },
|
|
};
|
|
|
|
it('400 when the session is not in progress', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.trainingSession.findUnique.mockResolvedValue({ ...ownedInProgress, status: 'COMPLETED' });
|
|
await expect(completeSession(prisma, 's1', 'u1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Session is not in progress',
|
|
});
|
|
});
|
|
|
|
it('400 when some planned (norm) sets are still pending', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.trainingSession.findUnique.mockResolvedValue(ownedInProgress);
|
|
prisma.sessionExercise.findMany.mockResolvedValue([
|
|
{
|
|
id: 'se1',
|
|
removed: false,
|
|
sets: [
|
|
{ id: 'st1', planSetId: 'ps1', reps: 10, removed: false },
|
|
{ id: 'st2', planSetId: 'ps2', reps: null, removed: false },
|
|
],
|
|
},
|
|
{ id: 'se2', removed: false, sets: [{ id: 'st3', planSetId: 'ps3', reps: null, removed: false }] },
|
|
]);
|
|
await expect(completeSession(prisma, 's1', 'u1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'All planned sets must be recorded before completing the session (2 remaining)',
|
|
});
|
|
});
|
|
|
|
it('completes the session when all norm sets are recorded (extra sets pending = bonus)', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.trainingSession.findUnique.mockResolvedValue(ownedInProgress);
|
|
prisma.sessionExercise.findMany.mockResolvedValue([
|
|
{
|
|
id: 'se1',
|
|
removed: false,
|
|
sets: [
|
|
{ id: 'st1', planSetId: 'ps1', reps: 10, removed: false },
|
|
{ id: 'st2', planSetId: 'ps2', reps: 8, removed: false },
|
|
{ id: 'st3', planSetId: null, reps: null, removed: false },
|
|
],
|
|
},
|
|
]);
|
|
prisma.$transaction.mockResolvedValue([{}, {}]);
|
|
|
|
await expect(completeSession(prisma, 's1', 'u1')).resolves.toEqual({ ok: true });
|
|
expect(prisma.$transaction).toHaveBeenCalled();
|
|
});
|
|
|
|
it('ignores removed sets when counting pending norm sets', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.trainingSession.findUnique.mockResolvedValue(ownedInProgress);
|
|
prisma.sessionExercise.findMany.mockResolvedValue([
|
|
{
|
|
id: 'se1',
|
|
removed: false,
|
|
sets: [
|
|
{ id: 'st1', planSetId: 'ps1', reps: 10, removed: false },
|
|
{ id: 'st2', planSetId: 'ps2', reps: null, removed: true },
|
|
],
|
|
},
|
|
]);
|
|
prisma.$transaction.mockResolvedValue([{}, {}]);
|
|
|
|
await expect(completeSession(prisma, 's1', 'u1')).resolves.toEqual({ ok: true });
|
|
});
|
|
});
|
|
|
|
describe('abandonSession', () => {
|
|
const ownedInProgress = {
|
|
id: 's1',
|
|
status: 'IN_PROGRESS',
|
|
dayId: 'd1',
|
|
day: { week: { plan: { userId: 'u1' } } },
|
|
};
|
|
|
|
it('400 when the session is not in progress', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.trainingSession.findUnique.mockResolvedValue({ ...ownedInProgress, status: 'ABANDONED' });
|
|
await expect(abandonSession(prisma, 's1', 'u1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Session is not in progress',
|
|
});
|
|
});
|
|
|
|
it('abandons the session and marks the day skipped (the training cannot be executed again)', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.trainingSession.findUnique.mockResolvedValue(ownedInProgress);
|
|
prisma.$transaction.mockResolvedValue([{}, {}]);
|
|
|
|
const result = await abandonSession(prisma, 's1', 'u1');
|
|
|
|
expect(result).toEqual({ ok: true });
|
|
expect(prisma.trainingSession.update).toHaveBeenCalledWith(
|
|
expect.objectContaining({ where: { id: 's1' }, data: expect.objectContaining({ status: 'ABANDONED' }) }),
|
|
);
|
|
expect(prisma.planDay.update).toHaveBeenCalledWith(
|
|
expect.objectContaining({ where: { id: 'd1' }, data: expect.objectContaining({ status: 'SKIPPED' }) }),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('addSet', () => {
|
|
const ownedExercise = {
|
|
id: 'se1',
|
|
session: {
|
|
id: 's1',
|
|
status: 'IN_PROGRESS',
|
|
day: { week: { plan: { userId: 'u1' } } },
|
|
},
|
|
};
|
|
|
|
it('400 when the session is not in progress', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.sessionExercise.findUnique.mockResolvedValue({
|
|
...ownedExercise,
|
|
session: { id: 's1', status: 'COMPLETED', day: { week: { plan: { userId: 'u1' } } } },
|
|
});
|
|
await expect(addSet(prisma, 'se1', 'u1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Session is not in progress',
|
|
});
|
|
});
|
|
|
|
it('400 when planned (norm) sets are still pending', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.sessionExercise.findUnique.mockResolvedValue(ownedExercise);
|
|
prisma.sessionSet.findMany.mockResolvedValue([
|
|
{ id: 'st1', planSetId: 'ps1', reps: 10, removed: false },
|
|
{ id: 'st2', planSetId: 'ps2', reps: null, removed: false },
|
|
]);
|
|
await expect(addSet(prisma, 'se1', 'u1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'All planned sets must be recorded before adding an extra set',
|
|
});
|
|
});
|
|
|
|
it('400 when an extra set is already in progress', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.sessionExercise.findUnique.mockResolvedValue(ownedExercise);
|
|
prisma.sessionSet.findMany.mockResolvedValue([
|
|
{ id: 'st1', planSetId: 'ps1', reps: 10, removed: false },
|
|
{ id: 'st2', planSetId: null, reps: null, removed: false },
|
|
]);
|
|
await expect(addSet(prisma, 'se1', 'u1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'An extra set is already in progress',
|
|
});
|
|
});
|
|
|
|
it('adds an extra set when all norm sets are done and no extra is pending', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.sessionExercise.findUnique.mockResolvedValue(ownedExercise);
|
|
prisma.sessionSet.findMany.mockResolvedValue([
|
|
{ id: 'st1', planSetId: 'ps1', reps: 10, removed: false },
|
|
{ id: 'st2', planSetId: null, reps: 8, removed: false },
|
|
]);
|
|
prisma.sessionSet.aggregate.mockResolvedValue({ _max: { index: 1 } });
|
|
prisma.sessionSet.create.mockResolvedValue({ id: 'st3', index: 2 });
|
|
|
|
const result = await addSet(prisma, 'se1', 'u1');
|
|
|
|
expect(result).toEqual({ setId: 'st3', index: 2 });
|
|
expect(prisma.sessionSet.create).toHaveBeenCalledWith({
|
|
data: { exerciseId: 'se1', index: 2 },
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('deleteSet', () => {
|
|
const ownedSet = {
|
|
id: 'st1',
|
|
planSetId: null,
|
|
exercise: {
|
|
session: {
|
|
id: 's1',
|
|
status: 'IN_PROGRESS',
|
|
day: { week: { plan: { userId: 'u1' } } },
|
|
},
|
|
},
|
|
};
|
|
|
|
it('404 when the set belongs to another user', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.sessionSet.findUnique.mockResolvedValue({
|
|
...ownedSet,
|
|
exercise: { session: { id: 's1', status: 'IN_PROGRESS', day: { week: { plan: { userId: 'other' } } } } },
|
|
});
|
|
await expect(deleteSet(prisma, 'st1', 'u1')).rejects.toMatchObject({
|
|
status: 404,
|
|
message: 'Set not found',
|
|
});
|
|
});
|
|
|
|
it('400 when the set is a planned (norm) set', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.sessionSet.findUnique.mockResolvedValue({ ...ownedSet, planSetId: 'ps1' });
|
|
await expect(deleteSet(prisma, 'st1', 'u1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Only extra sets can be removed',
|
|
});
|
|
});
|
|
|
|
it('400 when the session is not in progress', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.sessionSet.findUnique.mockResolvedValue({
|
|
...ownedSet,
|
|
exercise: { session: { id: 's1', status: 'COMPLETED', day: { week: { plan: { userId: 'u1' } } } } },
|
|
});
|
|
await expect(deleteSet(prisma, 'st1', 'u1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Session is not in progress',
|
|
});
|
|
});
|
|
|
|
it('soft-removes an extra set', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.sessionSet.findUnique.mockResolvedValue(ownedSet);
|
|
prisma.sessionSet.update.mockResolvedValue({ id: 'st1', removed: true });
|
|
|
|
const result = await deleteSet(prisma, 'st1', 'u1');
|
|
|
|
expect(result).toEqual({ ok: true });
|
|
expect(prisma.sessionSet.update).toHaveBeenCalledWith({ where: { id: 'st1' }, data: { removed: true } });
|
|
});
|
|
});
|
|
|
|
describe('addExercise', () => {
|
|
it('appends after the highest existing order', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.trainingSession.findUnique.mockResolvedValue({
|
|
id: 's1',
|
|
status: 'IN_PROGRESS',
|
|
day: { week: { plan: { userId: 'u1' } } },
|
|
});
|
|
prisma.exercise.findUnique.mockResolvedValue({ id: 'pz9' });
|
|
prisma.sessionExercise.aggregate.mockResolvedValue({ _max: { order: 3 } });
|
|
prisma.sessionExercise.create.mockResolvedValue({ id: 'se9', order: 4 });
|
|
|
|
const result = await addExercise(prisma, 's1', 'u1', 'pz9');
|
|
|
|
expect(result).toEqual({ exerciseId: 'se9', order: 4 });
|
|
expect(prisma.sessionExercise.create).toHaveBeenCalledWith({
|
|
data: { sessionId: 's1', exerciseId: 'pz9', order: 4 },
|
|
});
|
|
});
|
|
|
|
it('400 when the session is not in progress', async () => {
|
|
const prisma = mockPrisma();
|
|
prisma.trainingSession.findUnique.mockResolvedValue({
|
|
id: 's1',
|
|
status: 'COMPLETED',
|
|
day: { week: { plan: { userId: 'u1' } } },
|
|
});
|
|
await expect(addExercise(prisma, 's1', 'u1', 'pz9')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Session is not in progress',
|
|
});
|
|
});
|
|
});
|