fix(onebot): report self-sent private files

Preserve authoritative receipts for private file sends and emit one message_sent event per actual QQ message. Split source uploads from chat publishing so failures surface instead of returning a synthetic zero-sequence receipt.

Fixes #282
This commit is contained in:
DrSmoothl committed 2026-07-25 21:55:44 +08:00
1 parent 3a7253315f
commit 5ad8027524
4 files changed
+306 -38

No files matched your search

+174 -3
View File
@@ -15,23 +15,51 @@ const RECEIPT = {
random: 123456,
timestamp: 1_700_000_000,
};
type Receipt = typeof RECEIPT;
function makeContext(receipt = RECEIPT): {
function makeContext(receiptOrReceipts: Receipt | Receipt[] = RECEIPT): {
ctx: OneBotInstanceContext;
dispatchEvent: ReturnType<typeof vi.fn<(event: JsonObject) => void>>;
events: Map<number, JsonObject>;
} {
const receipts = Array.isArray(receiptOrReceipts) ? receiptOrReceipts : [receiptOrReceipts];
let receiptIndex = 0;
const nextReceipt = () => {
const receipt = receipts[receiptIndex++];
if (!receipt) throw new Error('unexpected extra private send');
return receipt;
};
const events = new Map<number, JsonObject>();
const dispatchEvent = vi.fn<(event: JsonObject) => void>();
const bridge = {
identity: { nickname: 'SnowLuma' },
apis: {
message: {
sendPrivate: vi.fn(async () => receipt),
sendGroupTempMessage: vi.fn(async () => receipt),
sendPrivate: vi.fn(async () => nextReceipt()),
sendGroupTempMessage: vi.fn(async () => nextReceipt()),
sendC2cFile: vi.fn(async () => nextReceipt()),
},
groupFile: {
uploadPrivate: vi.fn(async () => ({
fileId: 'uploaded-file-uuid',
fileHash: 'uploaded-file-hash',
})),
},
},
resolveUserUid: vi.fn(async () => 'u_peer'),
recallUploadedFile: vi.fn((fileId: string) => fileId === 'uploaded-file-uuid'
? {
fileId,
scope: 'private',
userId: PEER_ID,
fileName: 'inline.txt',
fileSize: 3,
fileMd5: new Uint8Array(16),
fileSha1: new Uint8Array(20),
fileHash: 'uploaded-file-hash',
rememberedAt: Date.now(),
}
: undefined),
} as unknown as BridgeInterface;
const ctx = {
@@ -134,6 +162,149 @@ describe('OneBot self-sent events', () => {
}), 'send');
});
it('reports a private file sent through an action', async () => {
const { ctx, dispatchEvent } = makeContext();
const api = buildApiContext(ctx);
const result = await api.sendPrivateMessage(PEER_ID, [{
type: 'file',
data: {
file_id: 'file-uuid',
name: 'report.txt',
size: 128,
md5: '00112233445566778899aabbccddeeff',
},
}], false);
expect(result.messageId).not.toBe(0);
expect(dispatchEvent).toHaveBeenCalledOnce();
expect(dispatchEvent).toHaveBeenCalledWith(expect.objectContaining({
post_type: 'message_sent',
message_type: 'private',
self_id: SELF_ID,
user_id: SELF_ID,
target_id: PEER_ID,
message: [expect.objectContaining({
type: 'file',
data: expect.objectContaining({
file_id: 'file-uuid',
file: 'report.txt',
file_size: 128,
}),
})],
}), 'send');
});
it('reports a private file uploaded and sent through an action', async () => {
const { ctx, dispatchEvent } = makeContext();
const api = buildApiContext(ctx);
await api.sendPrivateMessage(PEER_ID, [{
type: 'file',
data: {
file: 'base64://AQID',
name: 'inline.txt',
},
}], false);
expect(dispatchEvent).toHaveBeenCalledOnce();
expect(dispatchEvent).toHaveBeenCalledWith(expect.objectContaining({
post_type: 'message_sent',
message_type: 'private',
target_id: PEER_ID,
message: [expect.objectContaining({
type: 'file',
data: expect.objectContaining({
file: 'inline.txt',
url: 'base64://AQID',
}),
})],
}), 'send');
});
it('reports each QQ message produced by a mixed private action', async () => {
const fileReceipt: Receipt = {
messageId: 654321,
sequence: 88,
clientSequence: 10,
random: 654321,
timestamp: RECEIPT.timestamp + 1,
};
const { ctx, dispatchEvent } = makeContext([RECEIPT, fileReceipt]);
const api = buildApiContext(ctx);
const result = await api.sendPrivateMessage(PEER_ID, [
{ type: 'text', data: { text: 'attachment:' } },
{
type: 'file',
data: {
file_id: 'mixed-file-uuid',
name: 'mixed.txt',
size: 64,
md5: '00112233445566778899aabbccddeeff',
},
},
], false);
expect(dispatchEvent).toHaveBeenCalledTimes(2);
expect(dispatchEvent.mock.calls[0]?.[0]).toMatchObject({
post_type: 'message_sent',
message_seq: RECEIPT.clientSequence,
message: [{ type: 'text', data: { text: 'attachment:' } }],
});
expect(dispatchEvent.mock.calls[1]?.[0]).toMatchObject({
post_type: 'message_sent',
message_seq: fileReceipt.clientSequence,
message: [{
type: 'file',
data: expect.objectContaining({ file_id: 'mixed-file-uuid' }),
}],
});
expect(result.messageId).toBe(hashMessageIdInt32(
fileReceipt.sequence,
PEER_ID,
PRIVATE_NT_MESSAGE_EVENT,
));
});
it('reports each file in a multi-file private action separately', async () => {
const secondReceipt: Receipt = {
messageId: 654321,
sequence: 88,
clientSequence: 10,
random: 654321,
timestamp: RECEIPT.timestamp + 1,
};
const { ctx, dispatchEvent } = makeContext([RECEIPT, secondReceipt]);
const api = buildApiContext(ctx);
await api.sendPrivateMessage(PEER_ID, [
{
type: 'file',
data: {
file_id: 'first-file',
name: 'first.txt',
size: 1,
md5: '00112233445566778899aabbccddeeff',
},
},
{
type: 'file',
data: {
file_id: 'second-file',
name: 'second.txt',
size: 2,
md5: 'ffeeddccbbaa00998877665544332211',
},
},
], false);
expect(dispatchEvent).toHaveBeenCalledTimes(2);
expect(dispatchEvent.mock.calls.map(([event]) =>
((event.message as JsonObject[])[0]?.data as JsonObject)?.file_id,
)).toEqual(['first-file', 'second-file']);
});
it('does not report a later QQ echo after the action event', () => {
const { dispatch, emitEvent, stored } = makeInstanceHarness();
const sentEvent = privateSentEvent();
@@ -250,18 +250,27 @@ 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.
it('file segment with url uploads without publishing, then sends once with the cached metadata', async () => {
const uploadPrivate = vi.fn(async (_uid: number, _src: string, _name: string, _doUpload: boolean) =>
({ fileId: 'auto-fid', fileHash: null }));
const sendC2cFileMessage = vi.fn();
({ fileId: 'auto-fid', fileHash: 'auto-hash' }));
const sendC2cFileMessage = vi.fn(async () => goodReceipt);
const bridge = fakeBridge({
apis: {
message: { sendPrivate: vi.fn(), sendC2cFile: sendC2cFileMessage },
groupFile: { uploadPrivate },
} as any,
resolveUserUid: vi.fn(async () => 'u_peer'),
recallUploadedFile: vi.fn(() => undefined),
recallUploadedFile: vi.fn(() => ({
fileId: 'auto-fid',
scope: 'private',
userId: 67890,
fileName: 'audio.wav',
fileSize: 3,
fileMd5: new Uint8Array(16),
fileSha1: new Uint8Array(20),
fileHash: 'auto-hash',
rememberedAt: Date.now(),
})),
} as any);
const ctx = makeCtx(bridge);
@@ -275,7 +284,56 @@ describe('send_private_msg with {type:"file"} segment', () => {
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();
expect(uploadPrivate).toHaveBeenCalledWith(67890, '/tmp/audio.wav', 'audio.wav', true, false);
expect(sendC2cFileMessage).toHaveBeenCalledOnce();
expect(sendC2cFileMessage).toHaveBeenCalledWith(67890, 'u_peer', expect.objectContaining({
fileId: 'auto-fid',
fileName: 'audio.wav',
fileSize: 3,
fileHash: 'auto-hash',
}));
});
it('surfaces a failed chat post after uploading a file source', async () => {
const uploadPrivate = vi.fn(async () => ({
fileId: 'uploaded-but-unsent',
fileHash: 'upload-hash',
}));
const sendC2cFile = vi.fn(async () => {
throw new Error('chat post rejected');
});
const bridge = fakeBridge({
apis: {
message: { sendPrivate: vi.fn(), sendC2cFile },
groupFile: { uploadPrivate },
} as any,
resolveUserUid: vi.fn(async () => 'u_peer'),
recallUploadedFile: vi.fn(() => ({
fileId: 'uploaded-but-unsent',
scope: 'private',
userId: 67890,
fileName: 'failed.txt',
fileSize: 3,
fileMd5: new Uint8Array(16),
fileSha1: new Uint8Array(20),
fileHash: 'upload-hash',
rememberedAt: Date.now(),
})),
} as any);
const ctx = makeCtx(bridge);
await expect(sendPrivateMessage(ctx, 67890, [{
type: 'file',
data: { file: 'base64://AQID', name: 'failed.txt' },
}] as any, false)).rejects.toThrow('chat post rejected');
expect(uploadPrivate).toHaveBeenCalledWith(
67890,
'base64://AQID',
'failed.txt',
true,
false,
);
expect(sendC2cFile).toHaveBeenCalledOnce();
});
});