File size: 6,253 Bytes
7b2787b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Tests for the FastAPI endpoints.
"""

import pytest
from fastapi.testclient import TestClient
from httpx import AsyncClient, ASGITransport

from app.main import app


# ============================================================
# Sync Test Client (for simple tests)
# ============================================================

client = TestClient(app)


class TestRootEndpoints:
    """Tests for root endpoints."""
    
    def test_root(self):
        """Test root endpoint."""
        response = client.get("/")
        assert response.status_code == 200
        
        data = response.json()
        assert "name" in data
        assert "version" in data
        assert "endpoints" in data
    
    def test_health(self):
        """Test health endpoint."""
        response = client.get("/health")
        assert response.status_code == 200
        
        data = response.json()
        assert data["status"] == "healthy"


class TestToolsEndpoints:
    """Tests for tools endpoints."""
    
    def test_list_tools(self):
        """Test listing tools."""
        response = client.get("/tools/")
        assert response.status_code == 200
        
        data = response.json()
        assert "tools" in data
        assert "total" in data
        assert data["total"] > 0
        
        # Check that built-in tools are present
        tool_names = [t["name"] for t in data["tools"]]
        assert "extract_functions" in tool_names
        assert "calculate_complexity" in tool_names
    
    def test_get_tool(self):
        """Test getting a specific tool."""
        response = client.get("/tools/extract_functions")
        assert response.status_code == 200
        
        data = response.json()
        assert data["name"] == "extract_functions"
        assert "description" in data
    
    def test_get_nonexistent_tool(self):
        """Test getting a tool that doesn't exist."""
        response = client.get("/tools/nonexistent_tool")
        assert response.status_code == 404


class TestGraphEndpoints:
    """Tests for graph endpoints."""
    
    def test_list_graphs(self):
        """Test listing graphs."""
        response = client.get("/graph/")
        assert response.status_code == 200
        
        data = response.json()
        assert "graphs" in data
        assert "total" in data
    
    def test_get_demo_workflow(self):
        """Test getting the demo workflow."""
        response = client.get("/graph/code-review-demo")
        assert response.status_code == 200
        
        data = response.json()
        assert data["graph_id"] == "code-review-demo"
        assert data["name"] == "Code Review Demo"
        assert "mermaid_diagram" in data
    
    def test_create_graph(self):
        """Test creating a new graph."""
        graph_data = {
            "name": "test_workflow",
            "description": "A test workflow",
            "nodes": [
                {"name": "start", "handler": "extract_functions"},
                {"name": "end", "handler": "calculate_complexity"}
            ],
            "edges": {
                "start": "end"
            },
            "entry_point": "start"
        }
        
        response = client.post("/graph/create", json=graph_data)
        assert response.status_code == 201
        
        data = response.json()
        assert "graph_id" in data
        assert data["name"] == "test_workflow"
        assert data["node_count"] == 2
    
    def test_create_graph_invalid_handler(self):
        """Test creating a graph with invalid handler."""
        graph_data = {
            "name": "invalid_workflow",
            "nodes": [
                {"name": "bad", "handler": "nonexistent_handler"}
            ],
            "edges": {}
        }
        
        response = client.post("/graph/create", json=graph_data)
        assert response.status_code == 404


# ============================================================
# Async Tests (for async endpoints)
# ============================================================

@pytest.fixture
def anyio_backend():
    return "asyncio"


@pytest.mark.asyncio
async def test_run_demo_workflow():
    """Test running the demo workflow."""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as ac:
        run_data = {
            "graph_id": "code-review-demo",
            "initial_state": {
                "code": "def hello():\n    print('world')",
                "quality_threshold": 5.0
            },
            "async_execution": False
        }
        
        response = await ac.post("/graph/run", json=run_data)
        assert response.status_code == 200
        
        data = response.json()
        assert "run_id" in data
        assert data["status"] in ["completed", "failed"]
        assert "execution_log" in data


@pytest.mark.asyncio
async def test_async_execution():
    """Test async execution mode."""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as ac:
        run_data = {
            "graph_id": "code-review-demo",
            "initial_state": {
                "code": "def test(): pass",
                "quality_threshold": 5.0
            },
            "async_execution": True
        }
        
        response = await ac.post("/graph/run", json=run_data)
        assert response.status_code == 200
        
        data = response.json()
        assert "run_id" in data
        assert data["status"] == "pending"
        
        # Check run state
        run_id = data["run_id"]
        state_response = await ac.get(f"/graph/state/{run_id}")
        assert state_response.status_code == 200


@pytest.mark.asyncio
async def test_run_nonexistent_graph():
    """Test running a graph that doesn't exist."""
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as ac:
        run_data = {
            "graph_id": "nonexistent-graph",
            "initial_state": {}
        }
        
        response = await ac.post("/graph/run", json=run_data)
        assert response.status_code == 404


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