const mockDb = { event: { findMany: jest.fn(), updateMany: jest.fn() }, seriesFollow: { findMany: jest.fn() }, }; jest.mock('../src/config/db', () => mockDb); const mockFilter = jest.fn(); jest.mock('../src/utils/privacy', () => ({ filterRecipients: (...a) => mockFilter(...a), bulkEmailFooter: () => ({ html: '

unsubscribe

', text: '\nunsubscribe', headers: { 'List-Unsubscribe': '' } }), })); const mockSend = jest.fn(); jest.mock('../src/utils/email', () => ({ sendMail: (...a) => mockSend(...a), emailWrapper: body => `${body}`, ctaButton: (label, url) => `${label}`, })); const series = require('../src/utils/series'); const { isBlocked } = require('../src/hub/readOnly'); describe('event series', () => { test('a new series needs a name; the web address comes from it', () => { expect(() => series.cleanSeriesInput({ name: ' ' })).toThrow('name'); expect(series.cleanSeriesInput({ name: "Men's Breakfast", description: ' Monthly ' })).toEqual({ name: "Men's Breakfast", slug: 'men-s-breakfast', description: 'Monthly' }); expect(series.cleanSeriesInput({ name: 'Camp', slug: 'Family Camp!' }).slug).toBe('family-camp'); }); test('changing a series only touches what was sent', () => { expect(series.cleanSeriesInput({ description: '' }, { name: 'Camp' })).toEqual({ description: null }); }); test('upcoming soonest first, past latest first', () => { const now = new Date('2026-06-01T00:00:00Z'); const ev = (id, start, end) => ({ id, startDate: new Date(start), endDate: new Date(end) }); const { upcoming, past } = series.splitEvents([ ev('2024', '2024-03-01', '2024-03-04'), ev('2027', '2027-03-01', '2027-03-04'), ev('2025', '2025-03-01', '2025-03-04'), ev('2026', '2026-05-30', '2026-06-02'), ], now); expect(upcoming.map(e => e.id)).toEqual(['2026', '2027']); expect(past.map(e => e.id)).toEqual(['2025', '2024']); }); test('creating and changing series is blocked in licence read-only mode; following is not', () => { expect(isBlocked('POST', '/api/v2/series')).toBe(true); expect(isBlocked('PATCH', '/api/v2/series/abc')).toBe(true); expect(isBlocked('PUT', '/api/v2/series/abc/follow')).toBe(false); }); describe('year-on-year comparison', () => { test('each event against the one before it', () => { const rows = series.withChanges([ { registrations: 40, tickets: 50, income: 10000, checkedIn: 45 }, { registrations: 50, tickets: 50, income: 9000, checkedIn: 0 }, { registrations: 25, tickets: 60, income: 9000, checkedIn: 30 }, ]); expect(rows[0].change).toBeNull(); expect(rows[1].change).toEqual({ registrations: 25, tickets: 0, income: -10, checkedIn: -100 }); // Nothing to compare with after a zero. expect(rows[2].change).toEqual({ registrations: -50, tickets: 20, income: 0, checkedIn: null }); }); test('counts per event, check-ins averaged per session, only events in scope', async () => { const db = { eventSeries: { findUnique: jest.fn().mockResolvedValue({ id: 's1', name: 'Family Camp', slug: 'family-camp' }) }, event: { findMany: jest.fn().mockResolvedValue([ { id: 'e25', title: 'Camp 2025', startDate: new Date('2025-03-01'), endDate: new Date('2025-03-04'), scheduleType: 'single', isActive: true, campus: null, _count: { sessions: 0 } }, { id: 'e26', title: 'Camp 2026', startDate: new Date('2026-03-01'), endDate: new Date('2026-03-04'), scheduleType: 'sessions', isActive: true, campus: null, _count: { sessions: 3 } }, ]), }, registration: { groupBy: jest.fn().mockResolvedValue([{ eventId: 'e25', _count: { _all: 40 } }, { eventId: 'e26', _count: { _all: 50 } }]) }, ticket: { groupBy: jest.fn().mockResolvedValue([{ eventId: 'e25', _sum: { quantity: 50 } }, { eventId: 'e26', _sum: { quantity: 60 } }]) }, payment: { groupBy: jest.fn().mockResolvedValue([{ eventId: 'e25', _sum: { amount: 10000 } }, { eventId: 'e26', _sum: { amount: 12000.456 } }]) }, ticketUsage: { aggregate: jest.fn(({ where }) => Promise.resolve({ _sum: { quantityRedeemed: where.ticket.eventId === 'e25' ? 45 : 150 } })) }, }; const scopeWhere = { OR: [{ campusId: { in: ['north'] } }, { campusId: null }] }; const out = await series.compareSeries('s1', { scopeWhere }, db); expect(db.event.findMany.mock.calls[0][0].where).toEqual({ AND: [{ seriesId: 's1' }, scopeWhere] }); expect(out.events.map(e => [e.title, e.registrations, e.tickets, e.income, e.checkedIn, e.attendanceRate])).toEqual([ ['Camp 2025', 40, 50, 10000, 45, 90], ['Camp 2026', 50, 60, 12000.46, 50, 83], // 150 check-ins over 3 sessions ]); expect(out.events[1].change).toEqual({ registrations: 25, tickets: 20, income: 20, checkedIn: 11 }); }); test('an unknown series is a 404', async () => { const db = { eventSeries: { findUnique: jest.fn().mockResolvedValue(null) } }; await expect(series.compareSeries('nope', {}, db)).rejects.toMatchObject({ statusCode: 404 }); }); }); describe('announcing a new event to followers', () => { const now = new Date('2026-06-01T08:00:00Z'); const event = { id: 'e1', title: 'Camp 2027', startDate: new Date('2027-03-01T08:00:00Z'), location: 'The farm', series: { id: 's1', name: 'Family Camp', slug: 'family-camp' } }; beforeEach(() => { jest.clearAllMocks(); mockDb.event.findMany.mockResolvedValue([event]); mockDb.seriesFollow.findMany.mockResolvedValue([ { user: { id: 'u1', name: 'Thandi', email: 'thandi@test.local' } }, { user: { id: 'u2', name: 'No News', email: 'nonews@test.local' } }, ]); // Only Thandi opted in to news. mockFilter.mockImplementation(async list => ({ recipients: list.filter(r => r.userId === 'u1'), skipped: list.length - 1 })); mockSend.mockResolvedValue(); }); test('only public events in a series that have not started and were not announced yet', async () => { mockDb.event.updateMany.mockResolvedValue({ count: 1 }); await series.runAnnounceJob(now); const where = mockDb.event.findMany.mock.calls[0][0].where; expect(where).toMatchObject({ isActive: true, isHidden: false, goLiveAt: { lte: now }, seriesId: { not: null }, seriesAnnouncedAt: null, startDate: { gt: now } }); }); test('emails followers who want news, as news', async () => { mockDb.event.updateMany.mockResolvedValue({ count: 1 }); const out = await series.runAnnounceJob(now); expect(out).toEqual({ announced: 1, sent: 1, skipped: 1 }); expect(mockFilter.mock.calls[0][1]).toEqual({ channel: 'email', kind: 'news' }); const mail = mockSend.mock.calls[0][0]; expect(mail.to).toBe('thandi@test.local'); expect(mail.subject).toBe('New in Family Camp: Camp 2027'); expect(mail.html).toContain('/events/e1'); expect(mail.html).toContain('unsubscribe'); expect(mail.headers['List-Unsubscribe']).toBeTruthy(); }); test('an event another run already claimed is not sent again', async () => { mockDb.event.updateMany.mockResolvedValue({ count: 0 }); const out = await series.runAnnounceJob(now); expect(out).toEqual({ announced: 0, sent: 0, skipped: 0 }); expect(mockSend).not.toHaveBeenCalled(); }); }); });