396 lines
15 KiB
TypeScript
396 lines
15 KiB
TypeScript
import { describe, it, expect, beforeAll } from 'vitest';
|
|
import { prisma } from '../../src/lib/prisma.js';
|
|
import {
|
|
buildApp,
|
|
createExercise,
|
|
createUserWithToken,
|
|
rand,
|
|
seedPlan,
|
|
seedSession,
|
|
} from '../helpers.js';
|
|
|
|
async function createMembership(userId: string, gymId: string, roles: string[]) {
|
|
return prisma.gymMembership.create({
|
|
data: { userId, gymId, roles: roles as never },
|
|
});
|
|
}
|
|
|
|
describe('messages — trainer↔trainee conversations', () => {
|
|
let app: ReturnType<typeof buildApp>;
|
|
let trainer: { id: string; token: string };
|
|
let trainee: { id: string; token: string };
|
|
let plain: { id: string; token: string };
|
|
let gymId: string;
|
|
let coachingId: string;
|
|
let completedSessionId: string;
|
|
let completedExerciseId: string;
|
|
let inProgressSessionId: string;
|
|
let foreignSessionId: string;
|
|
let foreignExerciseId: string;
|
|
|
|
beforeAll(async () => {
|
|
app = buildApp();
|
|
const t = await createUserWithToken({ firstName: 'Tomasz' });
|
|
const tr = await createUserWithToken({ firstName: 'Anna' });
|
|
const p = await createUserWithToken({ firstName: 'Piotr' });
|
|
const gym = await prisma.gym.create({ data: { name: `Msg Gym ${rand()}` } });
|
|
gymId = gym.id;
|
|
await createMembership(t.user.id, gymId, ['TRAINER']);
|
|
await createMembership(tr.user.id, gymId, ['MEMBER']);
|
|
await createMembership(p.user.id, gymId, ['MEMBER']);
|
|
trainer = { id: t.user.id, token: t.token };
|
|
trainee = { id: tr.user.id, token: tr.token };
|
|
plain = { id: p.user.id, token: p.token };
|
|
await prisma.user.update({ where: { id: trainee.id }, data: { nickname: 'annafit' } });
|
|
|
|
// ACTIVE relation: trainer invites, trainee accepts.
|
|
const invite = await app
|
|
.post('/api/v1/trainer/invitations')
|
|
.set('Authorization', `Bearer ${trainer.token}`)
|
|
.send({ userId: trainee.id });
|
|
coachingId = invite.body.coaching.id;
|
|
const accept = await app
|
|
.post(`/api/v1/trainer/invitations/${coachingId}/accept`)
|
|
.set('Authorization', `Bearer ${trainee.token}`);
|
|
expect(accept.status).toBe(200);
|
|
|
|
// Trainee sessions: one COMPLETED (with a completed exercise), one IN_PROGRESS.
|
|
const exercise = await createExercise();
|
|
const plan = await seedPlan({
|
|
userId: trainee.id,
|
|
weeks: [
|
|
{
|
|
index: 0,
|
|
days: [
|
|
{
|
|
dayOfWeek: 1,
|
|
exercises: [
|
|
{ exerciseId: exercise.id, order: 0, sets: [{ index: 0, reps: 10, loadKg: 60 }] },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|
|
const day = plan.weeks[0].days[0];
|
|
const done = await seedSession(day.id, {
|
|
status: 'COMPLETED',
|
|
exercises: [
|
|
{
|
|
exerciseId: exercise.id,
|
|
planExerciseId: day.exercises[0].id,
|
|
order: 0,
|
|
sets: [{ planSetId: day.exercises[0].sets[0].id, reps: 10, rir: 2 }],
|
|
},
|
|
],
|
|
});
|
|
completedSessionId = done.id;
|
|
completedExerciseId = done.exercises[0].id;
|
|
const ongoing = await seedSession(day.id, { status: 'IN_PROGRESS' });
|
|
inProgressSessionId = ongoing.id;
|
|
|
|
// A completed session of another user (plain).
|
|
const pExercise = await createExercise();
|
|
const pPlan = await seedPlan({
|
|
userId: plain.id,
|
|
weeks: [
|
|
{
|
|
index: 0,
|
|
days: [
|
|
{
|
|
dayOfWeek: 2,
|
|
exercises: [{ exerciseId: pExercise.id, order: 0, sets: [{ index: 0, reps: 5, loadKg: 20 }] }],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|
|
const pDay = pPlan.weeks[0].days[0];
|
|
const pDone = await seedSession(pDay.id, {
|
|
status: 'COMPLETED',
|
|
exercises: [
|
|
{
|
|
exerciseId: pExercise.id,
|
|
planExerciseId: pDay.exercises[0].id,
|
|
order: 0,
|
|
sets: [{ planSetId: pDay.exercises[0].sets[0].id, reps: 5 }],
|
|
},
|
|
],
|
|
});
|
|
foreignSessionId = pDone.id;
|
|
foreignExerciseId = pDone.exercises[0].id;
|
|
});
|
|
|
|
describe('GET /messages/conversations', () => {
|
|
it('lists the ACTIVE relation for both parties with unread counts', async () => {
|
|
// trainee writes first
|
|
const sent = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainee.token}`)
|
|
.send({ body: 'Cześć trenerze!' });
|
|
expect(sent.status).toBe(201);
|
|
|
|
const asTrainer = await app
|
|
.get('/api/v1/messages/conversations')
|
|
.set('Authorization', `Bearer ${trainer.token}`);
|
|
expect(asTrainer.status).toBe(200);
|
|
expect(asTrainer.body.conversations).toHaveLength(1);
|
|
expect(asTrainer.body.conversations[0]).toMatchObject({
|
|
coachingId,
|
|
role: 'trainer',
|
|
other: { id: trainee.id, firstName: 'Anna', nickname: 'annafit' },
|
|
unreadCount: 1,
|
|
});
|
|
expect(asTrainer.body.conversations[0].lastMessage).toMatchObject({
|
|
body: 'Cześć trenerze!',
|
|
senderId: trainee.id,
|
|
});
|
|
|
|
const asTrainee = await app
|
|
.get('/api/v1/messages/conversations')
|
|
.set('Authorization', `Bearer ${trainee.token}`);
|
|
expect(asTrainee.body.conversations).toHaveLength(1);
|
|
expect(asTrainee.body.conversations[0]).toMatchObject({
|
|
coachingId,
|
|
role: 'trainee',
|
|
other: { id: trainer.id, firstName: 'Tomasz', nickname: null },
|
|
unreadCount: 0,
|
|
});
|
|
});
|
|
|
|
it('does not list the relation for a stranger', async () => {
|
|
const res = await app
|
|
.get('/api/v1/messages/conversations')
|
|
.set('Authorization', `Bearer ${plain.token}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.conversations).toEqual([]);
|
|
});
|
|
|
|
it('returns 401 without a token', async () => {
|
|
const res = await app.get('/api/v1/messages/conversations');
|
|
expect(res.status).toBe(401);
|
|
});
|
|
});
|
|
|
|
describe('POST /messages/conversations/:coachingId', () => {
|
|
it('sends a plain message', async () => {
|
|
const res = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`)
|
|
.send({ body: 'Widzę, że trenujesz!' });
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.message).toMatchObject({ body: 'Widzę, że trenujesz!', senderId: trainer.id });
|
|
});
|
|
|
|
it('attaches a completed session of the trainee', async () => {
|
|
const res = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`)
|
|
.send({ body: 'Sesja w załączniku', sessionId: completedSessionId });
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.message).toMatchObject({ sessionId: completedSessionId, sessionExerciseId: null });
|
|
});
|
|
|
|
it('attaches a completed session + specific exercise', async () => {
|
|
const res = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`)
|
|
.send({ body: 'Konkretne ćwiczenie', sessionId: completedSessionId, sessionExerciseId: completedExerciseId });
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.message).toMatchObject({
|
|
sessionId: completedSessionId,
|
|
sessionExerciseId: completedExerciseId,
|
|
});
|
|
});
|
|
|
|
it('rejects an empty body with 400', async () => {
|
|
const res = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`)
|
|
.send({ body: '' });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects a body over 1000 chars with 400', async () => {
|
|
const res = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`)
|
|
.send({ body: 'x'.repeat(1001) });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects an IN_PROGRESS session with 400', async () => {
|
|
const res = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`)
|
|
.send({ body: 'w trakcie', sessionId: inProgressSessionId });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects a session of another user with 400', async () => {
|
|
const res = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`)
|
|
.send({ body: 'obca sesja', sessionId: foreignSessionId });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects an exercise from a different session with 400', async () => {
|
|
const res = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`)
|
|
.send({ body: 'obce ćwiczenie', sessionId: completedSessionId, sessionExerciseId: foreignExerciseId });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects sessionExerciseId without sessionId with 400', async () => {
|
|
const res = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`)
|
|
.send({ body: 'bez sesji', sessionExerciseId: completedExerciseId });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('returns 404 for a user outside the relation', async () => {
|
|
const res = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${plain.token}`)
|
|
.send({ body: 'nie jestem stroną' });
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it('returns 404 for a non-existent relation', async () => {
|
|
const res = await app
|
|
.post('/api/v1/messages/conversations/nope')
|
|
.set('Authorization', `Bearer ${trainer.token}`)
|
|
.send({ body: 'nie ma relacji' });
|
|
expect(res.status).toBe(404);
|
|
});
|
|
});
|
|
|
|
describe('GET /messages/conversations/:coachingId (thread)', () => {
|
|
it('returns messages ascending with role, other and computed attachments', async () => {
|
|
const res = await app
|
|
.get(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.role).toBe('trainer');
|
|
expect(res.body.other).toMatchObject({ id: trainee.id, nickname: 'annafit' });
|
|
const messages = res.body.messages;
|
|
expect(messages.length).toBeGreaterThanOrEqual(4);
|
|
// ascending
|
|
for (let i = 1; i < messages.length; i++) {
|
|
expect(messages[i].createdAt >= messages[i - 1].createdAt).toBe(true);
|
|
}
|
|
const withExercise = messages.find(
|
|
(m: { attachment: { exercise: unknown } | null }) => m.attachment?.exercise,
|
|
);
|
|
expect(withExercise).toBeTruthy();
|
|
expect(withExercise.attachment).toMatchObject({
|
|
sessionId: completedSessionId,
|
|
plan: expect.any(String),
|
|
});
|
|
expect(withExercise.attachment.exercise.sets).toEqual([
|
|
expect.objectContaining({ index: 0, reps: 10, rir: 2, planned: expect.anything() }),
|
|
]);
|
|
const withSession = messages.find(
|
|
(m: { attachment: { exercise: unknown; exercises: unknown[] } | null }) =>
|
|
m.attachment && !m.attachment.exercise,
|
|
);
|
|
expect(withSession).toBeTruthy();
|
|
expect(withSession.attachment.exercises).toHaveLength(1);
|
|
expect(withSession.attachment.exercises[0]).toMatchObject({ setsDone: 1, setsTotal: 1 });
|
|
const plainMsg = messages.find((m: { attachment: unknown }) => !m.attachment);
|
|
expect(plainMsg).toBeTruthy();
|
|
});
|
|
|
|
it('returns role trainee for the trainee side', async () => {
|
|
const res = await app
|
|
.get(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainee.token}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.role).toBe('trainee');
|
|
expect(res.body.other).toMatchObject({ id: trainer.id });
|
|
});
|
|
|
|
it('reading the thread clears the unread count', async () => {
|
|
// trainee sends a new message
|
|
await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainee.token}`)
|
|
.send({ body: 'nowa wiadomość' });
|
|
let list = await app
|
|
.get('/api/v1/messages/conversations')
|
|
.set('Authorization', `Bearer ${trainer.token}`);
|
|
expect(list.body.conversations[0].unreadCount).toBeGreaterThanOrEqual(1);
|
|
|
|
// trainer reads the thread
|
|
const thread = await app
|
|
.get(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`);
|
|
expect(thread.status).toBe(200);
|
|
|
|
list = await app
|
|
.get('/api/v1/messages/conversations')
|
|
.set('Authorization', `Bearer ${trainer.token}`);
|
|
expect(list.body.conversations[0].unreadCount).toBe(0);
|
|
});
|
|
|
|
it('returns 404 for a stranger', async () => {
|
|
const res = await app
|
|
.get(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${plain.token}`);
|
|
expect(res.status).toBe(404);
|
|
});
|
|
});
|
|
|
|
describe('DELETE /messages/:id', () => {
|
|
it('deletes the author message with 204', async () => {
|
|
const created = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`)
|
|
.send({ body: 'do usunięcia' });
|
|
const res = await app
|
|
.delete(`/api/v1/messages/${created.body.message.id}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`);
|
|
expect(res.status).toBe(204);
|
|
});
|
|
|
|
it('rejects deletion by a non-author with 404', async () => {
|
|
const created = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainee.token}`)
|
|
.send({ body: 'moja wiadomość' });
|
|
const res = await app
|
|
.delete(`/api/v1/messages/${created.body.message.id}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`);
|
|
expect(res.status).toBe(404);
|
|
});
|
|
});
|
|
|
|
describe('ARCHIVED relation', () => {
|
|
it('hides the conversation and returns 404 on the thread', async () => {
|
|
const archive = await app
|
|
.delete(`/api/v1/trainer/trainees/${trainee.id}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`);
|
|
expect(archive.status).toBe(204);
|
|
|
|
const list = await app
|
|
.get('/api/v1/messages/conversations')
|
|
.set('Authorization', `Bearer ${trainer.token}`);
|
|
expect(list.body.conversations).toEqual([]);
|
|
|
|
const thread = await app
|
|
.get(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainer.token}`);
|
|
expect(thread.status).toBe(404);
|
|
|
|
const send = await app
|
|
.post(`/api/v1/messages/conversations/${coachingId}`)
|
|
.set('Authorization', `Bearer ${trainee.token}`)
|
|
.send({ body: 'po archiwizacji' });
|
|
expect(send.status).toBe(404);
|
|
});
|
|
});
|
|
});
|