diff --git a/packages/onebot/src/message-parser.ts b/packages/onebot/src/message-parser.ts index 1f92a68f..2ac266c1 100644 --- a/packages/onebot/src/message-parser.ts +++ b/packages/onebot/src/message-parser.ts @@ -408,6 +408,10 @@ export async function parseMessage(message: JsonValue, autoEscape: boolean, opti const elements: MessageElement[] = []; for (const seg of message) { const data = segmentPayload(seg); + // Some OneBot clients insert empty text between media as a separator. + // Only the exact empty string is a no-op; malformed and whitespace text + // must still pass through strict element validation. + if (seg.type.trim().toLowerCase() === 'text' && data.text === '') continue; const elem = await segmentToElement(seg.type, data, options); if (elem) elements.push(elem); } diff --git a/packages/onebot/tests/message-parser.test.ts b/packages/onebot/tests/message-parser.test.ts index 9e4cfbbf..6fd101b8 100644 --- a/packages/onebot/tests/message-parser.test.ts +++ b/packages/onebot/tests/message-parser.test.ts @@ -91,6 +91,70 @@ describe('parseMessage', () => { expect(result).toEqual([{ type: 'text', text: 'hello' }]); }); + it('ignores explicit empty text placeholders between sendable segments (issue #273)', async () => { + const result = await parseMessage( + [ + { type: 'image', data: { file: 'https://example.com/first.png' } }, + { type: 'text', data: { text: '' } }, + { type: 'image', data: { file: 'https://example.com/second.png' } }, + ] as any, + false, + ); + + expect(result).toHaveLength(2); + expect(result).toMatchObject([ + { type: 'image', url: 'https://example.com/first.png' }, + { type: 'image', url: 'https://example.com/second.png' }, + ]); + }); + + it('rejects a segment array containing only empty text placeholders', async () => { + await expect(parseMessage( + [ + { type: 'text', data: { text: '' } }, + { type: 'text', data: { text: '' } }, + ] as any, + false, + )).rejects.toMatchObject({ + code: 'MISSING_FIELD', + field: 'message', + }); + }); + + it('keeps a single empty text segment invalid', async () => { + await expect(parseMessage( + { type: 'text', data: { text: '' } } as any, + false, + )).rejects.toMatchObject({ + code: 'MISSING_FIELD', + elementType: 'text', + }); + }); + + it('does not treat missing or null text fields as empty placeholders', async () => { + await expect(parseMessage( + [{ type: 'text', data: {} }] as any, + false, + )).rejects.toMatchObject({ + code: 'MISSING_FIELD', + elementType: 'text', + }); + await expect(parseMessage( + [{ type: 'text', data: { text: null } }] as any, + false, + )).rejects.toMatchObject({ + code: 'MISSING_FIELD', + elementType: 'text', + }); + }); + + it('preserves whitespace-only text segments', async () => { + await expect(parseMessage( + [{ type: 'text', data: { text: ' ' } }] as any, + false, + )).resolves.toEqual([{ type: 'text', text: ' ' }]); + }); + it('rejects object-valued fields instead of stringifying caller input', async () => { await expect(parseMessage( [{ type: 'text', data: { text: { bad: true } } }] as any,