import { CalendarService } from './calendar.service'; import { PrismaService } from '../prisma/prisma.service'; describe('CalendarService', () => { let service: CalendarService; let prisma: { task: { findMany: jest.Mock }; inspection: { findMany: jest.Mock }; treatment: { findMany: jest.Mock }; feeding: { findMany: jest.Mock }; harvest: { findMany: jest.Mock }; queenColonyHistory: { findMany: jest.Mock }; }; beforeEach(() => { prisma = { task: { findMany: jest.fn().mockResolvedValue([]) }, inspection: { findMany: jest.fn().mockResolvedValue([]) }, treatment: { findMany: jest.fn().mockResolvedValue([]) }, feeding: { findMany: jest.fn().mockResolvedValue([]) }, harvest: { findMany: jest.fn().mockResolvedValue([]) }, queenColonyHistory: { findMany: jest.fn().mockResolvedValue([]) }, }; service = new CalendarService(prisma as unknown as PrismaService); }); it('merges tasks, inspections, and harvests into one sorted event list', async () => { prisma.task.findMany.mockResolvedValueOnce([ { id: 't1', title: 'Kontrolle', dueDate: new Date('2026-06-05') }, ]); prisma.inspection.findMany.mockResolvedValueOnce([ { id: 'i1', date: new Date('2026-06-01'), colonyId: 'c1', colony: { code: 'V-001' }, }, ]); prisma.harvest.findMany.mockResolvedValueOnce([ { id: 'h1', date: new Date('2026-06-10'), apiaryId: 'a1' }, ]); const events = await service.getEvents({ from: new Date('2026-06-01'), to: new Date('2026-06-30'), }); expect(events.map((event) => event.type)).toEqual([ 'INSPECTION', 'TASK', 'HARVEST', ]); }); it('filters colony-linked events by apiary when apiaryId is given', async () => { await service.getEvents({ from: new Date('2026-06-01'), to: new Date('2026-06-30'), apiaryId: 'a1', }); const [[inspectionArg]] = prisma.inspection.findMany.mock.calls as [ [{ where: { colony?: { apiaryId?: string } } }], ]; expect(inspectionArg.where.colony).toEqual({ apiaryId: 'a1' }); const [[harvestArg]] = prisma.harvest.findMany.mock.calls as [ [{ where: { apiaryId?: string } }], ]; expect(harvestArg.where.apiaryId).toBe('a1'); }); it('excludes tasks without a due date', async () => { prisma.task.findMany.mockResolvedValueOnce([ { id: 't1', title: 'Ohne Datum', dueDate: null }, ]); const events = await service.getEvents({ from: new Date('2026-06-01'), to: new Date('2026-06-30'), }); expect(events).toHaveLength(0); }); });