|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// Setup → Email states which transport is LIVE. The out-of-the-box `mail` |
| 4 | +// namespace stores provider `smtp` with an empty host, so the form reads as |
| 5 | +// SMTP while every message goes to the development log and is recorded as |
| 6 | +// sent. This pins the page's reading of `IEmailService.isConfigured()`: the |
| 7 | +// statement this plugin registers through `registerLiveStatus`, and that it |
| 8 | +// follows a transport swap made from the same page. |
| 9 | + |
| 10 | +import { describe, it, expect, vi } from 'vitest'; |
| 11 | +import { EmailServicePlugin } from './email-plugin.js'; |
| 12 | +import { EmailService, LogTransport } from './email-service.js'; |
| 13 | +import { SmtpTransport } from './transports/smtp.js'; |
| 14 | +import { describeLiveMailTransport, type LiveMailTransportStatus } from './live-transport-status.js'; |
| 15 | + |
| 16 | +vi.mock('nodemailer', () => { |
| 17 | + const createTransport = vi.fn(() => ({ sendMail: vi.fn(), close: vi.fn() })); |
| 18 | + return { default: { createTransport }, createTransport }; |
| 19 | +}); |
| 20 | + |
| 21 | +interface Resolved { value: unknown; source?: string } |
| 22 | + |
| 23 | +function fakeSettings(values: Record<string, Resolved>) { |
| 24 | + const listeners: Array<() => void> = []; |
| 25 | + let liveStatus: (() => Promise<LiveMailTransportStatus | undefined>) | undefined; |
| 26 | + return { |
| 27 | + createClient: () => ({}), |
| 28 | + getNamespace: async () => ({ values }), |
| 29 | + subscribe: (_ns: string, cb: () => void) => { listeners.push(cb); }, |
| 30 | + registerAction: () => {}, |
| 31 | + registerLiveStatus: (ns: string, fn: () => Promise<LiveMailTransportStatus | undefined>) => { |
| 32 | + if (ns === 'mail') liveStatus = fn; |
| 33 | + }, |
| 34 | + /** Patch the snapshot and emit settings:changed, WITHOUT waiting for the re-apply. */ |
| 35 | + emitSave(patch: Record<string, Resolved>) { |
| 36 | + Object.assign(values, patch); |
| 37 | + for (const l of listeners) l(); |
| 38 | + }, |
| 39 | + /** What the page states, as the settings GET route asks for it. */ |
| 40 | + read: () => liveStatus!(), |
| 41 | + get registered() { return liveStatus !== undefined; }, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +async function boot(mailValues: Record<string, Resolved>) { |
| 46 | + const settings = fakeSettings({ |
| 47 | + provider: { value: 'smtp', source: 'default' }, |
| 48 | + smtp_port: { value: 587, source: 'default' }, |
| 49 | + from_email: { value: 'no-reply@example.test', source: 'global' }, |
| 50 | + ...mailValues, |
| 51 | + }); |
| 52 | + // Only what this boot reads: the settings binding waits for an engine, and |
| 53 | + // nothing in these tests writes a row. |
| 54 | + const engine = { async find() { return []; } }; |
| 55 | + const services: Record<string, unknown> = { manifest: { register: () => {} }, objectql: engine, settings }; |
| 56 | + const hooks: Record<string, Array<() => Promise<void> | void>> = {}; |
| 57 | + const ctx = { |
| 58 | + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, |
| 59 | + getService: <T,>(name: string): T => { |
| 60 | + if (!(name in services)) throw new Error(`service '${name}' not registered`); |
| 61 | + return services[name] as T; |
| 62 | + }, |
| 63 | + registerService: (name: string, svc: unknown) => { services[name] = svc; }, |
| 64 | + hook: (name: string, fn: () => Promise<void> | void) => { (hooks[name] ??= []).push(fn); }, |
| 65 | + }; |
| 66 | + const plugin = new EmailServicePlugin({}); |
| 67 | + await plugin.init(ctx as never); |
| 68 | + await plugin.start(ctx as never); |
| 69 | + for (const fn of hooks['kernel:ready'] ?? []) await fn(); |
| 70 | + return { settings, service: services.email as EmailService }; |
| 71 | +} |
| 72 | + |
| 73 | +describe('describeLiveMailTransport — the reading of isConfigured()', () => { |
| 74 | + it('isConfigured() false: says nothing leaves this machine', () => { |
| 75 | + const svc = new EmailService({ transport: new LogTransport() }); |
| 76 | + expect(svc.isConfigured()).toBe(false); |
| 77 | + const s = describeLiveMailTransport(svc); |
| 78 | + expect(s.bannerSeverity).toBe('warning'); |
| 79 | + expect(s.label).toBe('No mail leaves this machine'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('isConfigured() true: names the live transport', () => { |
| 83 | + const svc = new EmailService({ transport: new SmtpTransport({ host: 'smtp.example.test', port: 2525 }) }); |
| 84 | + expect(svc.isConfigured()).toBe(true); |
| 85 | + const s = describeLiveMailTransport(svc); |
| 86 | + expect(s.bannerSeverity).toBe('info'); |
| 87 | + expect(s.label).toBe('Mail is delivered through SMTP (smtp.example.test:2525)'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('the branch is isConfigured(), not the transport class', () => { |
| 91 | + const smtp = new SmtpTransport({ host: 'smtp.example.test' }); |
| 92 | + expect(describeLiveMailTransport({ isConfigured: () => false, options: { transport: smtp } }).label) |
| 93 | + .toBe('No mail leaves this machine'); |
| 94 | + expect(describeLiveMailTransport({ isConfigured: () => true, options: { transport: new LogTransport() } }).bannerSeverity) |
| 95 | + .toBe('info'); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('EmailServicePlugin — the statement Setup → Email makes', () => { |
| 100 | + it('the out-of-the-box state (provider smtp, no host) says nothing leaves this machine', async () => { |
| 101 | + const { settings, service } = await boot({}); |
| 102 | + expect(settings.registered).toBe(true); |
| 103 | + expect(service.isConfigured()).toBe(false); |
| 104 | + expect((await settings.read())?.label).toBe('No mail leaves this machine'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('follows a transport swap saved from the same page, read before the re-apply settles', async () => { |
| 108 | + const { settings, service } = await boot({}); |
| 109 | + expect((await settings.read())?.bannerSeverity).toBe('warning'); |
| 110 | + |
| 111 | + settings.emitSave({ |
| 112 | + provider: { value: 'smtp', source: 'global' }, |
| 113 | + smtp_host: { value: 'smtp.saved.test', source: 'global' }, |
| 114 | + }); |
| 115 | + // The re-apply is still running here; the statement waits for it. |
| 116 | + const after = await settings.read(); |
| 117 | + expect(service.isConfigured()).toBe(true); |
| 118 | + expect(after?.bannerSeverity).toBe('info'); |
| 119 | + expect(after?.label).toBe('Mail is delivered through SMTP (smtp.saved.test:587)'); |
| 120 | + }); |
| 121 | +}); |
0 commit comments