import { beforeEach, describe, expect, it, mock, vi } from 'bun:test'; import { Hono } from 'hono'; const db = { group: { findMany: mock(), }, payment: { findMany: mock(), count: mock(), }, }; mock.module('@/lib/prisma', () => ({ default: db, getPrismaClient: mock(), UnitOfWork: class {}, })); import prisma from '@/lib/prisma'; import { paymentsRoutes } from '@/modules/payments'; const userId = 'user-1'; const payment = { id: 'payment-1', groupId: 'group-1', attendeeId: 'attendee-1', amount: { toString: () => '150.50' } as { toString(): string }, currency: 'ARS', status: 'PENDING', description: 'Mensualidad de Septiembre', checkoutUrl: 'https://mercadopago.com/checkout/payment-1', dueDate: new Date('2026-09-01T10:00:00.000Z'), paidAt: null, createdAt: new Date('2026-08-01T10:00:00.000Z'), updatedAt: new Date('2026-08-01T10:00:00.000Z'), group: { name: 'Cuadrilla Alfa', price: 150.5 }, attendee: { fullName: 'Juana Pérez' }, }; function makeApp(userValue: unknown) { const app = new Hono(); app.use('*', async (c, next) => { c.set('user', userValue as never); await next(); }); app.route('/payments', paymentsRoutes); return app; } describe('payments routes', () => { beforeEach(() => { vi.clearAllMocks(); }); it('lists payments scoped to the user, with attendee and group names', async () => { prisma.group.findMany.mockResolvedValue([{ id: 'group-1' }]); prisma.payment.findMany.mockResolvedValue([payment]); prisma.payment.count.mockResolvedValue(1); const res = await makeApp({ id: userId }).request('/payments'); expect(res.status).toBe(200); expect(await res.json()).toEqual({ data: [ { id: 'payment-1', groupId: 'group-1', groupName: 'Cuadrilla Alfa', attendeeId: 'attendee-1', attendeeName: 'Juana Pérez', amount: 150.5, currency: 'ARS', status: 'PENDING', description: 'Mensualidad de Septiembre', checkoutUrl: payment.checkoutUrl, dueDate: payment.dueDate.toISOString(), paidAt: null, createdAt: payment.createdAt.toISOString(), updatedAt: payment.updatedAt.toISOString(), }, ], pagination: { page: 1, pageSize: 10, total: 1, totalPages: 1 }, }); expect(prisma.group.findMany).toHaveBeenCalledWith({ where: { OR: [{ createdById: userId }, { members: { some: { userId } } }], }, select: { id: true }, }); expect(prisma.payment.findMany).toHaveBeenCalledWith({ where: { groupId: { in: ['group-1'] } }, include: { group: { select: { name: true, price: true } }, attendee: { select: { fullName: true } }, }, skip: 0, take: 10, orderBy: { createdAt: 'desc' }, }); }); it('filters payments by group and status', async () => { prisma.group.findMany.mockResolvedValue([{ id: 'group-1' }]); prisma.payment.findMany.mockResolvedValue([]); prisma.payment.count.mockResolvedValue(0); const res = await makeApp({ id: userId }).request( '/payments?groupId=group-1&status=OVERDUE', ); expect(res.status).toBe(200); expect(prisma.payment.findMany).toHaveBeenCalledWith( expect.objectContaining({ where: { groupId: 'group-1', status: 'OVERDUE' }, }), ); }); it('rejects requests without a session user', async () => { const res = await makeApp(null).request('/payments'); expect(res.status).toBe(401); }); it('rejects invalid pagination query parameters', async () => { const res = await makeApp({ id: userId }).request('/payments?pageSize=101'); expect(res.status).toBe(400); }); });