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/series')).toBe(true); expect(isBlocked('PUT', '/api/series/abc')).toBe(true); expect(isBlocked('POST', '/api/series/abc/follow')).toBe(false); }); 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(); }); }); });