Sesje: modyfikacje i zakonczenie pod blokada wiersza (SELECT FOR UPDATE) + straz statusu w makerzu 24h (eliminacja wyscigow)

This commit is contained in:
tisachi
2026-10-11 17:56:32 +02:00
parent 05ab05a3da
commit 68d06fed7c
4 changed files with 117 additions and 55 deletions

View File

@@ -65,7 +65,8 @@ describe('scheduler', () => {
select: { id: true, dayId: true },
});
expect(sessionUpdateMany).toHaveBeenCalledWith({
where: { id: { in: ['s1', 's2'] } },
// warunek status w where: nie nadpisze sesji zakończonej w międzyczasie
where: { id: { in: ['s1', 's2'] }, status: 'IN_PROGRESS' },
data: { status: 'ABANDONED', endedAt: expect.any(Date) },
});
// dzień porzuconej sesji nie może zostać ponownie wykonany (jak przy ręcznym abandon),

View File

@@ -48,6 +48,11 @@ function mockPrisma() {
return prisma;
}
/** Wiersz sesji widoczny dla blokady SELECT ... FOR UPDATE w inSessionTransaction. */
function mockSessionRow(prisma: any, status: 'IN_PROGRESS' | 'COMPLETED' | 'ABANDONED') {
prisma.trainingSession.findUnique.mockResolvedValue({ id: 's1', status });
}
const ownedDay = {
id: 'd1',
status: 'PLANNED',
@@ -387,6 +392,7 @@ describe('updateExercise', () => {
planExerciseId: null,
session: { id: 's1', status: 'IN_PROGRESS', day: { week: { plan: { userId: 'u1' } } } },
});
mockSessionRow(prisma, 'IN_PROGRESS');
prisma.sessionExercise.update.mockResolvedValue({ id: 'se1' });
await expect(updateExercise(prisma, 'se1', 'u1', { removed: true })).resolves.toEqual({
ok: true,
@@ -404,6 +410,7 @@ describe('updateExercise', () => {
planExerciseId: 'pe1',
session: { id: 's1', status: 'IN_PROGRESS', day: { week: { plan: { userId: 'u1' } } } },
});
mockSessionRow(prisma, 'IN_PROGRESS');
prisma.sessionExercise.update.mockResolvedValue({ id: 'se1' });
await expect(updateExercise(prisma, 'se1', 'u1', { hidden: true })).resolves.toEqual({
ok: true,
@@ -423,6 +430,7 @@ describe('addSet', () => {
it('400 when the session is not in progress', async () => {
const prisma = mockPrisma();
mockSessionRow(prisma, 'COMPLETED');
prisma.sessionExercise.findUnique.mockResolvedValue({
...ownedExercise,
session: { id: 's1', status: 'COMPLETED', day: { week: { plan: { userId: 'u1' } } } },
@@ -435,7 +443,9 @@ describe('addSet', () => {
it('400 when planned (norm) sets are still pending', async () => {
const prisma = mockPrisma();
mockSessionRow(prisma, 'IN_PROGRESS');
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 },
@@ -448,7 +458,9 @@ describe('addSet', () => {
it('400 when an extra set is already in progress', async () => {
const prisma = mockPrisma();
mockSessionRow(prisma, 'IN_PROGRESS');
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 },
@@ -461,7 +473,9 @@ describe('addSet', () => {
it('adds an extra set when all norm sets are done and no extra is pending', async () => {
const prisma = mockPrisma();
mockSessionRow(prisma, 'IN_PROGRESS');
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 },
@@ -516,6 +530,7 @@ describe('deleteSet', () => {
it('400 when the session is not in progress', async () => {
const prisma = mockPrisma();
mockSessionRow(prisma, 'COMPLETED');
prisma.sessionSet.findUnique.mockResolvedValue({
...ownedSet,
exercise: {
@@ -530,7 +545,9 @@ describe('deleteSet', () => {
it('soft-removes an extra set', async () => {
const prisma = mockPrisma();
mockSessionRow(prisma, 'IN_PROGRESS');
prisma.sessionSet.findUnique.mockResolvedValue(ownedSet);
prisma.sessionSet.update.mockResolvedValue({ id: 'st1', removed: true });
const result = await deleteSet(prisma, 'st1', 'u1');
@@ -546,6 +563,7 @@ describe('deleteSet', () => {
describe('addExercise', () => {
it('appends after the highest existing order', async () => {
const prisma = mockPrisma();
mockSessionRow(prisma, 'IN_PROGRESS');
prisma.trainingSession.findUnique.mockResolvedValue({
id: 's1',
status: 'IN_PROGRESS',