fix onebot video file sending

This commit is contained in:
DrSmoothl committed 2026-05-11 23:26:45 +08:00
1 parent e917720d9d
commit 6f506ff52c
8 files changed
+145 -48

No files matched your search

@@ -0,0 +1,50 @@
import { describe, expect, it, vi } from 'vitest';
import type { SendPacketResult } from '../src/protocol/packet-sender';
import { protoDecode, protoEncode } from '../src/protobuf/decode';
import { SendMessageRequestSchema, SendMessageResponseSchema } from '../src/bridge/proto/action';
vi.mock('../src/bridge/builders/element-builder', () => ({
buildSendElems: vi.fn(async () => [{ text: { str: 'stub media elem' } }]),
}));
describe('Bridge private media routing', () => {
it('includes resolved uid in the final c2c send request for media messages', async () => {
const { Bridge } = await import('../src/bridge/bridge');
const { QQInfo } = await import('../src/bridge/qq-info');
class TestBridge extends Bridge {
capturedBody: Uint8Array | null = null;
override async resolveUserUid(uin: number): Promise<string> {
expect(uin).toBe(12345);
return 'u_peer_12345';
}
override async sendRawPacket(serviceCmd: string, body: Uint8Array): Promise<SendPacketResult> {
expect(serviceCmd).toBe('MessageSvc.PbSendMsg');
this.capturedBody = body;
return {
success: true,
gotResponse: true,
errorCode: 0,
errorMessage: '',
responseData: Buffer.from(protoEncode({
result: 0,
privateSequence: 88,
timestamp1: 1710000000,
}, SendMessageResponseSchema)),
};
}
}
const bridge = new TestBridge(new QQInfo('10000'));
await bridge.sendPrivateMessage(12345, [{ type: 'video', url: 'file:///tmp/clip.mp4' } as any]);
expect(bridge.capturedBody).toBeInstanceOf(Uint8Array);
const request = protoDecode(bridge.capturedBody as Uint8Array, SendMessageRequestSchema);
expect(request?.routingHead?.c2c).toMatchObject({
uin: 12345,
uid: 'u_peer_12345',
});
});
});
+28
View File
@@ -0,0 +1,28 @@
import fs from 'fs';
import os from 'os';
import path from 'path';
import { pathToFileURL } from 'url';
import { describe, expect, it } from 'vitest';
import { loadBinarySource, resolveLocalFilePath } from '../src/bridge/highway/utils';
describe('highway source paths', () => {
it('normalizes file URLs with an extra leading slash on POSIX', () => {
if (process.platform === 'win32') return;
expect(resolveLocalFilePath('file:////AstrBot/data/plugin/cache/BV-test.mp4'))
.toBe('/AstrBot/data/plugin/cache/BV-test.mp4');
});
it('loads encoded file URLs from the local filesystem', async () => {
const filePath = path.join(os.tmpdir(), `snowluma video ${process.pid}-${Date.now()}.txt`);
fs.writeFileSync(filePath, 'ok');
try {
const source = pathToFileURL(filePath).href;
const loaded = await loadBinarySource(source, 'test file');
expect(Buffer.from(loaded.bytes).toString('utf8')).toBe('ok');
expect(loaded.fileName).toBe(path.basename(filePath));
} finally {
try { fs.unlinkSync(filePath); } catch { /* ignore */ }
}
});
});
@@ -108,6 +108,14 @@ describe('parseMessage', () => {
expect(result).toEqual([{ type: 'video', url: '/tmp/clip.mp4', thumbUrl: '/tmp/clip.png' }]);
});
it('parses AstrBot-style Video segment with top-level media', async () => {
const result = await parseMessage(
[{ type: 'Video', media: 'file:////AstrBot/data/cache/BV-test.mp4' }] as any,
false,
);
expect(result).toEqual([{ type: 'video', url: 'file:////AstrBot/data/cache/BV-test.mp4', thumbUrl: undefined }]);
});
it('drops video segment with empty source', async () => {
const result = await parseMessage(
[{ type: 'video', data: {} }] as any,