// What installs are told about releases and notices (docs/hub-protocol.md §3). jest.mock('../src/config/db', () => ({})); const { releasesFor } = require('../src/utils/releases'); const { noticesFor, recipients, emailText } = require('../src/utils/notices'); const NOW = new Date('2026-10-28T10:00:00Z'); const at = '2026-10-20T08:00:00.000Z'; describe('releases for an install', () => { const releases = [ { version: '1.9.0', notes: 'Old', urgent: false, releasedAt: at }, { version: '1.10.1', notes: '- Security fix', urgent: true, releasedAt: at }, { version: '1.10.0', notes: '- Rosters', urgent: false, releasedAt: at }, ]; test('only newer versions, newest first, compared as numbers (1.10 > 1.9)', () => { expect(releasesFor(releases, '1.9.0').map(r => r.version)).toEqual(['1.10.1', '1.10.0']); expect(releasesFor(releases, '1.10.1')).toEqual([]); }); test('sends the notes and the urgent flag, nothing else', () => { expect(releasesFor(releases, '1.10.0')).toEqual([{ version: '1.10.1', releasedAt: at, urgent: true, notes: '- Security fix' }]); }); test('at most 20', () => { const many = Array.from({ length: 30 }, (_, i) => ({ version: `1.${i + 1}.0`, notes: '', urgent: false, releasedAt: at })); const sent = releasesFor(many, '1.0.0'); expect(sent).toHaveLength(20); expect(sent[0].version).toBe('1.30.0'); }); }); describe('notices for an install', () => { const base = { level: 'info', title: 'T', message: 'M', showBanner: true, allInstalls: true, installIds: [], startsAt: null, endsAt: null, updatedAt: at }; test('every install, or only the chosen ones', () => { const notices = [{ ...base, id: 'all' }, { ...base, id: 'some', allInstalls: false, installIds: ['inst_2'] }]; expect(noticesFor(notices, 'inst_1', NOW).map(n => n.id)).toEqual(['all']); expect(noticesFor(notices, 'inst_2', NOW).map(n => n.id)).toEqual(['all', 'some']); }); test('ended and email-only notices are not sent; ones starting later are', () => { const notices = [ { ...base, id: 'ended', endsAt: '2026-10-28T09:00:00Z' }, { ...base, id: 'email-only', showBanner: false }, { ...base, id: 'later', startsAt: '2026-11-01T18:00:00Z', endsAt: '2026-11-01T20:00:00Z' }, ]; expect(noticesFor(notices, 'inst_1', NOW)).toEqual([ { id: 'later', level: 'info', title: 'T', message: 'M', startsAt: '2026-11-01T18:00:00.000Z', endsAt: '2026-11-01T20:00:00.000Z', updatedAt: at }, ]); }); }); describe('notice emails', () => { test('to each customer email and admin contact once, skipping blanks and nonsense', () => { const installs = [ { customer: { email: 'office@hope.org' }, adminContact: { email: 'Jane@hope.org' } }, { customer: { email: 'OFFICE@hope.org' }, adminContact: { email: 'jane@hope.org' } }, { customer: { email: null }, adminContact: { email: 'not an email' } }, { customer: { email: 'grace@grace.org' }, adminContact: null }, ]; expect(recipients(installs)).toEqual(['office@hope.org', 'Jane@hope.org', 'grace@grace.org']); }); test('the message, with the dates when there are any', () => { expect(emailText({ message: 'Hello ' })).toBe('Hello\n\n— Cross Events'); const text = emailText({ message: 'Down for maintenance.', startsAt: '2026-11-01T18:00:00Z', endsAt: '2026-11-01T20:00:00Z' }); expect(text).toContain('From: 1 November 2026'); expect(text).toContain('Until: 1 November 2026'); }); });