import { beforeEach, describe, expect, it, mock, vi } from 'bun:test'; import { Hono } from 'hono'; const db = { group: { findUnique: mock(), }, attendee: { findFirst: mock(), count: mock(), create: mock(), update: mock(), }, }; mock.module('@/lib/prisma', () => ({ default: db, getPrismaClient: mock(), UnitOfWork: class { executeResult = mock(async (cb: (tx: unknown) => Promise) => cb(db)); execute = mock(async (cb: (tx: unknown) => Promise) => cb(db)); }, })); import prisma from '@/lib/prisma'; import { groupsRoutes } from '@/modules/groups'; const teacherId = 'teacher-1'; const group = (overrides: Record = {}) => ({ id: 'group-1', name: 'Taller de Pintura', createdById: teacherId, capacity: 20, ...overrides, }); const targetGroup = (overrides: Record = {}) => group({ id: 'group-2', name: 'Taller de Cerámica', ...overrides }); const person = { id: 'person-1', fullName: 'Martín Gómez', phone: '+5491133445566', email: null, guardianName: null, guardianPhone: null, notes: null, }; const membership = (overrides: Record = {}) => ({ id: 'att-2', groupId: 'group-2', personId: 'person-1', person, notifyToken: 'notify-2', status: 'ACTIVE', createdAt: new Date('2026-11-02T10:00:00.000Z'), updatedAt: new Date('2026-11-02T10:00:00.000Z'), ...overrides, }); function addRequest(targetGroupId: string, userId = teacherId) { const app = new Hono(); app.use('*', async (c, next) => { c.set('user', { id: userId } as never); await next(); }); app.route('/groups', groupsRoutes); return app.request('/groups/group-1/attendees/att-1/add-to-group', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ targetGroupId }), }); } describe('POST /groups/:groupId/attendees/:attendeeId/add-to-group', () => { beforeEach(() => { vi.clearAllMocks(); prisma.group.findUnique.mockReset(); prisma.attendee.findFirst.mockReset(); prisma.group.findUnique .mockResolvedValueOnce(group() as never) .mockResolvedValueOnce(targetGroup() as never); prisma.attendee.count.mockResolvedValue(3); }); it('creates a membership of the same person in the target group', async () => { prisma.attendee.findFirst .mockResolvedValueOnce({ personId: 'person-1' } as never) .mockResolvedValueOnce(null); prisma.attendee.create.mockResolvedValue(membership() as never); const res = await addRequest('group-2'); expect(res.status).toBe(200); expect(await res.json()).toMatchObject({ id: 'att-2', groupId: 'group-2', personId: 'person-1', fullName: 'Martín Gómez', }); expect(prisma.attendee.create).toHaveBeenCalledWith({ data: { groupId: 'group-2', personId: 'person-1' }, include: { person: { select: expect.any(Object) } }, }); // The source membership is not touched. expect(prisma.attendee.update).not.toHaveBeenCalled(); }); it('reactivates a former (DROPPED) membership in the target group', async () => { prisma.attendee.findFirst .mockResolvedValueOnce({ personId: 'person-1' } as never) .mockResolvedValueOnce({ id: 'att-old', status: 'DROPPED' } as never); prisma.attendee.update.mockResolvedValue(membership({ id: 'att-old' }) as never); const res = await addRequest('group-2'); expect(res.status).toBe(200); expect(prisma.attendee.update).toHaveBeenCalledWith({ where: { id: 'att-old' }, data: { status: 'ACTIVE' }, include: { person: { select: expect.any(Object) } }, }); expect(prisma.attendee.create).not.toHaveBeenCalled(); }); it('rejects with 409 when the person is already in the target group', async () => { prisma.attendee.findFirst .mockResolvedValueOnce({ personId: 'person-1' } as never) .mockResolvedValueOnce({ id: 'att-2', status: 'PAUSED' } as never); const res = await addRequest('group-2'); expect(res.status).toBe(409); expect((await res.json()).code).toBe('attendee_already_registered'); expect(prisma.attendee.create).not.toHaveBeenCalled(); }); it('rejects with 409 when the target group reached its capacity', async () => { prisma.group.findUnique.mockReset(); prisma.group.findUnique .mockResolvedValueOnce(group() as never) .mockResolvedValueOnce(targetGroup({ capacity: 3 }) as never); prisma.attendee.findFirst .mockResolvedValueOnce({ personId: 'person-1' } as never) .mockResolvedValueOnce(null); const res = await addRequest('group-2'); expect(res.status).toBe(409); expect((await res.json()).code).toBe('group_capacity_reached'); expect(prisma.attendee.create).not.toHaveBeenCalled(); }); it('rejects adding to the same group', async () => { prisma.attendee.findFirst.mockResolvedValueOnce({ personId: 'person-1' } as never); const res = await addRequest('group-1'); expect(res.status).toBe(409); expect((await res.json()).code).toBe('attendee_same_group'); }); it('rejects a target group owned by another teacher', async () => { prisma.group.findUnique.mockReset(); prisma.group.findUnique .mockResolvedValueOnce(group() as never) .mockResolvedValueOnce(targetGroup({ createdById: 'other-user' }) as never); prisma.attendee.findFirst.mockResolvedValueOnce({ personId: 'person-1' } as never); const res = await addRequest('group-2'); expect(res.status).toBe(403); expect(prisma.attendee.create).not.toHaveBeenCalled(); }); });