import { NotFoundException } from '@nestjs/common'; import { SwarmControlService } from './swarm-control.service'; import { PrismaService } from '../prisma/prisma.service'; import { AuditLogService } from '../audit/audit-log.service'; import { AutomationService } from '../automation/automation.service'; describe('SwarmControlService', () => { let service: SwarmControlService; let prisma: { swarmControlCheck: { findMany: jest.Mock; findUnique: jest.Mock; create: jest.Mock; update: jest.Mock; }; }; let auditLog: { record: jest.Mock }; const check = { id: 'check-1', colonyId: 'colony-1', date: new Date('2026-05-01T00:00:00.000Z'), }; beforeEach(() => { prisma = { swarmControlCheck: { findMany: jest.fn(), findUnique: jest.fn(), create: jest.fn(), update: jest.fn(), }, }; auditLog = { record: jest.fn() }; const automation = { evaluateSwarmControlCheck: jest.fn().mockResolvedValue(undefined), }; service = new SwarmControlService( prisma as unknown as PrismaService, auditLog as unknown as AuditLogService, automation as unknown as AutomationService, ); }); it('findById throws NotFoundException when missing', async () => { prisma.swarmControlCheck.findUnique.mockResolvedValue(null); await expect(service.findById('missing')).rejects.toBeInstanceOf( NotFoundException, ); }); it('create stores the acting user and converts date fields', async () => { prisma.swarmControlCheck.create.mockResolvedValue(check); await service.create( { colonyId: 'colony-1', date: '2026-05-01', nextCheckDate: '2026-05-08', }, 'user-1', ); const [[createArg]] = prisma.swarmControlCheck.create.mock.calls as [ [ { data: { colonyId: string; userId: string; date: Date; nextCheckDate: Date; }; }, ], ]; expect(createArg.data.colonyId).toBe('colony-1'); expect(createArg.data.userId).toBe('user-1'); expect(createArg.data.date).toBeInstanceOf(Date); expect(createArg.data.nextCheckDate).toBeInstanceOf(Date); }); it('update writes an audit log entry', async () => { prisma.swarmControlCheck.findUnique.mockResolvedValue(check); const updated = { ...check, queenCellsFound: 3, queenCellsCapped: true }; prisma.swarmControlCheck.update.mockResolvedValue(updated); const result = await service.update( 'check-1', { queenCellsFound: 3, queenCellsCapped: true }, 'user-1', ); expect(result).toBe(updated); expect(auditLog.record).toHaveBeenCalledWith( expect.objectContaining({ entityType: 'SwarmControlCheck', entityId: 'check-1', action: 'UPDATE', beforeData: check, afterData: updated, }), ); }); it('update clears the next check date on null', async () => { prisma.swarmControlCheck.findUnique.mockResolvedValue({ ...check, nextCheckDate: new Date('2026-05-08T00:00:00.000Z'), }); prisma.swarmControlCheck.update.mockResolvedValue(check); await service.update('check-1', { nextCheckDate: null as never }, 'user-1'); const [[updateArg]] = prisma.swarmControlCheck.update.mock.calls as [ [{ data: { nextCheckDate?: Date | null } }], ]; expect(updateArg.data.nextCheckDate).toBeNull(); expect(auditLog.record).toHaveBeenCalledWith( expect.objectContaining({ action: 'UPDATE', entityId: 'check-1' }), ); }); it('findByColony orders results chronologically', async () => { prisma.swarmControlCheck.findMany.mockResolvedValue([check]); await service.findByColony('colony-1'); const [[queryArg]] = prisma.swarmControlCheck.findMany.mock.calls as [ [{ where: { colonyId: string }; orderBy: { date: string } }], ]; expect(queryArg.where.colonyId).toBe('colony-1'); expect(queryArg.orderBy.date).toBe('asc'); }); });