refactor(core): split HookManager into PipeWatcher + HookSession
HookManager was a 515-line god class with four tangled responsibilities
(state machine, watcher loop, per-PID serialization, lifecycle plumbing)
and no tests — every code path needed a real QQ.exe to exercise.
Split into three modules:
- PipeWatcher: the only module that polls the OS for QQ.exe processes
and live SnowLuma named pipes. Emits diff events (process-discovered,
process-gone, pipe-up, pipe-down, tick). Native list functions are
constructor deps so tests can drive ticks with in-memory fakes.
- HookSession: per-PID lifecycle. Owns its own state machine, its own
promise chain (serializes load/unload/refresh/onPipeUp/onPipeDown so
rapid user clicks don't race), and its own QqHookClient. Emits
high-level events (login, disconnected, status-changed, disposed)
that the manager forwards instead of reaching into BridgeManager
directly. Injector and pipe-client factory are deps for testability.
- HookManager: shrinks to a thin orchestrator (227 lines, was 515).
Routes user commands and watcher events to sessions, forwards session
events to BridgeManager, retries stuck-in-connecting sessions on every
watcher tick (preserves the original tickWatcher reconcile pass).
Public API (listProcesses/loadProcess/unloadProcess/refreshProcess)
unchanged for WebUI callers; only the constructor moves to a deps
object: new HookManager({ ntqq, bridgeManager }).
Behavior preserved including all the edge cases the original code
handles: re-using a SnowLuma DLL that survived a process restart
(fast-path via pipeWatcher.isPipeLive), stale close events from a
replaced client, DLL-unload-failed fallback to "watcher will
reconnect", QQ.exe killed with a live pipe (pipe-down emitted before
process-gone so disconnect runs before dispose).
Adds 27 unit tests across two new vitest suites (hook-session.test.ts
and pipe-watcher.test.ts) covering load fast-path, login emission,
duplicate-login suppression, connect retry on failure, pipe up/down
cycles, unload while logged in, unload verification failure, user
command serialization, and process-gone handling — none of which
previously had test coverage.
This commit is contained in:
1 parent
5ce8aa60de
commit
4fc52578a5
7 files changed
+1338
-468
No files matched your search
@@ -0,0 +1,309 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { EventEmitter } from 'events';
|
||||
import { HookSession } from '../src/hook/hook-session';
|
||||
import type { ManualMapHandle } from '../src/hook/injector';
|
||||
import type { QqHookClient } from '../src/hook/qq-hook-client';
|
||||
|
||||
const DUMMY_HANDLE: ManualMapHandle = { base: 0n, entry: 0n, exceptionTable: 0n, size: 0 };
|
||||
|
||||
/** Minimal stand-in for QqHookClient. Tests drive login/packet/error/close
|
||||
* events directly via fire* helpers. connectAll succeeds unless told otherwise. */
|
||||
class FakeClient extends EventEmitter {
|
||||
isClosed = false;
|
||||
isLoggedIn = false;
|
||||
shouldFailConnect = false;
|
||||
private loginState = { loggedIn: false, uin: '0', uinNumber: 0n };
|
||||
|
||||
async connectAll(_opts: { recv: boolean }): Promise<void> {
|
||||
if (this.shouldFailConnect) throw new Error('connect failed');
|
||||
if (this.isClosed) throw new Error('client is closed');
|
||||
}
|
||||
getLoginState() { return { ...this.loginState }; }
|
||||
close(): void {
|
||||
if (this.isClosed) return;
|
||||
this.isClosed = true;
|
||||
this.emit('close');
|
||||
}
|
||||
|
||||
fireLogin(uin: string): void {
|
||||
this.isLoggedIn = true;
|
||||
this.loginState = { loggedIn: true, uin, uinNumber: BigInt(uin) };
|
||||
this.emit('loginState', { ...this.loginState });
|
||||
}
|
||||
}
|
||||
|
||||
function makeSession(opts: { pid?: number; pipeLive?: boolean; clientFailsConnect?: boolean } = {}) {
|
||||
const pid = opts.pid ?? 1234;
|
||||
let pipeLive = opts.pipeLive ?? false;
|
||||
const clients: FakeClient[] = [];
|
||||
const injector = {
|
||||
inject: vi.fn(() => ({ method: 'loadModuleManual' as const, handle: DUMMY_HANDLE })),
|
||||
unload: vi.fn(),
|
||||
};
|
||||
|
||||
const session = new HookSession(pid, {
|
||||
injector,
|
||||
makeClient: () => {
|
||||
const c = new FakeClient();
|
||||
if (opts.clientFailsConnect) c.shouldFailConnect = true;
|
||||
clients.push(c);
|
||||
// Cast: FakeClient mirrors only the surface HookSession touches.
|
||||
return c as unknown as QqHookClient;
|
||||
},
|
||||
pipeWatcher: { isPipeLive: () => pipeLive },
|
||||
});
|
||||
|
||||
return {
|
||||
session,
|
||||
injector,
|
||||
clients,
|
||||
currentClient: () => clients[clients.length - 1],
|
||||
setPipeLive: (v: boolean) => { pipeLive = v; },
|
||||
};
|
||||
}
|
||||
|
||||
const flush = () => new Promise<void>(r => setImmediate(r));
|
||||
|
||||
describe('HookSession — load', () => {
|
||||
it('with no live pipe: injects, status → connecting, method from inject result', async () => {
|
||||
const { session, injector } = makeSession({ pipeLive: false });
|
||||
const info = await session.load();
|
||||
|
||||
expect(injector.inject).toHaveBeenCalledOnce();
|
||||
expect(info.status).toBe('connecting');
|
||||
expect(info.method).toBe('loadModuleManual');
|
||||
expect(info.injected).toBe(true);
|
||||
});
|
||||
|
||||
it('fast-path: pipe already live → skip inject, method → "reconnect"', async () => {
|
||||
const { session, injector } = makeSession({ pipeLive: true });
|
||||
const info = await session.load();
|
||||
|
||||
expect(injector.inject).not.toHaveBeenCalled();
|
||||
expect(info.method).toBe('reconnect');
|
||||
expect(info.injected).toBe(true);
|
||||
expect(info.status).toBe('connecting');
|
||||
});
|
||||
|
||||
it('inject failure → status error, error message captured', async () => {
|
||||
const injector = {
|
||||
inject: vi.fn(() => { throw new Error('inject boom'); }),
|
||||
unload: vi.fn(),
|
||||
};
|
||||
const session = new HookSession(1234, {
|
||||
injector,
|
||||
makeClient: () => new FakeClient() as unknown as QqHookClient,
|
||||
pipeWatcher: { isPipeLive: () => false },
|
||||
});
|
||||
const info = await session.load();
|
||||
expect(info.status).toBe('error');
|
||||
expect(info.error).toBe('inject boom');
|
||||
});
|
||||
});
|
||||
|
||||
describe('HookSession — pipe up/down → connect lifecycle', () => {
|
||||
it('onPipeUp after load: client connects → status loaded', async () => {
|
||||
const ctx = makeSession({ pipeLive: true });
|
||||
await ctx.session.load();
|
||||
ctx.session.onPipeUp();
|
||||
await flush();
|
||||
|
||||
expect(ctx.session.status).toBe('loaded');
|
||||
expect(ctx.clients).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('emits "login" with (uin, sender) after client signals loggedIn', async () => {
|
||||
const ctx = makeSession({ pipeLive: true });
|
||||
const loginSpy = vi.fn();
|
||||
ctx.session.on('login', loginSpy);
|
||||
|
||||
await ctx.session.load();
|
||||
ctx.session.onPipeUp();
|
||||
await flush();
|
||||
ctx.currentClient().fireLogin('10001');
|
||||
|
||||
expect(loginSpy).toHaveBeenCalledOnce();
|
||||
const [uin, sender] = loginSpy.mock.calls[0]!;
|
||||
expect(uin).toBe('10001');
|
||||
expect(typeof (sender as { sendPacket: unknown }).sendPacket).toBe('function');
|
||||
expect(ctx.session.status).toBe('online');
|
||||
expect(ctx.session.uin).toBe('10001');
|
||||
});
|
||||
|
||||
it('does not re-emit "login" on duplicate loginState with same uin', async () => {
|
||||
const ctx = makeSession({ pipeLive: true });
|
||||
const loginSpy = vi.fn();
|
||||
ctx.session.on('login', loginSpy);
|
||||
|
||||
await ctx.session.load();
|
||||
ctx.session.onPipeUp();
|
||||
await flush();
|
||||
|
||||
ctx.currentClient().fireLogin('10001');
|
||||
ctx.currentClient().fireLogin('10001');
|
||||
|
||||
expect(loginSpy).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('connect failure: stays in connecting, error message captured (will retry on next tick)', async () => {
|
||||
const ctx = makeSession({ pipeLive: true, clientFailsConnect: true });
|
||||
await ctx.session.load();
|
||||
ctx.session.onPipeUp();
|
||||
await flush();
|
||||
|
||||
expect(ctx.session.status).toBe('connecting');
|
||||
expect(ctx.session.error).toBe('connect failed');
|
||||
});
|
||||
|
||||
it('onPipeDown while logged in: emits disconnected(true), status disconnected', async () => {
|
||||
const ctx = makeSession({ pipeLive: true });
|
||||
const discSpy = vi.fn();
|
||||
ctx.session.on('disconnected', discSpy);
|
||||
|
||||
await ctx.session.load();
|
||||
ctx.session.onPipeUp();
|
||||
await flush();
|
||||
ctx.currentClient().fireLogin('10001');
|
||||
|
||||
ctx.session.onPipeDown();
|
||||
await flush();
|
||||
|
||||
expect(discSpy).toHaveBeenCalledWith(true);
|
||||
expect(ctx.session.status).toBe('disconnected');
|
||||
});
|
||||
|
||||
it('onPipeDown while connected-but-not-logged-in: emits disconnected(false), status connecting', async () => {
|
||||
const ctx = makeSession({ pipeLive: true });
|
||||
const discSpy = vi.fn();
|
||||
ctx.session.on('disconnected', discSpy);
|
||||
|
||||
await ctx.session.load();
|
||||
ctx.session.onPipeUp();
|
||||
await flush();
|
||||
// No login fired — we're connected but not logged in.
|
||||
|
||||
ctx.session.onPipeDown();
|
||||
await flush();
|
||||
|
||||
// disconnected(false) is currently NOT emitted — disconnect events are
|
||||
// reserved for "we owe BridgeManager a disconnect notification". This
|
||||
// preserves the original behaviour.
|
||||
expect(discSpy).not.toHaveBeenCalled();
|
||||
expect(ctx.session.status).toBe('connecting');
|
||||
});
|
||||
|
||||
it('repeated pipe down → up cycles reach consistent state', async () => {
|
||||
const ctx = makeSession({ pipeLive: true });
|
||||
await ctx.session.load();
|
||||
ctx.session.onPipeUp();
|
||||
await flush();
|
||||
ctx.currentClient().fireLogin('10001');
|
||||
expect(ctx.session.status).toBe('online');
|
||||
|
||||
ctx.session.onPipeDown();
|
||||
await flush();
|
||||
expect(ctx.session.status).toBe('disconnected');
|
||||
|
||||
// Pipe returns — session builds a fresh client.
|
||||
ctx.session.onPipeUp();
|
||||
await flush();
|
||||
expect(ctx.clients.length).toBeGreaterThan(1);
|
||||
expect(ctx.session.status).toBe('loaded');
|
||||
});
|
||||
});
|
||||
|
||||
describe('HookSession — unload', () => {
|
||||
it('while logged in: emits disconnected(true), calls unload, status available', async () => {
|
||||
const ctx = makeSession({ pipeLive: false });
|
||||
await ctx.session.load();
|
||||
ctx.setPipeLive(true);
|
||||
ctx.session.onPipeUp();
|
||||
await flush();
|
||||
ctx.currentClient().fireLogin('10001');
|
||||
|
||||
const discSpy = vi.fn();
|
||||
ctx.session.on('disconnected', discSpy);
|
||||
ctx.setPipeLive(false); // unload verification will see the pipe gone
|
||||
|
||||
await ctx.session.unload();
|
||||
|
||||
expect(discSpy).toHaveBeenCalledWith(true);
|
||||
expect(ctx.injector.unload).toHaveBeenCalledOnce();
|
||||
expect(ctx.session.status).toBe('available');
|
||||
});
|
||||
|
||||
it('when pipe stays live after unload: status connecting with retry message', async () => {
|
||||
const ctx = makeSession({ pipeLive: false });
|
||||
await ctx.session.load();
|
||||
// Simulate the unload-failed scenario: pipe is still up after unload.
|
||||
ctx.setPipeLive(true);
|
||||
|
||||
const info = await ctx.session.unload();
|
||||
|
||||
expect(info.status).toBe('connecting');
|
||||
expect(info.error).toContain('命名管道仍然存在');
|
||||
});
|
||||
});
|
||||
|
||||
describe('HookSession — serialization', () => {
|
||||
it('user mashing load → unload → load runs in order, end state consistent', async () => {
|
||||
const ctx = makeSession({ pipeLive: false });
|
||||
|
||||
const p1 = ctx.session.load();
|
||||
const p2 = ctx.session.unload();
|
||||
const p3 = ctx.session.load();
|
||||
await Promise.all([p1, p2, p3]);
|
||||
|
||||
expect(ctx.injector.inject).toHaveBeenCalledTimes(2);
|
||||
expect(ctx.injector.unload).toHaveBeenCalledTimes(1);
|
||||
expect(ctx.session.status).toBe('connecting');
|
||||
});
|
||||
|
||||
it('onPipeUp queued behind in-flight load resolves correctly', async () => {
|
||||
const ctx = makeSession({ pipeLive: true });
|
||||
|
||||
const loadPromise = ctx.session.load();
|
||||
ctx.session.onPipeUp(); // queued behind load
|
||||
|
||||
await loadPromise;
|
||||
await flush();
|
||||
|
||||
expect(ctx.session.status).toBe('loaded');
|
||||
});
|
||||
});
|
||||
|
||||
describe('HookSession — process gone', () => {
|
||||
it('emits disconnected(true) then disposed when killed while logged in', async () => {
|
||||
const ctx = makeSession({ pipeLive: true });
|
||||
await ctx.session.load();
|
||||
ctx.session.onPipeUp();
|
||||
await flush();
|
||||
ctx.currentClient().fireLogin('10001');
|
||||
|
||||
const events: string[] = [];
|
||||
ctx.session.on('disconnected', () => events.push('disconnected'));
|
||||
ctx.session.on('disposed', () => events.push('disposed'));
|
||||
|
||||
ctx.session.notifyProcessGone();
|
||||
await flush();
|
||||
|
||||
expect(events).toEqual(['disconnected', 'disposed']);
|
||||
expect(ctx.session.isDisposed).toBe(true);
|
||||
});
|
||||
|
||||
it('emits only disposed when killed before login', async () => {
|
||||
const ctx = makeSession({ pipeLive: true });
|
||||
await ctx.session.load();
|
||||
ctx.session.onPipeUp();
|
||||
await flush();
|
||||
|
||||
const events: string[] = [];
|
||||
ctx.session.on('disconnected', () => events.push('disconnected'));
|
||||
ctx.session.on('disposed', () => events.push('disposed'));
|
||||
|
||||
ctx.session.notifyProcessGone();
|
||||
await flush();
|
||||
|
||||
expect(events).toEqual(['disposed']);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,197 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { PipeWatcher } from '../src/hook/pipe-watcher';
|
||||
import type { HookProcessBaseInfo } from '../src/hook/injector';
|
||||
|
||||
const flush = () => new Promise<void>(r => setImmediate(r));
|
||||
|
||||
function setupWatcher(initial: { processes: HookProcessBaseInfo[]; live: Set<number> }) {
|
||||
let processes = initial.processes;
|
||||
let live = initial.live;
|
||||
const watcher = new PipeWatcher({
|
||||
listProcesses: () => processes,
|
||||
// Defensively copy so the watcher can't mutate the test's reference.
|
||||
listLivePipes: async () => new Set(live),
|
||||
intervalMs: 60_000, // disable the internal timer; tests drive ticks manually
|
||||
});
|
||||
return {
|
||||
watcher,
|
||||
setProcesses: (p: HookProcessBaseInfo[]) => { processes = p; },
|
||||
setLive: (s: Set<number>) => { live = s; },
|
||||
};
|
||||
}
|
||||
|
||||
type EventRow = [string, unknown];
|
||||
|
||||
function captureEvents(watcher: PipeWatcher): EventRow[] {
|
||||
const events: EventRow[] = [];
|
||||
watcher.on('process-discovered', info => events.push(['discovered', (info as HookProcessBaseInfo).pid]));
|
||||
watcher.on('process-gone', pid => events.push(['gone', pid]));
|
||||
watcher.on('pipe-up', pid => events.push(['up', pid]));
|
||||
watcher.on('pipe-down', pid => events.push(['down', pid]));
|
||||
return events;
|
||||
}
|
||||
|
||||
describe('PipeWatcher', () => {
|
||||
it('start() emits process-discovered + pipe-up for live PIDs on first tick', async () => {
|
||||
const ctx = setupWatcher({
|
||||
processes: [{ pid: 1234, name: 'QQ.exe', path: '' }],
|
||||
live: new Set([1234]),
|
||||
});
|
||||
const events = captureEvents(ctx.watcher);
|
||||
await ctx.watcher.start();
|
||||
|
||||
expect(events).toEqual([
|
||||
['discovered', 1234],
|
||||
['up', 1234],
|
||||
]);
|
||||
expect(ctx.watcher.isPipeLive(1234)).toBe(true);
|
||||
expect(ctx.watcher.isProcessAlive(1234)).toBe(true);
|
||||
|
||||
ctx.watcher.stop();
|
||||
});
|
||||
|
||||
it('pipe-down emitted when a previously-live pipe disappears', async () => {
|
||||
const ctx = setupWatcher({
|
||||
processes: [{ pid: 1234, name: 'QQ.exe', path: '' }],
|
||||
live: new Set([1234]),
|
||||
});
|
||||
await ctx.watcher.start();
|
||||
const events = captureEvents(ctx.watcher);
|
||||
|
||||
ctx.setLive(new Set());
|
||||
await ctx.watcher.tickNow();
|
||||
|
||||
expect(events).toEqual([['down', 1234]]);
|
||||
expect(ctx.watcher.isPipeLive(1234)).toBe(false);
|
||||
ctx.watcher.stop();
|
||||
});
|
||||
|
||||
it('process-gone + pipe-down both emitted (pipe-down first) when QQ.exe is killed with a live pipe', async () => {
|
||||
const ctx = setupWatcher({
|
||||
processes: [{ pid: 1234, name: 'QQ.exe', path: '' }],
|
||||
live: new Set([1234]),
|
||||
});
|
||||
await ctx.watcher.start();
|
||||
const events = captureEvents(ctx.watcher);
|
||||
|
||||
ctx.setProcesses([]);
|
||||
ctx.setLive(new Set());
|
||||
await ctx.watcher.tickNow();
|
||||
|
||||
expect(events).toEqual([
|
||||
['down', 1234],
|
||||
['gone', 1234],
|
||||
]);
|
||||
ctx.watcher.stop();
|
||||
});
|
||||
|
||||
it('ignores live pipes whose process is not running', async () => {
|
||||
const ctx = setupWatcher({
|
||||
processes: [],
|
||||
live: new Set([9999]),
|
||||
});
|
||||
const events = captureEvents(ctx.watcher);
|
||||
await ctx.watcher.start();
|
||||
|
||||
expect(events).toEqual([]);
|
||||
expect(ctx.watcher.isPipeLive(9999)).toBe(false);
|
||||
ctx.watcher.stop();
|
||||
});
|
||||
|
||||
it('emits pipe-up again after a pipe-down → pipe-up cycle', async () => {
|
||||
const ctx = setupWatcher({
|
||||
processes: [{ pid: 1234, name: 'QQ.exe', path: '' }],
|
||||
live: new Set(),
|
||||
});
|
||||
await ctx.watcher.start();
|
||||
const events = captureEvents(ctx.watcher);
|
||||
|
||||
ctx.setLive(new Set([1234]));
|
||||
await ctx.watcher.tickNow();
|
||||
expect(events).toEqual([['up', 1234]]);
|
||||
|
||||
ctx.setLive(new Set());
|
||||
await ctx.watcher.tickNow();
|
||||
expect(events).toEqual([['up', 1234], ['down', 1234]]);
|
||||
|
||||
ctx.setLive(new Set([1234]));
|
||||
await ctx.watcher.tickNow();
|
||||
expect(events).toEqual([['up', 1234], ['down', 1234], ['up', 1234]]);
|
||||
|
||||
ctx.watcher.stop();
|
||||
});
|
||||
|
||||
it('does not double-emit on idempotent ticks (no state change)', async () => {
|
||||
const ctx = setupWatcher({
|
||||
processes: [{ pid: 1234, name: 'QQ.exe', path: '' }],
|
||||
live: new Set([1234]),
|
||||
});
|
||||
await ctx.watcher.start();
|
||||
const events = captureEvents(ctx.watcher);
|
||||
|
||||
await ctx.watcher.tickNow();
|
||||
await ctx.watcher.tickNow();
|
||||
|
||||
expect(events).toEqual([]);
|
||||
ctx.watcher.stop();
|
||||
});
|
||||
|
||||
it('emits "tick" after every poll', async () => {
|
||||
const ctx = setupWatcher({ processes: [], live: new Set() });
|
||||
let ticks = 0;
|
||||
ctx.watcher.on('tick', () => { ticks++; });
|
||||
|
||||
await ctx.watcher.start();
|
||||
expect(ticks).toBe(1);
|
||||
|
||||
await ctx.watcher.tickNow();
|
||||
expect(ticks).toBe(2);
|
||||
|
||||
ctx.watcher.stop();
|
||||
});
|
||||
|
||||
it('wake() pulls the next tick forward', async () => {
|
||||
const ctx = setupWatcher({
|
||||
processes: [{ pid: 1234, name: 'QQ.exe', path: '' }],
|
||||
live: new Set(),
|
||||
});
|
||||
await ctx.watcher.start();
|
||||
const events = captureEvents(ctx.watcher);
|
||||
|
||||
// Wait on 'tick' rather than guessing how many event-loop turns
|
||||
// setTimeout(0) needs (it's clamped to ~1ms and races setImmediate).
|
||||
const nextTick = new Promise<void>(resolve => ctx.watcher.once('tick', () => resolve()));
|
||||
ctx.setLive(new Set([1234]));
|
||||
ctx.watcher.wake();
|
||||
await nextTick;
|
||||
|
||||
expect(events).toEqual([['up', 1234]]);
|
||||
ctx.watcher.stop();
|
||||
});
|
||||
|
||||
it('survives a throwing listProcesses without crashing the watcher', async () => {
|
||||
let processes: HookProcessBaseInfo[] = [];
|
||||
let live = new Set<number>();
|
||||
let shouldThrow = true;
|
||||
const watcher = new PipeWatcher({
|
||||
listProcesses: () => {
|
||||
if (shouldThrow) throw new Error('boom');
|
||||
return processes;
|
||||
},
|
||||
listLivePipes: async () => new Set(live),
|
||||
intervalMs: 60_000,
|
||||
});
|
||||
|
||||
await watcher.start();
|
||||
expect(watcher.isProcessAlive(1234)).toBe(false);
|
||||
|
||||
shouldThrow = false;
|
||||
processes = [{ pid: 1234, name: 'QQ.exe', path: '' }];
|
||||
live = new Set([1234]);
|
||||
const events = captureEvents(watcher);
|
||||
await watcher.tickNow();
|
||||
|
||||
expect(events).toEqual([['discovered', 1234], ['up', 1234]]);
|
||||
watcher.stop();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user