// 3.8.0 (docs/APP-1.0-PLAN.md ยง8, decision 11): STOP to the church's WhatsApp number means no // WhatsApp from any of its products: kept by number and per account, checked before every send, // passed on to Accounts, and turned back on there (or by START without Accounts). jest.mock('../src/config/db', () => ({ user: { findFirst: jest.fn(), updateMany: jest.fn() }, whatsAppStop: { findUnique: jest.fn(), upsert: jest.fn(), deleteMany: jest.fn() }, appSetting: { findMany: jest.fn().mockResolvedValue([{ key: 'WAWP_ACCESS_TOKEN', value: 'token' }, { key: 'WAWP_INSTANCE_ID', value: 'HOPE01' }]), findUnique: jest.fn().mockResolvedValue({ value: 's3cret' }), upsert: jest.fn().mockResolvedValue({}), }, })); jest.mock('axios', () => ({ post: jest.fn().mockResolvedValue({ data: {} }) })); jest.mock('../src/accounts/whatsappOptOut', () => ({ stopInAccounts: jest.fn().mockResolvedValue(true), accountPageUrl: jest.fn() })); jest.mock('../src/utils/settingsCache', () => ({ getSettingSync: () => 'Hope Family Church' })); jest.mock('../src/utils/privacy', () => ({ ...jest.requireActual('../src/utils/privacy'), whatsAppStop: jest.fn().mockResolvedValue('account') })); jest.mock('../src/utils/encryption', () => ({ encrypt: (v) => v, decrypt: (v) => v })); const prisma = require('../src/config/db'); const axios = require('axios'); const { stopInAccounts, accountPageUrl } = require('../src/accounts/whatsappOptOut'); const privacy = require('../src/utils/privacy'); const stop = require('../src/utils/whatsappStop'); const { sendText } = require('../src/utils/whatsapp'); const { handleWebhook } = require('../src/controllers/whatsappController'); beforeEach(() => { jest.clearAllMocks(); prisma.whatsAppStop.findUnique.mockResolvedValue(null); prisma.user.findFirst.mockResolvedValue(null); accountPageUrl.mockResolvedValue('https://accounts.hope.test/account'); }); const res = () => ({ status() { return this; }, json() { return this; } }); /** WAWP's message event, through the real handler (which answers first, then works). */ async function wawp(body, from = '27821234567@c.us', secret = 's3cret') { await handleWebhook({ query: { secret }, body: { event: 'message', payload: { from, body, fromMe: false } } }, res()); } const sentTexts = () => axios.post.mock.calls.filter(([url]) => url.endsWith('/send/text')).map(([, b]) => ({ to: b.chatId, text: b.message })); describe('isWhatsAppStopped', () => { test('a number that replied STOP here', async () => { prisma.whatsAppStop.findUnique.mockResolvedValueOnce({ phone: '27821234567' }); expect(await stop.isWhatsAppStopped('082 123 4567')).toBe(true); expect(prisma.whatsAppStop.findUnique).toHaveBeenCalledWith({ where: { phone: '27821234567' } }); }); test('an account Accounts says opted out, however its number is written', async () => { prisma.user.findFirst.mockResolvedValueOnce({ id: 'u1' }); expect(await stop.isWhatsAppStopped('+27821234567')).toBe(true); expect(prisma.user.findFirst.mock.calls[0][0].where).toEqual({ whatsappOptedOut: true, phoneNumber: { in: ['27821234567', '+27821234567', '0821234567'] } }); }); test('anyone else, and numbers WhatsApp can\'t reach', async () => { expect(await stop.isWhatsAppStopped('0821234567')).toBe(false); expect(await stop.isWhatsAppStopped('not a number')).toBe(false); }); }); describe('every send checks it', () => { test('nothing goes to a stopped number', async () => { prisma.whatsAppStop.findUnique.mockResolvedValue({ phone: '27821234567' }); expect(await sendText('0821234567', 'Reminder: Youth Camp starts tomorrow')).toBe(false); expect(axios.post).not.toHaveBeenCalled(); }); test('other numbers are sent to as before', async () => { await sendText('0829999999', 'Hello'); expect(sentTexts()).toEqual([{ to: '27829999999@c.us', text: 'Hello' }]); }); }); describe('a STOP reply', () => { test('is kept, turns news off, goes to Accounts, and is answered once with how to undo it', async () => { await wawp('Stop'); expect(prisma.whatsAppStop.upsert.mock.calls[0][0].create).toMatchObject({ phone: '27821234567', keyword: 'STOP', source: 'events' }); expect(prisma.user.updateMany).toHaveBeenCalledWith({ where: { phoneNumber: { in: ['27821234567', '+27821234567', '0821234567'] } }, data: { whatsappOptedOut: true } }); expect(privacy.whatsAppStop).toHaveBeenCalledWith('27821234567'); expect(stopInAccounts).toHaveBeenCalledWith('27821234567', 'Stop'); const [reply] = sentTexts(); expect(reply.to).toBe('27821234567@c.us'); expect(reply.text).toMatch(/won't get WhatsApp messages from Hope Family Church/); expect(reply.text).toContain('https://accounts.hope.test/account'); }); test('Afrikaans STOPP and UITTEKEN count; other messages, groups and a wrong secret don\'t', async () => { await wawp('STOPP'); await wawp('uitteken'); expect(stopInAccounts).toHaveBeenCalledTimes(2); jest.clearAllMocks(); await wawp('Thanks for the reminder!'); await wawp('STOP', '1203630@g.us'); await wawp('STOP', '27821234567@c.us', 'wrong'); expect(prisma.whatsAppStop.upsert).not.toHaveBeenCalled(); expect(stopInAccounts).not.toHaveBeenCalled(); }); }); describe('START', () => { test('without Accounts: WhatsApp back on for the number', async () => { accountPageUrl.mockResolvedValue(null); await wawp('START'); expect(prisma.whatsAppStop.deleteMany).toHaveBeenCalledWith({ where: { phone: '27821234567' } }); expect(prisma.user.updateMany).toHaveBeenCalledWith({ where: { phoneNumber: { in: ['27821234567', '+27821234567', '0821234567'] } }, data: { whatsappOptedOut: false } }); expect(sentTexts()[0].text).toMatch(/back on/); }); test('with Accounts: only a pointer to the account page (Accounts keeps the opt-out)', async () => { await wawp('start'); expect(prisma.whatsAppStop.deleteMany).not.toHaveBeenCalled(); expect(sentTexts()[0].text).toContain('https://accounts.hope.test/account'); }); });