// 3.8.0 (docs/APP-1.0-PLAN.md ยง8, decision 11): the WhatsApp opt-out Accounts keeps for every // product reaches Events in user.updated (`whatsapp_opted_out`), both ways. const mockUpdates: { id: string; data: Record }[] = []; let mockBefore: { whatsappOptedOut: boolean; phoneNumber: string | null } | null = null; jest.mock('../src/config/db', () => ({ user: { findFirst: async () => null, findUnique: async () => mockBefore, update: async ({ where, data }: { where: { id: string }; data: Record }) => { mockUpdates.push({ id: where.id, data }); return {}; }, }, })); jest.mock('../src/utils/whatsappStop', () => ({ clearStop: jest.fn().mockResolvedValue('27821234567') })); import { updateLocalCopy } from '../src/accounts/signin'; // eslint-disable-next-line @typescript-eslint/no-require-imports const { clearStop } = require('../src/utils/whatsappStop') as { clearStop: jest.Mock }; const profile = (over: Record = {}) => ({ name: 'Thandi Example', email: 'thandi@hope.test', phone_number: '+27821234567', news_consent: { email: true, whatsapp: false }, ...over, }); beforeEach(() => { mockUpdates.length = 0; mockBefore = null; clearStop.mockClear(); }); test('opted out in Accounts: no WhatsApp here either', async () => { await updateLocalCopy('u1', profile({ whatsapp_opted_out: true })); expect(mockUpdates[0]!.data.whatsappOptedOut).toBe(true); expect(clearStop).not.toHaveBeenCalled(); }); test('turned back on in Accounts: on here, and this site\'s own STOP for the number goes', async () => { mockBefore = { whatsappOptedOut: true, phoneNumber: '+27821234567' }; await updateLocalCopy('u1', profile({ whatsapp_opted_out: false })); expect(mockUpdates[0]!.data.whatsappOptedOut).toBe(false); expect(clearStop).toHaveBeenCalledWith('+27821234567'); }); test('an Accounts before 1.9.0 (no field) changes nothing about it', async () => { await updateLocalCopy('u1', profile()); expect('whatsappOptedOut' in mockUpdates[0]!.data).toBe(false); });