refactor(oidb): migrate contacts.ts OIDB cmds to namespaces

6 OIDB cmds moved; pagination + per-group cache + inflight
coalescing stay on the facade because they need cross-call state.

  - FetchFriendListPage       (0xFD4_1) — facade drives the cursor loop
  - FetchGroupList            (0xFE5_2) — uinForm; verbatim config blob
  - FetchGroupMemberListPage  (0xFE7_3) — facade drives the token loop +
                                          owns the TTL cache + inflight
                                          coalescing that keeps busy
                                          OneBot clients from tripping
                                          Tencent risk-control
  - FetchUserProfile          (0xFE1_2) — uinForm; full property-key
                                          catalogue cross-checked against
                                          LagrangeV2 FetchStrangerService.cs
  - FetchGroupRequests        (0x10C0_1/_2) — dynamic subcmd via filtered flag
  - FetchDownloadRkeys        (0x9067_202) — uinForm; respHead.retCode
                                             check moved into deserialize

35 new namespace-level tests covering serialize / deserialize edge
cases (property decoding, sex enum mapping, avatar URL suffix,
empty body handling, pagination cursor behaviour).

Verification:
  - typecheck green
  - @snowluma/bridge: 30 files / 233 tests
  - @snowluma/core: 302 tests
Wire bytes unchanged.
This commit is contained in:
DrSmoothl committed 2026-05-22 23:51:33 +08:00
1 parent 80afa31f5c
commit 014b87c7ce
14 files changed
+864 -248

No files matched your search

