import { describe, expect, it } from 'vitest'; import { StreamingMessageParser } from './message-parser'; describe('StreamingMessageParser', () => { it('should pass through normal text', () => { const parser = new StreamingMessageParser(); expect(parser.parse('test_id', 'Hello, world!')).toBe('Hello, world!'); }); it('should allow normal HTML tags', () => { const parser = new StreamingMessageParser(); expect(parser.parse('test_id', 'Hello world!')).toBe('Hello world!'); }); it.each([ ['Foo bar', 'Foo bar'], ['Foo bar <', 'Foo bar '], ['Foo bar foo Some more text', 'Some text before Some more text'], [['Some text before foo Some more text'], 'Some text before Some more text'], [['Some text before foo Some more text'], 'Some text before Some more text'], [['Some text before fo', 'o Some more text'], 'Some text before Some more text'], [ ['Some text before fo', 'o', '<', '/boltArtifact> Some more text'], 'Some text before Some more text', ], [ ['Some text before fo', 'o<', '/boltArtifact> Some more text'], 'Some text before Some more text', ], ['Before foo After', 'Before foo After'], ['Before foo After', 'Before foo After'], ['Before foo After', 'Before After'], [ 'Before npm install After', 'Before After', [{ type: 'shell', content: 'npm install' }], ], [ 'Before npm installsome content After', 'Before After', [ { type: 'shell', content: 'npm install' }, { type: 'file', filePath: 'index.js', content: 'some content\n' }, ], ], ])('should correctly parse chunks and strip out bolt artifacts', (input, expected, expectedActions = []) => { let actionCounter = 0; const expectedArtifactId = 'artifact_1'; const expectedMessageId = 'message_1'; const parser = new StreamingMessageParser({ artifactElement: '', callbacks: { onAction: ({ artifactId, messageId, action }) => { expect(artifactId).toBe(expectedArtifactId); expect(messageId).toBe(expectedMessageId); expect(action).toEqual(expectedActions[actionCounter]); actionCounter++; }, }, }); let message = ''; let result = ''; const chunks = Array.isArray(input) ? input : input.split(''); for (const chunk of chunks) { message += chunk; result += parser.parse(expectedMessageId, message); } expect(actionCounter).toBe(expectedActions.length); expect(result).toEqual(expected); }); });