import { herdStatusIn } from './herd-status'; const NOW = new Date('2027-03-01T10:00:00.000Z'); function settings() { return { id: 'singleton', noticeOnColonyCountChange: true, noticeOnApiaryChange: true, tskExcludedColonyTypes: [], }; } function dbWith(baseline: unknown) { return { complianceSettings: { findUnique: jest.fn().mockResolvedValue(settings()), upsert: jest.fn().mockResolvedValue(settings()), }, authorityNotice: { findFirst: jest.fn().mockResolvedValue(baseline) }, apiary: { findMany: jest.fn().mockResolvedValue([ { id: 'a', name: 'A', address: null, location: null, latitude: null, longitude: null, colonies: [{ colonyType: 'PRODUCTION_COLONY' }], }, ]), }, }; } describe('herdStatusIn', () => { it('no baseline means deviation null, not a herd change', async () => { const status = await herdStatusIn(dbWith(null) as never, NOW); expect(status.baseline).toBeNull(); expect(status.deviation).toBeNull(); expect(status.current.totalLiving).toBe(1); }); it('the baseline is the newest SENT registration or herd change by sentOn and ignores TSK reports', async () => { const db = dbWith(null); await herdStatusIn(db as never, NOW); expect(db.authorityNotice.findFirst).toHaveBeenCalledWith( expect.objectContaining({ where: { status: 'SENT', kind: { in: ['REGISTRATION', 'HERD_CHANGE'] }, }, orderBy: [{ sentOn: 'desc' }, { createdAt: 'desc' }, { id: 'desc' }], }), ); }); it('reports the deviation against the baseline', async () => { const baseline = { id: 'n1', totalColonies: 3, snapshot: { version: 1, takenAt: NOW.toISOString(), apiaries: [], totalLiving: 3, }, }; const status = await herdStatusIn(dbWith(baseline) as never, NOW); expect(status.deviation).toMatchObject({ triggered: true, colonyCountChanged: true, apiariesAdded: ['a'], }); }); });