|
| 1 | +import { |
| 2 | + normalizeToolNames, |
| 3 | + repairText, |
| 4 | +} from '../../../src/app/ai/mcp/llm-query-router'; |
| 5 | + |
| 6 | +describe('normalizeToolNames', () => { |
| 7 | + it('should return array as-is when input is already an array', () => { |
| 8 | + expect(normalizeToolNames(['Tool1', 'Tool2'])).toEqual(['Tool1', 'Tool2']); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should split comma-separated string into array and trim whitespace', () => { |
| 12 | + expect(normalizeToolNames('Tool1, Tool2, Tool3')).toEqual([ |
| 13 | + 'Tool1', |
| 14 | + 'Tool2', |
| 15 | + 'Tool3', |
| 16 | + ]); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should return empty array for invalid input', () => { |
| 20 | + expect(normalizeToolNames(null)).toEqual([]); |
| 21 | + expect(normalizeToolNames(undefined)).toEqual([]); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +describe('repairText', () => { |
| 26 | + it('should handle tool_names as comma-separated string (bug fix)', () => { |
| 27 | + const input = JSON.stringify({ |
| 28 | + tool_names: 'Get_Run_Details, Get_Latest_Flow_Version_By_Id', |
| 29 | + query_classification: ['openops', 'run_investigation'], |
| 30 | + reasoning: 'Some reasoning', |
| 31 | + user_facing_reasoning: 'User facing reasoning', |
| 32 | + }); |
| 33 | + |
| 34 | + const result = repairText(input); |
| 35 | + const parsed = JSON.parse(result || '{}'); |
| 36 | + |
| 37 | + expect(parsed.tool_names).toEqual([ |
| 38 | + 'Get_Run_Details', |
| 39 | + 'Get_Latest_Flow_Version_By_Id', |
| 40 | + ]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should handle tool_names as an array (correct format)', () => { |
| 44 | + const input = JSON.stringify({ |
| 45 | + tool_names: ['Tool1', 'Tool2'], |
| 46 | + query_classification: ['general'], |
| 47 | + reasoning: 'Some reasoning', |
| 48 | + user_facing_reasoning: 'User facing reasoning', |
| 49 | + }); |
| 50 | + |
| 51 | + const result = repairText(input); |
| 52 | + const parsed = JSON.parse(result || '{}'); |
| 53 | + |
| 54 | + expect(parsed.tool_names).toEqual(['Tool1', 'Tool2']); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should return null for invalid JSON', () => { |
| 58 | + expect(repairText('not valid json')).toBeNull(); |
| 59 | + }); |
| 60 | +}); |
0 commit comments