feat(core): add process facts to ordered account notifications (#539)
* feat(core): carry hook process status in notifications `offline` alone cannot tell an external supervisor whether the QQ session dropped (log back in) or the process is gone (restart the client). Resolve the hook process behind a UIN at notify time and expose `status`, `pid` and `error` as template variables; a failing lookup degrades to empty values so delivery is never blocked. The default webhook/email templates gain a process-status line; existing channels keep their stored template untouched. * fix(core): preserve notification lifecycle ordering and simplify state --------- Co-authored-by: zhngzhu <[email protected]> Co-authored-by: 手瓜一十雪 <[email protected]>
This commit is contained in:
19 files changed
+616
-387
No files matched your search
@@ -1,10 +1,12 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { subscribeLogs, type LogEntry } from '@snowluma/common/logger';
|
||||
import {
|
||||
NotificationManager,
|
||||
createDefaultPost,
|
||||
selectChannels,
|
||||
type MailPayload,
|
||||
type NotificationManagerDeps,
|
||||
type NotificationProcessFacts,
|
||||
type PostResult,
|
||||
} from '../src/notifications/manager';
|
||||
import type { NotificationChannel, NotificationsConfig } from '../src/notifications/config';
|
||||
@@ -105,6 +107,48 @@ describe('NotificationManager — dispatch + history', () => {
|
||||
expect(posts[0].body).toBe('offline:123:Alice');
|
||||
});
|
||||
|
||||
it('carries process facts into the template so a supervisor can act on them', async () => {
|
||||
const { posts, mgr } = setup({
|
||||
loadConfig: () => ({
|
||||
version: 1,
|
||||
debounceSeconds: 30,
|
||||
channels: [ch({ bodyTemplate: '{event}:{uin}:{status}:{pid}:{error}' })],
|
||||
}),
|
||||
resolveProcess: async () => ({ status: 'disconnected', pid: 4242, error: '' }),
|
||||
});
|
||||
await mgr.notify('123', 'offline');
|
||||
expect(posts[0].body).toBe('offline:123:disconnected:4242:');
|
||||
});
|
||||
|
||||
it('renders fact variables empty when no resolver is configured', async () => {
|
||||
const { posts, mgr } = setup({
|
||||
loadConfig: () => ({
|
||||
version: 1,
|
||||
debounceSeconds: 30,
|
||||
channels: [ch({ bodyTemplate: '{event}:{status}:{pid}' })],
|
||||
}),
|
||||
});
|
||||
await mgr.notify('123', 'offline');
|
||||
// No resolver → empty, not the literal placeholders.
|
||||
expect(posts[0].body).toBe('offline::');
|
||||
});
|
||||
|
||||
it('still notifies when the resolver throws', async () => {
|
||||
const { posts, mgr } = setup({
|
||||
loadConfig: () => ({
|
||||
version: 1,
|
||||
debounceSeconds: 30,
|
||||
channels: [ch({ bodyTemplate: '{event}:{status}' })],
|
||||
}),
|
||||
resolveProcess: async () => {
|
||||
throw new Error('hook manager unavailable');
|
||||
},
|
||||
});
|
||||
await mgr.notify('123', 'offline');
|
||||
expect(posts).toHaveLength(1);
|
||||
expect(posts[0].body).toBe('offline:');
|
||||
});
|
||||
|
||||
it('records ok and failed deliveries, most-recent-first', async () => {
|
||||
let n = 0;
|
||||
const { mgr } = setup({
|
||||
@@ -119,6 +163,90 @@ describe('NotificationManager — dispatch + history', () => {
|
||||
expect(recent[1]).toMatchObject({ event: 'offline', ok: true, status: 200 });
|
||||
});
|
||||
|
||||
it('keeps offline before recovery when the offline process lookup is delayed', async () => {
|
||||
let resolve!: (facts: NotificationProcessFacts) => void;
|
||||
const delayed = new Promise<NotificationProcessFacts>((done) => {
|
||||
resolve = done;
|
||||
});
|
||||
const resolveProcess = vi
|
||||
.fn()
|
||||
.mockReturnValueOnce(delayed)
|
||||
.mockResolvedValue({ status: 'online', pid: 4242 });
|
||||
const { posts, mgr } = setup({ resolveProcess });
|
||||
const offline = mgr.notify('123', 'offline');
|
||||
const online = mgr.notify('123', 'online');
|
||||
await new Promise<void>((done) => setImmediate(done));
|
||||
const whileResolving = [...posts];
|
||||
resolve({ status: 'disconnected', pid: 4242 });
|
||||
await Promise.all([offline, online]);
|
||||
expect(whileResolving).toEqual([]);
|
||||
expect(posts.map((post) => post.body)).toEqual(['offline:123:123', 'online:123:123']);
|
||||
});
|
||||
|
||||
it('finishes every channel before sending the recovery, even when a delivery fails', async () => {
|
||||
let reject!: (error: Error) => void;
|
||||
const delayed = new Promise<PostResult>((_resolve, fail) => {
|
||||
reject = fail;
|
||||
});
|
||||
const deliveries: string[] = [];
|
||||
const { mgr } = setup({
|
||||
loadConfig: () => ({
|
||||
version: 1,
|
||||
debounceSeconds: 0,
|
||||
channels: [ch(), ch({ id: 'c2' })],
|
||||
}),
|
||||
loadChannelIds: () => ['c1', 'c2'],
|
||||
post: async (_url, body) => {
|
||||
deliveries.push(body);
|
||||
return deliveries.length === 1 ? delayed : { ok: true };
|
||||
},
|
||||
});
|
||||
const offline = mgr.notify('123', 'offline');
|
||||
const online = mgr.notify('123', 'online');
|
||||
await new Promise<void>((done) => setImmediate(done));
|
||||
reject(new Error('delivery failed'));
|
||||
await Promise.all([offline, online]);
|
||||
expect(deliveries).toEqual([
|
||||
'offline:123:123',
|
||||
'offline:123:123',
|
||||
'online:123:123',
|
||||
'online:123:123',
|
||||
]);
|
||||
expect(mgr.getRecent().filter((record) => !record.ok)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('does not block another account behind a slow process lookup', async () => {
|
||||
let resolve!: (facts: undefined) => void;
|
||||
const delayed = new Promise<undefined>((done) => {
|
||||
resolve = done;
|
||||
});
|
||||
const { posts, mgr } = setup({
|
||||
resolveProcess: (uin) => (uin === '123' ? delayed : Promise.resolve(undefined)),
|
||||
});
|
||||
const slow = mgr.notify('123', 'offline');
|
||||
await mgr.notify('456', 'offline');
|
||||
const whileResolving = [...posts];
|
||||
resolve(undefined);
|
||||
await slow;
|
||||
expect(whileResolving.map((post) => post.body)).toEqual(['offline:456:456']);
|
||||
expect(posts.map((post) => post.body)).toEqual(['offline:456:456', 'offline:123:123']);
|
||||
});
|
||||
|
||||
it('drops queued notifications on disposal and does not deliver after a pending lookup', async () => {
|
||||
let resolve!: (facts: undefined) => void;
|
||||
const delayed = new Promise<undefined>((done) => {
|
||||
resolve = done;
|
||||
});
|
||||
const { posts, mgr } = setup({ resolveProcess: () => delayed });
|
||||
const offline = mgr.notify('123', 'offline');
|
||||
const online = mgr.notify('123', 'online');
|
||||
await new Promise<void>((done) => setImmediate(done));
|
||||
mgr.dispose();
|
||||
resolve(undefined);
|
||||
await Promise.all([offline, online]);
|
||||
expect(posts).toEqual([]);
|
||||
});
|
||||
|
||||
it('skips channels the UIN did not opt into', async () => {
|
||||
const { posts, mgr } = setup({ loadChannelIds: () => [] });
|
||||
await mgr.notify('123', 'offline');
|
||||
@@ -309,9 +437,7 @@ describe('NotificationManager — debounce integration (fake timers)', () => {
|
||||
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);
|
||||
expect(vi.getTimerCount()).toBe(0);
|
||||
await vi.advanceTimersByTimeAsync(60_000);
|
||||
expect(events).toEqual([]);
|
||||
});
|
||||
@@ -333,6 +459,50 @@ describe('NotificationManager — debounce integration (fake timers)', () => {
|
||||
expect(events).toEqual(['offline']);
|
||||
});
|
||||
|
||||
it('reports config failure without inventing a debounce window or a recovery event', async () => {
|
||||
const loadConfig = vi.fn<NotificationManagerDeps['loadConfig']>().mockImplementation(() => {
|
||||
throw new Error('config unreadable');
|
||||
});
|
||||
const post = vi.fn<NotificationManagerDeps['post']>().mockResolvedValue({ ok: true });
|
||||
const mgr = new NotificationManager({
|
||||
loadConfig,
|
||||
loadChannelIds: () => ['c1'],
|
||||
post,
|
||||
now: () => 1000,
|
||||
});
|
||||
const logs: LogEntry[] = [];
|
||||
const unsubscribe = subscribeLogs((entry) => logs.push(entry));
|
||||
|
||||
try {
|
||||
mgr.handleOffline('123', 'Bob');
|
||||
expect(vi.getTimerCount()).toBe(0);
|
||||
expect(logs).toContainEqual(
|
||||
expect.objectContaining({
|
||||
scope: 'Notifications',
|
||||
level: 'warn',
|
||||
message: 'offline(123) skipped — config load failed: config unreadable',
|
||||
}),
|
||||
);
|
||||
|
||||
loadConfig.mockReturnValue({ version: 1, debounceSeconds: 5, channels: [ch()] });
|
||||
await vi.advanceTimersByTimeAsync(30_000);
|
||||
mgr.handleOnline('123', 'Bob');
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
expect(post).not.toHaveBeenCalled();
|
||||
|
||||
mgr.handleOffline('123', 'Bob');
|
||||
await vi.advanceTimersByTimeAsync(5_000);
|
||||
expect(post).toHaveBeenCalledExactlyOnceWith(
|
||||
'https://hook.example/c1',
|
||||
'offline:123:Bob',
|
||||
undefined,
|
||||
);
|
||||
} finally {
|
||||
unsubscribe();
|
||||
mgr.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
it('ignores online/offline after dispose so process exit is not a runtime drop', async () => {
|
||||
const { events, mgr } = setup(0);
|
||||
mgr.dispose();
|
||||
|
||||
Reference in new issue
Block a user