feat(onebot): support moving friends between categories

Add the set_friends_category action with either an existing category ID or an exact unique category name.

Validate the current friend and category roster before applying a change, reject missing or ambiguous targets, and surface failed updates instead of reporting false success. Expose the action through the SDK types and cover validation, successful updates, repeat requests, service rejection, and registry completeness.

Closes #297
This commit is contained in:
DrSmoothl committed 2026-07-30 10:37:16 +08:00
1 parent a3b790d472
commit f0ff8da2fa
10 files changed
+429 -3

No files matched your search

+20
View File
@@ -1189,6 +1189,26 @@ export const actions = [
},
}),
defineAction({
name: 'set_friends_category',
summary: '移动好友到指定分组',
returns: '成功时返回空数据。',
params: {
uin: f.userId().describe('要移动的好友 QQ 号'),
categoryId: f.int({ min: 0 }).optional().describe('目标分组 ID'),
categoryName: f.string({ allowEmpty: false }).optional().describe('目标分组名称(必须唯一且完全匹配)'),
},
rules: (rules) => [rules.exactlyOneOf('categoryId', 'categoryName')],
run: async (params, ctx) => {
await ctx.bridge.apis.contacts.setFriendCategory({
uin: params.uin,
categoryId: params.categoryId,
categoryName: params.categoryName,
});
return okResponse();
},
}),
// get_recent_contact — 最近会话列表(占位)。QQ 原生该接口是内核本地快照
// (getRecentContactListSnapShot),返回带 peerName/remark/lastestMsg 等丰富
// 元信息;SnowLuma 既无对应 SSO/packet wire,自有 message store 也只覆盖
@@ -90,9 +90,9 @@ describe('compileActionRegistry namespace conflicts', () => {
});
describe('compiled production Action registry', () => {
it('preserves all 180 canonical Action docs', () => {
expect(ACTION_REGISTRY.actions).toHaveLength(180);
expect(collectActionDocs()).toHaveLength(180);
it('preserves all 181 canonical Action docs', () => {
expect(ACTION_REGISTRY.actions).toHaveLength(181);
expect(collectActionDocs()).toHaveLength(181);
});
it('resolves every executable name to exactly one canonical doc', () => {
@@ -38,6 +38,7 @@ function fakeMeta(overrides: Partial<MessageMeta> = {}): MessageMeta {
const APIS_ROUTING: Record<string, [string, string]> = {
fetchFriendList: ['contacts', 'fetchFriendList'],
fetchFriendCategories: ['contacts', 'fetchFriendCategories'],
setFriendCategory: ['contacts', 'setFriendCategory'],
fetchGroupList: ['contacts', 'fetchGroupList'],
fetchGroupMemberList: ['contacts', 'fetchGroupMemberList'],
fetchUserProfile: ['contacts', 'fetchUserProfile'],
@@ -265,6 +266,72 @@ describe('extended-actions / get_friends_with_category', () => {
});
});
describe('extended-actions / set_friends_category', () => {
it('moves a friend by category ID', async () => {
const setFriendCategory = vi.fn(async () => undefined);
const bridge = fakeBridge({ setFriendCategory });
const response = await makeHandler(fakeCtx(bridge))
.handle('set_friends_category', { uin: 10001, categoryId: 7 });
expect(response).toMatchObject({ status: 'ok', retcode: 0, data: null });
expect(setFriendCategory).toHaveBeenCalledWith({
uin: 10001,
categoryId: 7,
categoryName: undefined,
});
});
it('moves a friend by an exact category name', async () => {
const setFriendCategory = vi.fn(async () => undefined);
const bridge = fakeBridge({ setFriendCategory });
const response = await makeHandler(fakeCtx(bridge))
.handle('set_friends_category', { uin: 10001, categoryName: 'Work' });
expect(response).toMatchObject({ status: 'ok', retcode: 0, data: null });
expect(setFriendCategory).toHaveBeenCalledWith({
uin: 10001,
categoryId: undefined,
categoryName: 'Work',
});
});
it('rejects missing or conflicting category selectors before calling QQ', async () => {
const setFriendCategory = vi.fn(async () => undefined);
const bridge = fakeBridge({ setFriendCategory });
const handler = makeHandler(fakeCtx(bridge));
const missing = await handler.handle('set_friends_category', { uin: 10001 });
const conflicting = await handler.handle('set_friends_category', {
uin: 10001,
categoryId: 7,
categoryName: 'Work',
});
expect(missing).toMatchObject({ status: 'failed', retcode: 1400 });
expect(conflicting).toMatchObject({ status: 'failed', retcode: 1400 });
expect(setFriendCategory).not.toHaveBeenCalled();
});
it('surfaces roster or server failures instead of reporting success', async () => {
const bridge = fakeBridge({
setFriendCategory: vi.fn(async () => {
throw new Error('friend 10001 is not in the live roster');
}),
});
const response = await makeHandler(fakeCtx(bridge))
.handle('set_friends_category', { uin: 10001, categoryId: 7 });
expect(response).toMatchObject({
status: 'failed',
retcode: 100,
wording: 'friend 10001 is not in the live roster',
});
});
});
// ─── Tier 1: send_packet / .send_packet ───
describe('extended-actions / send_packet', () => {