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', path: 'index.js', content: 'some content\n' },
],
],
])('should correctly parse chunks and strip out bolt artifacts', (input, expected, expectedActions = []) => {
let actionCounter = 0;
const testId = 'test_id';
const parser = new StreamingMessageParser({
artifactElement: '',
callbacks: {
onAction: (id, action) => {
expect(testId).toBe(id);
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(testId, message);
}
expect(actionCounter).toBe(expectedActions.length);
expect(result).toEqual(expected);
});
});