|
11 | 11 | */ |
12 | 12 | import { describe, expect, it, vi } from 'vitest'; |
13 | 13 | import { EmailNotifier } from '../src/notify/email-notifier.js'; |
14 | | -import { renderFilledEmail, renderInterestEmail } from '../src/notify/templates.js'; |
| 14 | +import { |
| 15 | + renderFilledEmail, |
| 16 | + renderInterestEmail, |
| 17 | + renderWelcomeEmail, |
| 18 | +} from '../src/notify/templates.js'; |
15 | 19 | import type { |
16 | 20 | HelpWantedFillNotification, |
17 | 21 | HelpWantedInterestNotification, |
| 22 | + WelcomeNotification, |
18 | 23 | } from '../src/notify/index.js'; |
19 | 24 |
|
20 | 25 | const noopLogger = { |
@@ -51,6 +56,12 @@ const baseFill: HelpWantedFillNotification = { |
51 | 56 | filledBySlug: 'jane-doe', |
52 | 57 | }; |
53 | 58 |
|
| 59 | +const baseWelcome: WelcomeNotification = { |
| 60 | + email: 'new-user@example.com', |
| 61 | + fullName: 'New User', |
| 62 | + slug: 'new-user', |
| 63 | +}; |
| 64 | + |
54 | 65 | function makeNotifier(emails: { send: ReturnType<typeof vi.fn> }): EmailNotifier { |
55 | 66 | return new EmailNotifier({ |
56 | 67 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
@@ -155,6 +166,76 @@ describe('EmailNotifier.notifyHelpWantedInterest', () => { |
155 | 166 | }); |
156 | 167 | }); |
157 | 168 |
|
| 169 | +describe('renderWelcomeEmail', () => { |
| 170 | + it('builds subject + text + html with the user fullName + slug', () => { |
| 171 | + const tpl = renderWelcomeEmail(baseWelcome, 'codeforphilly.org'); |
| 172 | + expect(tpl.subject).toContain('New User'); |
| 173 | + expect(tpl.text).toContain('Hey New User'); |
| 174 | + expect(tpl.text).toContain('https://codeforphilly.org/members/new-user'); |
| 175 | + expect(tpl.text).toContain('https://codeforphilly.org/projects'); |
| 176 | + expect(tpl.html).toContain('<strong>New User</strong>'); |
| 177 | + expect(tpl.html).toContain('href="https://codeforphilly.org/members/new-user"'); |
| 178 | + }); |
| 179 | + |
| 180 | + it('escapes HTML in fullName', () => { |
| 181 | + const tpl = renderWelcomeEmail( |
| 182 | + { ...baseWelcome, fullName: '<script>alert(1)</script>' }, |
| 183 | + 'codeforphilly.org', |
| 184 | + ); |
| 185 | + expect(tpl.html).not.toContain('<script>'); |
| 186 | + expect(tpl.html).toContain('<script>'); |
| 187 | + }); |
| 188 | + |
| 189 | + it('URL-encodes the slug for the profile link', () => { |
| 190 | + const tpl = renderWelcomeEmail( |
| 191 | + { ...baseWelcome, slug: 'name with spaces' }, |
| 192 | + 'codeforphilly.org', |
| 193 | + ); |
| 194 | + expect(tpl.html).toContain('href="https://codeforphilly.org/members/name%20with%20spaces"'); |
| 195 | + }); |
| 196 | +}); |
| 197 | + |
| 198 | +describe('EmailNotifier.notifyWelcomeOnSignup', () => { |
| 199 | + it('sends via Resend and returns delivered:true', async () => { |
| 200 | + const send = vi.fn().mockResolvedValue({ data: { id: 'msg-welcome' }, error: null }); |
| 201 | + const notifier = makeNotifier({ send }); |
| 202 | + |
| 203 | + const result = await notifier.notifyWelcomeOnSignup(baseWelcome); |
| 204 | + expect(result).toEqual({ delivered: true }); |
| 205 | + expect(send).toHaveBeenCalledTimes(1); |
| 206 | + const arg = send.mock.calls[0]![0]!; |
| 207 | + expect(arg.to).toBe('new-user@example.com'); |
| 208 | + expect(arg.subject).toContain('Welcome'); |
| 209 | + expect(arg.text).toContain('New User'); |
| 210 | + expect(arg.html).toContain('<strong>New User</strong>'); |
| 211 | + }); |
| 212 | + |
| 213 | + it('returns delivered:false when Resend reports an error', async () => { |
| 214 | + const send = vi.fn().mockResolvedValue({ |
| 215 | + data: null, |
| 216 | + error: { message: 'Sender domain unverified' }, |
| 217 | + }); |
| 218 | + const notifier = makeNotifier({ send }); |
| 219 | + const result = await notifier.notifyWelcomeOnSignup(baseWelcome); |
| 220 | + expect(result).toEqual({ delivered: false }); |
| 221 | + }); |
| 222 | + |
| 223 | + it('returns delivered:false when the SDK throws', async () => { |
| 224 | + const send = vi.fn().mockRejectedValue(new Error('network blip')); |
| 225 | + const notifier = makeNotifier({ send }); |
| 226 | + const result = await notifier.notifyWelcomeOnSignup(baseWelcome); |
| 227 | + expect(result).toEqual({ delivered: false }); |
| 228 | + }); |
| 229 | + |
| 230 | + it('returns delivered:false on empty email (defensive)', async () => { |
| 231 | + const send = vi.fn(); |
| 232 | + const notifier = makeNotifier({ send }); |
| 233 | + const result = await notifier.notifyWelcomeOnSignup({ ...baseWelcome, email: '' }); |
| 234 | + expect(result).toEqual({ delivered: false }); |
| 235 | + expect(send).not.toHaveBeenCalled(); |
| 236 | + }); |
| 237 | +}); |
| 238 | + |
158 | 239 | describe('EmailNotifier.notifyHelpWantedFilled', () => { |
159 | 240 | it('sends via Resend and returns delivered:true', async () => { |
160 | 241 | const send = vi.fn().mockResolvedValue({ data: { id: 'msg-456' }, error: null }); |
|
0 commit comments