feat(notifications): deliver online/offline alerts over SMTP

Fixes #459
This commit is contained in:
DrSmoothl committed 2026-09-16 10:36:04 +08:00
1 parent 3cd35a76dd
commit f2d58d2f79
13 files changed
+1600 -109

No files matched your search

@@ -0,0 +1,91 @@
import { renderToStaticMarkup } from 'react-dom/server';
import { describe, expect, it, vi } from 'vitest';
import {
DEFAULT_EMAIL_BODY,
DEFAULT_WEBHOOK_BODY,
NotificationChannelDialog,
} from '../src/components/settings/notification-channel-dialog';
vi.mock('@/contexts/ThemeContext', () => ({
useTheme: () => ({ appearance: { disableMotion: true, reduceMotion: true } }),
}));
vi.mock('@/components/interior/modal', () => ({
Modal: ({
title,
description,
children,
footer,
}: {
title: string;
description?: string;
children?: unknown;
footer?: unknown;
}) => (
<div>
<h1>{title}</h1>
{description ? <p>{description}</p> : null}
{children}
<div>{footer}</div>
</div>
),
}));
describe('NotificationChannelDialog', () => {
it('shows webhook fields for a new webhook channel', () => {
const markup = renderToStaticMarkup(
<NotificationChannelDialog
open
onOpenChange={() => undefined}
isEdit={false}
initial={{
id: 'channel-1',
name: '',
type: 'webhook',
url: '',
bodyTemplate: DEFAULT_WEBHOOK_BODY,
enabled: true,
}}
otherIds={[]}
onSubmit={() => undefined}
/>,
);
expect(markup).toContain('Webhook URL');
expect(markup).toContain('请求头');
expect(markup).not.toContain('SMTP 主机');
expect(markup).toContain('向该 Webhook POST');
});
it('shows SMTP fields for an email channel', () => {
const markup = renderToStaticMarkup(
<NotificationChannelDialog
open
onOpenChange={() => undefined}
isEdit
initial={{
id: 'ops-mail',
name: '运维邮箱',
type: 'email',
url: '',
bodyTemplate: DEFAULT_EMAIL_BODY,
enabled: true,
smtpHost: 'smtp.qq.com',
smtpPort: 465,
smtpSecure: true,
from: '[email protected]',
to: '[email protected]',
}}
otherIds={[]}
onSubmit={() => undefined}
/>,
);
expect(markup).toContain('SMTP 主机');
expect(markup).toContain('smtp.qq.com');
expect(markup).toContain('发件人');
expect(markup).toContain('收件人');
expect(markup).toContain('主题模板');
expect(markup).toContain('隐式 TLS');
expect(markup).not.toContain('Webhook URL');
expect(markup).toContain('向该邮箱发送');
});
});
@@ -0,0 +1,107 @@
import { describe, expect, it } from 'vitest';
import {
applyType,
DEFAULT_EMAIL_BODY,
DEFAULT_EMAIL_SUBJECT,
DEFAULT_WEBHOOK_BODY,
toChannel,
} from '../src/components/settings/notification-channel-dialog';
import { notificationChannelType, type NotificationChannel } from '../src/types';
function webhook(over: Partial<NotificationChannel> = {}): NotificationChannel {
return {
id: 'hook',
name: 'Hook',
type: 'webhook',
url: 'https://example.com/hook',
bodyTemplate: DEFAULT_WEBHOOK_BODY,
enabled: true,
...over,
};
}
describe('notificationChannelType', () => {
it('treats a missing type as webhook', () => {
expect(notificationChannelType({ type: undefined })).toBe('webhook');
expect(notificationChannelType({ type: 'email' })).toBe('email');
});
});
describe('applyType', () => {
it('swaps the default webhook body for the email body when switching', () => {
const next = applyType(webhook(), 'email');
expect(next.type).toBe('email');
expect(next.url).toBe('');
expect(next.bodyTemplate).toBe(DEFAULT_EMAIL_BODY);
expect(next.subjectTemplate).toBe(DEFAULT_EMAIL_SUBJECT);
expect(next.smtpPort).toBe(465);
expect(next.smtpSecure).toBe(true);
});
it('keeps a custom body when switching to email', () => {
const next = applyType(webhook({ bodyTemplate: 'custom {event}' }), 'email');
expect(next.bodyTemplate).toBe('custom {event}');
});
it('drops SMTP fields when switching back to webhook', () => {
const email = applyType(webhook(), 'email');
const back = applyType({ ...email, smtpPass: 'secret' }, 'webhook');
expect(back.type).toBe('webhook');
expect(back).not.toHaveProperty('smtpPass');
expect(back).not.toHaveProperty('smtpHost');
expect(back.bodyTemplate).toBe(DEFAULT_WEBHOOK_BODY);
});
});
describe('toChannel', () => {
it('emits a webhook without email fields and with trimmed headers', () => {
const ch = toChannel(webhook({ headers: { leftover: 'x' } }), [
{ name: ' Authorization ', value: ' Bearer a ' },
{ name: '', value: 'skip' },
]);
expect(ch).toEqual({
id: 'hook',
name: 'Hook',
type: 'webhook',
url: 'https://example.com/hook',
bodyTemplate: DEFAULT_WEBHOOK_BODY,
enabled: true,
headers: { Authorization: 'Bearer a' },
});
});
it('emits email fields and an empty url', () => {
const ch = toChannel({
id: ' mail ',
name: ' Ops ',
type: 'email',
url: 'https://ignored',
bodyTemplate: DEFAULT_EMAIL_BODY,
enabled: true,
smtpHost: ' smtp.qq.com ',
smtpPort: 587,
smtpSecure: false,
smtpUser: ' [email protected] ',
smtpPass: 'secret',
from: ' SnowLuma <[email protected]> ',
to: ' [email protected] ',
subjectTemplate: ' {event} ',
}, []);
expect(ch).toEqual({
id: 'mail',
name: 'Ops',
type: 'email',
url: '',
bodyTemplate: DEFAULT_EMAIL_BODY,
enabled: true,
smtpHost: 'smtp.qq.com',
smtpPort: 587,
smtpSecure: false,
smtpUser: '[email protected]',
smtpPass: 'secret',
from: 'SnowLuma <[email protected]>',
to: '[email protected]',
subjectTemplate: '{event}',
});
});
});