|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { sendEmail } from './resend'; |
| 3 | + |
| 4 | +// Mock fetch globally |
| 5 | +global.fetch = vi.fn(); |
| 6 | + |
| 7 | +describe('Resend Email Service', () => { |
| 8 | + beforeEach(() => { |
| 9 | + vi.clearAllMocks(); |
| 10 | + // Set required environment variables |
| 11 | + process.env.RESEND_API_KEY = 'test-api-key'; |
| 12 | + process.env.REPLY_TO_EMAIL = 'support@example.com'; |
| 13 | + }); |
| 14 | + |
| 15 | + describe('sendEmail', () => { |
| 16 | + it('should send email successfully', async () => { |
| 17 | + const mockResponse = { |
| 18 | + ok: true, |
| 19 | + json: async () => ({ id: 'msg_123' }), |
| 20 | + }; |
| 21 | + (global.fetch as any).mockResolvedValue(mockResponse); |
| 22 | + |
| 23 | + const result = await sendEmail({ |
| 24 | + to: 'merchant@example.com', |
| 25 | + subject: 'Test Email', |
| 26 | + html: '<p>Test content</p>', |
| 27 | + }); |
| 28 | + |
| 29 | + expect(result.success).toBe(true); |
| 30 | + expect(result.messageId).toBe('msg_123'); |
| 31 | + expect(global.fetch).toHaveBeenCalledTimes(1); |
| 32 | + |
| 33 | + const fetchCall = (global.fetch as any).mock.calls[0]; |
| 34 | + expect(fetchCall[0]).toBe('https://api.resend.com/emails'); |
| 35 | + expect(fetchCall[1].method).toBe('POST'); |
| 36 | + expect(fetchCall[1].headers['Content-Type']).toBe('application/json'); |
| 37 | + expect(fetchCall[1].headers['Authorization']).toBe('Bearer test-api-key'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should handle missing API key', async () => { |
| 41 | + delete process.env.RESEND_API_KEY; |
| 42 | + |
| 43 | + const result = await sendEmail({ |
| 44 | + to: 'merchant@example.com', |
| 45 | + subject: 'Test Email', |
| 46 | + html: '<p>Test content</p>', |
| 47 | + }); |
| 48 | + |
| 49 | + expect(result.success).toBe(false); |
| 50 | + expect(result.error).toContain('Resend API key'); |
| 51 | + expect(global.fetch).not.toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should validate email address', async () => { |
| 55 | + const result = await sendEmail({ |
| 56 | + to: 'invalid-email', |
| 57 | + subject: 'Test Email', |
| 58 | + html: '<p>Test content</p>', |
| 59 | + }); |
| 60 | + |
| 61 | + expect(result.success).toBe(false); |
| 62 | + expect(result.error).toContain('Invalid email address'); |
| 63 | + expect(global.fetch).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should validate subject is not empty', async () => { |
| 67 | + const result = await sendEmail({ |
| 68 | + to: 'merchant@example.com', |
| 69 | + subject: '', |
| 70 | + html: '<p>Test content</p>', |
| 71 | + }); |
| 72 | + |
| 73 | + expect(result.success).toBe(false); |
| 74 | + expect(result.error).toContain('Subject is required'); |
| 75 | + expect(global.fetch).not.toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should validate HTML content is not empty', async () => { |
| 79 | + const result = await sendEmail({ |
| 80 | + to: 'merchant@example.com', |
| 81 | + subject: 'Test Email', |
| 82 | + html: '', |
| 83 | + }); |
| 84 | + |
| 85 | + expect(result.success).toBe(false); |
| 86 | + expect(result.error).toContain('HTML content is required'); |
| 87 | + expect(global.fetch).not.toHaveBeenCalled(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should handle Resend API errors', async () => { |
| 91 | + const mockResponse = { |
| 92 | + ok: false, |
| 93 | + status: 400, |
| 94 | + json: async () => ({ message: 'Invalid request' }), |
| 95 | + }; |
| 96 | + (global.fetch as any).mockResolvedValue(mockResponse); |
| 97 | + |
| 98 | + const result = await sendEmail({ |
| 99 | + to: 'merchant@example.com', |
| 100 | + subject: 'Test Email', |
| 101 | + html: '<p>Test content</p>', |
| 102 | + }); |
| 103 | + |
| 104 | + expect(result.success).toBe(false); |
| 105 | + expect(result.error).toContain('Invalid request'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should handle network errors', async () => { |
| 109 | + (global.fetch as any).mockRejectedValue(new Error('Network error')); |
| 110 | + |
| 111 | + const result = await sendEmail({ |
| 112 | + to: 'merchant@example.com', |
| 113 | + subject: 'Test Email', |
| 114 | + html: '<p>Test content</p>', |
| 115 | + }); |
| 116 | + |
| 117 | + expect(result.success).toBe(false); |
| 118 | + expect(result.error).toContain('Network error'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should send correct JSON body', async () => { |
| 122 | + const mockResponse = { |
| 123 | + ok: true, |
| 124 | + json: async () => ({ id: 'msg_123' }), |
| 125 | + }; |
| 126 | + (global.fetch as any).mockResolvedValue(mockResponse); |
| 127 | + |
| 128 | + await sendEmail({ |
| 129 | + to: 'merchant@example.com', |
| 130 | + subject: 'Test Email', |
| 131 | + html: '<p>Test content</p>', |
| 132 | + }); |
| 133 | + |
| 134 | + const fetchCall = (global.fetch as any).mock.calls[0]; |
| 135 | + const body = JSON.parse(fetchCall[1].body); |
| 136 | + expect(body.to).toEqual(['merchant@example.com']); |
| 137 | + expect(body.subject).toBe('Test Email'); |
| 138 | + expect(body.html).toBe('<p>Test content</p>'); |
| 139 | + expect(body.reply_to).toBe('support@example.com'); |
| 140 | + expect(body.from).toContain('CoinPay'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should use custom from address when provided', async () => { |
| 144 | + const mockResponse = { |
| 145 | + ok: true, |
| 146 | + json: async () => ({ id: 'msg_123' }), |
| 147 | + }; |
| 148 | + (global.fetch as any).mockResolvedValue(mockResponse); |
| 149 | + |
| 150 | + await sendEmail({ |
| 151 | + to: 'merchant@example.com', |
| 152 | + subject: 'Test Email', |
| 153 | + html: '<p>Test content</p>', |
| 154 | + from: 'Custom <custom@example.com>', |
| 155 | + }); |
| 156 | + |
| 157 | + const fetchCall = (global.fetch as any).mock.calls[0]; |
| 158 | + const body = JSON.parse(fetchCall[1].body); |
| 159 | + expect(body.from).toBe('Custom <custom@example.com>'); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments