fix(onebot): accept compatible JSON card payloads (#325)
This commit is contained in:
5 files changed
+97
-18
No files matched your search
@@ -88,6 +88,33 @@ describe('outbound message validation at the Action boundary', () => {
|
||||
expect(bridgeSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('sends object-valued JSON cards from AstrBot-compatible clients', async () => {
|
||||
const bridgeSend = vi.fn(async () => sendReceipt);
|
||||
const ref = instanceContext(bridgeSend);
|
||||
const api = new ApiHandler({
|
||||
sendGroupMessage: (groupId, message, autoEscape) =>
|
||||
sendGroupMessage(ref, groupId, message, autoEscape),
|
||||
} as ApiActionContext);
|
||||
|
||||
const response = await api.handle('send_group_msg', {
|
||||
group_id: 12345,
|
||||
message: [{
|
||||
type: 'json',
|
||||
data: {
|
||||
data: { app: 'com.tencent.music.lua', meta: { music: { title: '晴天' } } },
|
||||
config: { token: 'signed-token' },
|
||||
},
|
||||
}],
|
||||
});
|
||||
|
||||
expect(response).toMatchObject({ status: 'ok', retcode: 0 });
|
||||
expect(bridgeSend).toHaveBeenCalledOnce();
|
||||
expect(bridgeSend.mock.calls[0]?.[1]).toEqual([{
|
||||
type: 'json',
|
||||
text: '{"app":"com.tencent.music.lua","meta":{"music":{"title":"晴天"}}}',
|
||||
}]);
|
||||
});
|
||||
|
||||
it('reports the public data field when a JSON segment uses data.text', async () => {
|
||||
const bridgeSend = vi.fn();
|
||||
const ref = instanceContext(bridgeSend);
|
||||
@@ -107,7 +134,7 @@ describe('outbound message validation at the Action boundary', () => {
|
||||
expect(response).toMatchObject({
|
||||
status: 'failed',
|
||||
retcode: 1400,
|
||||
wording: 'message segment "json" field "data" must be a non-empty JSON string',
|
||||
wording: 'message segment "json" field "data" must be a JSON object or non-empty JSON string',
|
||||
});
|
||||
expect(bridgeSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user