File size: 2,745 Bytes
75bea1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Integration tests for the API."""

import pytest
from fastapi.testclient import TestClient
from unittest.mock import patch, AsyncMock, MagicMock


class TestAPIEndpoints:
    """Tests for API endpoints."""

    @pytest.fixture
    def client(self):
        """Create test client."""
        from src.api.app import app
        return TestClient(app)

    def test_root_endpoint(self, client):
        """Test root endpoint."""
        response = client.get("/")

        assert response.status_code == 200
        data = response.json()
        assert "name" in data
        assert "Ask-the-Web" in data["name"]

    def test_health_endpoint(self, client):
        """Test health check endpoint."""
        with patch("src.api.routes.get_settings") as mock_settings:
            mock_settings.return_value = MagicMock(
                openai_api_key="test-key",
                anthropic_api_key=None,
                tavily_api_key="test-tavily",
            )

            response = client.get("/api/v1/health")

            assert response.status_code == 200
            data = response.json()
            assert "status" in data
            assert "version" in data

    @patch("src.api.routes.AskTheWebAgent")
    def test_query_endpoint(self, mock_agent_class, client):
        """Test query endpoint."""
        # Setup mock
        mock_agent = AsyncMock()
        mock_agent.query = AsyncMock(return_value=MagicMock(
            answer="Test answer",
            sources=[{"title": "Test", "url": "https://test.com", "snippet": "snippet"}],
            follow_up_questions=["Follow up?"],
            confidence=0.8,
            metadata={},
        ))
        mock_agent_class.return_value = mock_agent

        response = client.post(
            "/api/v1/query",
            json={"query": "What is Python?"},
        )

        assert response.status_code == 200
        data = response.json()
        assert "answer" in data
        assert "sources" in data
        assert "confidence" in data

    def test_query_validation(self, client):
        """Test query validation."""
        # Empty query should fail
        response = client.post(
            "/api/v1/query",
            json={"query": ""},
        )

        assert response.status_code == 422  # Validation error

    def test_conversation_not_found(self, client):
        """Test getting non-existent conversation."""
        response = client.get("/api/v1/conversation/nonexistent-id")

        assert response.status_code == 404

    def test_delete_conversation_not_found(self, client):
        """Test deleting non-existent conversation."""
        response = client.delete("/api/v1/conversation/nonexistent-id")

        assert response.status_code == 404