Trening: wznowienie ukonczonej sesji (naprawa pomyly) - endpoint /reopen, akcja w UI, koniec wraca do edycji

This commit is contained in:
Tilumca
2026-10-14 22:37:52 +02:00
parent 9f7a0afdec
commit 931d54a89d
10 changed files with 296 additions and 17 deletions

View File

@@ -6,6 +6,7 @@ import {
completeSession,
deleteSet,
getActiveSession,
reopenSession,
startSession,
updateExercise,
} from '../../src/services/sessions.js';
@@ -246,8 +247,7 @@ describe('completeSession', () => {
]);
await expect(completeSession(prisma, 's1', 'u1')).rejects.toMatchObject({
status: 400,
message:
'Extra sets must be recorded or removed before completing the session (1 remaining)',
message: 'Extra sets must be recorded or removed before completing the session (1 remaining)',
});
expect(prisma.trainingSession.updateMany).not.toHaveBeenCalled();
});
@@ -394,6 +394,73 @@ describe('abandonSession', () => {
});
});
describe('reopenSession', () => {
const ownedCompleted = {
id: 's1',
status: 'COMPLETED',
dayId: 'd1',
day: { week: { plan: { userId: 'u1' } } },
};
it('400 when the session is not completed', async () => {
const prisma = mockPrisma();
prisma.trainingSession.findUnique.mockResolvedValue({
...ownedCompleted,
status: 'ABANDONED',
});
await expect(reopenSession(prisma, 's1', 'u1')).rejects.toMatchObject({
status: 400,
message: 'Only a completed session can be reopened',
});
});
it('409 when the user already has another active session', async () => {
const prisma = mockPrisma();
prisma.trainingSession.findUnique.mockResolvedValue(ownedCompleted);
prisma.trainingSession.findFirst.mockResolvedValue({ id: 's2' });
await expect(reopenSession(prisma, 's1', 'u1')).rejects.toMatchObject({
status: 409,
message: 'You already have an active session',
});
expect(prisma.$transaction).not.toHaveBeenCalled();
});
it('reopens the session and returns the day to planned (start reset to now)', async () => {
const prisma = mockPrisma();
prisma.trainingSession.findUnique.mockResolvedValue(ownedCompleted);
prisma.trainingSession.findFirst.mockResolvedValue(null);
prisma.trainingSession.updateMany.mockResolvedValue({ count: 1 });
prisma.planDay.updateMany.mockResolvedValue({ count: 1 });
await expect(reopenSession(prisma, 's1', 'u1')).resolves.toEqual({
sessionId: 's1',
status: 'IN_PROGRESS',
});
// stary endedAt/usunięty, startedAt = teraz — inaczej reguła 24h zbiłaby wznowienie
expect(prisma.trainingSession.updateMany).toHaveBeenCalledWith({
where: { id: 's1', status: 'COMPLETED' },
data: { status: 'IN_PROGRESS', startedAt: expect.any(Date), endedAt: null },
});
expect(prisma.planDay.updateMany).toHaveBeenCalledWith({
where: { id: 'd1', status: 'COMPLETED' },
data: { status: 'PLANNED', completedAt: null },
});
});
it('409 when the day is no longer completed (e.g. concurrently expired)', async () => {
const prisma = mockPrisma();
prisma.trainingSession.findUnique.mockResolvedValue(ownedCompleted);
prisma.trainingSession.findFirst.mockResolvedValue(null);
prisma.trainingSession.updateMany.mockResolvedValue({ count: 1 });
prisma.planDay.updateMany.mockResolvedValue({ count: 0 });
await expect(reopenSession(prisma, 's1', 'u1')).rejects.toMatchObject({
status: 409,
message: 'Training day is no longer completed',
});
});
});
describe('updateExercise', () => {
it('400 when removing an exercise that comes from the plan', async () => {
const prisma = mockPrisma();