// 3.2.0: the heartbeat's `stats.live` (what's on now), which the Hub reads before an emergency // update takes the site down. A session on now, or a one-day event without sessions, counts; a // multi-week course with no session today doesn't. const NOW = new Date('2026-11-22T09:00:00.000Z'); const H = 3600e3; const mockSessions = []; const mockEvents = []; let mockCheckIns = 0; jest.mock('../src/config/db', () => ({ eventSession: { findMany: async ({ where }) => mockSessions.filter(s => s.cancelledAt === null && s.startsAt <= where.startsAt.lte && s.endsAt >= where.endsAt.gte).map(s => ({ eventId: s.eventId })), }, event: { findMany: async ({ where }) => mockEvents.filter(e => !e.hasSessions && e.startDate <= where.startDate.lte && e.endDate >= where.endDate.gte), }, ticketUsage: { count: async () => mockCheckIns }, })); const { liveNow } = require('../src/hub/report'); beforeEach(() => { mockSessions.length = 0; mockEvents.length = 0; mockCheckIns = 0; }); test('a session on now and a one-day event on now count; a long course and a later session don\'t', async () => { mockSessions.push({ eventId: 'course', startsAt: new Date(NOW.getTime() - H), endsAt: new Date(NOW.getTime() + H), cancelledAt: null }); mockSessions.push({ eventId: 'later', startsAt: new Date(NOW.getTime() + 5 * H), endsAt: new Date(NOW.getTime() + 6 * H), cancelledAt: null }); mockSessions.push({ eventId: 'cancelled', startsAt: new Date(NOW.getTime() - H), endsAt: new Date(NOW.getTime() + H), cancelledAt: new Date() }); mockEvents.push({ id: 'breakfast', startDate: new Date(NOW.getTime() - H), endDate: new Date(NOW.getTime() + 2 * H) }); mockEvents.push({ id: 'term', startDate: new Date(NOW.getTime() - 20 * 24 * H), endDate: new Date(NOW.getTime() + 40 * 24 * H) }); mockCheckIns = 12; expect(await liveNow(NOW)).toEqual({ eventsNow: 2, checkInsLastHour: 12 }); }); test('starting within half an hour counts as on', async () => { mockEvents.push({ id: 'soon', startDate: new Date(NOW.getTime() + 20 * 60e3), endDate: new Date(NOW.getTime() + 3 * H) }); expect((await liveNow(NOW)).eventsNow).toBe(1); });