| import json |
| from unittest.mock import AsyncMock, patch |
| import pytest |
| from enhanced_ai_workflow_endpoints import RealAIWorkflowService |
|
|
|
|
| @pytest.mark.asyncio |
| async def test_routing_logic_sales(mock_env_vars): |
| """Test that the system routes to SALES logic when LLM returns Sales intent""" |
| |
| with patch('aiohttp.ClientSession', return_value=AsyncMock()) as mock_session_cls: |
| service = RealAIWorkflowService() |
| await service.initialize_sessions() |
| |
| |
| mock_response = { |
| "intent": "Create a new lead", |
| "workflow_suggestion": { |
| "nodes": [ |
| {"service": "salesforce", "action": "create_lead", "params": {"name": "Test Lead"}} |
| ] |
| }, |
| "confidence": 0.99, |
| "ai_provider_used": "mock_provider" |
| } |
| |
| fixed_json_string = json.dumps(mock_response) |
| |
| |
| |
| with patch.object(service, 'call_openai_api', return_value={ |
| 'content': fixed_json_string, |
| 'confidence': 0.99, |
| 'token_usage': {}, |
| 'provider': 'openai' |
| }): |
| |
| result = await service.process_with_nlu("This input does not matter", provider="openai") |
| |
| |
| assert result['intent'] == "Create a new lead" |
| nodes = result['workflow_suggestion']['nodes'] |
| assert nodes[0]['service'] == 'salesforce' |
| assert nodes[0]['action'] == 'create_lead' |
|
|
| @pytest.mark.asyncio |
| async def test_malformed_llm_response(mock_env_vars): |
| """Test behavior when LLM returns garbage non-JSON""" |
| |
| with patch('aiohttp.ClientSession', return_value=AsyncMock()) as mock_session_cls: |
| service = RealAIWorkflowService() |
| await service.initialize_sessions() |
| |
| with patch.object(service, 'call_openai_api', return_value={ |
| 'content': "I am not returning JSON, I am just chatting.", |
| 'confidence': 0.5, |
| 'token_usage': {}, |
| 'provider': 'openai' |
| }): |
| |
| |
| result = await service.process_with_nlu("test", provider="openai") |
| |
| |
| |
| assert "intent" in result |
| assert "tasks" in result |
| assert result['ai_provider_used'] == 'openai' |
|
|