fix(onebot): accept compatible JSON card payloads (#325)

This commit is contained in:
DrSmoothl committed 2026-08-08 10:14:39 +08:00
1 parent 751217c13d
commit 65683bb44d
5 files changed
+97 -18

No files matched your search

+47 -9
View File
@@ -168,7 +168,7 @@ describe('parseMessage', () => {
)).resolves.toEqual([{ type: 'text', text: ' ' }]);
});
it('rejects object-valued fields instead of stringifying caller input', async () => {
it('rejects object-valued fields for scalar-only segments', async () => {
await expect(parseMessage(
[{ type: 'text', data: { text: { bad: true } } }] as any,
false,
@@ -177,14 +177,6 @@ describe('parseMessage', () => {
elementType: 'text',
field: 'text',
});
await expect(parseMessage(
[{ type: 'json', data: { data: { app: 'unexpected-object' } } }] as any,
false,
)).rejects.toMatchObject({
code: 'INVALID_FIELD',
elementType: 'json',
field: 'data',
});
});
it('parses face segment', async () => {
@@ -586,6 +578,52 @@ describe('parseMessage', () => {
expect(result[0].text).toBe('{"app":"test"}');
});
it('accepts an object JSON payload produced by AstrBot-compatible clients', async () => {
const result = await parseMessage(
[{
type: 'json',
data: {
data: {
app: 'com.tencent.music.lua',
meta: { music: { title: '晴天' } },
},
config: { token: 'signed-token' },
},
}] as any,
false,
);
expect(result).toEqual([{
type: 'json',
text: '{"app":"com.tencent.music.lua","meta":{"music":{"title":"晴天"}}}',
}]);
});
it('accepts the JSON raw-string segment shorthand used by compatible clients', async () => {
await expect(parseMessage(
[{ type: 'json', data: '{"app":"com.tencent.music.lua"}' }] as any,
false,
)).resolves.toEqual([{
type: 'json',
text: '{"app":"com.tencent.music.lua"}',
}]);
});
it.each([
{ label: 'array', value: [] },
{ label: 'null', value: null },
{ label: 'number', value: 1 },
])('rejects a $label JSON payload instead of sending a non-object card', async ({ value }) => {
await expect(parseMessage(
[{ type: 'json', data: { data: value } }] as any,
false,
)).rejects.toMatchObject({
code: 'INVALID_FIELD',
elementType: 'json',
field: 'data',
});
});
it('reads canonical xml resid and file_size fields', async () => {
const xml = await parseMessage(
[{ type: 'xml', data: { data: '<msg/>', resid: '35' } }] as any,