fix(file): route group publish through OIDB 0x6d9_4 and cache upload meta for c2c resend
The chat-publish step after upload_group_file used to ship a PbSendMsg
with a transElem(elemType=24) — but that wire shape is RECEIVE-side
only, and current QQ-NT rejects it on send with `result=79`. Lagrange.
Core V2 uses a dedicated `OidbSvcTrpcTcp.0x6d9_4` for the publish
(`GroupSendFileService.cs`); mirror that. Send_group_msg with a
`{type:'file', file_id}` segment now routes through the same path
after the OneBot layer splits file segments out (matching the c2c
file split pattern).
Separately, send_private_msg with just `{file_id}` was shipping a 0 B
file because the wire packet for c2c file send needs fileSize/fileMd5/
fileName/fileHash but the OneBot caller only echoes the file_id from
upload_private_file. Add a bounded (1024-entry, FIFO) bridge cache
populated at upload time so the send-message path can rehydrate the
full tuple; inline segment fields still take precedence so callers
that track the metadata themselves can bypass the cache. Parser now
also accepts `file_hash`/`fileHash` on file segments.
Also: file segments no longer get a `[空消息]` log line — the format
string now shows `[文件:<name-or-id>]`.
This commit is contained in:
12 files changed
+634
-158
No files matched your search
@@ -0,0 +1,120 @@
|
||||
// Regression coverage for the group-file-in-send_group_msg bug.
|
||||
//
|
||||
// Symptom in the wild: bot uploaded a group file via `upload_group_file`,
|
||||
// then sent `{type:'file', file_id}` through `send_group_msg`; the
|
||||
// element-builder wrapped it as `transElem(elemType=24, ...)` inside
|
||||
// `richText.elems` and shipped a regular `MessageSvc.PbSendMsg`. The
|
||||
// QQ-NT server rejected the message with `result=79` because that wire
|
||||
// shape is RECEIVE-side only (rich-body-decoder unpacks it into a
|
||||
// FileEntity for inbound messages, but the server's intake validator
|
||||
// flags it on the send side).
|
||||
//
|
||||
// Fix: split file segments off at the OneBot layer (same pattern as
|
||||
// the c2c-file split for private messages) and dispatch through
|
||||
// `bridge.sendGroupFileMessage`, which calls dedicated OIDB
|
||||
// `OidbSvcTrpcTcp.0x6d9_4` — Lagrange.Core V2's
|
||||
// `GroupSendFileService.cs`.
|
||||
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import type { BridgeInterface } from '../../src/bridge/bridge-interface';
|
||||
import type { OneBotInstanceContext } from '../../src/onebot/instance-context';
|
||||
import { sendGroupMessage } from '../../src/onebot/modules/message-actions';
|
||||
|
||||
function fakeBridge(overrides: Partial<BridgeInterface> = {}): BridgeInterface {
|
||||
return new Proxy(overrides as BridgeInterface, {
|
||||
get(target, prop) {
|
||||
if (prop in target) return (target as any)[prop];
|
||||
throw new Error(`fakeBridge: '${String(prop)}' was not stubbed for this test`);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function makeCtx(bridge: BridgeInterface): OneBotInstanceContext {
|
||||
return {
|
||||
uin: '10001',
|
||||
bridge,
|
||||
messageStore: { findEvent: () => null, resolveReplySequence: () => 0 } as any,
|
||||
cacheMessageMeta: vi.fn(),
|
||||
mediaStore: {} as any,
|
||||
musicSignUrl: '',
|
||||
} as unknown as OneBotInstanceContext;
|
||||
}
|
||||
|
||||
const goodReceipt = {
|
||||
messageId: 1, sequence: 100, clientSequence: 0, random: 1, timestamp: 1700000000,
|
||||
};
|
||||
|
||||
describe('send_group_msg with {type:"file"} segment', () => {
|
||||
it('file-only message routes through sendGroupFileMessage (not sendGroupMessage_bridge)', async () => {
|
||||
// Pure-file case — the elems[] path used to ship a transElem(24)
|
||||
// and got result=79. The dedicated OIDB-0x6d9_4 route must take
|
||||
// over for group file publishing.
|
||||
const sendGroupMessage_bridge = vi.fn();
|
||||
const sendGroupFileMessage = vi.fn(async () => undefined);
|
||||
const bridge = fakeBridge({
|
||||
sendGroupMessage: sendGroupMessage_bridge,
|
||||
sendGroupFileMessage,
|
||||
resolveUserUid: vi.fn(),
|
||||
} as any);
|
||||
const ctx = makeCtx(bridge);
|
||||
|
||||
await sendGroupMessage(ctx, 12345, [{
|
||||
type: 'file', data: { file_id: 'gfid-abc', name: 'doc.txt', size: 123 },
|
||||
}] as any, false);
|
||||
|
||||
expect(sendGroupFileMessage).toHaveBeenCalledOnce();
|
||||
expect(sendGroupMessage_bridge).not.toHaveBeenCalled();
|
||||
|
||||
const [groupId, fileId] = sendGroupFileMessage.mock.calls[0]!;
|
||||
expect(groupId).toBe(12345);
|
||||
expect(fileId).toBe('gfid-abc');
|
||||
});
|
||||
|
||||
it('mixed text + file splits across two sends (text via elems[], file via OIDB)', async () => {
|
||||
const sendGroupMessage_bridge = vi.fn(async (_gid: number, _elements: any[]) => goodReceipt);
|
||||
const sendGroupFileMessage = vi.fn(async () => undefined);
|
||||
const bridge = fakeBridge({
|
||||
sendGroupMessage: sendGroupMessage_bridge,
|
||||
sendGroupFileMessage,
|
||||
resolveUserUid: vi.fn(async () => 'u_peer'),
|
||||
} as any);
|
||||
const ctx = makeCtx(bridge);
|
||||
|
||||
await sendGroupMessage(ctx, 12345, [
|
||||
{ type: 'text', data: { text: 'here is the file:' } },
|
||||
{ type: 'file', data: { file_id: 'gfid-xyz', name: 'pkg.zip' } },
|
||||
] as any, false);
|
||||
|
||||
expect(sendGroupMessage_bridge).toHaveBeenCalledOnce();
|
||||
expect(sendGroupFileMessage).toHaveBeenCalledOnce();
|
||||
|
||||
const [textGid, textElements] = sendGroupMessage_bridge.mock.calls[0]!;
|
||||
expect(textGid).toBe(12345);
|
||||
expect(textElements).toEqual([{ type: 'text', text: 'here is the file:' }]);
|
||||
|
||||
const [fileGid, fileFileId] = sendGroupFileMessage.mock.calls[0]!;
|
||||
expect(fileGid).toBe(12345);
|
||||
expect(fileFileId).toBe('gfid-xyz');
|
||||
});
|
||||
|
||||
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 sendGroupFileMessage = vi.fn();
|
||||
const bridge = fakeBridge({
|
||||
sendGroupMessage: sendGroupMessage_bridge,
|
||||
sendGroupFileMessage,
|
||||
resolveUserUid: vi.fn(),
|
||||
} as any);
|
||||
const ctx = makeCtx(bridge);
|
||||
|
||||
await sendGroupMessage(ctx, 12345, [
|
||||
{ type: 'text', data: { text: 'with bad file segment' } },
|
||||
{ type: 'file', data: {} }, // no file_id
|
||||
] as any, false);
|
||||
|
||||
expect(sendGroupMessage_bridge).toHaveBeenCalledOnce();
|
||||
expect(sendGroupFileMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -50,10 +50,12 @@ describe('send_private_msg with {type:"file"} segment', () => {
|
||||
const sendPrivateMessage_bridge = vi.fn();
|
||||
const sendC2cFileMessage = vi.fn(async (_uin: number, _uid: string, _info: any) => goodReceipt);
|
||||
const resolveUserUid = vi.fn(async () => 'u_peer');
|
||||
const recallUploadedFile = vi.fn(() => undefined);
|
||||
const bridge = fakeBridge({
|
||||
sendPrivateMessage: sendPrivateMessage_bridge,
|
||||
sendC2cFileMessage,
|
||||
resolveUserUid,
|
||||
recallUploadedFile,
|
||||
} as any);
|
||||
const ctx = makeCtx(bridge);
|
||||
|
||||
@@ -77,6 +79,84 @@ describe('send_private_msg with {type:"file"} segment', () => {
|
||||
expect(info.fileMd5).toEqual(Buffer.from('00112233', 'hex'));
|
||||
});
|
||||
|
||||
it('file-only with just file_id hydrates fileName/size/md5 from the upload cache', async () => {
|
||||
// Reproduces the "0 B file" bug: a caller passes only `file_id` from
|
||||
// a previous upload_private_file, with no size/md5/name. Without
|
||||
// the cache lookup the wire packet ships fileSize=0/md5=empty and
|
||||
// the recipient sees a 0-byte file. With the cache we recover the
|
||||
// real tuple.
|
||||
const sendC2cFileMessage = vi.fn(async (_uin: number, _uid: string, _info: any) => goodReceipt);
|
||||
const recallUploadedFile = vi.fn((id: string) => id === 'uuid-cached' ? {
|
||||
fileId: 'uuid-cached',
|
||||
scope: 'private' as const,
|
||||
userId: 67890,
|
||||
fileName: 'real-doc.pdf',
|
||||
fileSize: 4096,
|
||||
fileMd5: Buffer.from('aabbccddeeff', 'hex'),
|
||||
fileSha1: new Uint8Array(20),
|
||||
fileHash: 'srv-hash',
|
||||
rememberedAt: 0,
|
||||
} : undefined);
|
||||
const bridge = fakeBridge({
|
||||
sendPrivateMessage: vi.fn(),
|
||||
sendC2cFileMessage,
|
||||
resolveUserUid: vi.fn(async () => 'u_peer'),
|
||||
recallUploadedFile,
|
||||
} as any);
|
||||
const ctx = makeCtx(bridge);
|
||||
|
||||
await sendPrivateMessage(ctx, 67890, [{
|
||||
type: 'file', data: { file_id: 'uuid-cached' },
|
||||
}] as any, false);
|
||||
|
||||
expect(sendC2cFileMessage).toHaveBeenCalledOnce();
|
||||
const [, , info] = sendC2cFileMessage.mock.calls[0]!;
|
||||
expect(info).toMatchObject({
|
||||
fileId: 'uuid-cached',
|
||||
fileName: 'real-doc.pdf',
|
||||
fileSize: 4096,
|
||||
fileHash: 'srv-hash',
|
||||
});
|
||||
expect(info.fileMd5).toEqual(Buffer.from('aabbccddeeff', 'hex'));
|
||||
});
|
||||
|
||||
it('inline segment fields take precedence over the upload cache', async () => {
|
||||
// If the caller threads the metadata inline (e.g. they tracked the
|
||||
// upload response themselves), honour that over the cache so a
|
||||
// refresh of an evicted file still works.
|
||||
const sendC2cFileMessage = vi.fn(async (_uin: number, _uid: string, _info: any) => goodReceipt);
|
||||
const recallUploadedFile = vi.fn(() => ({
|
||||
fileId: 'uuid-x',
|
||||
scope: 'private' as const,
|
||||
userId: 67890,
|
||||
fileName: 'stale-cache-name.txt',
|
||||
fileSize: 99,
|
||||
fileMd5: new Uint8Array(16),
|
||||
fileSha1: new Uint8Array(20),
|
||||
fileHash: 'stale-hash',
|
||||
rememberedAt: 0,
|
||||
}));
|
||||
const bridge = fakeBridge({
|
||||
sendPrivateMessage: vi.fn(),
|
||||
sendC2cFileMessage,
|
||||
resolveUserUid: vi.fn(async () => 'u_peer'),
|
||||
recallUploadedFile,
|
||||
} as any);
|
||||
const ctx = makeCtx(bridge);
|
||||
|
||||
await sendPrivateMessage(ctx, 67890, [{
|
||||
type: 'file', data: { file_id: 'uuid-x', name: 'inline.txt', size: 200, md5: '11223344', file_hash: 'inline-hash' },
|
||||
}] as any, false);
|
||||
|
||||
const [, , info] = sendC2cFileMessage.mock.calls[0]!;
|
||||
expect(info).toMatchObject({
|
||||
fileName: 'inline.txt',
|
||||
fileSize: 200,
|
||||
fileHash: 'inline-hash',
|
||||
});
|
||||
expect(info.fileMd5).toEqual(Buffer.from('11223344', 'hex'));
|
||||
});
|
||||
|
||||
it('mixed file + text splits across two sends (text first, file second)', async () => {
|
||||
const sendPrivateMessage_bridge = vi.fn(async (_uin: number, _elements: any[]) => goodReceipt);
|
||||
const sendC2cFileMessage = vi.fn(async (_uin: number, _uid: string, _info: any) => goodReceipt);
|
||||
@@ -85,6 +165,7 @@ describe('send_private_msg with {type:"file"} segment', () => {
|
||||
sendPrivateMessage: sendPrivateMessage_bridge,
|
||||
sendC2cFileMessage,
|
||||
resolveUserUid,
|
||||
recallUploadedFile: vi.fn(() => undefined),
|
||||
} as any);
|
||||
const ctx = makeCtx(bridge);
|
||||
|
||||
@@ -118,6 +199,7 @@ describe('send_private_msg with {type:"file"} segment', () => {
|
||||
sendPrivateMessage: sendPrivateMessage_bridge,
|
||||
sendC2cFileMessage,
|
||||
resolveUserUid: vi.fn(),
|
||||
recallUploadedFile: vi.fn(() => undefined),
|
||||
} as any);
|
||||
const ctx = makeCtx(bridge);
|
||||
|
||||
@@ -141,6 +223,7 @@ describe('send_private_msg with {type:"file"} segment', () => {
|
||||
sendPrivateMessage: vi.fn(),
|
||||
sendC2cFileMessage,
|
||||
resolveUserUid: vi.fn(async () => ''), // intentional empty
|
||||
recallUploadedFile: vi.fn(() => undefined),
|
||||
} as any);
|
||||
const ctx = makeCtx(bridge);
|
||||
|
||||
|
||||
Reference in new issue
Block a user