|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import type { Env } from '../../lib/config.js'; |
| 3 | + |
| 4 | +const createTransportMock = vi.fn(() => ({ |
| 5 | + sendMail: vi.fn(), |
| 6 | +})); |
| 7 | +const setApiKeyMock = vi.fn(); |
| 8 | + |
| 9 | +vi.mock('nodemailer', () => ({ |
| 10 | + default: { |
| 11 | + createTransport: createTransportMock, |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock('@sendgrid/mail', () => ({ |
| 16 | + default: { |
| 17 | + send: vi.fn(), |
| 18 | + setApiKey: setApiKeyMock, |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +const createEnv = (overrides: Partial<Env> = {}): Env => ({ |
| 23 | + DATABASE_URL: 'postgres://robocon:password@localhost:5432/robocon', |
| 24 | + PORT: 8787, |
| 25 | + BETTER_AUTH_SECRET: 'dev-secret', |
| 26 | + BETTER_AUTH_URL: 'http://localhost:8787', |
| 27 | + APP_URL: 'http://localhost:3000', |
| 28 | + CORS_ALLOWED_ORIGINS: ['http://localhost:3000'], |
| 29 | + S3_ENDPOINT: 'http://localhost:9000', |
| 30 | + S3_REGION: 'us-east-1', |
| 31 | + S3_ACCESS_KEY: 'minioadmin', |
| 32 | + S3_SECRET_KEY: 'minioadmin', |
| 33 | + S3_BUCKET_RULES: 'robocon-rules', |
| 34 | + S3_BUCKET_SUBMISSIONS: 'robocon-submissions', |
| 35 | + S3_FORCE_PATH_STYLE: true, |
| 36 | + EMAIL_PROVIDER: 'console', |
| 37 | + SENDGRID_API_KEY: undefined, |
| 38 | + SENDGRID_FROM: 'sendgrid@example.com', |
| 39 | + SMTP_HOST: undefined, |
| 40 | + SMTP_PORT: 587, |
| 41 | + SMTP_USER: undefined, |
| 42 | + SMTP_PASS: undefined, |
| 43 | + SMTP_FROM: undefined, |
| 44 | + SMTP_SECURE: false, |
| 45 | + SMTP_REQUIRE_TLS: false, |
| 46 | + ...overrides, |
| 47 | +}); |
| 48 | + |
| 49 | +describe('createEmailService', () => { |
| 50 | + beforeEach(() => { |
| 51 | + vi.clearAllMocks(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('uses console email by default', async () => { |
| 55 | + const { createEmailService } = await import('./index.js'); |
| 56 | + const { ConsoleEmailService } = await import('./console.js'); |
| 57 | + |
| 58 | + expect(createEmailService(createEnv())).toBeInstanceOf(ConsoleEmailService); |
| 59 | + }); |
| 60 | + |
| 61 | + it('uses SendGrid when configured with an API key', async () => { |
| 62 | + const { createEmailService } = await import('./index.js'); |
| 63 | + const { SendGridEmailService } = await import('./sendgrid.js'); |
| 64 | + |
| 65 | + const service = createEmailService( |
| 66 | + createEnv({ |
| 67 | + EMAIL_PROVIDER: 'sendgrid', |
| 68 | + SENDGRID_API_KEY: 'sendgrid-key', |
| 69 | + }), |
| 70 | + ); |
| 71 | + |
| 72 | + expect(service).toBeInstanceOf(SendGridEmailService); |
| 73 | + expect(setApiKeyMock).toHaveBeenCalledWith('sendgrid-key'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('keeps the existing SendGrid fallback when the API key is missing', async () => { |
| 77 | + const { createEmailService } = await import('./index.js'); |
| 78 | + const { ConsoleEmailService } = await import('./console.js'); |
| 79 | + |
| 80 | + expect(createEmailService(createEnv({ EMAIL_PROVIDER: 'sendgrid' }))).toBeInstanceOf( |
| 81 | + ConsoleEmailService, |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('uses SMTP when all required SMTP settings are present', async () => { |
| 86 | + const { createEmailService } = await import('./index.js'); |
| 87 | + const { SmtpEmailService } = await import('./smtp.js'); |
| 88 | + |
| 89 | + const service = createEmailService( |
| 90 | + createEnv({ |
| 91 | + EMAIL_PROVIDER: 'smtp', |
| 92 | + SMTP_HOST: 'smtp.example.com', |
| 93 | + SMTP_PORT: 465, |
| 94 | + SMTP_USER: 'smtp-user', |
| 95 | + SMTP_PASS: 'smtp-pass', |
| 96 | + SMTP_FROM: 'from@example.com', |
| 97 | + SMTP_SECURE: true, |
| 98 | + SMTP_REQUIRE_TLS: true, |
| 99 | + }), |
| 100 | + ); |
| 101 | + |
| 102 | + expect(service).toBeInstanceOf(SmtpEmailService); |
| 103 | + expect(createTransportMock).toHaveBeenCalledWith({ |
| 104 | + host: 'smtp.example.com', |
| 105 | + port: 465, |
| 106 | + secure: true, |
| 107 | + auth: { |
| 108 | + user: 'smtp-user', |
| 109 | + pass: 'smtp-pass', |
| 110 | + }, |
| 111 | + requireTLS: true, |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('throws when SMTP provider is missing required settings', async () => { |
| 116 | + const { createEmailService } = await import('./index.js'); |
| 117 | + |
| 118 | + expect(() => createEmailService(createEnv({ EMAIL_PROVIDER: 'smtp' }))).toThrow( |
| 119 | + 'EMAIL_PROVIDER=smtp requires SMTP_HOST, SMTP_USER, SMTP_PASS, SMTP_FROM.', |
| 120 | + ); |
| 121 | + }); |
| 122 | +}); |
0 commit comments