From 05feca9a2c06aac396d51cd32b36aedd70db66af Mon Sep 17 00:00:00 2001 From: Shxiao Date: Wed, 7 Oct 2026 23:05:51 +0900 Subject: [PATCH] =?UTF-8?q?fix(onebot):=20get=5Fgroup=5Ffiles=5Fby=5Ffolde?= =?UTF-8?q?r=20=E4=B8=8D=E5=86=8D=E4=B8=A2=E5=BC=83=E5=AD=90=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9=20(#2095)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 该接口的 returnSchema 声明了 folders(文件夹列表),actionDescription 也写着「获取指定群文件夹下的文件及子文件夹列表」,但 _handle 把 folders 硬编码成空数组。 它调用的 MsgApi.getGroupFileList 与 get_group_root_files 相同,返回项 既可能带 fileInfo 也可能带 folderInfo;兄弟实现 GetGroupRootFiles 把 两类都映射了出来。后果是进入某个子目录后无法继续往下遍历,调用方永远 只能拿到空的 folders。 改为按兄弟接口的方式映射 folderInfo。 --- .../action/go-cqhttp/GetGroupFilesByFolder.ts | 3 +- .../napcat-test/groupFilesByFolder.test.ts | 67 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 packages/napcat-test/groupFilesByFolder.test.ts diff --git a/packages/napcat-onebot/action/go-cqhttp/GetGroupFilesByFolder.ts b/packages/napcat-onebot/action/go-cqhttp/GetGroupFilesByFolder.ts index 8fc45483..f8db4d2c 100644 --- a/packages/napcat-onebot/action/go-cqhttp/GetGroupFilesByFolder.ts +++ b/packages/napcat-onebot/action/go-cqhttp/GetGroupFilesByFolder.ts @@ -43,7 +43,8 @@ export class GetGroupFilesByFolder extends OneBotAction return { files: ret.filter(item => item.fileInfo) .map(item => OB11Construct.file(item.peerId, item.fileInfo!)), - folders: [], + folders: ret.filter(item => item.folderInfo) + .map(item => OB11Construct.folder(item.peerId, item.folderInfo!)), }; } } diff --git a/packages/napcat-test/groupFilesByFolder.test.ts b/packages/napcat-test/groupFilesByFolder.test.ts new file mode 100644 index 00000000..0072471c --- /dev/null +++ b/packages/napcat-test/groupFilesByFolder.test.ts @@ -0,0 +1,67 @@ +// StreamBasic must be evaluated before OneBotAction: the two modules import each other, +// and loading OneBotAction first leaves BasicStream extending an uninitialized binding. +import '../napcat-onebot/action/stream/StreamBasic'; +import { describe, expect, test, vi } from 'vitest'; +import { GetGroupFilesByFolder } from '../napcat-onebot/action/go-cqhttp/GetGroupFilesByFolder'; + +const FOLDER_ITEM = { + peerId: '123', + folderInfo: { + folderId: 'sub_folder_1', + folderName: '子目录', + createTime: 1, + createUin: '9', + creatorName: '创建者', + totalFileCount: 2, + modifyTime: 2, + modifyUin: '9', + modifyName: '修改者', + }, +}; + +function createAction (items: unknown[]) { + const getGroupFileList = vi.fn().mockResolvedValue(items); + const action = new GetGroupFilesByFolder( + undefined as never, + { + apis: { + MsgApi: { getGroupFileList }, + }, + } as never + ); + return { action, getGroupFileList }; +} + +describe('get_group_files_by_folder subfolders', () => { + test('maps folderInfo entries into folders', async () => { + const { action } = createAction([FOLDER_ITEM]); + + const res = await action._handle({ group_id: '123', file_count: 50 }); + + expect(res.folders).toHaveLength(1); + expect(res.folders[0]).toMatchObject({ + group_id: 123, + folder_id: 'sub_folder_1', + folder_name: '子目录', + total_file_count: 2, + }); + expect(res.files).toHaveLength(0); + }); + + test('returns empty arrays when the folder holds nothing', async () => { + const { action } = createAction([]); + + const res = await action._handle({ group_id: '123', file_count: 50 }); + + expect(res.files).toHaveLength(0); + expect(res.folders).toHaveLength(0); + }); + + test('passes the requested folder id through to the core API', async () => { + const { action, getGroupFileList } = createAction([]); + + await action._handle({ group_id: '123', file_count: 50, folder_id: 'parent_1' }); + + expect(getGroupFileList).toHaveBeenCalledWith('123', expect.objectContaining({ folderId: 'parent_1' })); + }); +});