File size: 8,869 Bytes
b6ae7b8 | 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 | #!/usr/bin/env python3
"""
Integration Tests for Stack 2.9 Agent + CLI
Agent and CLI integration tests.
"""
import pytest
import sys
from pathlib import Path
from unittest.mock import MagicMock, patch
# Add stack_cli to path
sys.path.insert(0, str(Path(__file__).parent.parent / "stack_cli"))
from stack_cli.agent import (
StackAgent,
create_agent,
AgentResponse,
ToolCall,
QueryIntent
)
from stack_cli.cli import StackCLI, ChatMode, CommandMode
from stack_cli.context import ContextManager, create_context_manager
from stack_cli.tools import TOOLS, get_tool
class TestAgentCLIIntegration:
"""Test agent and CLI integration."""
def test_agent_in_cli(self):
"""Test agent is used in CLI."""
with patch('stack_cli.cli.create_agent') as mock_create:
mock_agent = MagicMock()
mock_create.return_value = mock_agent
cli = StackCLI()
assert cli.agent is not None
def test_chat_mode_uses_agent(self):
"""Test chat mode uses agent."""
with patch('stack_cli.cli.create_agent') as mock_create:
mock_agent = MagicMock()
mock_response = AgentResponse(content="test", tool_calls=[])
mock_agent.process.return_value = mock_response
mock_create.return_value = mock_agent
chat = ChatMode(mock_agent)
# Simulate user input
with patch('stack_cli.agent.StackAgent.process', return_value=mock_response):
chat.default("test query")
# Should have added to history
assert len(chat.history) >= 0
def test_command_mode_uses_agent(self):
"""Test command mode uses agent."""
with patch('stack_cli.cli.create_agent') as mock_create:
mock_agent = MagicMock()
mock_response = AgentResponse(content="result", tool_calls=[])
mock_agent.process.return_value = mock_response
mock_create.return_value = mock_agent
cmd = CommandMode(mock_agent)
# Execute should call agent
result = cmd.execute("test query")
# Result should be formatted
class TestAgentContextIntegration:
"""Test agent and context integration."""
def test_agent_uses_context_manager(self):
"""Test agent uses context manager."""
with patch('stack_cli.context.create_context_manager'):
agent = create_agent()
assert agent.context_manager is not None
def test_agent_records_tool_usage(self):
"""Test agent records tool usage."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": True})
mock_get_tool.return_value = mock_tool
agent = StackAgent()
response = agent.process("read test.py")
# Agent should record tool usage in context
assert agent.context_manager is not None
def test_agent_gets_context(self):
"""Test agent can get context."""
with patch('stack_cli.context.create_context_manager'):
agent = StackAgent()
context = agent.get_context()
assert context is not None
assert isinstance(context, str)
class TestAgentToolsIntegration:
"""Test agent and tools integration."""
def test_agent_gets_schemas(self):
"""Test agent can get tool schemas."""
with patch('stack_cli.context.create_context_manager'):
agent = StackAgent()
schemas = agent.get_schemas()
assert isinstance(schemas, list)
if schemas:
assert "name" in schemas[0]
def test_agent_process_with_forced_tools(self):
"""Test agent can process with forced tools."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": True})
mock_get_tool.return_value = mock_tool
agent = StackAgent()
response = agent.process_with_tools("test", ["read", "write"])
assert response is not None
assert isinstance(response, AgentResponse)
class TestFullWorkflow:
"""Test complete agent-CLI workflows."""
def test_read_file_workflow(self):
"""Test read file workflow."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={
"success": True,
"content": "file content here",
"total_lines": 10
})
mock_get_tool.return_value = mock_tool
agent = StackAgent()
response = agent.process("read test.py")
assert response.content is not None
def test_write_file_workflow(self):
"""Test write file workflow."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": True})
mock_get_tool.return_value = mock_tool
agent = StackAgent()
response = agent.process("write output.txt with test content")
assert response is not None
def test_git_workflow(self):
"""Test git operations workflow."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": True, "files": ["test.py"]})
mock_get_tool.return_value = mock_tool
agent = StackAgent()
response = agent.process("git status")
assert response is not None
class TestMultiToolIntegration:
"""Test multiple tools working together."""
def test_read_then_process(self):
"""Test reading then processing."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
call_count = [0]
def mock_tool(**kwargs):
call_count[0] += 1
if call_count[0] == 1:
return {"success": True, "content": "data"}
return {"success": True}
mock_get_tool.return_value = mock_tool
agent = StackAgent()
response = agent.process("read data.txt and process it")
assert response is not None
def test_multiple_git_operations(self):
"""Test multiple git operations."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": True})
mock_get_tool.return_value = mock_tool
agent = StackAgent()
response = agent.process("check git status and commit")
assert response is not None
class TestErrorIntegration:
"""Test error handling in integration."""
def test_tool_failure_handling(self):
"""Test handling of tool failures."""
with patch('stack_cli.context.create_context_manager'):
with patch('stack_cli.tools.get_tool') as mock_get_tool:
mock_tool = MagicMock(return_value={"success": False, "error": "Not found"})
mock_get_tool.return_value = mock_tool
agent = StackAgent()
response = agent.process("read missing.py")
# Should still return a response
assert response is not None
def test_context_error_recovery(self):
"""Test recovery from context errors."""
with patch('stack_cli.context.create_context_manager') as mock_cm:
mock_cm.side_effect = RuntimeError("Context error")
# Should handle gracefully
try:
agent = create_agent()
except:
pass # Expected
if __name__ == "__main__":
pytest.main([__file__, "-v"])
|