fix(onebot): preserve public Qzone input semantics

Keep comment and reply text unchanged and support structured input in the debug console. Report malformed input before sending a request.

Refs #587
This commit is contained in:
DrSmoothl committed 2026-10-25 23:26:55 +08:00
1 parent 2f1aadffbe
commit dafe2082f1
10 files changed
+464 -113

No files matched your search

+54
View File
@@ -143,6 +143,60 @@ describe('f.object — open JSON object', () => {
});
});
describe('f.textOrObjectArray — literal text and upstream rich items', () => {
const spec = defineAction({
name: 't',
params: { content: f.textOrObjectArray() },
run: () => okResponse(),
});
it.each(['[]', '[1,2]', '[{"type":0,"content":"hello"}]', ' \n中文 '])(
'preserves literal string %j without decoding or trimming it',
(content) => {
expect(expectOk(spec.parse({ content })).content).toBe(content);
},
);
it('preserves the original object array and unknown fields', () => {
const content = [{ type: 0, content: 'hello', future: { values: [null, 1] } }];
expect(expectOk(spec.parse({ content })).content).toBe(content);
});
it.each(
['', [], null, 1, false, {}, [null], [1], [[]], [{ type: 0 }, 'text']].map((content) => ({
content,
})),
)('rejects invalid rich content $content at the named field', ({ content }) => {
expect(expectErr(spec.parse({ content })).field).toBe('content');
});
it('retains required and optional field policies', () => {
expect(expectErr(spec.parse({})).reason).toBe('is required');
const optional = defineAction({
name: 't',
params: { content: f.textOrObjectArray().optional() },
run: () => okResponse(),
});
expect(expectOk(optional.parse({}))).toEqual({ content: undefined });
expect(expectErr(optional.parse({ content: null })).field).toBe('content');
});
it('describes text and object arrays without OneBot message semantics', () => {
const field = f.textOrObjectArray();
expect(field.doc.type).toBe('string | object[]');
expect(field.toJsonSchema()).toEqual({
anyOf: [
{ type: 'string', minLength: 1 },
{
type: 'array',
minItems: 1,
items: { type: 'object', additionalProperties: true },
},
],
});
});
});
describe('f.message — union passthrough + JSON-array string', () => {
const spec = defineAction({ name: 't', params: { m: f.message() }, run: () => okResponse() });
it('parses a JSON-array string, passes plain string / array through', () => {
@@ -245,15 +245,34 @@ describe('qzone public actions', () => {
},
);
it('accepts a JSON-array string through the existing message coercer', async () => {
const commentPublicFeed = vi.fn().mockResolvedValue(result);
it.each(['comment_qzone_public_feed', 'reply_qzone_public_feed'])(
'%s preserves literal text even when it looks like JSON',
async (action) => {
const { method } = cases.find((entry) => entry.action === action)!;
const invoke = vi.fn().mockResolvedValue(result);
const handler = makeHandler({ [method]: invoke });
await makeHandler({ commentPublicFeed }).handle('comment_qzone_public_feed', {
feed,
content: '[{"type":0,"content":"hello"}]',
});
expect(commentPublicFeed).toHaveBeenCalledWith(feed, [{ type: 0, content: 'hello' }]);
});
for (const content of [
'[]',
'[1,2]',
'[{"type":0,"content":"hello"}]',
' ["中文"]\n',
'{"text":"hello"}',
]) {
expect(
await handler.handle(action, { feed, comment, content, reply }),
).toMatchObject({
status: 'ok',
retcode: 0,
});
expect(invoke).toHaveBeenLastCalledWith(
...(action === 'comment_qzone_public_feed'
? [feed, content]
: [feed, comment, content, reply]),
);
}
},
);
it.each(['comment_qzone_public_feed', 'reply_qzone_public_feed'])(
'%s rejects missing, empty, scalar or mixed content without effects',