Files
SnowLuma/packages/core/tests/notifications-debounce.test.ts
DrSmoothl 30bcd97a3f 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).
2026-06-16 19:20:55 +08:00

72 lines
3.0 KiB
TypeScript

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
});
});