feat(notifications): P2 manager — debounce state machine + render/POST + history
Global singleton observing every account's online/offline edge: - Pure per-UIN debounce state machine (debounce.ts): an offline surviving debounceSeconds fires "offline"; recovery fires the paired "online"; a self-heal within the window fires nothing; a cold online fires nothing. - NotificationManager (manager.ts): renders each opted-in + enabled channel's body template and POSTs it (single attempt + timeout, JSON), recording the outcome in a bounded in-memory history (lost on restart). Side effects (config/channelIds/post/clock) are injected for testability. - BridgeManager: single-callback setters → additive listeners so OneBotManager and NotificationManager observe independently; close edge now carries the bridge (read nickname before dispose); fire-all with per-listener try/catch. - Wired into the composition root, bound after OneBotManager. Tests: debounce machine (pure, all misfire sequences) + manager (selectChannels, dispatch, history cap, fake-timer self-heal/pairing). 23 green, typecheck clean. Adversarial review: 0 blockers / 0 should-fix (2 NITs applied).
This commit is contained in:
7 files changed
+581
-14
No files matched your search
@@ -0,0 +1,71 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { DebounceMachine } from '../src/notifications/debounce';
|
||||
|
||||
describe('DebounceMachine — online↔offline debounce (Uptime-Kuma semantics)', () => {
|
||||
it('a cold online (no prior offline) fires nothing', () => {
|
||||
const m = new DebounceMachine();
|
||||
expect(m.onOnline('u')).toEqual({ kind: 'none' });
|
||||
});
|
||||
|
||||
it('an offline that self-heals within the window fires nothing', () => {
|
||||
const m = new DebounceMachine();
|
||||
expect(m.onOffline('u', 30)).toEqual({ kind: 'schedule', delayMs: 30_000 });
|
||||
expect(m.onOnline('u')).toEqual({ kind: 'cancel' });
|
||||
// back to baseline — a later offline schedules fresh
|
||||
expect(m.onOffline('u', 30)).toEqual({ kind: 'schedule', delayMs: 30_000 });
|
||||
});
|
||||
|
||||
it('an offline that survives the window fires offline, then recovery fires online (paired)', () => {
|
||||
const m = new DebounceMachine();
|
||||
expect(m.onOffline('u', 30)).toEqual({ kind: 'schedule', delayMs: 30_000 });
|
||||
expect(m.onTimerElapsed('u')).toEqual({ kind: 'emit', event: 'offline' });
|
||||
expect(m.onOnline('u')).toEqual({ kind: 'emit', event: 'online' });
|
||||
// after recovery completes, the next online is cold again
|
||||
expect(m.onOnline('u')).toEqual({ kind: 'none' });
|
||||
});
|
||||
|
||||
it('debounceSeconds<=0 fires offline immediately', () => {
|
||||
const m = new DebounceMachine();
|
||||
expect(m.onOffline('u', 0)).toEqual({ kind: 'emit', event: 'offline' });
|
||||
expect(m.onOnline('u')).toEqual({ kind: 'emit', event: 'online' });
|
||||
});
|
||||
|
||||
it('treats a non-finite debounceSeconds as no-debounce (fires immediately)', () => {
|
||||
const m = new DebounceMachine();
|
||||
expect(m.onOffline('u', Number.NaN)).toEqual({ kind: 'emit', event: 'offline' });
|
||||
});
|
||||
|
||||
it('a second offline while pending is a no-op', () => {
|
||||
const m = new DebounceMachine();
|
||||
expect(m.onOffline('u', 30)).toEqual({ kind: 'schedule', delayMs: 30_000 });
|
||||
expect(m.onOffline('u', 30)).toEqual({ kind: 'none' });
|
||||
});
|
||||
|
||||
it('a second offline after one already fired is a no-op', () => {
|
||||
const m = new DebounceMachine();
|
||||
m.onOffline('u', 0); // fires offline
|
||||
expect(m.onOffline('u', 0)).toEqual({ kind: 'none' });
|
||||
});
|
||||
|
||||
it('a timer elapsing after a cancel is a no-op (stale timer)', () => {
|
||||
const m = new DebounceMachine();
|
||||
m.onOffline('u', 30); // schedule
|
||||
m.onOnline('u'); // cancel
|
||||
expect(m.onTimerElapsed('u')).toEqual({ kind: 'none' });
|
||||
});
|
||||
|
||||
it('tracks UINs independently', () => {
|
||||
const m = new DebounceMachine();
|
||||
expect(m.onOffline('a', 0)).toEqual({ kind: 'emit', event: 'offline' });
|
||||
expect(m.onOffline('b', 30)).toEqual({ kind: 'schedule', delayMs: 30_000 });
|
||||
expect(m.onOnline('b')).toEqual({ kind: 'cancel' });
|
||||
expect(m.onOnline('a')).toEqual({ kind: 'emit', event: 'online' });
|
||||
});
|
||||
|
||||
it('forget() resets a UIN to baseline', () => {
|
||||
const m = new DebounceMachine();
|
||||
m.onOffline('u', 0); // fired offline
|
||||
m.forget('u');
|
||||
expect(m.onOnline('u')).toEqual({ kind: 'none' }); // state cleared → no recovery
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,151 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
NotificationManager,
|
||||
selectChannels,
|
||||
type NotificationManagerDeps,
|
||||
type PostResult,
|
||||
} from '../src/notifications/manager';
|
||||
import type { NotificationChannel, NotificationsConfig } from '../src/notifications/config';
|
||||
|
||||
function ch(over: Partial<NotificationChannel> = {}): NotificationChannel {
|
||||
return {
|
||||
id: 'c1',
|
||||
name: 'C1',
|
||||
url: 'https://hook.example/c1',
|
||||
bodyTemplate: '{event}:{uin}:{nickname}',
|
||||
enabled: true,
|
||||
...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' })];
|
||||
expect(selectChannels(channels, ['c', 'a', 'b']).map((c) => c.id)).toEqual(['a', 'c']);
|
||||
});
|
||||
|
||||
it('is empty when the UIN opted into nothing', () => {
|
||||
expect(selectChannels([ch({ id: 'a' })], [])).toEqual([]);
|
||||
});
|
||||
|
||||
it('ignores opted-in ids with no matching channel', () => {
|
||||
expect(selectChannels([ch({ id: 'a' })], ['ghost'])).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('NotificationManager — dispatch + history', () => {
|
||||
function setup(over: Partial<NotificationManagerDeps> = {}) {
|
||||
const posts: { url: string; body: string }[] = [];
|
||||
const cfg: NotificationsConfig = { version: 1, debounceSeconds: 30, channels: [ch()] };
|
||||
const deps: NotificationManagerDeps = {
|
||||
loadConfig: () => cfg,
|
||||
loadChannelIds: () => ['c1'],
|
||||
post: async (url, body): Promise<PostResult> => {
|
||||
posts.push({ url, body });
|
||||
return { ok: true, status: 200 };
|
||||
},
|
||||
now: () => 1000,
|
||||
...over,
|
||||
};
|
||||
return { posts, mgr: new NotificationManager(deps) };
|
||||
}
|
||||
|
||||
it('renders {event}{uin}{nickname} and posts to opted-in channels', async () => {
|
||||
const { posts, mgr } = setup();
|
||||
await mgr.notify('123', 'offline');
|
||||
// nickname falls back to the uin when none was captured
|
||||
expect(posts).toEqual([{ url: 'https://hook.example/c1', body: 'offline:123:123' }]);
|
||||
});
|
||||
|
||||
it('uses the nickname captured on an online/offline edge', async () => {
|
||||
const { posts, mgr } = setup();
|
||||
mgr.handleOnline('123', 'Alice'); // caches nickname; cold online → no post
|
||||
expect(posts).toHaveLength(0);
|
||||
await mgr.notify('123', 'offline');
|
||||
expect(posts[0].body).toBe('offline:123:Alice');
|
||||
});
|
||||
|
||||
it('records ok and failed deliveries, most-recent-first', async () => {
|
||||
let n = 0;
|
||||
const { mgr } = setup({
|
||||
post: async (): Promise<PostResult> => (++n === 1 ? { ok: true, status: 200 } : { ok: false, status: 500 }),
|
||||
});
|
||||
await mgr.notify('123', 'offline');
|
||||
await mgr.notify('123', 'online');
|
||||
const recent = mgr.getRecent();
|
||||
expect(recent).toHaveLength(2);
|
||||
expect(recent[0]).toMatchObject({ event: 'online', ok: false, status: 500 });
|
||||
expect(recent[1]).toMatchObject({ event: 'offline', ok: true, status: 200 });
|
||||
});
|
||||
|
||||
it('skips channels the UIN did not opt into', async () => {
|
||||
const { posts, mgr } = setup({ loadChannelIds: () => [] });
|
||||
await mgr.notify('123', 'offline');
|
||||
expect(posts).toEqual([]);
|
||||
});
|
||||
|
||||
it('a post that throws is captured as a failed record, not propagated', async () => {
|
||||
const { mgr } = setup({
|
||||
post: async (): Promise<PostResult> => {
|
||||
throw new Error('boom');
|
||||
},
|
||||
});
|
||||
await expect(mgr.notify('123', 'offline')).resolves.toBeUndefined();
|
||||
expect(mgr.getRecent()[0]).toMatchObject({ ok: false, error: 'boom' });
|
||||
});
|
||||
|
||||
it('caps history at the configured limit (oldest dropped)', async () => {
|
||||
const { mgr } = setup({ historyLimit: 3 });
|
||||
for (let i = 0; i < 5; i++) await mgr.notify('123', 'offline');
|
||||
expect(mgr.getRecent(99)).toHaveLength(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('NotificationManager — debounce integration (fake timers)', () => {
|
||||
beforeEach(() => vi.useFakeTimers());
|
||||
afterEach(() => vi.useRealTimers());
|
||||
|
||||
function setup(debounceSeconds: number) {
|
||||
const events: string[] = [];
|
||||
const deps: NotificationManagerDeps = {
|
||||
loadConfig: () => ({ version: 1, debounceSeconds, channels: [ch()] }),
|
||||
loadChannelIds: () => ['c1'],
|
||||
post: async (_url, body) => {
|
||||
events.push(body.split(':')[0]);
|
||||
return { ok: true, status: 200 };
|
||||
},
|
||||
now: () => 1000,
|
||||
};
|
||||
return { events, mgr: new NotificationManager(deps) };
|
||||
}
|
||||
|
||||
it('suppresses an offline that self-heals within the window', async () => {
|
||||
const { events, mgr } = setup(30);
|
||||
mgr.handleOnline('123', 'Bob'); // cold online
|
||||
mgr.handleOffline('123', 'Bob'); // arm 30s timer
|
||||
await vi.advanceTimersByTimeAsync(10_000);
|
||||
mgr.handleOnline('123', 'Bob'); // self-heal → cancel timer
|
||||
// The machine's state guard alone would suppress the misfire; assert the
|
||||
// manager ALSO cleared the timer (defense-in-depth a pure test can't see).
|
||||
expect((mgr as unknown as { timers: Map<string, unknown> }).timers.size).toBe(0);
|
||||
await vi.advanceTimersByTimeAsync(60_000);
|
||||
expect(events).toEqual([]);
|
||||
});
|
||||
|
||||
it('fires offline after the window, then online on recovery (paired)', async () => {
|
||||
const { events, mgr } = setup(30);
|
||||
mgr.handleOffline('123', 'Bob'); // arm
|
||||
await vi.advanceTimersByTimeAsync(30_000); // window elapses → offline fires
|
||||
expect(events).toEqual(['offline']);
|
||||
mgr.handleOnline('123', 'Bob'); // recovery
|
||||
await vi.advanceTimersByTimeAsync(0); // flush the dispatch microtasks
|
||||
expect(events).toEqual(['offline', 'online']);
|
||||
});
|
||||
|
||||
it('with debounce 0, offline fires immediately', async () => {
|
||||
const { events, mgr } = setup(0);
|
||||
mgr.handleOffline('123', 'Bob');
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
expect(events).toEqual(['offline']);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user