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
@@ -0,0 +1,97 @@
|
||||
import { IdentityService } from '@snowluma/protocol/identity-service';
|
||||
import type { PacketSender } from '@snowluma/common/packet-sender';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { BridgeManager } from '../src/bridge/manager';
|
||||
import { NotificationManager, type NotificationManagerDeps } from '../src/notifications/manager';
|
||||
|
||||
const sender: PacketSender = {
|
||||
sendPacket: async () => ({
|
||||
success: false,
|
||||
gotResponse: false,
|
||||
errorCode: -1,
|
||||
errorMessage: 'test: no QQ connection',
|
||||
responseData: null,
|
||||
}),
|
||||
};
|
||||
|
||||
describe('notification lifecycle PID', () => {
|
||||
let bridges: BridgeManager;
|
||||
let notifications: NotificationManager;
|
||||
const posts: string[] = [];
|
||||
const resolveProcess = vi.fn<NonNullable<NotificationManagerDeps['resolveProcess']>>(
|
||||
async (_uin, pid) => (pid === null ? undefined : { pid }),
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.spyOn(IdentityService, 'openForUin').mockImplementation((uin) =>
|
||||
IdentityService.memory(uin),
|
||||
);
|
||||
posts.length = 0;
|
||||
resolveProcess.mockClear();
|
||||
bridges = new BridgeManager();
|
||||
notifications = new NotificationManager({
|
||||
loadConfig: () => ({
|
||||
version: 1,
|
||||
debounceSeconds: 30,
|
||||
channels: [
|
||||
{
|
||||
id: 'c1',
|
||||
name: 'C1',
|
||||
type: 'webhook',
|
||||
url: 'https://hook.example/c1',
|
||||
enabled: true,
|
||||
bodyTemplate: '{event}:{uin}:{pid}',
|
||||
},
|
||||
],
|
||||
}),
|
||||
loadChannelIds: () => ['c1'],
|
||||
post: async (_url, body) => {
|
||||
posts.push(body);
|
||||
return { ok: true };
|
||||
},
|
||||
now: () => Date.now(),
|
||||
resolveProcess,
|
||||
});
|
||||
notifications.bind(bridges);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
notifications.dispose();
|
||||
await bridges.dispose();
|
||||
vi.restoreAllMocks();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('retains the final detached PID through debounce and uses the new PID on recovery', async () => {
|
||||
bridges.onHookLogin(1001, '12345', sender);
|
||||
bridges.onHookLogin(2002, '12345', sender);
|
||||
bridges.onPidDisconnected(1001);
|
||||
await vi.advanceTimersByTimeAsync(30_000);
|
||||
expect(posts).toEqual([]);
|
||||
|
||||
bridges.onPidDisconnected(2002);
|
||||
expect(bridges.getSession('12345')).toBeNull();
|
||||
await vi.advanceTimersByTimeAsync(30_000);
|
||||
expect(resolveProcess).toHaveBeenNthCalledWith(1, '12345', 2002);
|
||||
|
||||
bridges.onHookLogin(3003, '12345', sender);
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
expect(resolveProcess).toHaveBeenNthCalledWith(2, '12345', 3003);
|
||||
expect(posts).toEqual(['offline:12345:2002', 'online:12345:3003']);
|
||||
});
|
||||
|
||||
it('keeps queued event context when the same UIN recovers on another PID', async () => {
|
||||
bridges.onHookLogin(1001, '12345', sender);
|
||||
bridges.onPidDisconnected(1001);
|
||||
const offline = notifications.notify('12345', 'offline');
|
||||
bridges.onHookLogin(2002, '12345', sender);
|
||||
const online = notifications.notify('12345', 'online');
|
||||
await Promise.all([offline, online]);
|
||||
expect(resolveProcess.mock.calls).toEqual([
|
||||
['12345', 1001],
|
||||
['12345', 2002],
|
||||
]);
|
||||
expect(posts).toEqual(['offline:12345:1001', 'online:12345:2002']);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user