File size: 10,666 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
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
#!/usr/bin/env python3
"""
Integration Tests for Stack 2.9 Tool Chains
Multi-tool sequence 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
from stack_cli.tools import get_tool, list_tools


class TestToolChains:
    """Test tool chain sequences."""

    def test_read_grep_chain(self):
        """Test read then grep workflow."""
        call_log = []
        
        def mock_tool(**kwargs):
            call_log.append(kwargs)
            if "path" in kwargs:
                return {"success": True, "content": "line1\nline2\nline3"}
            return {"success": True, "matches": []}
        
        with patch('stack_cli.context.create_context_manager'):
            with patch('stack_cli.tools.get_tool', return_value=mock_tool):
                agent = StackAgent()
                
                # Simulate reading then grepping
                read_result = get_tool("read")(path="test.py")
                grep_result = get_tool("grep")(path="test.py", pattern="line")
                
                assert read_result["success"] is True
                assert grep_result["success"] is True

    def test_search_copy_chain(self):
        """Test search then copy 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, "matches": ["file1.py"]})
                mock_get_tool.return_value = mock_tool
                
                agent = StackAgent()
                
                # Search for files
                search_result = get_tool("search")(path=".", pattern="*.py")
                
                # If found, copy
                if search_result.get("success") and search_result.get("matches"):
                    copy_result = get_tool("copy")(source="file1.py", destination="backup.py")
                    assert copy_result["success"] is True

    def test_git_status_branch_chain(self):
        """Test git status then branch 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": []})
                mock_get_tool.return_value = mock_tool
                
                agent = StackAgent()
                
                # Check status
                status_result = get_tool("git_status")(repo_path=".")
                
                # List branches
                branch_result = get_tool("git_branch")(repo_path=".")
                
                assert status_result["success"] is True
                assert branch_result["success"] is True


class TestComplexToolSequences:
    """Test complex multi-tool sequences."""

    def test_file_edit_save_sequence(self):
        """Test file edit and save sequence."""
        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()
                
                # Read file
                read_result = get_tool("read")(path="test.py")
                
                # Edit
                edit_result = get_tool("edit")(
                    path="test.py",
                    old_text="old_content",
                    new_text="new_content"
                )
                
                # Write back
                write_result = get_tool("write")(
                    path="test.py",
                    content="new_content"
                )
                
                assert edit_result["success"] is True

    def test_code_test_lint_sequence(self):
        """Test code test and lint sequence."""
        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, "output": "ok"})
                mock_get_tool.return_value = mock_tool
                
                agent = StackAgent()
                
                # Run tests
                test_result = get_tool("test")(path=".", pattern="test*.py")
                
                # Lint
                lint_result = get_tool("lint")(path=".")
                
                assert test_result.get("success") or "output" in test_result
                assert lint_result.get("success") or "output" in lint_result

    def test_project_scan_context_sequence(self):
        """Test project scan then load context."""
        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,
                    "project": {"name": "test", "files": ["a.py", "b.py"]}
                })
                mock_get_tool.return_value = mock_tool
                
                agent = StackAgent()
                
                # Scan project
                scan_result = get_tool("project_scan")(path=".")
                
                # Load context
                context_result = get_tool("context_load")()
                
                assert scan_result["success"] is True
                assert context_result["success"] is True


class TestToolChainErrors:
    """Test error handling in tool chains."""

    def test_chain_continues_on_error(self):
        """Test that chain continues when one tool fails."""
        call_count = [0]
        
        def mock_tool(**kwargs):
            call_count[0] += 1
            if call_count[0] == 1:
                return {"success": False, "error": "File not found"}
            return {"success": True}
        
        with patch('stack_cli.context.create_context_manager'):
            with patch('stack_cli.tools.get_tool', return_value=mock_tool):
                agent = StackAgent()
                
                # First tool fails
                result1 = get_tool("read")(path="missing.py")
                
                # Second tool should still work
                result2 = get_tool("write")(path="new.txt", content="x")
                
                assert result1["success"] is False
                assert result2["success"] is True
                assert call_count[0] == 2

    def test_rollback_on_error(self):
        """Test rollback behavior on error."""
        with patch('stack_cli.context.create_context_manager'):
            with patch('stack_cli.tools.get_tool') as mock_get_tool:
                mock_tool = MagicMock(side_effect=[
                    {"success": True},
                    {"success": False, "error": "Operation failed"},
                    {"success": True}  # rollback
                ])
                mock_get_tool.return_value = mock_tool
                
                # First operation
                result1 = get_tool("write")(path="a.txt", content="x")
                assert result1["success"] is True
                
                # Second operation fails - rollback attempted
                try:
                    result2 = get_tool("write")(path="b.txt", content="y")
                except:
                    pass


class TestParallelToolExecution:
    """Test parallel tool execution."""

    def test_parallel_file_operations(self):
        """Test parallel file 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
                
                # Would simulate parallel execution
                # In real implementation, use asyncio
                
                result1 = get_tool("read")(path="file1.txt")
                result2 = get_tool("read")(path="file2.txt")
                
                assert result1["success"] is True
                assert result2["success"] is True


class TestToolDependencyResolution:
    """Test tool dependency resolution."""

    def test_git_needs_repo(self):
        """Test that git tools need repo path."""
        # Git operations require repo path
        result = get_tool("git_status")(repo_path=".")
        
        # Success depends on whether it's a valid git repo
        # But it should not crash
        assert "success" in result

    def test_edit_needs_file(self):
        """Test that edit tool needs existing file."""
        result = get_tool("edit")(
            path="/nonexistent/file.txt",
            old_text="old",
            new_text="new"
        )
        
        assert result["success"] is False

    def test_memory_needs_workspace(self):
        """Test that memory tools need workspace."""
        result = get_tool("memory_save")(key="test", value="data")
        
        # Should succeed or fail gracefully
        assert "success" in result


class TestToolChainsPerformance:
    """Test tool chain performance."""

    def test_rapid_tool_calls(self):
        """Test rapid sequential tool calls."""
        import time
        
        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
                
                start = time.time()
                
                for _ in range(10):
                    get_tool("read")(path="test.py")
                
                elapsed = time.time() - start
                
                # Should complete reasonably fast
                assert elapsed < 5.0  # 5 seconds for 10 calls

    def test_memory_efficiency(self):
        """Test memory efficiency of tool chains."""
        with patch('stack_cli.context.create_context_manager'):
            agent = StackAgent()
            
            # Process multiple queries
            for _ in range(5):
                agent.process("test query")
            
            # Should not accumulate too much data
            assert len(agent.conversation_history) >= 0


if __name__ == "__main__":
    pytest.main([__file__, "-v"])