#!/usr/bin/env python3 """Tests for MVP3 Sprint 2 Execute Button functionality.""" from unittest.mock import patch from app import handle_execute_plan from kg_services.ontology import MCPPrompt, MCPTool, PlannedStep class TestExecuteButton: """Test cases for the execute button functionality.""" def test_execute_button_with_valid_inputs(self): """Test execute button with valid query and inputs.""" # Mock the global agents with ( patch("app.planner_agent") as mock_planner, patch("app.executor_agent") as mock_executor, ): # Setup mock planner mock_tool = MCPTool( tool_id="test-tool", name="Test Tool", description="A test MCP tool", tags=["test"], invocation_command_stub="test_command", ) mock_prompt = MCPPrompt( prompt_id="test-prompt", name="Test Prompt", description="A test prompt", target_tool_id="test-tool", template_string="Test template with {var1}", input_variables=["var1"], ) mock_plan = PlannedStep(tool=mock_tool, prompt=mock_prompt) mock_planner.generate_plan.return_value = [mock_plan] # Setup mock executor mock_executor.simulate_execution.return_value = { "status": "simulated_success", "step": { "tool": { "name": "Test Tool", "description": "A test MCP tool", "id": "test-tool", }, "prompt": { "name": "Test Prompt", "description": "A test prompt", "template_string": "Test template with {var1}", "template_variables": ["var1"], }, "inputs": {"var1": "test input"}, "output": "**Test Output**", "confidence": 0.95, "execution_details": { "processing_time_ms": 1250, "model_used": "test-model", "tokens": {"input": 25, "output": 15}, }, }, "metadata": {"simulation_version": "MVP3_Sprint4"}, } # Test the handler result = handle_execute_plan("test query", "test input") # Verify the result contains expected elements assert "Execution Complete" in result assert "Test Tool" in result assert "Simulated Success" in result def test_execute_button_with_empty_query(self): """Test execute button with empty query.""" with ( patch("app.planner_agent"), patch("app.executor_agent"), ): result = handle_execute_plan("", "test input") assert "Error" in result assert "Original query is missing" in result def test_execute_button_with_no_planner(self): """Test execute button when planner is not available.""" with ( patch("app.planner_agent", None), patch("app.executor_agent"), ): result = handle_execute_plan("test query", "test input") assert "Error" in result assert "Planner service not available" in result def test_execute_button_with_no_executor(self): """Test execute button when executor is not available.""" with ( patch("app.planner_agent"), patch("app.executor_agent", None), ): result = handle_execute_plan("test query", "test input") assert "Error" in result assert "Executor service not available" in result def test_execute_button_with_no_plan_generated(self): """Test execute button when no plan can be generated.""" with ( patch("app.planner_agent") as mock_planner, patch("app.executor_agent"), ): # Mock planner to return empty list mock_planner.generate_plan.return_value = [] result = handle_execute_plan("test query", "test input") assert "Error" in result assert "Could not retrieve the current action plan" in result def test_execute_button_with_multiple_inputs(self): """Test execute button with multiple input values.""" with ( patch("app.planner_agent") as mock_planner, patch("app.executor_agent") as mock_executor, ): # Setup mock with multiple input variables mock_tool = MCPTool( tool_id="test-tool", name="Test Tool", description="A test MCP tool", tags=["test"], invocation_command_stub="test_command", ) mock_prompt = MCPPrompt( prompt_id="test-prompt", name="Test Prompt", description="A test prompt", target_tool_id="test-tool", template_string="Test template with {var1} and {var2}", input_variables=["var1", "var2"], ) mock_plan = PlannedStep(tool=mock_tool, prompt=mock_prompt) mock_planner.generate_plan.return_value = [mock_plan] # Setup mock executor mock_executor.simulate_execution.return_value = { "status": "simulated_success", "step": {}, "metadata": {"simulation_version": "MVP3_Sprint4"}, } # Test with multiple inputs handle_execute_plan("test query", "input1", "input2") # Verify executor was called with correct inputs mock_executor.simulate_execution.assert_called_once() call_args = mock_executor.simulate_execution.call_args inputs_dict = call_args[0][1] # Second argument is the inputs dict assert inputs_dict["var1"] == "input1" assert inputs_dict["var2"] == "input2" def test_execute_button_error_handling(self): """Test execute button error handling.""" with ( patch("app.planner_agent") as mock_planner, patch("app.executor_agent"), ): # Mock planner to raise an exception mock_planner.generate_plan.side_effect = Exception("Test error") result = handle_execute_plan("test query", "test input") assert "Execution Error" in result assert "Test error" in result def test_execute_button_performance(self): """Test that execute button handler responds quickly.""" import time with ( patch("app.planner_agent") as mock_planner, patch("app.executor_agent") as mock_executor, ): # Setup quick mock responses mock_tool = MCPTool( tool_id="test-tool", name="Test Tool", description="A test MCP tool", tags=["test"], invocation_command_stub="test_command", ) mock_prompt = MCPPrompt( prompt_id="test-prompt", name="Test Prompt", description="A test prompt", target_tool_id="test-tool", template_string="Test template", input_variables=[], ) mock_plan = PlannedStep(tool=mock_tool, prompt=mock_prompt) mock_planner.generate_plan.return_value = [mock_plan] mock_executor.simulate_execution.return_value = { "status": "simulated_success", "step": { "tool": {"name": "Test Tool", "description": "A test MCP tool"}, "prompt": {"name": "Test Prompt", "description": "A test prompt"}, "inputs": {}, "output": "Quick response", }, "metadata": {"simulation_version": "MVP3_Sprint4"}, } # Measure execution time start_time = time.time() result = handle_execute_plan("test query", "test input") end_time = time.time() execution_time_ms = (end_time - start_time) * 1000 # Should complete quickly (excluding any real API calls in mocks) assert execution_time_ms < 1000 # Less than 1 second for handler logic assert "Execution Complete" in result def test_execute_button_result_formatting(self): """Test that execute button results are properly formatted.""" with ( patch("app.planner_agent") as mock_planner, patch("app.executor_agent") as mock_executor, ): # Setup mock mock_tool = MCPTool( tool_id="test-tool", name="Test Tool", description="A test MCP tool", tags=["test"], invocation_command_stub="test_command", ) mock_prompt = MCPPrompt( prompt_id="test-prompt", name="Test Prompt", description="A test prompt", target_tool_id="test-tool", template_string="Test template", input_variables=[], ) mock_plan = PlannedStep(tool=mock_tool, prompt=mock_prompt) mock_planner.generate_plan.return_value = [mock_plan] mock_executor.simulate_execution.return_value = { "status": "simulated_success", "step": { "tool": { "name": "Test Tool", "description": "A test MCP tool", "id": "test-tool", }, "prompt": { "name": "Test Prompt", "description": "A test prompt", "template_string": "Test template", }, "inputs": {}, "output": "**Test Output**", "confidence": 0.95, "execution_details": { "processing_time_ms": 1250, "model_used": "test-model", "tokens": {"input": 25, "output": 15}, }, }, } result = handle_execute_plan("test query") # Check formatting elements assert result.startswith("# 🎉 **Execution Complete!**") assert "## 🔧 **Tool & Prompt Information**" in result assert "Test Tool" in result assert "Simulated Success" in result