685 lines
25 KiB
TypeScript
685 lines
25 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest';
|
|
import {
|
|
abandonPlan,
|
|
activatePlan,
|
|
assertPlanOwner,
|
|
completePlan,
|
|
createPlan,
|
|
deletePlan,
|
|
getMyTrainers,
|
|
getPlan,
|
|
listPlans,
|
|
listSharedWithMe,
|
|
sharePlan,
|
|
unsharePlan,
|
|
updatePlan,
|
|
} from '../../src/services/plans.js';
|
|
|
|
function mockPrisma() {
|
|
const tx = {
|
|
planWeek: { deleteMany: vi.fn() },
|
|
trainingPlan: { update: vi.fn(), findFirst: vi.fn() },
|
|
planDay: { update: vi.fn() },
|
|
};
|
|
const prisma = {
|
|
trainingPlan: {
|
|
findUnique: vi.fn(),
|
|
findFirst: vi.fn(),
|
|
findMany: vi.fn(),
|
|
create: vi.fn(),
|
|
update: vi.fn(),
|
|
delete: vi.fn(),
|
|
},
|
|
exercise: { findMany: vi.fn() },
|
|
coaching: { findUnique: vi.fn(), findMany: vi.fn() },
|
|
planWeek: { findMany: vi.fn() },
|
|
planShare: {
|
|
findUnique: vi.fn(),
|
|
create: vi.fn(),
|
|
deleteMany: vi.fn(),
|
|
findMany: vi.fn(),
|
|
},
|
|
$transaction: vi.fn(async (cb: (tx: typeof tx) => Promise<void>) => cb(tx)),
|
|
};
|
|
return { prisma, tx };
|
|
}
|
|
|
|
const draftPlan = { id: 'p1', userId: 'u1', status: 'DRAFT' };
|
|
|
|
// Poniedziałek o przesunięcie o N tygodni (0 = bieżący tydzień) w formacie YYYY-MM-DD.
|
|
function mondayIso(offsetWeeks: number): string {
|
|
const d = new Date();
|
|
d.setDate(d.getDate() - ((d.getDay() + 6) % 7) + offsetWeeks * 7);
|
|
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
|
|
}
|
|
|
|
describe('ownership', () => {
|
|
it('getPlan: 404 when the plan belongs to another user', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, userId: 'other' });
|
|
await expect(getPlan(prisma, 'p1', 'u1')).rejects.toMatchObject({
|
|
status: 404,
|
|
message: 'Plan not found',
|
|
});
|
|
});
|
|
|
|
it('getPlan: 404 when the plan does not exist', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(null);
|
|
await expect(getPlan(prisma, 'p1', 'u1')).rejects.toMatchObject({ status: 404 });
|
|
});
|
|
});
|
|
|
|
describe('draft-only rules', () => {
|
|
it('updatePlan: rejects a non-draft plan with 400', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, status: 'ACTIVE' });
|
|
await expect(
|
|
updatePlan(prisma, 'p1', 'u1', { title: 't', weeksCount: 1, weeks: [] }),
|
|
).rejects.toMatchObject({ status: 400, message: 'Only draft plans can be edited' });
|
|
});
|
|
|
|
it('deletePlan: rejects a non-draft plan with 400', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, status: 'COMPLETED' });
|
|
await expect(deletePlan(prisma, 'p1', 'u1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Only draft plans can be deleted',
|
|
});
|
|
});
|
|
|
|
it('abandonPlan: rejects an already closed plan with 400', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, status: 'ABANDONED' });
|
|
await expect(abandonPlan(prisma, 'p1', 'u1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Plan is already closed',
|
|
});
|
|
});
|
|
|
|
it('completePlan: rejects an already closed plan with 400', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, status: 'COMPLETED' });
|
|
await expect(completePlan(prisma, 'p1', 'u1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Plan is already closed',
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('createPlan', () => {
|
|
it('rejects an unknown exerciseId with 400', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.exercise.findMany.mockResolvedValue([{ id: 'pz1' }]);
|
|
const data = {
|
|
title: 't',
|
|
weeksCount: 1,
|
|
weeks: [
|
|
{
|
|
index: 0,
|
|
days: [
|
|
{
|
|
dayOfWeek: 1,
|
|
exercises: [
|
|
{ exerciseId: 'pz1', order: 0, sets: [{ index: 0 }] },
|
|
{ exerciseId: 'pz-missing', order: 1, sets: [{ index: 0 }] },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
await expect(createPlan(prisma, 'u1', data)).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Unknown exerciseId in plan',
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('activatePlan', () => {
|
|
it('rejects a non-Monday start date with 400', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
|
// 2026-08-25 is a Tuesday
|
|
await expect(activatePlan(prisma, 'p1', 'u1', '2026-08-25')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Start date must be a Monday',
|
|
});
|
|
});
|
|
|
|
it('rejects an invalid start date with 400', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
|
await expect(activatePlan(prisma, 'p1', 'u1', 'not-a-date')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Invalid start date',
|
|
});
|
|
});
|
|
|
|
it('rejects when the user already has an active plan with 409', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
|
prisma.trainingPlan.findFirst.mockResolvedValue({ id: 'other-active' });
|
|
await expect(activatePlan(prisma, 'p1', 'u1', mondayIso(1))).rejects.toMatchObject({
|
|
status: 409,
|
|
message: 'User already has an active plan',
|
|
});
|
|
});
|
|
|
|
it('rejects a past-week start date with 400', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
|
await expect(activatePlan(prisma, 'p1', 'u1', mondayIso(-1))).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Start date cannot be in the past',
|
|
});
|
|
});
|
|
|
|
it('rejects with 409 when another plan becomes active inside the transaction (race)', async () => {
|
|
const { prisma, tx } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
|
// Pre-check poza transakcją nic nie widzi…
|
|
prisma.trainingPlan.findFirst.mockResolvedValue(null);
|
|
prisma.planWeek.findMany.mockResolvedValue([]);
|
|
// …ale wewnątrz transakcji inny plan właściciela jest już ACTIVE.
|
|
tx.trainingPlan.findFirst.mockResolvedValue({ id: 'other-active' });
|
|
await expect(activatePlan(prisma, 'p1', 'u1', mondayIso(1))).rejects.toMatchObject({
|
|
status: 409,
|
|
message: 'User already has an active plan',
|
|
});
|
|
expect(tx.trainingPlan.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('computes day dates from the Monday and activates the plan', async () => {
|
|
const { prisma, tx } = mockPrisma();
|
|
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: 1, days: [{ id: 'd3', dayOfWeek: 1 }] },
|
|
]);
|
|
tx.trainingPlan.findFirst.mockResolvedValue(null);
|
|
tx.planDay.update.mockResolvedValue({});
|
|
tx.trainingPlan.update.mockResolvedValue({});
|
|
|
|
const start = mondayIso(1);
|
|
const [y, m, d] = start.split('-').map(Number);
|
|
const plus = (n: number) => new Date(y, m - 1, d + n);
|
|
|
|
await activatePlan(prisma, 'p1', 'u1', start);
|
|
|
|
expect(tx.planDay.update).toHaveBeenCalledWith({
|
|
where: { id: 'd1' },
|
|
data: { date: plus(0) },
|
|
});
|
|
expect(tx.planDay.update).toHaveBeenCalledWith({
|
|
where: { id: 'd2' },
|
|
data: { date: plus(2) },
|
|
});
|
|
expect(tx.planDay.update).toHaveBeenCalledWith({
|
|
where: { id: 'd3' },
|
|
data: { date: plus(7) },
|
|
});
|
|
expect(tx.trainingPlan.update).toHaveBeenCalledWith({
|
|
where: { id: 'p1' },
|
|
data: { status: 'ACTIVE', startDate: plus(0) },
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('listPlans', () => {
|
|
it('returns the user plans ordered by createdAt desc, with creator and progress', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findMany.mockResolvedValue([
|
|
{
|
|
id: 'p1',
|
|
weeks: [{ days: [{ status: 'COMPLETED' }, { status: 'PLANNED' }] }],
|
|
},
|
|
]);
|
|
const result = await listPlans(prisma, 'u1');
|
|
expect(prisma.trainingPlan.findMany).toHaveBeenCalledWith({
|
|
where: { userId: 'u1' },
|
|
orderBy: { createdAt: 'desc' },
|
|
include: {
|
|
creator: { select: { id: true, firstName: true, lastName: true } },
|
|
gym: { select: { id: true, name: true } },
|
|
weeks: { select: { days: { select: { status: true } } } },
|
|
},
|
|
});
|
|
expect(result).toEqual({
|
|
plans: [{ id: 'p1', progress: { completed: 1, total: 2, percent: 50 } }],
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('assertPlanAccess (via getPlan)', () => {
|
|
it('allows the owner (trainee) to read the plan', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, userId: 'u1', shares: [] });
|
|
await expect(getPlan(prisma, 'p1', 'u1')).resolves.toMatchObject({
|
|
plan: { id: 'p1', canManage: true },
|
|
});
|
|
});
|
|
|
|
it('allows a trainer with an ACTIVE coaching relation to read the plan', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({
|
|
...draftPlan,
|
|
userId: 'trainee-1',
|
|
shares: [],
|
|
});
|
|
prisma.coaching.findUnique.mockResolvedValue({ status: 'ACTIVE' });
|
|
const result = await getPlan(prisma, 'p1', 'trainer-1');
|
|
expect(result.plan).toMatchObject({ id: 'p1', canManage: true });
|
|
// trener nie dostaje listy odbiorców share'u
|
|
expect(result.plan.shares).toBeUndefined();
|
|
});
|
|
|
|
it('rejects a trainer with a PENDING relation with 404', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, userId: 'trainee-1' });
|
|
prisma.coaching.findUnique.mockResolvedValue({ status: 'PENDING' });
|
|
await expect(getPlan(prisma, 'p1', 'trainer-1')).rejects.toMatchObject({
|
|
status: 404,
|
|
message: 'Plan not found',
|
|
});
|
|
});
|
|
|
|
it('rejects a trainer with an ARCHIVED relation with 404', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, userId: 'trainee-1' });
|
|
prisma.coaching.findUnique.mockResolvedValue({ status: 'ARCHIVED' });
|
|
await expect(getPlan(prisma, 'p1', 'trainer-1')).rejects.toMatchObject({ status: 404 });
|
|
});
|
|
|
|
it('allows a share recipient to read the plan (read-only)', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({
|
|
...draftPlan,
|
|
userId: 'trainee-1',
|
|
shares: [],
|
|
});
|
|
prisma.coaching.findUnique.mockResolvedValue(null);
|
|
prisma.planShare.findUnique.mockResolvedValue({ id: 'share-1' });
|
|
const result = await getPlan(prisma, 'p1', 'recipient-1');
|
|
expect(result.plan).toMatchObject({ id: 'p1', canManage: false });
|
|
expect(result.plan.shares).toBeUndefined();
|
|
});
|
|
|
|
it('rejects an unrelated user (no coaching relation, no share) with 404', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, userId: 'trainee-1' });
|
|
prisma.coaching.findUnique.mockResolvedValue(null);
|
|
prisma.planShare.findUnique.mockResolvedValue(null);
|
|
await expect(getPlan(prisma, 'p1', 'stranger')).rejects.toMatchObject({ status: 404 });
|
|
});
|
|
|
|
it('rejects a missing plan with 404', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(null);
|
|
await expect(getPlan(prisma, 'p1', 'u1')).rejects.toMatchObject({ status: 404 });
|
|
});
|
|
});
|
|
|
|
describe('assertPlanOwner', () => {
|
|
it('allows the owner', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, userId: 'u1' });
|
|
await expect(assertPlanOwner(prisma, 'p1', 'u1')).resolves.toMatchObject({ id: 'p1' });
|
|
});
|
|
|
|
it('rejects a non-owner (even a trainer) with 404', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, userId: 'trainee-1' });
|
|
await expect(assertPlanOwner(prisma, 'p1', 'trainer-1')).rejects.toMatchObject({
|
|
status: 404,
|
|
message: 'Plan not found',
|
|
});
|
|
});
|
|
|
|
it('rejects a missing plan with 404', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(null);
|
|
await expect(assertPlanOwner(prisma, 'p1', 'u1')).rejects.toMatchObject({ status: 404 });
|
|
});
|
|
});
|
|
|
|
describe('createPlan creatorId', () => {
|
|
it('sets creatorId to the given creator (trainer) when provided', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.exercise.findMany.mockResolvedValue([]);
|
|
prisma.trainingPlan.create.mockResolvedValue({ id: 'p1' });
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({
|
|
id: 'p1',
|
|
title: 't',
|
|
status: 'DRAFT',
|
|
weeksCount: 1,
|
|
startDate: null,
|
|
});
|
|
await createPlan(prisma, 'trainee-1', { title: 't', weeksCount: 1, weeks: [] }, 'trainer-1');
|
|
expect(prisma.trainingPlan.create).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
data: expect.objectContaining({ userId: 'trainee-1', creatorId: 'trainer-1' }),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('defaults creatorId to the owner when not provided', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.exercise.findMany.mockResolvedValue([]);
|
|
prisma.trainingPlan.create.mockResolvedValue({ id: 'p1' });
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({
|
|
id: 'p1',
|
|
title: 't',
|
|
status: 'DRAFT',
|
|
weeksCount: 1,
|
|
startDate: null,
|
|
});
|
|
await createPlan(prisma, 'u1', { title: 't', weeksCount: 1, weeks: [] });
|
|
expect(prisma.trainingPlan.create).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
data: expect.objectContaining({ userId: 'u1', creatorId: 'u1' }),
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('createPlan (startDate przy istniejącym aktywnym planie)', () => {
|
|
it('tworzy plan DRAFT z activationSkipped, gdy użytkownik ma już aktywny plan', async () => {
|
|
const { prisma, tx } = mockPrisma();
|
|
prisma.exercise.findMany.mockResolvedValue([]);
|
|
prisma.trainingPlan.create.mockResolvedValue({ id: 'p1' });
|
|
prisma.planWeek.findMany.mockResolvedValue([]);
|
|
// W transakcji aktywacji użytkownik ma już aktywny plan → konflikt.
|
|
tx.trainingPlan.findFirst.mockResolvedValue({ id: 'other-active' });
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({
|
|
id: 'p1',
|
|
title: 't',
|
|
status: 'DRAFT',
|
|
weeksCount: 1,
|
|
startDate: null,
|
|
});
|
|
const result = await createPlan(prisma, 'u1', {
|
|
title: 't',
|
|
weeksCount: 1,
|
|
weeks: [],
|
|
startDate: mondayIso(1),
|
|
});
|
|
expect(result).toMatchObject({
|
|
plan: { id: 'p1', status: 'DRAFT' },
|
|
activationSkipped: true,
|
|
});
|
|
expect(tx.planDay.update).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('updatePlan (startDate = aktualizacja + aktywacja)', () => {
|
|
it('bez startDate: aktualizuje strukturę, plan pozostaje DRAFT', async () => {
|
|
const { prisma, tx } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
|
prisma.exercise.findMany.mockResolvedValue([]);
|
|
tx.trainingPlan.update.mockResolvedValue({});
|
|
await updatePlan(prisma, 'p1', 'u1', { title: 't', weeksCount: 1, weeks: [] });
|
|
expect(tx.trainingPlan.update).toHaveBeenCalledWith({
|
|
where: { id: 'p1' },
|
|
data: expect.objectContaining({ title: 't' }),
|
|
});
|
|
// bez aktywacji — brak wyliczania dat
|
|
expect(tx.planDay.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('z startDate: aktualizuje strukturę i aktywuje plan (daty wyliczone)', async () => {
|
|
const { prisma, tx } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
|
prisma.exercise.findMany.mockResolvedValue([]);
|
|
prisma.planWeek.findMany.mockResolvedValue([
|
|
{ index: 0, days: [{ id: 'd1', dayOfWeek: 1 }] },
|
|
]);
|
|
tx.trainingPlan.update.mockResolvedValue({});
|
|
tx.planDay.update.mockResolvedValue({});
|
|
|
|
const start = mondayIso(1);
|
|
const [y, m, d] = start.split('-').map(Number);
|
|
await updatePlan(prisma, 'p1', 'u1', {
|
|
title: 't',
|
|
weeksCount: 1,
|
|
weeks: [],
|
|
startDate: start,
|
|
});
|
|
// aktywacja: daty dni + status ACTIVE
|
|
expect(tx.planDay.update).toHaveBeenCalledWith({
|
|
where: { id: 'd1' },
|
|
data: { date: new Date(y, m - 1, d) },
|
|
});
|
|
expect(tx.trainingPlan.update).toHaveBeenCalledWith({
|
|
where: { id: 'p1' },
|
|
data: { status: 'ACTIVE', startDate: new Date(y, m - 1, d) },
|
|
});
|
|
});
|
|
|
|
it('z startDate nie-poniedziałkiem: 400 (fail-fast, przed aktualizacją)', async () => {
|
|
const { prisma, tx } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
|
prisma.exercise.findMany.mockResolvedValue([]);
|
|
// 2026-08-25 to wtorek
|
|
await expect(
|
|
updatePlan(prisma, 'p1', 'u1', {
|
|
title: 't',
|
|
weeksCount: 1,
|
|
weeks: [],
|
|
startDate: '2026-08-25',
|
|
}),
|
|
).rejects.toMatchObject({ status: 400, message: 'Start date must be a Monday' });
|
|
// struktura nie została dotknięta
|
|
expect(tx.trainingPlan.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('z startDate w minionym tygodniu: 400', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
|
prisma.exercise.findMany.mockResolvedValue([]);
|
|
await expect(
|
|
updatePlan(prisma, 'p1', 'u1', {
|
|
title: 't',
|
|
weeksCount: 1,
|
|
weeks: [],
|
|
startDate: mondayIso(-1),
|
|
}),
|
|
).rejects.toMatchObject({ status: 400, message: 'Start date cannot be in the past' });
|
|
});
|
|
|
|
it('z startDate gdy właściciel ma już aktywny plan: struktura zapisana, plan DRAFT (activationSkipped)', async () => {
|
|
const { prisma, tx } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(draftPlan);
|
|
prisma.exercise.findMany.mockResolvedValue([]);
|
|
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' });
|
|
const result = await updatePlan(prisma, 'p1', 'u1', {
|
|
title: 't',
|
|
weeksCount: 1,
|
|
weeks: [],
|
|
startDate: mondayIso(1),
|
|
});
|
|
expect(result).toEqual({ ok: true, activationSkipped: true });
|
|
// Struktura zaktualizowana, ale plan nie został aktywowany (brak dat dni).
|
|
expect(tx.trainingPlan.update).toHaveBeenCalledWith({
|
|
where: { id: 'p1' },
|
|
data: expect.objectContaining({ title: 't' }),
|
|
});
|
|
expect(tx.planDay.update).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('sharePlan', () => {
|
|
const ownerPlan = { ...draftPlan, userId: 'u1' };
|
|
|
|
it('właściciel udostępnia plan podopiecznemu (ACTIVE coaching)', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(ownerPlan);
|
|
// u1 = trener, trainee-1 = podopieczny (właściciel → assertPlanAccess bez coachingu)
|
|
prisma.coaching.findUnique.mockResolvedValue({ status: 'ACTIVE' });
|
|
prisma.planShare.create.mockResolvedValue({ id: 'share-1' });
|
|
const result = await sharePlan(prisma, 'p1', 'u1', 'trainee-1');
|
|
expect(prisma.planShare.create).toHaveBeenCalledWith({
|
|
data: { planId: 'p1', sharedById: 'u1', sharedWithId: 'trainee-1' },
|
|
});
|
|
expect(result).toEqual({ share: { id: 'share-1' } });
|
|
});
|
|
|
|
it('właściciel udostępnia plan swojemu trenerowi (odwrotny kierunek coachingu)', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(ownerPlan);
|
|
prisma.coaching.findUnique
|
|
.mockResolvedValueOnce(null) // u1 jako trener? nie…
|
|
.mockResolvedValueOnce({ status: 'ACTIVE' }); // …ale trainer-1 jest trenerem u1
|
|
prisma.planShare.create.mockResolvedValue({ id: 'share-2' });
|
|
await sharePlan(prisma, 'p1', 'u1', 'trainer-1');
|
|
expect(prisma.planShare.create).toHaveBeenCalledWith({
|
|
data: { planId: 'p1', sharedById: 'u1', sharedWithId: 'trainer-1' },
|
|
});
|
|
});
|
|
|
|
it('nie-właściciel (trener): 403', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, userId: 'trainee-1' });
|
|
prisma.coaching.findUnique.mockResolvedValue({ status: 'ACTIVE' });
|
|
await expect(sharePlan(prisma, 'p1', 'trainer-1', 'u1')).rejects.toMatchObject({
|
|
status: 403,
|
|
message: 'Only the plan owner can share the plan',
|
|
});
|
|
});
|
|
|
|
it('udostępnienie samemu sobie: 400', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(ownerPlan);
|
|
await expect(sharePlan(prisma, 'p1', 'u1', 'u1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'Cannot share a plan with yourself',
|
|
});
|
|
});
|
|
|
|
it('brak aktywnego coachingu: 400', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(ownerPlan);
|
|
prisma.coaching.findUnique.mockResolvedValue(null);
|
|
await expect(sharePlan(prisma, 'p1', 'u1', 'stranger')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'No active coaching relation with this user',
|
|
});
|
|
});
|
|
|
|
it('PENDING coaching: 400', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(ownerPlan);
|
|
prisma.coaching.findUnique.mockResolvedValue({ status: 'PENDING' });
|
|
await expect(sharePlan(prisma, 'p1', 'u1', 'trainee-1')).rejects.toMatchObject({
|
|
status: 400,
|
|
message: 'No active coaching relation with this user',
|
|
});
|
|
});
|
|
|
|
it('duplikat: 409', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue(ownerPlan);
|
|
prisma.coaching.findUnique.mockResolvedValue({ status: 'ACTIVE' });
|
|
prisma.planShare.create.mockRejectedValue(
|
|
Object.assign(new Error('Unique constraint failed'), { code: 'P2002' }),
|
|
);
|
|
await expect(sharePlan(prisma, 'p1', 'u1', 'trainee-1')).rejects.toMatchObject({
|
|
status: 409,
|
|
message: 'Plan is already shared with this user',
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('unsharePlan', () => {
|
|
it('właściciel cofa udostępnienie', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, userId: 'u1' });
|
|
prisma.planShare.deleteMany.mockResolvedValue({ count: 1 });
|
|
const result = await unsharePlan(prisma, 'p1', 'u1', 'trainee-1');
|
|
expect(prisma.planShare.deleteMany).toHaveBeenCalledWith({
|
|
where: { planId: 'p1', sharedWithId: 'trainee-1' },
|
|
});
|
|
expect(result).toEqual({ ok: true });
|
|
});
|
|
|
|
it('nie-właściciel: 403', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, userId: 'trainee-1' });
|
|
prisma.coaching.findUnique.mockResolvedValue({ status: 'ACTIVE' });
|
|
await expect(unsharePlan(prisma, 'p1', 'trainer-1', 'u1')).rejects.toMatchObject({
|
|
status: 403,
|
|
});
|
|
});
|
|
|
|
it('brak share\'a: 404', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.trainingPlan.findUnique.mockResolvedValue({ ...draftPlan, userId: 'u1' });
|
|
prisma.planShare.deleteMany.mockResolvedValue({ count: 0 });
|
|
await expect(unsharePlan(prisma, 'p1', 'u1', 'trainee-1')).rejects.toMatchObject({
|
|
status: 404,
|
|
message: 'Plan is not shared with this user',
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('listSharedWithMe', () => {
|
|
it('zwraca udostępnione plany z imieniem udostępniającego i postępem', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.planShare.findMany.mockResolvedValue([
|
|
{
|
|
sharedBy: { id: 'trainer-1', firstName: 'Marek', lastName: 'Trener' },
|
|
plan: {
|
|
id: 'p1',
|
|
title: 'Plan trenera',
|
|
status: 'ACTIVE',
|
|
weeksCount: 4,
|
|
startDate: null,
|
|
gym: null,
|
|
weeks: [{ days: [{ status: 'COMPLETED' }, { status: 'PLANNED' }] }],
|
|
},
|
|
},
|
|
]);
|
|
const result = await listSharedWithMe(prisma, 'u1');
|
|
expect(result).toEqual({
|
|
plans: [
|
|
{
|
|
id: 'p1',
|
|
title: 'Plan trenera',
|
|
status: 'ACTIVE',
|
|
weeksCount: 4,
|
|
startDate: null,
|
|
gym: null,
|
|
sharedBy: { id: 'trainer-1', firstName: 'Marek', lastName: 'Trener' },
|
|
progress: { completed: 1, total: 2, percent: 50 },
|
|
},
|
|
],
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('getMyTrainers', () => {
|
|
it('zwraca trenerów z AKTYWNYM coachingiem (ja = podopieczny)', async () => {
|
|
const { prisma } = mockPrisma();
|
|
prisma.coaching.findMany.mockResolvedValue([
|
|
{ trainer: { id: 'trainer-1', firstName: 'Marek', lastName: 'Trener', nickname: null } },
|
|
]);
|
|
const result = await getMyTrainers(prisma, 'u1');
|
|
expect(prisma.coaching.findMany).toHaveBeenCalledWith({
|
|
where: { userId: 'u1', status: 'ACTIVE' },
|
|
include: {
|
|
trainer: { select: { id: true, firstName: true, lastName: true, nickname: true } },
|
|
},
|
|
orderBy: { createdAt: 'asc' },
|
|
});
|
|
expect(result).toEqual({
|
|
trainers: [{ id: 'trainer-1', firstName: 'Marek', lastName: 'Trener', nickname: null }],
|
|
});
|
|
});
|
|
});
|