@@ -0,0 +1,69 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { NTV2RichMediaReq, NTV2RichMediaResp } from '@snowluma/proto-defs/oidb-actions/media';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { FetchDownloadRkeys } from '../../../src/oidb-services/contacts/fetch-download-rkeys';
function makeSender(body?: NTV2RichMediaResp) {
const responseData = body !== undefined
? Buffer.from(protobuf_encode<OidbBase<NTV2RichMediaResp>>({ body }))
: Buffer.alloc(0);
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
return { sendRawPacket: vi.fn(async () => r) };
}
describe('FetchDownloadRkeys namespace', () => {
it('declares 0x9067_202 with uinForm=true', () => {
expect(FetchDownloadRkeys.command).toBe(0x9067);
expect(FetchDownloadRkeys.subCommand).toBe(202);
expect(FetchDownloadRkeys.uinForm).toBe(true);
});
describe('serialize', () => {
it('requests the (private=10, group=20, fallback=2) image scopes', () => {
const out = FetchDownloadRkeys.serialize({});
expect(out.downloadRkey?.types).toEqual([10, 20, 2]);
});
it('packages the fixed reqHead { common, scene, client }', () => {
const out = FetchDownloadRkeys.serialize({});
expect(out.reqHead).toEqual({
common: { requestId: 1, command: 202 },
scene: { requestType: 2, businessType: 1, sceneType: 0 },
client: { agentType: 2 },
});
});
});
describe('deserialize', () => {
it('throws when respHead.retCode is non-zero', () => {
expect(() => FetchDownloadRkeys.deserialize({
respHead: { retCode: 42, message: 'bad' },
} as any)).toThrow('bad');
});
it('passes through on retCode = 0', () => {
const body = { respHead: { retCode: 0 }, downloadRkey: { rkeys: [] } };
expect(FetchDownloadRkeys.deserialize(body as any)).toBe(body);
});
it('falls back to a generic message when none is provided', () => {
expect(() => FetchDownloadRkeys.deserialize({
respHead: { retCode: 1 },
} as any)).toThrow('fetch download rkey failed');
});
});
describe('invoke (e2e)', () => {
it('routes to OidbSvcTrpcTcp.0x9067_202 with reserved=1', async () => {
const sender = makeSender({ respHead: { retCode: 0 }, downloadRkey: { rkeys: [] } } as any);
await FetchDownloadRkeys.invoke(sender);
const [wireName, bytes] = sender.sendRawPacket.mock.calls[0]!;
expect(wireName).toBe('OidbSvcTrpcTcp.0x9067_202');
const env = protobuf_decode<OidbBase<NTV2RichMediaReq>>(bytes);
expect(env.reserved).toBe(1);
});
});
});
@@ -0,0 +1,68 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase, OidbSvcTrpcTcp0xFD4_1Response } from '@snowluma/proto-defs/oidb';
import type { OidbFriendListRequest } from '@snowluma/proto-defs/oidb-actions/base';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { FetchFriendListPage } from '../../../src/oidb-services/contacts/fetch-friend-list-page';
function makeSender(body?: OidbSvcTrpcTcp0xFD4_1Response) {
const responseData = body !== undefined
? Buffer.from(protobuf_encode<OidbBase<OidbSvcTrpcTcp0xFD4_1Response>>({ body }))
: Buffer.alloc(0);
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
return { sendRawPacket: vi.fn(async () => r) };
}
describe('FetchFriendListPage namespace', () => {
it('declares 0xFD4_1', () => {
expect(FetchFriendListPage.command).toBe(0xFD4);
expect(FetchFriendListPage.subCommand).toBe(1);
});
describe('serialize', () => {
it('omits nextUin on the first page', () => {
const out = FetchFriendListPage.serialize({ nextUin: null }) as any;
expect(out.nextUin).toBeUndefined();
expect(out.friendCount).toBe(300);
expect(out.field4).toBe(0);
});
it('includes nextUin sub-message on follow-up pages', () => {
const out = FetchFriendListPage.serialize({ nextUin: 10001 }) as any;
expect(out.nextUin).toEqual({ uin: 10001 });
});
it('emits the verbatim property request list (codes 100/101/102/103/20002/27394)', () => {
const out = FetchFriendListPage.serialize({ nextUin: null }) as any;
expect(out.body).toEqual([
{ type: 1, number: { numbers: [103, 102, 20002, 27394] } },
{ type: 4, number: { numbers: [100, 101, 102] } },
]);
});
});
describe('invoke (e2e)', () => {
it('routes to OidbSvcTrpcTcp.0xfd4_1', async () => {
const sender = makeSender({ friends: [] });
await FetchFriendListPage.invoke(sender, { nextUin: null });
expect(sender.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0xfd4_1');
});
it('returns the wire body verbatim (facade does the FriendInfo mapping)', async () => {
const body = { friends: [{ uin: 10001, uid: 'u', additional: [] }], next: { uin: 0 } };
const sender = makeSender(body as any);
const out = await FetchFriendListPage.invoke(sender, { nextUin: null });
expect(out.friends).toBeDefined();
expect(out.next?.uin ?? 0).toBe(0);
});
it('encodes envelope body correctly', async () => {
const sender = makeSender({ friends: [] });
await FetchFriendListPage.invoke(sender, { nextUin: 10001 });
const [, bytes] = sender.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbFriendListRequest>>(bytes);
expect(env.body?.nextUin).toEqual({ uin: 10001 });
});
});
});
@@ -0,0 +1,43 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { OidbGroupListRequest } from '@snowluma/proto-defs/oidb-actions/base';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { FetchGroupList } from '../../../src/oidb-services/contacts/fetch-group-list';
function makeSender() {
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: Buffer.alloc(0) };
return { sendRawPacket: vi.fn(async () => r) };
}
describe('FetchGroupList namespace', () => {
it('declares 0xFE5_2 with uinForm=true', () => {
expect(FetchGroupList.command).toBe(0xFE5);
expect(FetchGroupList.subCommand).toBe(2);
expect(FetchGroupList.uinForm).toBe(true);
});
describe('invoke (e2e)', () => {
it('routes to OidbSvcTrpcTcp.0xfe5_2 with envelope reserved=1', async () => {
const sender = makeSender();
await FetchGroupList.invoke(sender);
const [wireName, bytes] = sender.sendRawPacket.mock.calls[0]!;
expect(wireName).toBe('OidbSvcTrpcTcp.0xfe5_2');
const env = protobuf_decode<OidbBase<OidbGroupListRequest>>(bytes);
expect(env.reserved).toBe(1);
});
it('sends the verbatim config blob (field5002/5003 OFF to avoid EPIPE on big rosters)', async () => {
const sender = makeSender();
await FetchGroupList.invoke(sender);
const [, bytes] = sender.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbGroupListRequest>>(bytes);
expect(env.body?.config?.config1?.groupName).toBe(true);
expect(env.body?.config?.config1?.memberCount).toBe(true);
// proto3 default false — omitted on wire, decoded as null.
expect(env.body?.config?.config1?.field5002 ?? false).toBe(false);
expect(env.body?.config?.config1?.field5003 ?? false).toBe(false);
});
});
});
@@ -0,0 +1,57 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { OidbGroupMemberListRequest } from '@snowluma/proto-defs/oidb-actions/base';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { FetchGroupMemberListPage } from '../../../src/oidb-services/contacts/fetch-group-member-list-page';
function makeSender() {
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: Buffer.alloc(0) };
return { sendRawPacket: vi.fn(async () => r) };
}
describe('FetchGroupMemberListPage namespace', () => {
it('declares 0xFE7_3', () => {
expect(FetchGroupMemberListPage.command).toBe(0xFE7);
expect(FetchGroupMemberListPage.subCommand).toBe(3);
});
describe('serialize', () => {
it('omits the token on the first page', () => {
const out = FetchGroupMemberListPage.serialize({ groupId: 12345, token: '' }) as any;
expect(out.token).toBeUndefined();
expect(out.groupUin).toBe(12345);
});
it('threads the server-issued token into follow-up pages', () => {
const out = FetchGroupMemberListPage.serialize({ groupId: 12345, token: 'next-cursor' }) as any;
expect(out.token).toBe('next-cursor');
});
it('always requests the full member-field projection', () => {
const out = FetchGroupMemberListPage.serialize({ groupId: 1, token: '' }) as any;
expect(out.body).toMatchObject({
memberName: true, memberCard: true, level: true,
joinTimestamp: true, lastMsgTimestamp: true, shutUpTimestamp: true,
permission: true,
});
});
});
describe('invoke (e2e)', () => {
it('routes to OidbSvcTrpcTcp.0xfe7_3', async () => {
const sender = makeSender();
await FetchGroupMemberListPage.invoke(sender, { groupId: 1, token: '' });
expect(sender.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0xfe7_3');
});
it('encodes envelope body with the groupId', async () => {
const sender = makeSender();
await FetchGroupMemberListPage.invoke(sender, { groupId: 12345, token: '' });
const [, bytes] = sender.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbGroupMemberListRequest>>(bytes);
expect(env.body?.groupUin).toBe(12345);
});
});
});
@@ -0,0 +1,56 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { OidbGroupRequestList } from '@snowluma/proto-defs/oidb-actions/base';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { FetchGroupRequests } from '../../../src/oidb-services/contacts/fetch-group-requests';
function makeSender() {
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: Buffer.alloc(0) };
return { sendRawPacket: vi.fn(async () => r) };
}
describe('FetchGroupRequests namespace', () => {
it('declares 0x10C0', () => {
expect(FetchGroupRequests.command).toBe(0x10C0);
});
describe('resolveSubCommand', () => {
it('returns 1 when filtered=false (main inbox)', () => {
expect(FetchGroupRequests.resolveSubCommand({ filtered: false })).toBe(1);
});
it('returns 2 when filtered=true (low-priority inbox)', () => {
expect(FetchGroupRequests.resolveSubCommand({ filtered: true })).toBe(2);
});
});
describe('serialize', () => {
it('always sends count=20 / field2=0', () => {
expect(FetchGroupRequests.serialize({ filtered: false })).toEqual({ count: 20, field2: 0 });
expect(FetchGroupRequests.serialize({ filtered: true })).toEqual({ count: 20, field2: 0 });
});
});
describe('invoke (e2e)', () => {
it('routes to 0x10c0_1 for the main inbox', async () => {
const sender = makeSender();
await FetchGroupRequests.invoke(sender, { filtered: false });
expect(sender.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0x10c0_1');
});
it('routes to 0x10c0_2 for the filtered inbox', async () => {
const sender = makeSender();
await FetchGroupRequests.invoke(sender, { filtered: true });
expect(sender.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0x10c0_2');
});
it('encodes envelope body with count=20', async () => {
const sender = makeSender();
await FetchGroupRequests.invoke(sender, { filtered: false });
const [, bytes] = sender.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbGroupRequestList>>(bytes);
expect(env.body?.count).toBe(20);
});
});
});
@@ -0,0 +1,124 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type {
AvatarInfo, OidbUserInfoRequest, OidbUserInfoResponse,
} from '@snowluma/proto-defs/oidb-actions/base';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { FetchUserProfile } from '../../../src/oidb-services/contacts/fetch-user-profile';
function makeSender(body?: OidbUserInfoResponse) {
const responseData = body !== undefined
? Buffer.from(protobuf_encode<OidbBase<OidbUserInfoResponse>>({ body }))
: Buffer.alloc(0);
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
return { sendRawPacket: vi.fn(async () => r) };
}
describe('FetchUserProfile namespace', () => {
it('declares 0xFE1_2 with uinForm=true', () => {
expect(FetchUserProfile.command).toBe(0xFE1);
expect(FetchUserProfile.subCommand).toBe(2);
expect(FetchUserProfile.uinForm).toBe(true);
});
describe('invoke (e2e)', () => {
it('routes to OidbSvcTrpcTcp.0xfe1_2 with reserved=1', async () => {
const sender = makeSender({ body: { uin: 10001, uid: 'u' } as any });
await FetchUserProfile.invoke(sender, { uin: 10001 });
const [wireName, bytes] = sender.sendRawPacket.mock.calls[0]!;
expect(wireName).toBe('OidbSvcTrpcTcp.0xfe1_2');
const env = protobuf_decode<OidbBase<OidbUserInfoRequest>>(bytes);
expect(env.reserved).toBe(1);
});
it('requests every property key in the catalogue (nickname, level, …)', async () => {
const sender = makeSender({ body: { uin: 10001, uid: 'u' } as any });
await FetchUserProfile.invoke(sender, { uin: 10001 });
const [, bytes] = sender.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbUserInfoRequest>>(bytes);
const keys = env.body?.keys?.map(k => k.key);
expect(keys).toContain(20002); // nickname
expect(keys).toContain(105); // level
expect(keys).toContain(101); // avatar
expect(keys).toContain(102); // sign
expect(keys).toContain(103); // remark
});
it('decodes nickname / remark / qid / sign from bytes-properties', async () => {
const enc = (s: string) => new TextEncoder().encode(s);
const sender = makeSender({
body: {
uin: 10001, uid: 'u',
properties: {
bytesProperties: [
{ code: 20002, value: enc('Nick') },
{ code: 103, value: enc('Bestie') },
{ code: 27394, value: enc('myqid') },
{ code: 102, value: enc('Hello') },
],
numberProperties: [],
},
} as any,
});
const out = await FetchUserProfile.invoke(sender, { uin: 10001 });
expect(out.nickname).toBe('Nick');
expect(out.remark).toBe('Bestie');
expect(out.qid).toBe('myqid');
expect(out.sign).toBe('Hello');
});
it('decodes sex / age / level from number-properties (sex enum)', async () => {
const cases: Array<[number, string]> = [
[1, 'male'], [2, 'female'], [255, 'unknown'], [0, 'unknown'],
];
for (const [sexNum, expected] of cases) {
const sender = makeSender({
body: {
uin: 1, uid: 'u',
properties: {
bytesProperties: [],
numberProperties: [
{ number1: 20009, number2: sexNum },
{ number1: 20037, number2: 25 },
{ number1: 105, number2: 7 },
],
},
} as any,
});
const out = await FetchUserProfile.invoke(sender, { uin: 1 });
expect(out.sex).toBe(expected);
expect(out.age).toBe(25);
expect(out.level).toBe(7);
}
});
it('decodes the avatar URL with the "640" size suffix appended', async () => {
const avatarBytes = protobuf_encode<AvatarInfo>({ url: 'https://q.qlogo.cn/abc/' });
const sender = makeSender({
body: {
uin: 10001, uid: 'u',
properties: {
bytesProperties: [{ code: 101, value: avatarBytes }],
numberProperties: [],
},
} as any,
});
const out = await FetchUserProfile.invoke(sender, { uin: 10001 });
expect(out.avatar).toBe('https://q.qlogo.cn/abc/640');
});
it('defaults uin to the requested value when the server omits its echo', async () => {
const sender = makeSender({ body: { uid: 'u' } as any });
const out = await FetchUserProfile.invoke(sender, { uin: 99999 });
expect(out.uin).toBe(99999);
});
it('throws when body is entirely missing from the envelope', async () => {
const sender = makeSender({});
await expect(FetchUserProfile.invoke(sender, { uin: 1 }))
.rejects.toThrow('user info response body missing');
});
});
});