File size: 5,144 Bytes
ba5110e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Test cases for FastAPI endpoints.
Tests health, conversations, and rate limit APIs.
"""
import pytest
from fastapi.testclient import TestClient
from backend.app import app


client = TestClient(app)


class TestHealthEndpoint:
    """Test suite for health check endpoint."""

    def test_health_check(self):
        """TC-API-001: Health endpoint should return healthy status."""
        response = client.get("/api/health")
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "healthy"
        assert data["service"] == "algebra-chatbot"


class TestConversationEndpoints:
    """Test suite for conversation CRUD endpoints."""

    def test_list_conversations_empty(self):
        """TC-API-002: List conversations should return array."""
        response = client.get("/api/conversations")
        assert response.status_code == 200
        assert isinstance(response.json(), list)

    def test_create_conversation(self):
        """TC-API-003: Create conversation should return new conversation."""
        response = client.post("/api/conversations")
        assert response.status_code == 200
        data = response.json()
        assert "id" in data
        assert "created_at" in data
        return data["id"]

    def test_delete_conversation(self):
        """TC-API-004: Delete conversation should succeed."""
        # First create
        create_response = client.post("/api/conversations")
        conv_id = create_response.json()["id"]
        
        # Then delete
        delete_response = client.delete(f"/api/conversations/{conv_id}")
        assert delete_response.status_code == 200
        assert delete_response.json()["status"] == "deleted"

    def test_get_messages_empty(self):
        """TC-API-005: New conversation should have no messages."""
        # Create conversation
        create_response = client.post("/api/conversations")
        conv_id = create_response.json()["id"]
        
        # Get messages
        messages_response = client.get(f"/api/conversations/{conv_id}/messages")
        assert messages_response.status_code == 200
        assert messages_response.json() == []
        
        # Cleanup
        client.delete(f"/api/conversations/{conv_id}")


class TestRateLimitEndpoints:
    """Test suite for rate limit status endpoints."""

    def test_get_rate_limit_status(self):
        """TC-API-006: Rate limit status should return valid structure."""
        response = client.get("/api/rate-limit/test_session")
        assert response.status_code == 200
        data = response.json()
        assert "requests_this_minute" in data
        assert "tokens_today" in data
        assert "limits" in data

    def test_rate_limit_limits_structure(self):
        """TC-API-007: Rate limit should have correct limit values."""
        response = client.get("/api/rate-limit/test_session")
        data = response.json()
        limits = data["limits"]
        assert limits["rpm"] == 30
        assert limits["rpd"] == 1000
        assert limits["tpm"] == 8000
        assert limits["tpd"] == 200000


class TestWolframStatusEndpoint:
    """Test suite for Wolfram API status endpoint."""

    def test_wolfram_status(self):
        """TC-API-008: Wolfram status should return usage info."""
        response = client.get("/api/wolfram-status")
        assert response.status_code == 200
        data = response.json()
        assert "used" in data
        assert "limit" in data
        assert "remaining" in data
        assert "month" in data
        assert data["limit"] == 2000

    def test_wolfram_remaining_calculation(self):
        """TC-API-009: Remaining should equal limit minus used."""
        response = client.get("/api/wolfram-status")
        data = response.json()
        assert data["remaining"] == data["limit"] - data["used"]


class TestChatEndpoint:
    """Test suite for chat endpoint."""

    def test_chat_creates_session(self):
        """TC-API-010: Chat without session_id should create new session."""
        response = client.post(
            "/api/chat",
            data={"message": "Hello"},
        )
        assert response.status_code == 200
        # Should have session ID in header
        assert "X-Session-Id" in response.headers or response.status_code == 200

    def test_chat_with_session(self):
        """TC-API-011: Chat with existing session_id should work."""
        # Create conversation first
        create_response = client.post("/api/conversations")
        conv_id = create_response.json()["id"]
        
        response = client.post(
            "/api/chat",
            data={"message": "Test message", "session_id": conv_id},
        )
        assert response.status_code == 200
        
        # Cleanup
        client.delete(f"/api/conversations/{conv_id}")

    def test_chat_invalid_session(self):
        """TC-API-012: Chat with invalid session_id should return 404."""
        response = client.post(
            "/api/chat",
            data={"message": "Test", "session_id": "invalid-uuid-12345"},
        )
        assert response.status_code == 404