feat(notifications): deliver online/offline alerts over SMTP
Fixes #459
This commit is contained in:
13 files changed
+1600
-109
No files matched your search
@@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
NotificationManager,
|
||||
selectChannels,
|
||||
type MailPayload,
|
||||
type NotificationManagerDeps,
|
||||
type PostResult,
|
||||
} from '../src/notifications/manager';
|
||||
@@ -11,6 +12,7 @@ function ch(over: Partial<NotificationChannel> = {}): NotificationChannel {
|
||||
return {
|
||||
id: 'c1',
|
||||
name: 'C1',
|
||||
type: 'webhook',
|
||||
url: 'https://hook.example/c1',
|
||||
bodyTemplate: '{event}:{uin}:{nickname}',
|
||||
enabled: true,
|
||||
@@ -18,6 +20,26 @@ function ch(over: Partial<NotificationChannel> = {}): NotificationChannel {
|
||||
};
|
||||
}
|
||||
|
||||
function emailCh(over: Partial<NotificationChannel> = {}): NotificationChannel {
|
||||
return {
|
||||
id: 'mail',
|
||||
name: 'Mail',
|
||||
type: 'email',
|
||||
url: '',
|
||||
bodyTemplate: '{event}:{uin}:{nickname}',
|
||||
enabled: true,
|
||||
smtpHost: 'smtp.example.com',
|
||||
smtpPort: 465,
|
||||
smtpSecure: true,
|
||||
smtpUser: '[email protected]',
|
||||
smtpPass: 'secret',
|
||||
from: '[email protected]',
|
||||
to: '[email protected]',
|
||||
subjectTemplate: '{event} {uin}',
|
||||
...over,
|
||||
};
|
||||
}
|
||||
|
||||
describe('selectChannels (pure)', () => {
|
||||
it('keeps only enabled channels the UIN opted into, in config order', () => {
|
||||
const channels = [ch({ id: 'a' }), ch({ id: 'b', enabled: false }), ch({ id: 'c' })];
|
||||
@@ -166,6 +188,82 @@ describe('NotificationManager.testSend', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('NotificationManager — email channels', () => {
|
||||
it('sends mail instead of POSTing, with rendered subject and body', async () => {
|
||||
const posts: unknown[] = [];
|
||||
const mails: MailPayload[] = [];
|
||||
const mgr = new NotificationManager({
|
||||
loadConfig: () => ({ version: 1, debounceSeconds: 30, channels: [emailCh()] }),
|
||||
loadChannelIds: () => ['mail'],
|
||||
post: async (url, body) => {
|
||||
posts.push({ url, body });
|
||||
return { ok: true, status: 200 };
|
||||
},
|
||||
sendMail: async (mail) => {
|
||||
mails.push(mail);
|
||||
return { ok: true };
|
||||
},
|
||||
now: () => 1000,
|
||||
});
|
||||
await mgr.notify('123', 'offline');
|
||||
expect(posts).toEqual([]);
|
||||
expect(mails).toEqual([{
|
||||
host: 'smtp.example.com',
|
||||
port: 465,
|
||||
secure: true,
|
||||
user: '[email protected]',
|
||||
pass: 'secret',
|
||||
from: '[email protected]',
|
||||
to: '[email protected]',
|
||||
subject: 'offline 123',
|
||||
text: 'offline:123:123',
|
||||
}]);
|
||||
});
|
||||
|
||||
it('records a failed sendMail without calling post', async () => {
|
||||
const mgr = new NotificationManager({
|
||||
loadConfig: () => ({ version: 1, debounceSeconds: 30, channels: [emailCh()] }),
|
||||
loadChannelIds: () => ['mail'],
|
||||
post: async () => {
|
||||
throw new Error('webhook must not run');
|
||||
},
|
||||
sendMail: async () => ({ ok: false, error: 'SMTP 认证失败' }),
|
||||
now: () => 1000,
|
||||
});
|
||||
await mgr.notify('123', 'offline');
|
||||
expect(mgr.getRecent()[0]).toMatchObject({ ok: false, error: 'SMTP 认证失败', channelId: 'mail' });
|
||||
});
|
||||
|
||||
it('fails closed when sendMail is not wired', async () => {
|
||||
const mgr = new NotificationManager({
|
||||
loadConfig: () => ({ version: 1, debounceSeconds: 30, channels: [emailCh()] }),
|
||||
loadChannelIds: () => ['mail'],
|
||||
post: async () => ({ ok: true, status: 200 }),
|
||||
now: () => 1000,
|
||||
});
|
||||
await mgr.notify('123', 'offline');
|
||||
expect(mgr.getRecent()[0]).toMatchObject({ ok: false, error: 'email delivery is not configured' });
|
||||
});
|
||||
|
||||
it('testSend uses sendMail and still skips history', async () => {
|
||||
const mails: MailPayload[] = [];
|
||||
const mgr = new NotificationManager({
|
||||
loadConfig: () => ({ version: 1, debounceSeconds: 30, channels: [emailCh({ enabled: false })] }),
|
||||
loadChannelIds: () => [],
|
||||
post: async () => ({ ok: true, status: 200 }),
|
||||
sendMail: async (mail) => {
|
||||
mails.push(mail);
|
||||
return { ok: true };
|
||||
},
|
||||
now: () => 1000,
|
||||
});
|
||||
expect(await mgr.testSend('mail')).toMatchObject({ found: true, ok: true });
|
||||
expect(mails[0]?.subject).toBe('offline 10000');
|
||||
expect(mails[0]?.text).toBe('offline:10000:测试账号');
|
||||
expect(mgr.getRecent()).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('NotificationManager — debounce integration (fake timers)', () => {
|
||||
beforeEach(() => vi.useFakeTimers());
|
||||
afterEach(() => vi.useRealTimers());
|
||||
|
||||
Reference in new issue
Block a user