feat(core): retain online device snapshots
Store the latest online-device snapshot for each Bridge lifecycle and expose it through the bridge contract as an immutable, owned value. Distinguish a snapshot that has not yet been observed from a valid empty snapshot, replace state atomically on each QQ update, clear it during disposal, and cover lifecycle, immutability, and cleanup behavior with focused tests.
This commit is contained in:
3 files changed
+92
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import type { OnlineDeviceInfo } from '@snowluma/protocol/events';
|
||||
import type { BridgeContext } from './bridge-context';
|
||||
|
||||
export interface BridgeInterface extends BridgeContext {
|
||||
@@ -5,4 +6,7 @@ export interface BridgeInterface extends BridgeContext {
|
||||
/** False only when every attached QQ process is confirmed stale.
|
||||
* An unarmed watchdog remains true for compatibility. */
|
||||
readonly receiveHealthy: boolean;
|
||||
/** Latest device snapshot observed on this Bridge lifecycle. `null` means
|
||||
* QQ has not emitted its cache notification since SnowLuma attached. */
|
||||
getOnlineClients(): readonly Readonly<OnlineDeviceInfo>[] | null;
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { IdentityService } from '@snowluma/protocol/identity-service';
|
||||
import { MSG_PUSH_CMD, parseMsgPush, SysMsgDedup } from '@snowluma/protocol/msg-push';
|
||||
import { KICK_NT_CMD, parseKickNT } from '@snowluma/protocol/kick-nt';
|
||||
import { IncomingPacketPipeline, type CmdParser } from '@snowluma/protocol/packet-pipeline';
|
||||
import type { OnlineDeviceInfo } from '@snowluma/protocol/events';
|
||||
import { buildApiHub, type ApiHub } from './apis';
|
||||
import {
|
||||
AiVoiceChatType,
|
||||
@@ -35,6 +36,7 @@ export class Bridge implements BridgeInterface {
|
||||
private msgRandom_ = (Date.now() & 0xFFFFFFFF) >>> 0;
|
||||
// Per-account dedup for QQ NT's already-deduped system pushes (#137).
|
||||
private readonly sysMsgDedup_ = new SysMsgDedup();
|
||||
private onlineClients_: readonly Readonly<OnlineDeviceInfo>[] | null = null;
|
||||
|
||||
constructor(identity: IdentityService) {
|
||||
this.identity = identity;
|
||||
@@ -94,6 +96,13 @@ export class Bridge implements BridgeInterface {
|
||||
});
|
||||
this.pipeline.registerCmd(MSG_PUSH_CMD, (pkt, identity) => parseMsgPush(pkt, identity, this.sysMsgDedup_));
|
||||
this.pipeline.registerCmd(KICK_NT_CMD, parseKickNT);
|
||||
this.events.on('online_devices_changed', (event) => {
|
||||
const snapshot = event.devices.map((device) => Object.freeze({ ...device }));
|
||||
this.onlineClients_ = Object.freeze(snapshot);
|
||||
log.debug('online-device snapshot updated: uin=%s clients=%d types=%s',
|
||||
this.identity.uin, snapshot.length,
|
||||
snapshot.map((device) => device.clientType).join(','));
|
||||
});
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
@@ -102,6 +111,7 @@ export class Bridge implements BridgeInterface {
|
||||
this.packetClientsByPid_.clear();
|
||||
this.packetClient_ = null;
|
||||
this.packetClientPid_ = null;
|
||||
this.onlineClients_ = null;
|
||||
try {
|
||||
this.identity.close();
|
||||
} finally {
|
||||
@@ -202,6 +212,9 @@ export class Bridge implements BridgeInterface {
|
||||
}
|
||||
this.receiveHealthByPid_.set(pid, healthy);
|
||||
}
|
||||
getOnlineClients(): readonly Readonly<OnlineDeviceInfo>[] | null {
|
||||
return this.onlineClients_;
|
||||
}
|
||||
onPacket(pkt: PacketInfo): void {
|
||||
this.pipeline.process(pkt);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { PacketInfo } from '@snowluma/common/protocol-types';
|
||||
import type { OnlineDeviceInfo, QQEventVariant } from '@snowluma/protocol/events';
|
||||
import { IdentityService } from '@snowluma/protocol/identity-service';
|
||||
import { Bridge } from '../src/bridge/bridge';
|
||||
|
||||
function packet(sequence: number): PacketInfo {
|
||||
return {
|
||||
pid: 1,
|
||||
uin: '10001',
|
||||
serviceCmd: 'test.online_devices',
|
||||
seqId: sequence,
|
||||
retCode: 0,
|
||||
fromClient: false,
|
||||
body: new Uint8Array(0),
|
||||
};
|
||||
}
|
||||
|
||||
function device(overrides: Partial<OnlineDeviceInfo> = {}): OnlineDeviceInfo {
|
||||
return {
|
||||
appId: 537242075,
|
||||
instanceId: 202,
|
||||
clientType: 1,
|
||||
platform: 3,
|
||||
deviceName: 'DESKTOP-TEST',
|
||||
deviceKind: 'computer',
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe('Bridge online-device snapshot', () => {
|
||||
it('distinguishes not-yet-observed from an observed empty snapshot', () => {
|
||||
const bridge = new Bridge(IdentityService.memory('10001'));
|
||||
let current: OnlineDeviceInfo[] = [device()];
|
||||
bridge.registerCmd('test.online_devices', () => [{
|
||||
kind: 'online_devices_changed',
|
||||
time: 1710000000,
|
||||
selfUin: 10001,
|
||||
devices: current,
|
||||
} satisfies QQEventVariant]);
|
||||
|
||||
expect(bridge.getOnlineClients()).toBeNull();
|
||||
|
||||
bridge.onPacket(packet(1));
|
||||
expect(bridge.getOnlineClients()).toEqual([device()]);
|
||||
|
||||
current = [];
|
||||
bridge.onPacket(packet(2));
|
||||
expect(bridge.getOnlineClients()).toEqual([]);
|
||||
});
|
||||
|
||||
it('owns an immutable copy and clears it when the Bridge is disposed', () => {
|
||||
const bridge = new Bridge(IdentityService.memory('10001'));
|
||||
const source = device();
|
||||
bridge.registerCmd('test.online_devices', () => [{
|
||||
kind: 'online_devices_changed',
|
||||
time: 1710000000,
|
||||
selfUin: 10001,
|
||||
devices: [source],
|
||||
}]);
|
||||
|
||||
bridge.onPacket(packet(1));
|
||||
const snapshot = bridge.getOnlineClients();
|
||||
expect(snapshot).not.toBeNull();
|
||||
expect(snapshot?.[0]).not.toBe(source);
|
||||
expect(Object.isFrozen(snapshot)).toBe(true);
|
||||
expect(Object.isFrozen(snapshot?.[0])).toBe(true);
|
||||
|
||||
source.deviceName = 'MUTATED';
|
||||
expect(bridge.getOnlineClients()?.[0]?.deviceName).toBe('DESKTOP-TEST');
|
||||
|
||||
bridge.dispose();
|
||||
expect(bridge.getOnlineClients()).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user