File size: 10,974 Bytes
1f2d50a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
#!/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
|