File size: 2,629 Bytes
36102cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
from fastapi.testclient import TestClient
from app.main import app
from app.models.agent import QueryRequest


client = TestClient(app)


class TestAgentEndpoint:
    """Integration tests for the agent endpoint."""
    
    def test_agent_endpoint_selected_text_mode(self):
        """Test the agent endpoint with selected text mode."""
        request_data = {
            "question": "What does this text mean?",
            "selected_text": "The main themes include good vs evil and friendship."
        }
        
        response = client.post("/agent/query", json=request_data)
        
        assert response.status_code == 200
        data = response.json()
        assert "answer" in data
        assert "sources" in data
        assert "mode" in data
        assert data["mode"] == "selected_text"
    
    def test_agent_endpoint_book_mode(self):
        """Test the agent endpoint with book mode (default)."""
        request_data = {
            "question": "What are the main themes in this book?"
        }
        
        response = client.post("/agent/query", json=request_data)
        
        assert response.status_code == 200
        data = response.json()
        assert "answer" in data
        assert "sources" in data
        assert "mode" in data
        assert data["mode"] == "book"
    
    def test_agent_endpoint_general_mode(self):
        """Test the agent endpoint with general mode."""
        request_data = {
            "question": "What is the capital of France?",
            "mode": "general"
        }
        
        response = client.post("/agent/query", json=request_data)
        
        assert response.status_code == 200
        data = response.json()
        assert "answer" in data
        assert "sources" in data
        assert "mode" in data
        assert data["mode"] == "general"
    
    def test_agent_endpoint_invalid_request(self):
        """Test the agent endpoint with invalid request data."""
        request_data = {
            "question": ""  # Empty question should fail validation
        }
        
        response = client.post("/agent/query", json=request_data)
        
        # Should return 422 for validation error
        assert response.status_code == 422
    
    def test_agent_endpoint_missing_question(self):
        """Test the agent endpoint with missing question."""
        request_data = {
            # Missing required question field
        }
        
        response = client.post("/agent/query", json=request_data)
        
        # Should return 422 for validation error
        assert response.status_code == 422