File size: 3,781 Bytes
1bd7efb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Web Console Tests
==================
Tests for the FastAPI web application and developer console.
"""

import pytest
import sys
import os
from pathlib import Path

# Add src to path
sys.path.insert(0, str(Path(__file__).parent.parent / 'src'))

from httpx import AsyncClient, ASGITransport
from web.app import app


class TestWebConsole:
    """Tests for the web console endpoints."""
    
    @pytest.mark.asyncio
    async def test_health_endpoint(self):
        """Test that health endpoint returns healthy status."""
        transport = ASGITransport(app=app)
        async with AsyncClient(transport=transport, base_url="http://test") as client:
            response = await client.get("/health")
        
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "healthy"
    
    @pytest.mark.asyncio
    async def test_console_page_returns_html(self):
        """Test that root endpoint returns HTML console page."""
        transport = ASGITransport(app=app)
        async with AsyncClient(transport=transport, base_url="http://test") as client:
            response = await client.get("/")
        
        assert response.status_code == 200
        assert "text/html" in response.headers["content-type"]
        assert "AgentMask Developer Console" in response.text
    
    @pytest.mark.asyncio
    async def test_run_endpoint_returns_steps(self):
        """
        Test that POST /run returns 200 and JSON with 'steps' key.
        """
        transport = ASGITransport(app=app)
        async with AsyncClient(transport=transport, base_url="http://test") as client:
            response = await client.post(
                "/run",
                json={"query": "test query for AI research"}
            )
        
        assert response.status_code == 200
        data = response.json()
        
        # Assert response structure
        assert "steps" in data, "Response should contain 'steps' key"
        assert isinstance(data["steps"], list), "Steps should be a list"
        assert len(data["steps"]) >= 2, "Should have at least 2 steps"
        
        # Assert merkle root is present and valid format
        assert "merkle_root" in data
        assert len(data["merkle_root"]) == 64
        
        # Assert each step has required fields
        for step in data["steps"]:
            assert "agent" in step
            assert "output" in step
            assert "hash" in step
    
    @pytest.mark.asyncio
    async def test_run_endpoint_with_different_queries(self):
        """Test that different queries produce results."""
        transport = ASGITransport(app=app)
        async with AsyncClient(transport=transport, base_url="http://test") as client:
            response = await client.post(
                "/run",
                json={"query": "diabetes treatment options"}
            )
        
        assert response.status_code == 200
        data = response.json()
        assert data["success"] is True
        assert "final_output" in data
        assert "summary" in data["final_output"]
    
    @pytest.mark.asyncio
    async def test_run_endpoint_returns_final_output(self):
        """Test that run endpoint includes final output from summarizer."""
        transport = ASGITransport(app=app)
        async with AsyncClient(transport=transport, base_url="http://test") as client:
            response = await client.post(
                "/run",
                json={"query": "machine learning applications"}
            )
        
        data = response.json()
        assert "final_output" in data
        assert "summary" in data["final_output"]
        assert len(data["final_output"]["summary"]) > 10


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