File size: 9,119 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python3
"""
Stack 2.9 - Test Configuration and Fixtures
Pytest fixtures, mocks, and configuration.
"""

import os
import sys
import json
import tempfile
import shutil
from pathlib import Path
from typing import Any, Dict, List, Optional
from unittest.mock import MagicMock, patch, AsyncMock
from dataclasses import dataclass, field
from datetime import datetime

import pytest

# Add stack_cli to path
stack_cli_dir = Path(__file__).parent.parent / "stack_cli"
sys.path.insert(0, str(stack_cli_dir))


# ============================================================================
# FIXTURES: TEMP DIRECTORIES & FILES
# ============================================================================

@pytest.fixture
def temp_workspace(tmp_path):
    """Create a temporary workspace directory."""
    workspace = tmp_path / "workspace"
    workspace.mkdir()
    yield workspace
    # Cleanup handled by tmp_path


@pytest.fixture
def temp_project(temp_workspace):
    """Create a temporary project with files."""
    project = temp_workspace / "test_project"
    project.mkdir()
    
    # Create some test files
    (project / "pyproject.toml").write_text("""
[project]
name = "test-project"
version = "0.1.0"
dependencies = ["requests", "click"]
""")
    
    (project / "main.py").write_text("""
#!/usr/bin/env python3
\"\"\"Main module.\"\"\"

def main():
    print("Hello, World!")

if __name__ == "__main__":
    main()
""")
    
    (project / "README.md").write_text("# Test Project\n\nA test project.")
    
    (project / ".env.example").write_text("API_KEY=xxx")
    
    yield project


@pytest.fixture
def temp_file(temp_workspace):
    """Create a temporary file for testing."""
    file_path = temp_workspace / "test.txt"
    file_path.write_text("Line 1\nLine 2\nLine 3\nLine 4\nLine 5")
    yield file_path


@pytest.fixture
def temp_git_repo(temp_project):
    """Create a temp git repo."""
    os.system(f"cd {temp_project} && git init -q")
    os.system(f"cd {temp_project} && git config user.email 'test@test.com'")
    os.system(f"cd {temp_project} && git config user.name 'Test User'")
    yield temp_project


# ============================================================================
# FIXTURES: MOCKS
# ============================================================================

@pytest.fixture
def mock_agent():
    """Create a mock agent."""
    from stack_cli.agent import StackAgent, create_agent
    
    with patch('stack_cli.agent.create_context_manager') as mock_cm:
        mock_cm.return_value = MagicMock()
        agent = create_agent("/tmp")
    
    return agent


@pytest.fixture
def mock_context_manager():
    """Create a mock context manager."""
    from stack_cli.context import ContextManager, SessionMemory, ProjectContext
    
    cm = MagicMock(spec=ContextManager)
    cm.session = MagicMock(spec=SessionMemory)
    cm.session.messages = []
    cm.session.tools_used = []
    cm.session.files_touched = []
    cm.session.commands_run = []
    cm.get_workspace_context.return_value = "# Mock Context"
    cm.get_context_summary.return_value = {"workspace": "/tmp", "projects": []}
    
    return cm


@pytest.fixture
def mock_tool():
    """Create a mock tool function."""
    def tool_func(**kwargs):
        return {"success": True, "result": "mocked"}
    return tool_func


@pytest.fixture
def mock_subprocess():
    """Mock subprocess calls."""
    with patch('subprocess.run') as mock_run:
        mock_result = MagicMock()
        mock_result.returncode = 0
        mock_result.stdout = "test output"
        mock_result.stderr = ""
        mock_run.return_value = mock_result
        yield mock_run


@pytest.fixture
def mock_path():
    """Mock Path operations."""
    with patch('pathlib.Path') as mock_path_class:
        mock_path = MagicMock()
        mock_path.exists.return_value = True
        mock_path.is_file.return_value = True
        mock_path.is_dir.return_value = False
        mock_path.read_text.return_value = "mocked content"
        mock_path.write_text.return_value = None
        mock_path.rglob.return_value = []
        mock_path.__enter__ = MagicMock(return_value=mock_path)
        mock_path.__exit__ = MagicMock(return_value=False)
        
        mock_path_class.return_value = mock_path
        mock_path_class.exists.return_value = True
        
        yield mock_path_class


