SnowLuma v2.4.2 source snapshot
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
Publish MCP to npm / Build, test, and publish MCP (push) Waiting to run
Publish SDK to npm / Build, test, and publish SDK (push) Waiting to run
Sync action catalog to docs / sync (push) Waiting to run
Publish UI to npm / Build and publish UI (push) Waiting to run
Build and Release / preflight (push) Waiting to run

This commit is contained in:
TanTan committed 2026-11-13 09:02:00 +08:00
commit fa9c73363d
1367 files changed
+355735

No files matched your search

@@ -0,0 +1,310 @@
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 { Oidb0x7edReq, Oidb0x7edResp } from '@snowluma/proto-defs/oidb-actions/base';
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}`)),
};
}
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('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');
});
});
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);
});
});
});
@@ -0,0 +1,101 @@
import { describe, expect, it, vi } from 'vitest';
import type { SendPacketResult } from '@snowluma/common/packet-sender';
import type { IdentityService } from '../../../src/identity-service';
import { GetUnidirectionalFriendList } from '../../../src/oidb-services/profile/get-unidirectional-friend-list';
import { s, v } from '../_pb-oracle';
function makeDeps(json: unknown = { ActionStatus: 'OK', ErrorCode: 0, rpt_block_list: [] }) {
const result: SendPacketResult = {
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData: Buffer.from([
...v(1, 0xe17),
...v(2, 0),
...v(3, 0),
...s(4, JSON.stringify(json)),
]),
};
return {
sendRawPacket: vi.fn(async (_cmd: string, _body: Uint8Array) => result),
identity: { uin: '10001' } as IdentityService,
};
}
describe('GetUnidirectionalFriendList', () => {
it('uses the direct JSON request and response captured for this service', async () => {
const list = [{ uint64_uin: '20002', extra: { keep: true } }];
const deps = makeDeps({ ActionStatus: 'OK', ErrorCode: 0, rpt_block_list: list });
await expect(GetUnidirectionalFriendList.invoke(deps)).resolves.toEqual(list);
const [command, bytes] = deps.sendRawPacket.mock.calls[0]!;
expect(command).toBe('MQUpdateSvc_com_qq_ti.web.OidbSvc.0xe17_0');
expect(Buffer.from(bytes)).toEqual(
Buffer.from([
...v(2, 0),
...s(
3,
JSON.stringify({
uint64_uin: '10001',
uint64_top: 0,
uint32_req_num: 99,
bytes_cookies: '',
}),
),
]),
);
});
it('returns a verified empty list', async () => {
await expect(GetUnidirectionalFriendList.invoke(makeDeps())).resolves.toEqual([]);
});
it('surfaces JSON business failures instead of returning an empty list', async () => {
const deps = makeDeps({
ActionStatus: 'FAIL',
ErrorCode: 302,
ErrorInfo: 'parse json failed',
});
await expect(GetUnidirectionalFriendList.invoke(deps)).rejects.toThrow(
'302 parse json failed',
);
});
it.each([
null,
{},
{ ActionStatus: 'OK', ErrorCode: 0 },
{ ActionStatus: 'OK', ErrorCode: 0, rpt_block_list: [null] },
{ ActionStatus: 'OK', ErrorCode: 0, rpt_block_list: {} },
])('rejects malformed response data (%j)', async (json) => {
await expect(GetUnidirectionalFriendList.invoke(makeDeps(json))).rejects.toThrow();
});
it('surfaces outer errors, invalid JSON, empty replies and transport failures', async () => {
const deps = makeDeps();
for (const [responseData, message] of [
[Buffer.from([...v(3, 5), ...s(5, 'denied')]), 'denied'],
[Buffer.from(s(4, '{')), 'JSON'],
[Buffer.from(v(1, 0xe17)), 'empty'],
[Buffer.alloc(0), 'empty'],
] as const) {
deps.sendRawPacket.mockResolvedValue({
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData,
});
await expect(GetUnidirectionalFriendList.invoke(deps)).rejects.toThrow(message);
}
deps.sendRawPacket.mockResolvedValue({
success: false,
gotResponse: false,
errorCode: 5,
errorMessage: 'disconnected',
});
await expect(GetUnidirectionalFriendList.invoke(deps)).rejects.toThrow('disconnected');
});
});
@@ -0,0 +1,61 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { Oidb0xcd4Req } from '@snowluma/proto-defs/oidb-actions/base';
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),
};
}
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('resolves the target uid before encoding', async () => {
const deps = makeDeps();
await SetInputStatus.invoke(deps, { userId: 10001, eventType: 1 });
expect(deps.resolveUserUid).toHaveBeenCalledWith(10001);
});
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();
});
});
});
@@ -0,0 +1,81 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { OidbSetProfile } from '@snowluma/proto-defs/oidb-actions/base';
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,
};
}
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('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 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 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 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);
});
});
});
@@ -0,0 +1,61 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type { Oidb0x112aReq } from '@snowluma/proto-defs/oidb-actions/base';
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,
};
}
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('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('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');
});
});
});