CoolFace
Apppublic

ruby2210/rag-chatbot

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
test_history_management.py169 linesDownload Raw Back to integration
1"""2Integration test for chat history management user journey in the RAG Chatbot application.3"""4import pytest5from fastapi.testclient import TestClient6from src.api.main import app7 8 9@pytest.fixture10def client():11    """Create a test client for the API."""12    return TestClient(app)13 14 15def test_chat_history_management_journey(client):16    """Test the complete chat history management user journey."""17    # Step 1: Start a conversation by sending a message18    query_data = {19        "message": "What is the main concept of this book?",20        "context_type": "full_book"21    }22 23    response = client.post("/api/chat", json=query_data)24 25    assert response.status_code == 20026    data = response.json()27    assert "session_id" in data28    assert "response" in data29 30    session_id = data["session_id"]31    assert isinstance(session_id, str) and len(session_id) > 032 33    # Step 2: Send another message to add to the history34    follow_up_data = {35        "message": "Can you elaborate on that concept?",36        "session_id": session_id,37        "context_type": "full_book"38    }39 40    follow_up_response = client.post("/api/chat", json=follow_up_data)41 42    assert follow_up_response.status_code == 20043    follow_up_data = follow_up_response.json()44    assert follow_up_data["session_id"] == session_id45 46    # Step 3: Retrieve the chat history47    history_response = client.get(f"/api/chat/history?session_id={session_id}")48 49    assert history_response.status_code == 20050    history_data = history_response.json()51 52    assert "session_id" in history_data53    assert "history" in history_data54    assert history_data["session_id"] == session_id55    assert isinstance(history_data["history"], list)56 57    # Should have at least 2 messages in history (user query + AI response + follow-up + AI response)58    # Each interaction adds 2 messages (user + assistant)59    assert len(history_data["history"]) >= 260 61    # Verify message structure62    for message in history_data["history"]:63        assert "message_id" in message64        assert "role" in message65        assert "content" in message66        assert "timestamp" in message67        assert "context_type" in message68 69        assert isinstance(message["message_id"], str)70        assert isinstance(message["role"], str)71        assert isinstance(message["content"], str)72        assert isinstance(message["timestamp"], str)73        assert isinstance(message["context_type"], str)74 75        # Role should be either 'user' or 'assistant'76        assert message["role"] in ["user", "assistant"]77 78    # Step 4: Clear the chat history79    clear_data = {80        "session_id": session_id81    }82 83    clear_response = client.post("/api/chat/clear", json=clear_data)84 85    assert clear_response.status_code == 20086    clear_result = clear_response.json()87 88    assert "session_id" in clear_result89    assert "status" in clear_result90    assert "message" in clear_result91    assert clear_result["session_id"] == session_id92    assert clear_result["status"] == "cleared"93 94    # Step 5: Verify history is cleared by retrieving it again95    cleared_history_response = client.get(f"/api/chat/history?session_id={session_id}")96 97    assert cleared_history_response.status_code == 20098    cleared_history_data = cleared_history_response.json()99 100    assert "session_id" in cleared_history_data101    assert "history" in cleared_history_data102    assert cleared_history_data["session_id"] == session_id103    assert cleared_history_data["history"] == []104 105 106def test_session_independence(client):107    """Test that chat history is properly isolated between different sessions."""108    # Create first session and add messages109    query_data_1 = {110        "message": "What is the first concept?",111        "context_type": "full_book"112    }113 114    response_1 = client.post("/api/chat", json=query_data_1)115    assert response_1.status_code == 200116    data_1 = response_1.json()117    session_id_1 = data_1["session_id"]118 119    # Create second session and add messages120    query_data_2 = {121        "message": "What is the second concept?",122        "context_type": "full_book"123    }124 125    response_2 = client.post("/api/chat", json=query_data_2)126    assert response_2.status_code == 200127    data_2 = response_2.json()128    session_id_2 = data_2["session_id"]129 130    # Verify sessions are different131    assert session_id_1 != session_id_2132 133    # Retrieve history for first session134    history_1_response = client.get(f"/api/chat/history?session_id={session_id_1}")135    assert history_1_response.status_code == 200136    history_1_data = history_1_response.json()137 138    # Retrieve history for second session139    history_2_response = client.get(f"/api/chat/history?session_id={session_id_2}")140    assert history_2_response.status_code == 200141    history_2_data = history_2_response.json()142 143    # Histories should be independent144    assert history_1_data["session_id"] == session_id_1145    assert history_2_data["session_id"] == session_id_2146 147    # Each session should have its own history148    assert len(history_1_data["history"]) >= 1149    assert len(history_2_data["history"]) >= 1150 151    # Clear only the first session152    clear_data = {153        "session_id": session_id_1154    }155 156    clear_response = client.post("/api/chat/clear", json=clear_data)157    assert clear_response.status_code == 200158 159    # Verify first session is cleared but second session still has history160    cleared_history_1_response = client.get(f"/api/chat/history?session_id={session_id_1}")161    assert cleared_history_1_response.status_code == 200162    cleared_history_1_data = cleared_history_1_response.json()163    assert cleared_history_1_data["history"] == []164 165    remaining_history_2_response = client.get(f"/api/chat/history?session_id={session_id_2}")166    assert remaining_history_2_response.status_code == 200167    remaining_history_2_data = remaining_history_2_response.json()168    assert remaining_history_2_data["history"] != []169    assert len(remaining_history_2_data["history"]) >= 1