fix(onebot): support inline file url/path in send_group/private_msg (#80)
file 段现接受 file/url/path(无需先 upload_group_file/upload_private_file);群走 groupFile.upload()、私聊走 uploadPrivate()(内部已 publish/sendC2c),file_id 老路径不变。含 parser 与群/私聊文件发送测试。
This commit is contained in:
1 parent
69c8960ea9
commit
d3092a1806
5 files changed
+126
-83
No files matched your search
@@ -481,16 +481,22 @@ describe('parseMessage', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it('drops a file segment with no file_id (upload-first contract)', async () => {
|
||||
// Path-only segments aren't supported here — callers must
|
||||
// upload via `upload_group_file` / `upload_private_file` first
|
||||
// to obtain a file_id. Dropping with a warning is preferable
|
||||
// to silently kicking off an upload from inside the message
|
||||
// parser, which can't fail loudly without changing call sites.
|
||||
it('parses {type:"file", file:"/path"} (no file_id) into a file element with url', async () => {
|
||||
// Inline file path: the parser now accepts file/url/path and stores
|
||||
// it in `url`. The send layer (sendGroupMessage / sendPrivateMessage)
|
||||
// handles the actual upload so the parser stays side-effect-free.
|
||||
const result = await parseMessage(
|
||||
[{ type: 'file', data: { file: '/local/path/a.bin' } } as any],
|
||||
false,
|
||||
);
|
||||
expect(result).toEqual([{ type: 'file', url: '/local/path/a.bin' }]);
|
||||
});
|
||||
|
||||
it('drops a file segment with neither file_id nor file/url', async () => {
|
||||
const result = await parseMessage(
|
||||
[{ type: 'file', data: {} } as any],
|
||||
false,
|
||||
);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
|
||||
@@ -105,14 +105,13 @@ describe('send_group_msg with {type:"file"} segment', () => {
|
||||
});
|
||||
|
||||
it('file segment without file_id is skipped (with a warn-level log)', async () => {
|
||||
// Same upload-by-reference contract as the c2c file path —
|
||||
// missing file_id means there's nothing to publish.
|
||||
const sendGroupMessage_bridge = vi.fn(async (_gid: number, _elements: any[]) => goodReceipt);
|
||||
const publish = vi.fn();
|
||||
const upload = vi.fn();
|
||||
const bridge = fakeBridge({
|
||||
apis: {
|
||||
message: { sendGroup: sendGroupMessage_bridge },
|
||||
groupFile: { publish },
|
||||
groupFile: { publish, upload },
|
||||
} as any,
|
||||
resolveUserUid: vi.fn(),
|
||||
} as any);
|
||||
@@ -120,10 +119,42 @@ describe('send_group_msg with {type:"file"} segment', () => {
|
||||
|
||||
await sendGroupMessage(ctx, 12345, [
|
||||
{ type: 'text', data: { text: 'with bad file segment' } },
|
||||
{ type: 'file', data: {} }, // no file_id
|
||||
{ type: 'file', data: {} }, // no file_id and no url
|
||||
] as any, false);
|
||||
|
||||
expect(sendGroupMessage_bridge).toHaveBeenCalledOnce();
|
||||
expect(publish).not.toHaveBeenCalled();
|
||||
expect(upload).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('file segment with url (no file_id) auto-uploads via groupFile.upload (not publish)', async () => {
|
||||
// Happy path for inline file sending: the bot passes a local path
|
||||
// directly. upload() internally calls publish(), so we must NOT
|
||||
// call publish() a second time.
|
||||
const upload = vi.fn(async (_gid: number, _src: string, _name: string, _folder: string, _doUpload: boolean) =>
|
||||
({ fileId: 'auto-fid', fileHash: null }));
|
||||
const publish = vi.fn();
|
||||
const bridge = fakeBridge({
|
||||
apis: {
|
||||
message: { sendGroup: vi.fn() },
|
||||
groupFile: { upload, publish },
|
||||
} as any,
|
||||
resolveUserUid: vi.fn(),
|
||||
} as any);
|
||||
const ctx = makeCtx(bridge);
|
||||
|
||||
await sendGroupMessage(ctx, 12345, [{
|
||||
type: 'file', data: { file: '/tmp/audio.wav', name: 'audio.wav' },
|
||||
}] as any, false);
|
||||
|
||||
expect(upload).toHaveBeenCalledOnce();
|
||||
const [gid, src, name, folder, doUpload] = upload.mock.calls[0]!;
|
||||
expect(gid).toBe(12345);
|
||||
expect(src).toBe('/tmp/audio.wav');
|
||||
expect(name).toBe('audio.wav');
|
||||
expect(folder).toBe('/');
|
||||
expect(doUpload).toBe(true);
|
||||
// publish must NOT be called — upload() already handles it internally
|
||||
expect(publish).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -227,4 +227,33 @@ describe('send_private_msg with {type:"file"} segment', () => {
|
||||
|
||||
expect(sendC2cFileMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('file segment with url (no file_id) auto-uploads via groupFile.uploadPrivate (not sendC2cFile)', async () => {
|
||||
// uploadPrivate() internally calls sendC2cFile() — must NOT call it again.
|
||||
const uploadPrivate = vi.fn(async (_uid: number, _src: string, _name: string, _doUpload: boolean) =>
|
||||
({ fileId: 'auto-fid', fileHash: null }));
|
||||
const sendC2cFileMessage = vi.fn();
|
||||
const bridge = fakeBridge({
|
||||
apis: {
|
||||
message: { sendPrivate: vi.fn(), sendC2cFile: sendC2cFileMessage },
|
||||
groupFile: { uploadPrivate },
|
||||
} as any,
|
||||
resolveUserUid: vi.fn(async () => 'u_peer'),
|
||||
recallUploadedFile: vi.fn(() => undefined),
|
||||
} as any);
|
||||
const ctx = makeCtx(bridge);
|
||||
|
||||
await sendPrivateMessage(ctx, 67890, [{
|
||||
type: 'file', data: { file: '/tmp/audio.wav', name: 'audio.wav' },
|
||||
}] as any, false);
|
||||
|
||||
expect(uploadPrivate).toHaveBeenCalledOnce();
|
||||
const [uid, src, name, doUpload] = uploadPrivate.mock.calls[0]!;
|
||||
expect(uid).toBe(67890);
|
||||
expect(src).toBe('/tmp/audio.wav');
|
||||
expect(name).toBe('audio.wav');
|
||||
expect(doUpload).toBe(true);
|
||||
// sendC2cFile must NOT be called — uploadPrivate() handles it internally
|
||||
expect(sendC2cFileMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user