# ============================================================================
# FIXTURES: CONFIG OVERRIDES
# ============================================================================

@pytest.fixture
def config_override():
    """Override configuration values."""
    original_env = os.environ.copy()
    
    test_config = {
        "WORKSPACE_PATH": "/tmp/test_workspace",
        "MAX_CONTEXT_TOKENS": "4000",
        "ENABLE_SELF_REFLECTION": "true",
        "MAX_REFLECTION_ITERATIONS": "3"
    }
    
    os.environ.update(test_config)
    
    yield test_config
    
    # Restore original
    os.environ.clear()
    os.environ.update(original_env)


@pytest.fixture
def fake_model():
    """Create a fake model for testing."""
    from dataclasses import dataclass
    
    @dataclass
    class FakeModelResponse:
        content: str = "Mocked response"
        tool_calls: list = field(default_factory=list)
        confidence: float = 1.0
    
    return FakeModelResponse


# ============================================================================
# FIXTURES: STACK CLI COMPONENTS
# ============================================================================

@pytest.fixture
def sample_tools():
    """Return list of all tool names."""
    return [
        # File ops
        "read", "write", "edit", "search", "grep", "copy", "move", "delete",
        # Git
        "git_status", "git_commit", "git_push", "git_pull", "git_branch", "git_log", "git_diff",
        # Code execution
        "run", "test", "lint", "format", "typecheck", "server", "install",
        # Web
        "web_search", "fetch", "download", "check_url", "screenshot",
        # Memory
        "memory_recall", "memory_save", "memory_list", "context_load", "project_scan",
        # Tasks
        "create_task", "list_tasks", "update_task", "delete_task", "create_plan", "execute_plan"
    ]


@pytest.fixture
def sample_agent_response():
    """Create a sample agent response."""
    from stack_cli.agent import AgentResponse, ToolCall
    
    tool_calls = [
        ToolCall(tool_name="read", arguments={"path": "test.py"}, result={"success": True}, success=True),
        ToolCall(tool_name="run", arguments={"command": "echo hello"}, result={"success": True}, success=True)
    ]
    
    return AgentResponse(
        content="Test response with tool results",
        tool_calls=tool_calls,
        context_used=["context1"],
        confidence=0.9,
        needs_clarification=False
    )


# ============================================================================
# FIXTURES: TEST DATA
# ============================================================================

@pytest.fixture
def sample_file_content():
    """Sample file content for testing."""
    return """#!/usr/bin/env python3
\"\"\"Sample module for testing.\"\"\"

def hello(name: str) -> str:
    \"\"\"Say hello.\"\"\"
    return f"Hello, {name}!"

def add(a: int, b: int) -> int:
    \"\"\"Add two numbers.\"\"\"
    return a + b

class Calculator:
    \"\"\"Simple calculator.\"\"\"
    
    def __init__(self):
        self.value = 0
    
    def add(self, n):
        self.value += n
        return self
"""

@pytest.fixture
def sample_query_intents():
    """Sample queries and their expected intents."""
    return [
        ("read README.md", "file_read"),
        ("write test.py with content", "file_write"),
        ("edit config.json", "file_edit"),
        ("find files named *.py", "file_search"),
        ("git status", "git_operation"),
        ("run pytest", "code_execution"),
        ("search the web for python", "web_search"),
        ("remember this important thing", "memory"),
        ("create task to fix bug", "task"),
        ("what is python?", "question"),
    ]


# ============================================================================
# PYTEST CONFIGURATION
# ============================================================================

def pytest_configure(config):
    """Configure pytest with custom markers."""
    config.addinivalue_line("markers", "slow: marks tests as slow (deselect with '-m \"not slow\"')")
    config.addinivalue_line("markers", "integration: marks tests as integration tests")
    config.addinivalue_line("markers", "unit: marks tests as unit tests")
    config.addinivalue_line("markers", "benchmark: marks tests as benchmark tests")
    config.addinivalue_line("markers", "asyncio: marks tests as async tests")


# Auto-use fixtures
@pytest.fixture(autouse=True)
def reset_sys_path():
    """Ensure sys.path is properly set."""
    stack_cli_dir = Path(__file__).parent.parent / "stack_cli"
    if str(stack_cli_dir) not in sys.path:
        sys.path.insert(0, str(stack_cli_dir))
    yield