Plany i sesje: porzucenie/konczenie tylko z ACTIVE, przyszle dni bez skip, usuwanie tylko cwiczen dodanych do sesji
This commit is contained in:
@@ -54,7 +54,10 @@ describe('scheduler', () => {
|
||||
});
|
||||
|
||||
it('auto-abandons in-progress sessions older than 24h and skips their days', async () => {
|
||||
sessionFindMany.mockResolvedValue([{ id: 's1', dayId: 'd1' }, { id: 's2', dayId: 'd2' }]);
|
||||
sessionFindMany.mockResolvedValue([
|
||||
{ id: 's1', dayId: 'd1' },
|
||||
{ id: 's2', dayId: 'd2' },
|
||||
]);
|
||||
await tickOnce();
|
||||
const cutoff = new Date(NOW.getTime() - 24 * 60 * 60 * 1000);
|
||||
expect(sessionFindMany).toHaveBeenCalledWith({
|
||||
@@ -65,9 +68,14 @@ describe('scheduler', () => {
|
||||
where: { id: { in: ['s1', 's2'] } },
|
||||
data: { status: 'ABANDONED', endedAt: expect.any(Date) },
|
||||
});
|
||||
// dzień porzuconej sesji nie może zostać ponownie wykonany (jak przy ręcznym abandon)
|
||||
// dzień porzuconej sesji nie może zostać ponownie wykonany (jak przy ręcznym abandon),
|
||||
// ale dni z przyszłą datą zostają PLANNED — stąd filtr po dacie
|
||||
expect(planDayUpdateMany).toHaveBeenLastCalledWith({
|
||||
where: { id: { in: ['d1', 'd2'] }, status: 'PLANNED' },
|
||||
where: {
|
||||
id: { in: ['d1', 'd2'] },
|
||||
status: 'PLANNED',
|
||||
date: { lt: new Date(2026, 7, 23) },
|
||||
},
|
||||
data: { status: 'SKIPPED', skippedAt: expect.any(Date) },
|
||||
});
|
||||
});
|
||||
|
||||
@@ -21,7 +21,7 @@ function mockPrisma() {
|
||||
trainingPlan: { update: vi.fn(), findFirst: vi.fn() },
|
||||
planDay: { update: vi.fn() },
|
||||
};
|
||||
const prisma = {
|
||||
const prisma = {
|
||||
trainingPlan: {
|
||||
findUnique: vi.fn(),
|
||||
findFirst: vi.fn(),
|
||||
@@ -93,7 +93,16 @@ describe('draft-only rules', () => {
|
||||
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, status: 'ABANDONED' });
|
||||
await expect(abandonPlan(prisma, 'p1', 'u1')).rejects.toMatchObject({
|
||||
status: 400,
|
||||
message: 'Plan is already closed',
|
||||
message: 'Only an active plan can be abandoned',
|
||||
});
|
||||
});
|
||||
|
||||
it('abandonPlan: rejects a draft plan with 400', async () => {
|
||||
const { prisma } = mockPrisma();
|
||||
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
||||
await expect(abandonPlan(prisma, 'p1', 'u1')).rejects.toMatchObject({
|
||||
status: 400,
|
||||
message: 'Only an active plan can be abandoned',
|
||||
});
|
||||
});
|
||||
|
||||
@@ -102,7 +111,16 @@ describe('draft-only rules', () => {
|
||||
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, status: 'COMPLETED' });
|
||||
await expect(completePlan(prisma, 'p1', 'u1')).rejects.toMatchObject({
|
||||
status: 400,
|
||||
message: 'Plan is already closed',
|
||||
message: 'Only an active plan can be completed',
|
||||
});
|
||||
});
|
||||
|
||||
it('completePlan: rejects a draft plan with 400', async () => {
|
||||
const { prisma } = mockPrisma();
|
||||
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
||||
await expect(completePlan(prisma, 'p1', 'u1')).rejects.toMatchObject({
|
||||
status: 400,
|
||||
message: 'Only an active plan can be completed',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -195,7 +213,13 @@ describe('activatePlan', () => {
|
||||
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
||||
prisma.trainingPlan.findFirst.mockResolvedValue(null);
|
||||
prisma.planWeek.findMany.mockResolvedValue([
|
||||
{ index: 0, days: [{ id: 'd1', dayOfWeek: 1 }, { id: 'd2', dayOfWeek: 3 }] },
|
||||
{
|
||||
index: 0,
|
||||
days: [
|
||||
{ id: 'd1', dayOfWeek: 1 },
|
||||
{ id: 'd2', dayOfWeek: 3 },
|
||||
],
|
||||
},
|
||||
{ index: 1, days: [{ id: 'd3', dayOfWeek: 1 }] },
|
||||
]);
|
||||
tx.trainingPlan.findFirst.mockResolvedValue(null);
|
||||
@@ -432,9 +456,7 @@ describe('updatePlan (startDate = aktualizacja + aktywacja)', () => {
|
||||
const { prisma, tx } = mockPrisma();
|
||||
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
||||
prisma.exercise.findMany.mockResolvedValue([]);
|
||||
prisma.planWeek.findMany.mockResolvedValue([
|
||||
{ index: 0, days: [{ id: 'd1', dayOfWeek: 1 }] },
|
||||
]);
|
||||
prisma.planWeek.findMany.mockResolvedValue([{ index: 0, days: [{ id: 'd1', dayOfWeek: 1 }] }]);
|
||||
tx.trainingPlan.update.mockResolvedValue({});
|
||||
tx.planDay.update.mockResolvedValue({});
|
||||
|
||||
@@ -492,9 +514,7 @@ describe('updatePlan (startDate = aktualizacja + aktywacja)', () => {
|
||||
const { prisma, tx } = mockPrisma();
|
||||
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
||||
prisma.exercise.findMany.mockResolvedValue([]);
|
||||
prisma.planWeek.findMany.mockResolvedValue([
|
||||
{ index: 0, days: [{ id: 'd1', dayOfWeek: 1 }] },
|
||||
]);
|
||||
prisma.planWeek.findMany.mockResolvedValue([{ index: 0, days: [{ id: 'd1', dayOfWeek: 1 }] }]);
|
||||
tx.trainingPlan.update.mockResolvedValue({});
|
||||
// W transakcji aktywacji właściciel ma już aktywny plan → konflikt.
|
||||
tx.trainingPlan.findFirst.mockResolvedValue({ id: 'other-active' });
|
||||
@@ -617,7 +637,7 @@ describe('unsharePlan', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('brak share\'a: 404', async () => {
|
||||
it("brak share'a: 404", async () => {
|
||||
const { prisma } = mockPrisma();
|
||||
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, userId: 'u1' });
|
||||
prisma.planShare.deleteMany.mockResolvedValue({ count: 0 });
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { abandonSession, addExercise, addSet, completeSession, deleteSet, getActiveSession, startSession } from '../../src/services/sessions.js';
|
||||
import {
|
||||
abandonSession,
|
||||
addExercise,
|
||||
addSet,
|
||||
completeSession,
|
||||
deleteSet,
|
||||
getActiveSession,
|
||||
startSession,
|
||||
updateExercise,
|
||||
} from '../../src/services/sessions.js';
|
||||
|
||||
function mockPrisma() {
|
||||
const prisma: any = {
|
||||
@@ -181,7 +190,10 @@ describe('completeSession', () => {
|
||||
|
||||
it('400 when the session is not in progress', async () => {
|
||||
const prisma = mockPrisma();
|
||||
prisma.trainingSession.findUnique.mockResolvedValue({ ...ownedInProgress, status: 'COMPLETED' });
|
||||
prisma.trainingSession.findUnique.mockResolvedValue({
|
||||
...ownedInProgress,
|
||||
status: 'COMPLETED',
|
||||
});
|
||||
await expect(completeSession(prisma, 's1', 'u1')).rejects.toMatchObject({
|
||||
status: 400,
|
||||
message: 'Session is not in progress',
|
||||
@@ -200,7 +212,11 @@ describe('completeSession', () => {
|
||||
{ id: 'st2', planSetId: 'ps2', reps: null, removed: false },
|
||||
],
|
||||
},
|
||||
{ id: 'se2', removed: false, sets: [{ id: 'st3', planSetId: 'ps3', 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,
|
||||
@@ -241,7 +257,11 @@ describe('completeSession', () => {
|
||||
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: 'se1',
|
||||
removed: false,
|
||||
sets: [{ id: 'st1', planSetId: 'ps1', reps: 10, removed: false }],
|
||||
},
|
||||
]);
|
||||
prisma.trainingSession.updateMany.mockResolvedValue({ count: 1 });
|
||||
// dzień wygasł w schedulerze (SKIPPED) — warunkowe updateMany nic nie trafia
|
||||
@@ -257,7 +277,11 @@ describe('completeSession', () => {
|
||||
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: 'se1',
|
||||
removed: false,
|
||||
sets: [{ id: 'st1', planSetId: 'ps1', reps: 10, removed: false }],
|
||||
},
|
||||
]);
|
||||
prisma.trainingSession.updateMany.mockResolvedValue({ count: 0 });
|
||||
|
||||
@@ -297,7 +321,10 @@ describe('abandonSession', () => {
|
||||
|
||||
it('400 when the session is not in progress', async () => {
|
||||
const prisma = mockPrisma();
|
||||
prisma.trainingSession.findUnique.mockResolvedValue({ ...ownedInProgress, status: 'ABANDONED' });
|
||||
prisma.trainingSession.findUnique.mockResolvedValue({
|
||||
...ownedInProgress,
|
||||
status: 'ABANDONED',
|
||||
});
|
||||
await expect(abandonSession(prisma, 's1', 'u1')).rejects.toMatchObject({
|
||||
status: 400,
|
||||
message: 'Session is not in progress',
|
||||
@@ -319,10 +346,10 @@ describe('abandonSession', () => {
|
||||
data: expect.objectContaining({ status: 'ABANDONED' }),
|
||||
}),
|
||||
);
|
||||
// dzień oznaczany tylko gdy wciąż PLANNED (nie nadpisuje wygasłego)
|
||||
// dzień oznaczany tylko gdy wciąż PLANNED i już nadszedł jego czas (przyszły zostaje PLANNED)
|
||||
expect(prisma.planDay.updateMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { id: 'd1', status: 'PLANNED' },
|
||||
where: { id: 'd1', status: 'PLANNED', date: { lt: expect.any(Date) } },
|
||||
data: expect.objectContaining({ status: 'SKIPPED' }),
|
||||
}),
|
||||
);
|
||||
@@ -338,6 +365,52 @@ describe('abandonSession', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateExercise', () => {
|
||||
it('400 when removing an exercise that comes from the plan', async () => {
|
||||
const prisma = mockPrisma();
|
||||
prisma.sessionExercise.findUnique.mockResolvedValue({
|
||||
id: 'se1',
|
||||
planExerciseId: 'pe1',
|
||||
session: { id: 's1', status: 'IN_PROGRESS', day: { week: { plan: { userId: 'u1' } } } },
|
||||
});
|
||||
await expect(updateExercise(prisma, 'se1', 'u1', { removed: true })).rejects.toMatchObject({
|
||||
status: 400,
|
||||
message: 'Only exercises added to the session can be removed',
|
||||
});
|
||||
expect(prisma.sessionExercise.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('allows removing an exercise added to the session', async () => {
|
||||
const prisma = mockPrisma();
|
||||
prisma.sessionExercise.findUnique.mockResolvedValue({
|
||||
id: 'se1',
|
||||
planExerciseId: null,
|
||||
session: { id: 's1', status: 'IN_PROGRESS', day: { week: { plan: { userId: 'u1' } } } },
|
||||
});
|
||||
prisma.sessionExercise.update.mockResolvedValue({ id: 'se1' });
|
||||
await expect(updateExercise(prisma, 'se1', 'u1', { removed: true })).resolves.toEqual({
|
||||
ok: true,
|
||||
});
|
||||
expect(prisma.sessionExercise.update).toHaveBeenCalledWith({
|
||||
where: { id: 'se1' },
|
||||
data: { removed: true },
|
||||
});
|
||||
});
|
||||
|
||||
it('allows hiding a planned exercise (hidden does not exempt planned sets)', async () => {
|
||||
const prisma = mockPrisma();
|
||||
prisma.sessionExercise.findUnique.mockResolvedValue({
|
||||
id: 'se1',
|
||||
planExerciseId: 'pe1',
|
||||
session: { id: 's1', status: 'IN_PROGRESS', day: { week: { plan: { userId: 'u1' } } } },
|
||||
});
|
||||
prisma.sessionExercise.update.mockResolvedValue({ id: 'se1' });
|
||||
await expect(updateExercise(prisma, 'se1', 'u1', { hidden: true })).resolves.toEqual({
|
||||
ok: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('addSet', () => {
|
||||
const ownedExercise = {
|
||||
id: 'se1',
|
||||
@@ -422,7 +495,9 @@ describe('deleteSet', () => {
|
||||
const prisma = mockPrisma();
|
||||
prisma.sessionSet.findUnique.mockResolvedValue({
|
||||
...ownedSet,
|
||||
exercise: { session: { id: 's1', status: 'IN_PROGRESS', day: { week: { plan: { userId: 'other' } } } } },
|
||||
exercise: {
|
||||
session: { id: 's1', status: 'IN_PROGRESS', day: { week: { plan: { userId: 'other' } } } },
|
||||
},
|
||||
});
|
||||
await expect(deleteSet(prisma, 'st1', 'u1')).rejects.toMatchObject({
|
||||
status: 404,
|
||||
@@ -443,7 +518,9 @@ describe('deleteSet', () => {
|
||||
const prisma = mockPrisma();
|
||||
prisma.sessionSet.findUnique.mockResolvedValue({
|
||||
...ownedSet,
|
||||
exercise: { session: { id: 's1', status: 'COMPLETED', day: { week: { plan: { userId: 'u1' } } } } },
|
||||
exercise: {
|
||||
session: { id: 's1', status: 'COMPLETED', day: { week: { plan: { userId: 'u1' } } } },
|
||||
},
|
||||
});
|
||||
await expect(deleteSet(prisma, 'st1', 'u1')).rejects.toMatchObject({
|
||||
status: 400,
|
||||
@@ -459,7 +536,10 @@ describe('deleteSet', () => {
|
||||
const result = await deleteSet(prisma, 'st1', 'u1');
|
||||
|
||||
expect(result).toEqual({ ok: true });
|
||||
expect(prisma.sessionSet.update).toHaveBeenCalledWith({ where: { id: 'st1' }, data: { removed: true } });
|
||||
expect(prisma.sessionSet.update).toHaveBeenCalledWith({
|
||||
where: { id: 'st1' },
|
||||
data: { removed: true },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user