This commit is contained in:
DrSmoothl committed 2026-10-12 15:43:34 +08:00
1 parent effeb66554
commit 7c0519f75f
977 files changed
+198426 -158324

No files matched your search

@@ -6,258 +6,305 @@ import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { GetLike } from '../../../src/oidb-services/profile/get-like';
function makeDeps(opts: {
cachedSelfUid?: string | null;
resolveUserUid?: (uin: number) => Promise<string>;
findUinByUid?: (uid: string) => number | null;
resolveUin?: (uid: string) => Promise<number | null>;
responseBody?: Oidb0x7edResp;
} = {}) {
const responseData = opts.responseBody !== undefined
? Buffer.from(protobuf_encode<OidbBase<Oidb0x7edResp>>({ body: opts.responseBody }))
: Buffer.alloc(0);
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
return {
sendRawPacket: vi.fn(async () => r),
identity: {
uin: '10001',
selfUid: opts.cachedSelfUid ?? null,
findUinByUid: vi.fn(opts.findUinByUid ?? (() => null)),
resolveUin: vi.fn(opts.resolveUin ?? (async () => null)),
} as any,
resolveUserUid: vi.fn(opts.resolveUserUid ?? (async (uin: number) => `uid-of-${uin}`)),
};
function makeDeps(
opts: {
cachedSelfUid?: string | null;
resolveUserUid?: (uin: number) => Promise<string>;
findUinByUid?: (uid: string) => number | null;
resolveUin?: (uid: string) => Promise<number | null>;
responseBody?: Oidb0x7edResp;
} = {},
) {
const responseData =
opts.responseBody !== undefined
? Buffer.from(protobuf_encode<OidbBase<Oidb0x7edResp>>({ body: opts.responseBody }))
: Buffer.alloc(0);
const r: SendPacketResult = {
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData,
};
return {
sendRawPacket: vi.fn(async () => r),
identity: {
uin: '10001',
selfUid: opts.cachedSelfUid ?? null,
findUinByUid: vi.fn(opts.findUinByUid ?? (() => null)),
resolveUin: vi.fn(opts.resolveUin ?? (async () => null)),
} as any,
resolveUserUid: vi.fn(opts.resolveUserUid ?? (async (uin: number) => `uid-of-${uin}`)),
};
}
describe('GetLike namespace', () => {
it('uses the self and other-user subcommands selected by the native client', () => {
const deps = makeDeps({ cachedSelfUid: 'self-uid' });
expect(GetLike.command).toBe(0x7ED);
expect(GetLike.resolveSubCommand({}, deps)).toBe(13);
expect(GetLike.resolveSubCommand({ userId: 0 }, deps)).toBe(13);
expect(GetLike.resolveSubCommand({ userId: 10001 }, deps)).toBe(13);
expect(GetLike.resolveSubCommand({ userId: 20002 }, deps)).toBe(12);
});
describe('invoke (target uid resolution)', () => {
it('uses cached self uid when userId is omitted', async () => {
const deps = makeDeps({
cachedSelfUid: 'cached-self',
responseBody: { userLikeInfos: [{ uid: 'cached-self', time: 0, favoriteInfo: {}, voteInfo: {} }] },
});
await GetLike.invoke(deps, {});
expect(deps.resolveUserUid).not.toHaveBeenCalled();
const [serviceCmd, bytes] = deps.sendRawPacket.mock.calls[0]!;
expect(serviceCmd).toBe('OidbSvcTrpcTcp.0x7ed_13');
const env = protobuf_decode<OidbBase<Oidb0x7edReq>>(bytes);
expect(env.body?.targetUids).toEqual(['cached-self']);
it('uses the self and other-user subcommands selected by the native client', () => {
const deps = makeDeps({ cachedSelfUid: 'self-uid' });
expect(GetLike.command).toBe(0x7ed);
expect(GetLike.resolveSubCommand({}, deps)).toBe(13);
expect(GetLike.resolveSubCommand({ userId: 0 }, deps)).toBe(13);
expect(GetLike.resolveSubCommand({ userId: 10001 }, deps)).toBe(13);
expect(GetLike.resolveSubCommand({ userId: 20002 }, deps)).toBe(12);
});
it('falls back to resolveUserUid for self when cache is empty', async () => {
const deps = makeDeps({
cachedSelfUid: null,
resolveUserUid: vi.fn(async () => 'fresh-self') as any,
responseBody: { userLikeInfos: [{ uid: 'fresh-self', time: 0, favoriteInfo: {}, voteInfo: {} }] },
});
await GetLike.invoke(deps, {});
expect(deps.resolveUserUid).toHaveBeenCalledWith(10001);
describe('invoke (target uid resolution)', () => {
it('uses cached self uid when userId is omitted', async () => {
const deps = makeDeps({
cachedSelfUid: 'cached-self',
responseBody: {
userLikeInfos: [
{ uid: 'cached-self', time: 0, favoriteInfo: {}, voteInfo: {} },
],
},
});
await GetLike.invoke(deps, {});
expect(deps.resolveUserUid).not.toHaveBeenCalled();
const [serviceCmd, bytes] = deps.sendRawPacket.mock.calls[0]!;
expect(serviceCmd).toBe('OidbSvcTrpcTcp.0x7ed_13');
const env = protobuf_decode<OidbBase<Oidb0x7edReq>>(bytes);
expect(env.body?.targetUids).toEqual(['cached-self']);
});
it('falls back to resolveUserUid for self when cache is empty', async () => {
const deps = makeDeps({
cachedSelfUid: null,
resolveUserUid: vi.fn(async () => 'fresh-self') as any,
responseBody: {
userLikeInfos: [{ uid: 'fresh-self', time: 0, favoriteInfo: {}, voteInfo: {} }],
},
});
await GetLike.invoke(deps, {});
expect(deps.resolveUserUid).toHaveBeenCalledWith(10001);
});
it('resolves other users via resolveUserUid', async () => {
const deps = makeDeps({
responseBody: {
userLikeInfos: [
{ uid: 'uid-of-99999', time: 0, favoriteInfo: {}, voteInfo: {} },
],
},
});
await GetLike.invoke(deps, { userId: 99999 });
expect(deps.resolveUserUid).toHaveBeenCalledWith(99999);
expect(deps.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0x7ed_12');
});
it('treats an explicit self uin as a self query', async () => {
const deps = makeDeps({
cachedSelfUid: 'cached-self',
responseBody: { userLikeInfos: [{ uid: 'cached-self', time: 0 }] },
});
await GetLike.invoke(deps, { userId: 10001 });
expect(deps.resolveUserUid).not.toHaveBeenCalled();
expect(deps.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0x7ed_13');
});
it('throws when self uin is invalid and cache is empty', async () => {
const deps = makeDeps({ cachedSelfUid: null });
deps.identity.uin = 'invalid';
await expect(GetLike.invoke(deps, {})).rejects.toThrow('self uid is unavailable');
});
});
it('resolves other users via resolveUserUid', async () => {
const deps = makeDeps({
responseBody: { userLikeInfos: [{ uid: 'uid-of-99999', time: 0, favoriteInfo: {}, voteInfo: {} }] },
});
await GetLike.invoke(deps, { userId: 99999 });
expect(deps.resolveUserUid).toHaveBeenCalledWith(99999);
expect(deps.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0x7ed_12');
describe('serialize', () => {
it('requests the profile-backed vote list', async () => {
const deps = makeDeps({
responseBody: {
userLikeInfos: [{ uid: 'u', time: 0, favoriteInfo: {}, voteInfo: {} }],
},
});
await GetLike.invoke(deps, { userId: 1 });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0x7edReq>>(bytes);
expect(env.body?.basic).toBe(1);
expect(env.body?.vote).toBe(1);
expect(env.body?.favorite ?? 0).toBe(0);
expect(env.body?.userProfile).toBe(1);
});
it('threads start / limit through to the request', async () => {
const deps = makeDeps({
responseBody: {
userLikeInfos: [{ uid: 'u', time: 0, favoriteInfo: {}, voteInfo: {} }],
},
});
await GetLike.invoke(deps, { userId: 1, start: 5, limit: 50 });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0x7edReq>>(bytes);
expect(env.body?.start).toBe(5);
expect(env.body?.limit).toBe(50);
});
it('defaults start=0 / limit=10', async () => {
const deps = makeDeps({
responseBody: {
userLikeInfos: [{ uid: 'u', time: 0, favoriteInfo: {}, voteInfo: {} }],
},
});
await GetLike.invoke(deps, { userId: 1 });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0x7edReq>>(bytes);
expect(env.body?.start ?? 0).toBe(0);
expect(env.body?.limit).toBe(10);
});
});
it('treats an explicit self uin as a self query', async () => {
const deps = makeDeps({
cachedSelfUid: 'cached-self',
responseBody: { userLikeInfos: [{ uid: 'cached-self', time: 0 }] },
});
await GetLike.invoke(deps, { userId: 10001 });
expect(deps.resolveUserUid).not.toHaveBeenCalled();
expect(deps.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0x7ed_13');
describe('deserialize', () => {
it('shapes favorite + vote info with the expected key names', () => {
const deps = makeDeps({
findUinByUid: (uid) => (uid === 'vote-user' ? 12345 : null),
});
const out = GetLike.deserialize(deps, {
userLikeInfos: [
{
uid: 'u',
time: 1700000000,
favoriteInfo: {
totalCount: 5,
lastTime: 1,
todayCount: 1,
userInfos: [{ uid: 'favorite-user', nick: '收藏者', count: 3 }],
},
voteInfo: {
totalCount: 7,
newCount: 2,
newNearbyCount: 1,
lastVisitTime: 2,
userInfos: [
{
uid: 'vote-user',
src: 4,
latestTime: 3,
count: 2,
giftCount: 1,
customId: 5,
lastCharged: 6,
availableCount: 7,
todayVotedCount: 8,
nick: '点赞者',
gender: 1,
age: 20,
isFriend: true,
isVip: true,
isSvip: false,
},
],
},
},
],
});
expect(out.uid).toBe('u');
expect(out.time).toBe(1700000000);
expect(out.favoriteInfo).toEqual({
total_count: 5,
last_time: 1,
today_count: 1,
userInfos: [
expect.objectContaining({ uid: 'favorite-user', nick: '收藏者', count: 3 }),
],
});
expect(out.voteInfo).toEqual({
total_count: 7,
new_count: 2,
new_nearby_count: 1,
last_visit_time: 2,
userInfos: [
expect.objectContaining({
uid: 'vote-user',
uin: 12345,
src: 4,
latestTime: 3,
count: 2,
giftCount: 1,
customId: 5,
lastCharged: 6,
bAvailableCnt: 7,
bTodayVotedCnt: 8,
nick: '点赞者',
gender: 1,
age: 20,
isFriend: true,
isvip: true,
isSvip: false,
}),
],
});
});
it('throws when there are no userLikeInfos', () => {
expect(() => GetLike.deserialize({} as any, {})).toThrow('get profile like info empty');
expect(() => GetLike.deserialize({} as any, { userLikeInfos: [] })).toThrow(
'get profile like info empty',
);
});
it('throws when a returned user item has no uid', () => {
expect(() =>
GetLike.deserialize(makeDeps(), {
userLikeInfos: [{ uid: 'u', voteInfo: { userInfos: [{ nick: 'broken' }] } }],
}),
).toThrow('get profile like user uid missing');
});
it('defaults all count fields to 0 when omitted', () => {
const out = GetLike.deserialize(makeDeps(), { userLikeInfos: [{ uid: 'u', time: 0 }] });
expect(out.favoriteInfo.total_count).toBe(0);
expect(out.voteInfo.total_count).toBe(0);
});
});
it('throws when self uin is invalid and cache is empty', async () => {
const deps = makeDeps({ cachedSelfUid: null });
deps.identity.uin = 'invalid';
await expect(GetLike.invoke(deps, {})).rejects.toThrow('self uid is unavailable');
describe('invoke (inbound UIN fill)', () => {
it('fills leftover uin=0 rows through Identity.resolveUin once per uid', async () => {
const deps = makeDeps({
cachedSelfUid: 'self-uid',
findUinByUid: () => null,
resolveUin: async (uid) => (uid === 'shared-uid' ? 30003 : null),
responseBody: {
userLikeInfos: [
{
uid: 'self-uid',
favoriteInfo: { userInfos: [{ uid: 'shared-uid', nick: '收藏' }] },
voteInfo: {
userInfos: [
{ uid: 'shared-uid', nick: '点赞' },
{ uid: 'miss-uid' },
],
},
},
],
},
});
const out = await GetLike.invoke(deps, {});
expect(deps.identity.resolveUin).toHaveBeenCalledTimes(2);
expect(deps.identity.resolveUin).toHaveBeenCalledWith('shared-uid');
expect(deps.identity.resolveUin).toHaveBeenCalledWith('miss-uid');
expect(out.favoriteInfo.userInfos[0]?.uin).toBe(30003);
expect(out.voteInfo.userInfos[0]?.uin).toBe(30003);
expect(out.voteInfo.userInfos[1]?.uin).toBe(0);
});
it('does not call resolveUin when deserialize already filled the cache hit', async () => {
const deps = makeDeps({
cachedSelfUid: 'self-uid',
findUinByUid: (uid) => (uid === 'cached-uid' ? 20002 : null),
responseBody: {
userLikeInfos: [
{
uid: 'self-uid',
voteInfo: { userInfos: [{ uid: 'cached-uid', nick: '点赞者' }] },
},
],
},
});
const out = await GetLike.invoke(deps, {});
expect(deps.identity.resolveUin).not.toHaveBeenCalled();
expect(out.voteInfo.userInfos[0]?.uin).toBe(20002);
});
});
});
describe('serialize', () => {
it('requests the profile-backed vote list', async () => {
const deps = makeDeps({
responseBody: { userLikeInfos: [{ uid: 'u', time: 0, favoriteInfo: {}, voteInfo: {} }] },
});
await GetLike.invoke(deps, { userId: 1 });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0x7edReq>>(bytes);
expect(env.body?.basic).toBe(1);
expect(env.body?.vote).toBe(1);
expect(env.body?.favorite ?? 0).toBe(0);
expect(env.body?.userProfile).toBe(1);
});
it('threads start / limit through to the request', async () => {
const deps = makeDeps({
responseBody: { userLikeInfos: [{ uid: 'u', time: 0, favoriteInfo: {}, voteInfo: {} }] },
});
await GetLike.invoke(deps, { userId: 1, start: 5, limit: 50 });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0x7edReq>>(bytes);
expect(env.body?.start).toBe(5);
expect(env.body?.limit).toBe(50);
});
it('defaults start=0 / limit=10', async () => {
const deps = makeDeps({
responseBody: { userLikeInfos: [{ uid: 'u', time: 0, favoriteInfo: {}, voteInfo: {} }] },
});
await GetLike.invoke(deps, { userId: 1 });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0x7edReq>>(bytes);
expect(env.body?.start ?? 0).toBe(0);
expect(env.body?.limit).toBe(10);
});
});
describe('deserialize', () => {
it('shapes favorite + vote info with the expected key names', () => {
const deps = makeDeps({
findUinByUid: uid => uid === 'vote-user' ? 12345 : null,
});
const out = GetLike.deserialize(deps, {
userLikeInfos: [{
uid: 'u', time: 1700000000,
favoriteInfo: {
totalCount: 5,
lastTime: 1,
todayCount: 1,
userInfos: [{ uid: 'favorite-user', nick: '收藏者', count: 3 }],
},
voteInfo: {
totalCount: 7,
newCount: 2,
newNearbyCount: 1,
lastVisitTime: 2,
userInfos: [{
uid: 'vote-user',
src: 4,
latestTime: 3,
count: 2,
giftCount: 1,
customId: 5,
lastCharged: 6,
availableCount: 7,
todayVotedCount: 8,
nick: '点赞者',
gender: 1,
age: 20,
isFriend: true,
isVip: true,
isSvip: false,
}],
},
}],
});
expect(out.uid).toBe('u');
expect(out.time).toBe(1700000000);
expect(out.favoriteInfo).toEqual({
total_count: 5,
last_time: 1,
today_count: 1,
userInfos: [expect.objectContaining({ uid: 'favorite-user', nick: '收藏者', count: 3 })],
});
expect(out.voteInfo).toEqual({
total_count: 7,
new_count: 2,
new_nearby_count: 1,
last_visit_time: 2,
userInfos: [expect.objectContaining({
uid: 'vote-user',
uin: 12345,
src: 4,
latestTime: 3,
count: 2,
giftCount: 1,
customId: 5,
lastCharged: 6,
bAvailableCnt: 7,
bTodayVotedCnt: 8,
nick: '点赞者',
gender: 1,
age: 20,
isFriend: true,
isvip: true,
isSvip: false,
})],
});
});
it('throws when there are no userLikeInfos', () => {
expect(() => GetLike.deserialize({} as any, {})).toThrow('get profile like info empty');
expect(() => GetLike.deserialize({} as any, { userLikeInfos: [] })).toThrow('get profile like info empty');
});
it('throws when a returned user item has no uid', () => {
expect(() => GetLike.deserialize(makeDeps(), {
userLikeInfos: [{ uid: 'u', voteInfo: { userInfos: [{ nick: 'broken' }] } }],
})).toThrow('get profile like user uid missing');
});
it('defaults all count fields to 0 when omitted', () => {
const out = GetLike.deserialize(makeDeps(), { userLikeInfos: [{ uid: 'u', time: 0 }] });
expect(out.favoriteInfo.total_count).toBe(0);
expect(out.voteInfo.total_count).toBe(0);
});
});
describe('invoke (inbound UIN fill)', () => {
it('fills leftover uin=0 rows through Identity.resolveUin once per uid', async () => {
const deps = makeDeps({
cachedSelfUid: 'self-uid',
findUinByUid: () => null,
resolveUin: async (uid) => uid === 'shared-uid' ? 30003 : null,
responseBody: {
userLikeInfos: [{
uid: 'self-uid',
favoriteInfo: { userInfos: [{ uid: 'shared-uid', nick: '收藏' }] },
voteInfo: { userInfos: [{ uid: 'shared-uid', nick: '点赞' }, { uid: 'miss-uid' }] },
}],
},
});
const out = await GetLike.invoke(deps, {});
expect(deps.identity.resolveUin).toHaveBeenCalledTimes(2);
expect(deps.identity.resolveUin).toHaveBeenCalledWith('shared-uid');
expect(deps.identity.resolveUin).toHaveBeenCalledWith('miss-uid');
expect(out.favoriteInfo.userInfos[0]?.uin).toBe(30003);
expect(out.voteInfo.userInfos[0]?.uin).toBe(30003);
expect(out.voteInfo.userInfos[1]?.uin).toBe(0);
});
it('does not call resolveUin when deserialize already filled the cache hit', async () => {
const deps = makeDeps({
cachedSelfUid: 'self-uid',
findUinByUid: (uid) => uid === 'cached-uid' ? 20002 : null,
responseBody: {
userLikeInfos: [{
uid: 'self-uid',
voteInfo: { userInfos: [{ uid: 'cached-uid', nick: '点赞者' }] },
}],
},
});
const out = await GetLike.invoke(deps, {});
expect(deps.identity.resolveUin).not.toHaveBeenCalled();
expect(out.voteInfo.userInfos[0]?.uin).toBe(20002);
});
});
});
@@ -7,70 +7,87 @@ import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { GetUnidirectionalFriendList } from '../../../src/oidb-services/profile/get-unidirectional-friend-list';
function makeDeps(responseJson?: unknown) {
const responseData = responseJson !== undefined
? Buffer.from(protobuf_encode<OidbBase<Oidb0xe17Resp>>({
body: { jsonBody: JSON.stringify(responseJson) },
}))
: Buffer.alloc(0);
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData };
return {
sendRawPacket: vi.fn(async () => r),
identity: { uin: '10001' } as any,
};
const responseData =
responseJson !== undefined
? Buffer.from(
protobuf_encode<OidbBase<Oidb0xe17Resp>>({
body: { jsonBody: JSON.stringify(responseJson) },
}),
)
: Buffer.alloc(0);
const r: SendPacketResult = {
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData,
};
return {
sendRawPacket: vi.fn(async () => r),
identity: { uin: '10001' } as any,
};
}
describe('GetUnidirectionalFriendList namespace', () => {
it('declares 0xE17_0', () => {
expect(GetUnidirectionalFriendList.command).toBe(0xE17);
expect(GetUnidirectionalFriendList.subCommand).toBe(0);
});
it('overrides the wire name to MQUpdateSvc.OidbSvc.0xe17_0', async () => {
const deps = makeDeps({ rpt_block_list: [] });
await GetUnidirectionalFriendList.invoke(deps);
expect(deps.sendRawPacket.mock.calls[0]![0]).toBe('MQUpdateSvc_com_qq_ti.web.OidbSvc.0xe17_0');
});
describe('serialize', () => {
it('JSON-encodes the request body with the bot uin and fixed pagination', async () => {
const deps = makeDeps({ rpt_block_list: [] });
await GetUnidirectionalFriendList.invoke(deps);
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0xe17Req>>(bytes);
const parsed = JSON.parse(env.body?.jsonBody ?? '{}');
expect(parsed).toEqual({
uint64_uin: '10001',
uint64_top: 0,
uint32_req_num: 99,
bytes_cookies: '',
});
});
});
describe('deserialize', () => {
it('parses the embedded JSON body and returns rpt_block_list', () => {
const out = GetUnidirectionalFriendList.deserialize({} as any, {
jsonBody: JSON.stringify({ rpt_block_list: [{ uin: 10001 }, { uin: 10002 }] }),
});
expect(out).toEqual([{ uin: 10001 }, { uin: 10002 }]);
it('declares 0xE17_0', () => {
expect(GetUnidirectionalFriendList.command).toBe(0xe17);
expect(GetUnidirectionalFriendList.subCommand).toBe(0);
});
it('returns [] when rpt_block_list is absent', () => {
const out = GetUnidirectionalFriendList.deserialize({} as any, { jsonBody: JSON.stringify({}) });
expect(out).toEqual([]);
it('overrides the wire name to MQUpdateSvc.OidbSvc.0xe17_0', async () => {
const deps = makeDeps({ rpt_block_list: [] });
await GetUnidirectionalFriendList.invoke(deps);
expect(deps.sendRawPacket.mock.calls[0]![0]).toBe(
'MQUpdateSvc_com_qq_ti.web.OidbSvc.0xe17_0',
);
});
it('throws when jsonBody is empty / missing', () => {
expect(() => GetUnidirectionalFriendList.deserialize({} as any, {})).toThrow('get unidirectional friend list empty');
expect(() => GetUnidirectionalFriendList.deserialize({} as any, { jsonBody: '' })).toThrow('get unidirectional friend list empty');
describe('serialize', () => {
it('JSON-encodes the request body with the bot uin and fixed pagination', async () => {
const deps = makeDeps({ rpt_block_list: [] });
await GetUnidirectionalFriendList.invoke(deps);
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0xe17Req>>(bytes);
const parsed = JSON.parse(env.body?.jsonBody ?? '{}');
expect(parsed).toEqual({
uint64_uin: '10001',
uint64_top: 0,
uint32_req_num: 99,
bytes_cookies: '',
});
});
});
});
describe('invoke (e2e)', () => {
it('returns the parsed block list', async () => {
const deps = makeDeps({ rpt_block_list: [{ uin: 999 }] });
const out = await GetUnidirectionalFriendList.invoke(deps);
expect(out).toEqual([{ uin: 999 }]);
describe('deserialize', () => {
it('parses the embedded JSON body and returns rpt_block_list', () => {
const out = GetUnidirectionalFriendList.deserialize({} as any, {
jsonBody: JSON.stringify({ rpt_block_list: [{ uin: 10001 }, { uin: 10002 }] }),
});
expect(out).toEqual([{ uin: 10001 }, { uin: 10002 }]);
});
it('returns [] when rpt_block_list is absent', () => {
const out = GetUnidirectionalFriendList.deserialize({} as any, {
jsonBody: JSON.stringify({}),
});
expect(out).toEqual([]);
});
it('throws when jsonBody is empty / missing', () => {
expect(() => GetUnidirectionalFriendList.deserialize({} as any, {})).toThrow(
'get unidirectional friend list empty',
);
expect(() =>
GetUnidirectionalFriendList.deserialize({} as any, { jsonBody: '' }),
).toThrow('get unidirectional friend list empty');
});
});
describe('invoke (e2e)', () => {
it('returns the parsed block list', async () => {
const deps = makeDeps({ rpt_block_list: [{ uin: 999 }] });
const out = await GetUnidirectionalFriendList.invoke(deps);
expect(out).toEqual([{ uin: 999 }]);
});
});
});
});
@@ -7,48 +7,55 @@ import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { SetInputStatus } from '../../../src/oidb-services/profile/set-input-status';
function makeDeps(uid: string | null = 'resolved-uid') {
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: Buffer.alloc(0) };
return {
sendRawPacket: vi.fn(async () => r),
resolveUserUid: vi.fn(async () => uid as string),
};
const r: SendPacketResult = {
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData: Buffer.alloc(0),
};
return {
sendRawPacket: vi.fn(async () => r),
resolveUserUid: vi.fn(async () => uid as string),
};
}
describe('SetInputStatus namespace', () => {
it('declares 0xCD4_1', () => {
expect(SetInputStatus.command).toBe(0xCD4);
expect(SetInputStatus.subCommand).toBe(1);
});
describe('invoke', () => {
it('routes to OidbSvcTrpcTcp.0xcd4_1', async () => {
const deps = makeDeps();
await SetInputStatus.invoke(deps, { userId: 10001, eventType: 1 });
expect(deps.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0xcd4_1');
it('declares 0xCD4_1', () => {
expect(SetInputStatus.command).toBe(0xcd4);
expect(SetInputStatus.subCommand).toBe(1);
});
it('resolves the target uid before encoding', async () => {
const deps = makeDeps();
await SetInputStatus.invoke(deps, { userId: 10001, eventType: 1 });
expect(deps.resolveUserUid).toHaveBeenCalledWith(10001);
});
describe('invoke', () => {
it('routes to OidbSvcTrpcTcp.0xcd4_1', async () => {
const deps = makeDeps();
await SetInputStatus.invoke(deps, { userId: 10001, eventType: 1 });
expect(deps.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0xcd4_1');
});
it('packages uid + chatType=0 + eventType into reqBody', async () => {
const deps = makeDeps('alice-uid');
await SetInputStatus.invoke(deps, { userId: 10001, eventType: 7 });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0xcd4Req>>(bytes);
expect(env.body?.reqBody?.uid).toBe('alice-uid');
// chatType=0 is proto3 default — omitted on the wire, comes back as null/undefined.
expect(env.body?.reqBody?.chatType ?? 0).toBe(0);
expect(env.body?.reqBody?.eventType).toBe(7);
});
it('resolves the target uid before encoding', async () => {
const deps = makeDeps();
await SetInputStatus.invoke(deps, { userId: 10001, eventType: 1 });
expect(deps.resolveUserUid).toHaveBeenCalledWith(10001);
});
it('throws when the target uid cannot be resolved', async () => {
const deps = makeDeps(null);
await expect(SetInputStatus.invoke(deps, { userId: 10001, eventType: 1 }))
.rejects.toThrow('target uid not found');
expect(deps.sendRawPacket).not.toHaveBeenCalled();
it('packages uid + chatType=0 + eventType into reqBody', async () => {
const deps = makeDeps('alice-uid');
await SetInputStatus.invoke(deps, { userId: 10001, eventType: 7 });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0xcd4Req>>(bytes);
expect(env.body?.reqBody?.uid).toBe('alice-uid');
// chatType=0 is proto3 default — omitted on the wire, comes back as null/undefined.
expect(env.body?.reqBody?.chatType ?? 0).toBe(0);
expect(env.body?.reqBody?.eventType).toBe(7);
});
it('throws when the target uid cannot be resolved', async () => {
const deps = makeDeps(null);
await expect(
SetInputStatus.invoke(deps, { userId: 10001, eventType: 1 }),
).rejects.toThrow('target uid not found');
expect(deps.sendRawPacket).not.toHaveBeenCalled();
});
});
});
});
@@ -7,69 +7,75 @@ import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { SetProfile } from '../../../src/oidb-services/profile/set-profile';
function makeDeps() {
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: Buffer.alloc(0) };
return {
sendRawPacket: vi.fn(async () => r),
identity: { uin: '10001' } as any,
};
const r: SendPacketResult = {
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData: Buffer.alloc(0),
};
return {
sendRawPacket: vi.fn(async () => r),
identity: { uin: '10001' } as any,
};
}
describe('SetProfile namespace', () => {
it('declares 0x112A_2', () => {
expect(SetProfile.command).toBe(0x112A);
expect(SetProfile.subCommand).toBe(2);
});
describe('invoke', () => {
it('packages sex into integer field 20009', async () => {
const deps = makeDeps();
await SetProfile.invoke(deps, { sex: 1 });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbSetProfile>>(bytes);
expect(env.body?.intProfiles).toEqual([{ fieldId: 20009, value: 1n }]);
it('declares 0x112A_2', () => {
expect(SetProfile.command).toBe(0x112a);
expect(SetProfile.subCommand).toBe(2);
});
it('is a no-op when both nickname and personalNote are undefined', async () => {
const deps = makeDeps();
await SetProfile.invoke(deps, {});
expect(deps.sendRawPacket).not.toHaveBeenCalled();
});
describe('invoke', () => {
it('packages sex into integer field 20009', async () => {
const deps = makeDeps();
await SetProfile.invoke(deps, { sex: 1 });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbSetProfile>>(bytes);
expect(env.body?.intProfiles).toEqual([{ fieldId: 20009, value: 1n }]);
});
it('packages nickname into fieldId 20002', async () => {
const deps = makeDeps();
await SetProfile.invoke(deps, { nickname: 'New Nick' });
expect(deps.sendRawPacket).toHaveBeenCalledOnce();
const [wireName, bytes] = deps.sendRawPacket.mock.calls[0]!;
expect(wireName).toBe('OidbSvcTrpcTcp.0x112a_2');
const env = protobuf_decode<OidbBase<OidbSetProfile>>(bytes);
expect(env.body?.stringProfiles).toEqual([{ fieldId: 20002, value: 'New Nick' }]);
});
it('is a no-op when both nickname and personalNote are undefined', async () => {
const deps = makeDeps();
await SetProfile.invoke(deps, {});
expect(deps.sendRawPacket).not.toHaveBeenCalled();
});
it('packages personalNote into fieldId 102', async () => {
const deps = makeDeps();
await SetProfile.invoke(deps, { personalNote: 'on vacation' });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbSetProfile>>(bytes);
expect(env.body?.stringProfiles).toEqual([{ fieldId: 102, value: 'on vacation' }]);
});
it('packages nickname into fieldId 20002', async () => {
const deps = makeDeps();
await SetProfile.invoke(deps, { nickname: 'New Nick' });
expect(deps.sendRawPacket).toHaveBeenCalledOnce();
const [wireName, bytes] = deps.sendRawPacket.mock.calls[0]!;
expect(wireName).toBe('OidbSvcTrpcTcp.0x112a_2');
const env = protobuf_decode<OidbBase<OidbSetProfile>>(bytes);
expect(env.body?.stringProfiles).toEqual([{ fieldId: 20002, value: 'New Nick' }]);
});
it('packages both fields together', async () => {
const deps = makeDeps();
await SetProfile.invoke(deps, { nickname: 'Foo', personalNote: 'Bar' });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbSetProfile>>(bytes);
expect(env.body?.stringProfiles).toEqual([
{ fieldId: 20002, value: 'Foo' },
{ fieldId: 102, value: 'Bar' },
]);
});
it('packages personalNote into fieldId 102', async () => {
const deps = makeDeps();
await SetProfile.invoke(deps, { personalNote: 'on vacation' });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbSetProfile>>(bytes);
expect(env.body?.stringProfiles).toEqual([{ fieldId: 102, value: 'on vacation' }]);
});
it('passes the bot uin in the request body', async () => {
const deps = makeDeps();
await SetProfile.invoke(deps, { nickname: 'x' });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbSetProfile>>(bytes);
expect(env.body?.uin).toBe(10001n);
it('packages both fields together', async () => {
const deps = makeDeps();
await SetProfile.invoke(deps, { nickname: 'Foo', personalNote: 'Bar' });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbSetProfile>>(bytes);
expect(env.body?.stringProfiles).toEqual([
{ fieldId: 20002, value: 'Foo' },
{ fieldId: 102, value: 'Bar' },
]);
});
it('passes the bot uin in the request body', async () => {
const deps = makeDeps();
await SetProfile.invoke(deps, { nickname: 'x' });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<OidbSetProfile>>(bytes);
expect(env.body?.uin).toBe(10001n);
});
});
});
});
@@ -7,49 +7,55 @@ import type { SendPacketResult } from '@snowluma/common/packet-sender';
import { SetSelfLongNick } from '../../../src/oidb-services/profile/set-self-long-nick';
function makeDeps() {
const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData: Buffer.alloc(0) };
return {
sendRawPacket: vi.fn(async () => r),
identity: { uin: '10001' } as any,
};
const r: SendPacketResult = {
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData: Buffer.alloc(0),
};
return {
sendRawPacket: vi.fn(async () => r),
identity: { uin: '10001' } as any,
};
}
describe('SetSelfLongNick namespace', () => {
it('declares 0x112A_2', () => {
expect(SetSelfLongNick.command).toBe(0x112A);
expect(SetSelfLongNick.subCommand).toBe(2);
});
describe('invoke', () => {
it('routes to OidbSvcTrpcTcp.0x112a_2', async () => {
const deps = makeDeps();
await SetSelfLongNick.invoke(deps, { longNick: 'hello world' });
expect(deps.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0x112a_2');
it('declares 0x112A_2', () => {
expect(SetSelfLongNick.command).toBe(0x112a);
expect(SetSelfLongNick.subCommand).toBe(2);
});
it('wraps the long nick in profile { tag: 102, value }', async () => {
const deps = makeDeps();
await SetSelfLongNick.invoke(deps, { longNick: 'hello world' });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0x112aReq>>(bytes);
expect(env.body?.uin).toBe(10001n);
expect(env.body?.profile).toEqual({ tag: 102, value: 'hello world' });
});
describe('invoke', () => {
it('routes to OidbSvcTrpcTcp.0x112a_2', async () => {
const deps = makeDeps();
await SetSelfLongNick.invoke(deps, { longNick: 'hello world' });
expect(deps.sendRawPacket.mock.calls[0]![0]).toBe('OidbSvcTrpcTcp.0x112a_2');
});
it('omits the value field when clearing the signature', async () => {
const deps = makeDeps();
await SetSelfLongNick.invoke(deps, { longNick: '' });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
expect(Buffer.from(bytes).toString('hex')).toBe('08aa221002220708914e12020866');
});
it('wraps the long nick in profile { tag: 102, value }', async () => {
const deps = makeDeps();
await SetSelfLongNick.invoke(deps, { longNick: 'hello world' });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0x112aReq>>(bytes);
expect(env.body?.uin).toBe(10001n);
expect(env.body?.profile).toEqual({ tag: 102, value: 'hello world' });
});
it('coerces non-string longNick into a string (defensive)', async () => {
const deps = makeDeps();
// simulate a misbehaving caller passing a number
await SetSelfLongNick.invoke(deps, { longNick: 12345 as any });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0x112aReq>>(bytes);
expect(env.body?.profile?.value).toBe('12345');
it('omits the value field when clearing the signature', async () => {
const deps = makeDeps();
await SetSelfLongNick.invoke(deps, { longNick: '' });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
expect(Buffer.from(bytes).toString('hex')).toBe('08aa221002220708914e12020866');
});
it('coerces non-string longNick into a string (defensive)', async () => {
const deps = makeDeps();
// simulate a misbehaving caller passing a number
await SetSelfLongNick.invoke(deps, { longNick: 12345 as any });
const [, bytes] = deps.sendRawPacket.mock.calls[0]!;
const env = protobuf_decode<OidbBase<Oidb0x112aReq>>(bytes);
expect(env.body?.profile?.value).toBe('12345');
});
});
});
});