/** * The shared better-auth sendResetPassword callback must send a unique WELCOME * email to admin-created users (flagged via pending-welcome) and the standard * RESET email to everyone else — same link, different framing. */ import { describe, it, expect } from 'vitest'; import { buildAccountPasswordEmail } from '@/lib/auth/account-setup-email'; import { markPendingWelcome } from '@/lib/auth/pending-welcome'; const url = 'https://crm.example.com/set-password#token=tok'; describe('buildAccountPasswordEmail routing', () => { it('sends a welcome email when the recipient was flagged by create-user', async () => { const email = 'new-hire@example.test'; markPendingWelcome(email); const mail = await buildAccountPasswordEmail({ email, name: 'New Hire', url, appName: 'Port Nimara CRM', authBranding: null, }); expect(mail.subject.toLowerCase()).toContain('welcome'); expect(mail.subject.toLowerCase()).not.toContain('reset'); expect(mail.html).toContain('tok'); }); it('sends the standard reset email for an unflagged self-service reset', async () => { const mail = await buildAccountPasswordEmail({ email: 'existing@example.test', name: 'Existing User', url, appName: 'Port Nimara CRM', authBranding: null, }); expect(mail.subject.toLowerCase()).toContain('reset'); expect(mail.subject.toLowerCase()).not.toContain('welcome'); }); it('consumes the welcome flag (a second build for the same email is a reset)', async () => { const email = 'once@example.test'; markPendingWelcome(email); const first = await buildAccountPasswordEmail({ email, url, appName: 'Port Nimara CRM', authBranding: null, }); const second = await buildAccountPasswordEmail({ email, url, appName: 'Port Nimara CRM', authBranding: null, }); expect(first.subject.toLowerCase()).toContain('welcome'); expect(second.subject.toLowerCase()).toContain('reset'); }); });