jest.mock('../src/config/db', () => ({ privacyNotice: { findFirst: jest.fn(), findMany: jest.fn(), findUnique: jest.fn() }, operator: { findMany: jest.fn() }, appSetting: { findMany: jest.fn().mockResolvedValue([]), findUnique: jest.fn() }, })); const prisma = require('../src/config/db'); const notice = require('../src/utils/privacyNotice'); const { reportRowCount } = require('../src/utils/accessLog'); const ctx = { org: 'Hope Church', orgEmail: 'office@hope.test', website: 'hope.test', ioName: 'Ann IO', ioEmail: 'io@hope.test', medicalRetentionDays: '30', operators: [ { name: 'PayFast', location: 'South Africa', purpose: 'Online payments', dataShared: 'Name, email address' }, { name: 'WAWP', location: null, purpose: 'Delivering WhatsApp messages', dataShared: 'Mobile number' }, ], }; beforeEach(() => jest.clearAllMocks()); describe('renderSections', () => { test('fills placeholders and expands {operators} into one bullet each', () => { const [s] = notice.renderSections([{ id: 'a', title: 'A', body: 'Run by {org} — ask {ioName} ({ioEmail}).\n{operators}\nUnknown {nope} stays.' }], ctx); expect(s.body.split('\n')).toEqual([ 'Run by Hope Church — ask Ann IO (io@hope.test).', '• PayFast (South Africa) — Online payments. Receives: Name, email address.', '• WAWP — Delivering WhatsApp messages. Receives: Mobile number.', 'Unknown {nope} stays.', ]); }); test('{operators} inside a sentence is left alone', () => { const [s] = notice.renderSections([{ id: 'a', title: 'A', body: 'See {operators} below' }], ctx); expect(s.body).toBe('See {operators} below'); }); test('the built-in notice has no unfilled placeholders', () => { const rendered = notice.renderSections(notice.defaultSections(), ctx); for (const s of rendered) expect(s.body).not.toMatch(/\{\w+\}/); expect(rendered.find(s => s.id === 'sharing').body).toContain('• PayFast (South Africa)'); }); }); describe('cleanSections', () => { test('trims, makes ids from titles and keeps them unique', () => { const out = notice.cleanSections([ { title: ' 1. Introduction ', body: 'Hi\r\nthere ' }, { title: 'Introduction', body: '' }, ]); expect(out).toEqual([ { id: 'introduction', title: '1. Introduction', body: 'Hi\nthere' }, { id: 'introduction-2', title: 'Introduction', body: '' }, ]); }); test('refuses an empty notice or a section without a title', () => { expect(() => notice.cleanSections([])).toThrow(/at least one section/); expect(() => notice.cleanSections([{ title: ' ', body: 'x' }])).toThrow(/needs a title/); }); }); describe('noticePromptPending', () => { test('someone who never accepted any version is asked once (first)', async () => { prisma.privacyNotice.findFirst.mockResolvedValue(null); expect(await notice.noticePromptPending({ noticeVersionAccepted: null })).toBe('first'); }); test('nobody else is asked while no version needs re-acceptance', async () => { prisma.privacyNotice.findFirst.mockResolvedValue(null); expect(await notice.noticePromptPending({ noticeVersionAccepted: 0 })).toBe(false); }); test('asked when they accepted an older version than the newest one that needs it', async () => { prisma.privacyNotice.findFirst.mockResolvedValue({ version: 3 }); expect(await notice.noticePromptPending({ noticeVersionAccepted: 2 })).toBe('updated'); expect(await notice.noticePromptPending({ noticeVersionAccepted: 0 })).toBe('updated'); expect(await notice.noticePromptPending({ noticeVersionAccepted: 3 })).toBe(false); expect(await notice.noticePromptPending({ noticeVersionAccepted: 5 })).toBe(false); expect(prisma.privacyNotice.findFirst.mock.calls.at(-1)[0].where).toEqual({ requiresAcceptance: true }); }); }); describe('publicNotice', () => { beforeEach(() => prisma.operator.findMany.mockResolvedValue([])); test('the built-in notice is version 0 while nothing is published', async () => { prisma.privacyNotice.findMany.mockResolvedValue([]); const n = await notice.publicNotice(null); expect(n.version).toBe(0); expect(n.builtIn).toBe(true); expect(n.sections.length).toBeGreaterThan(10); }); test('once published, version 0 is no longer available as a past version', async () => { prisma.privacyNotice.findMany.mockResolvedValue([{ version: 1, publishedAt: new Date() }]); expect(await notice.publicNotice(0)).toBeNull(); }); test('a published version, marked current only when it is the newest', async () => { prisma.privacyNotice.findMany.mockResolvedValue([{ version: 2 }, { version: 1 }]); prisma.privacyNotice.findUnique.mockResolvedValue({ version: 1, publishedAt: new Date('2026-10-01T10:00:00Z'), changeSummary: 'x', sections: [{ id: 'a', title: 'A', body: 'Hello' }] }); const n = await notice.publicNotice(1); expect(n).toMatchObject({ version: 1, builtIn: false, current: false, changeSummary: 'x' }); expect(n.sections[0].body).toBe('Hello'); }); }); describe('reportRowCount', () => { test('counts table rows, layered items and extra tables', () => { expect(reportRowCount({ table: { rows: [[1], [2]] }, extraTables: [{ rows: [[1]] }] })).toBe(3); expect(reportRowCount({ layered: { sections: [{ items: ['a', 'b'] }, { items: ['c'] }] } })).toBe(3); expect(reportRowCount(null)).toBe(0); }); }); describe('changes to the built-in text', () => { test('a site that never published shows the built-in text, so there is nothing to review', async () => { prisma.privacyNotice.findFirst.mockResolvedValue(null); expect(await notice.builtInUpdates()).toEqual([]); }); test('a site with its own version is shown the changes it has not reviewed', async () => { prisma.privacyNotice.findFirst.mockResolvedValue({ version: 2 }); prisma.appSetting.findUnique.mockResolvedValue(null); expect((await notice.builtInUpdates()).map(u => u.revision)).toContain('1.48.1'); prisma.appSetting.findUnique.mockResolvedValue({ value: notice.BUILT_IN_REVISION }); expect(await notice.builtInUpdates()).toEqual([]); }); test('every changed section exists in the built-in text', () => { const ids = notice.defaultSections().map(s => s.id); for (const u of notice.BUILT_IN_UPDATES) for (const id of u.sections) expect(ids).toContain(id); expect(notice.revisionLess('1.47.9', '1.48.1')).toBe(true); expect(notice.revisionLess('1.48.10', '1.48.9')).toBe(false); }); test('the built-in text covers the features since 1.46', () => { const all = notice.defaultSections().map(s => s.body).join('\n'); for (const words of ['volunteer', 'series you follow', 'season pass', 'webhook', 'cross_events_dismissed_notices', 'activate an account']) { expect([words, all.toLowerCase().includes(words.toLowerCase())]).toEqual([words, true]); } }); });