WalkXR-AI / tests / test_reflection_loop_fail.py
test_reflection_loop_fail.py
Raw
"""
Integration test for the Small Moments agent with a self-reflection loop using 
a local Ollama setup.

This test exercises the LangGraph flow built in tasks T003.3, T003.4, and T004.3.

This test specifically tests the case in which the maximum number of retries for
the LLM to generate a response have been reached, in which case a generic
"fail-safe" response will be presented to the user.

To run this test, enter the following code in the terminal:
    .venv/bin/pip install pytest                                (if not already installed)
    .venv/bin/pip install ollama                                (if not already installed)
    PYTHONPATH=src pytest tests/test_reflection_loop_fail.py -q -s
"""

from walkxr_ai.agents.small_moments_roleplay_agent import SmallMomentsRoleplayAgent


def test_roleplay_agent() -> None:
    # Instantiate the agent
    # Initializes persona/retrieval engine/Ollama model and builds LangGraph initial state
    agent = SmallMomentsRoleplayAgent()

    # Pass deliberately bad LLM response
    bad_response_test = "LOL that sucks"

    # Fail-safe response
    safe_response = "Thank you for sharing your thoughts. Let me take a moment to process the feelings you have shared with me. Are there additional details you would like to discuss?"

    # Run the LangGraph flow and print the final response
    result = agent.get_response(
        user_input="Hello, I am feeling a little nervous today.",
        history=[],
        user_id="test_user",
        session_id="test_session",
        stage="conversation",
        max_retries_allowed = 1,            # Set to 1 so that we can test fail-safe response immediately
        safe_response = safe_response,
        debug_print = True,
        bad_response_test = bad_response_test
    )
    print()
    print(result)
    
    # Basic assertions to verify the structure of the response
    assert isinstance(result, dict)                   # Is the result a dictionary?
    assert "debug" in result                          # Does the debug info exist?
    assert isinstance(result["debug"], dict)          # Is the debug info a dictionary?
    assert result["debug"].get("error") is None       # Do any errors occur?
    assert "response_text" in result                  # Does the response exist?
    assert isinstance(result["response_text"], str)   # Is the response text a string?
    assert result["response_text"].strip()            # Is the response non-empty?
    assert result["response_text"] == safe_response   # Is the final response the fail-safe response?