import { parseTextToSilkeCommands } from './silke-command-parser'; import { SilkeCommand, SilkeCommandType } from './utils'; const availableCommands: SilkeCommand[] = [ { type: SilkeCommandType.MENTION, value: 'user' }, { type: SilkeCommandType.ACTION, value: 'command' }, ]; describe('parseTextToCommands', () => { test('parses text with mentions and actions', () => { const result = parseTextToSilkeCommands('Hello @user /command @partial', availableCommands); expect(result).toEqual([ { type: 'text', value: 'Hello ' }, { type: '@', value: 'user' }, { type: 'text', value: ' ' }, { type: '/', value: 'command' }, { type: 'text', value: ' ' }, { partialMatch: true, type: '@', value: 'partial' }, ]); }); test('parses plain text without commands', () => { const result = parseTextToSilkeCommands('Hello world', availableCommands); expect(result).toEqual([{ type: 'text', value: 'Hello world' }]); }); test('parses command at the start of text', () => { const result = parseTextToSilkeCommands('@user hello', availableCommands); expect(result).toEqual([ { type: '@', value: 'user' }, { type: 'text', value: ' hello' }, ]); }); test('parses command at the end of text', () => { const result = parseTextToSilkeCommands('hello @user', availableCommands); expect(result).toEqual([ { type: 'text', value: 'hello ' }, { type: '@', value: 'user' }, ]); }); test('parses multiple commands separated by space', () => { const result = parseTextToSilkeCommands('@user @unknown /command', availableCommands); expect(result).toEqual([ { type: '@', value: 'user' }, { type: 'text', value: ' ' }, { partialMatch: true, type: '@', value: 'unknown' }, { type: 'text', value: ' ' }, { type: '/', value: 'command' }, ]); }); test('does not parse command without preceding space', () => { const result = parseTextToSilkeCommands('hello@user email@test.com', availableCommands); expect(result).toEqual([{ type: 'text', value: 'hello@user email@test.com' }]); }); test('parses command with empty value', () => { const result = parseTextToSilkeCommands('@ /command', availableCommands); expect(result).toEqual([ { partialMatch: true, type: '@', value: '' }, { type: 'text', value: ' ' }, { type: '/', value: 'command' }, ]); }); test('handles empty text', () => { const result = parseTextToSilkeCommands('', availableCommands); expect(result).toEqual([]); }); test('handles command only', () => { const result = parseTextToSilkeCommands('@user', availableCommands); expect(result).toEqual([{ type: '@', value: 'user' }]); }); test('handles command with hyphens and underscores', () => { const commands: SilkeCommand[] = [{ type: SilkeCommandType.MENTION, value: 'user-name_123' }]; const result = parseTextToSilkeCommands('@user-name_123', commands); expect(result).toEqual([{ type: '@', value: 'user-name_123' }]); }); test('is case insensitive for command values', () => { const result = parseTextToSilkeCommands('@USER', availableCommands); // Note: the regex is case insensitive, so @USER matches, but user !== USER expect(result).toEqual([{ partialMatch: true, type: '@', value: 'USER' }]); }); });