|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * framework#5087 — what `EmailServicePlugin` is constructed with on the |
| 5 | + * `os serve` path, and specifically what happens when SMTP is selected. |
| 6 | + * |
| 7 | + * `OS_EMAIL_PROVIDER=smtp` used to be unreachable: the plugin knew three |
| 8 | + * providers (`log`/`resend`/`postmark`), so `smtp` fell into the "no apiKey" |
| 9 | + * arm and was silently rewritten to `log`. The server then booted "fine", |
| 10 | + * every send was recorded in `sys_email` as sent, and nothing left the box. |
| 11 | + * These pin the opposite: a complete SMTP configuration reaches the plugin, |
| 12 | + * and an incomplete one fails the boot instead of degrading into a transport |
| 13 | + * that reports success. |
| 14 | + */ |
| 15 | + |
| 16 | +import { describe, it, expect } from 'vitest'; |
| 17 | +import { resolveEmailCapabilityArg } from './serve.js'; |
| 18 | + |
| 19 | +describe('resolveEmailCapabilityArg', () => { |
| 20 | + it('defaults to the log provider when nothing is configured', () => { |
| 21 | + const { options, warning } = resolveEmailCapabilityArg({}, {}); |
| 22 | + expect(options).toMatchObject({ provider: 'log' }); |
| 23 | + expect(options).not.toHaveProperty('providerOptions'); |
| 24 | + expect(warning).toBeUndefined(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('assembles the SMTP connection from OS_EMAIL_SMTP_*', () => { |
| 28 | + const { options, warning } = resolveEmailCapabilityArg({}, { |
| 29 | + OS_EMAIL_PROVIDER: 'smtp', |
| 30 | + OS_EMAIL_SMTP_HOST: ' smtp.exmail.qq.com ', |
| 31 | + OS_EMAIL_SMTP_PORT: '465', |
| 32 | + OS_EMAIL_SMTP_SECURE: 'true', |
| 33 | + OS_EMAIL_SMTP_USER: 'ops@example.cn', |
| 34 | + OS_EMAIL_SMTP_PASSWORD: 'sekrit', |
| 35 | + OS_EMAIL_FROM: 'Acme <no-reply@example.cn>', |
| 36 | + }); |
| 37 | + expect(warning).toBeUndefined(); |
| 38 | + expect(options).toMatchObject({ |
| 39 | + provider: 'smtp', |
| 40 | + providerOptions: { |
| 41 | + host: 'smtp.exmail.qq.com', |
| 42 | + port: 465, |
| 43 | + secure: true, |
| 44 | + user: 'ops@example.cn', |
| 45 | + password: 'sekrit', |
| 46 | + }, |
| 47 | + defaultFrom: { name: 'Acme', address: 'no-reply@example.cn' }, |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('reads OS_EMAIL_SMTP_SECURE=false as plain-connect', () => { |
| 52 | + const { options } = resolveEmailCapabilityArg({}, { |
| 53 | + OS_EMAIL_PROVIDER: 'smtp', |
| 54 | + OS_EMAIL_SMTP_HOST: 'smtp.x', |
| 55 | + OS_EMAIL_SMTP_SECURE: 'false', |
| 56 | + }); |
| 57 | + expect((options.providerOptions as any).secure).toBe(false); |
| 58 | + expect(resolveEmailCapabilityArg({}, { |
| 59 | + OS_EMAIL_PROVIDER: 'smtp', OS_EMAIL_SMTP_HOST: 'smtp.x', OS_EMAIL_SMTP_SECURE: '0', |
| 60 | + }).options.providerOptions).toMatchObject({ secure: false }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('layers env over config.email.options', () => { |
| 64 | + const { options } = resolveEmailCapabilityArg( |
| 65 | + { provider: 'smtp', options: { host: 'smtp.config', port: 25 } }, |
| 66 | + { OS_EMAIL_SMTP_HOST: 'smtp.env' }, |
| 67 | + ); |
| 68 | + expect(options.providerOptions).toEqual({ host: 'smtp.env', port: 25 }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('accepts an SMTP host declared only in config.email.options', () => { |
| 72 | + const { options } = resolveEmailCapabilityArg({ provider: 'smtp', options: { host: 'smtp.config' } }, {}); |
| 73 | + expect(options).toMatchObject({ provider: 'smtp', providerOptions: { host: 'smtp.config' } }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('THROWS on provider=smtp without a host — never a silent LogTransport', () => { |
| 77 | + expect(() => resolveEmailCapabilityArg({}, { OS_EMAIL_PROVIDER: 'smtp' })) |
| 78 | + .toThrow(/no SMTP host is configured/); |
| 79 | + expect(() => resolveEmailCapabilityArg({}, { OS_EMAIL_PROVIDER: 'smtp', OS_EMAIL_SMTP_PORT: '587' })) |
| 80 | + .toThrow(/OS_EMAIL_SMTP_HOST/); |
| 81 | + }); |
| 82 | + |
| 83 | + it('does not apply the apiKey fallback to smtp', () => { |
| 84 | + // The `resend`/`postmark` arm degrades to `log` when the key is missing; |
| 85 | + // smtp must never reach it (it needs no apiKey at all). |
| 86 | + const { options, warning } = resolveEmailCapabilityArg({}, { |
| 87 | + OS_EMAIL_PROVIDER: 'smtp', |
| 88 | + OS_EMAIL_SMTP_HOST: 'smtp.x', |
| 89 | + }); |
| 90 | + expect(options.provider).toBe('smtp'); |
| 91 | + expect(warning).toBeUndefined(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('keeps the pre-existing resend/postmark behaviour', () => { |
| 95 | + const withKey = resolveEmailCapabilityArg({}, { OS_EMAIL_PROVIDER: 'resend', OS_EMAIL_API_KEY: 're_x' }); |
| 96 | + expect(withKey.options).toMatchObject({ provider: 'resend', apiKey: 're_x' }); |
| 97 | + expect(withKey.warning).toBeUndefined(); |
| 98 | + |
| 99 | + const noKey = resolveEmailCapabilityArg({}, { OS_EMAIL_PROVIDER: 'postmark' }); |
| 100 | + expect(noKey.options.provider).toBe('log'); |
| 101 | + expect(noKey.warning).toMatch(/no apiKey found/); |
| 102 | + }); |
| 103 | + |
| 104 | + it('still derives the fallback from-address and template context', () => { |
| 105 | + const { options } = resolveEmailCapabilityArg({}, { OS_APP_NAME: 'Acme CRM' }, 'ignored'); |
| 106 | + expect(options.defaultTemplateContext).toMatchObject({ appName: 'Acme CRM' }); |
| 107 | + expect(options.defaultFrom).toEqual({ name: 'Acme CRM', address: 'no-reply@acme-crm.local' }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments