File size: 9,989 Bytes
4ff8224
401799d
4ff8224
 
401799d
 
4ff8224
 
401799d
 
 
4ff8224
 
401799d
4ff8224
 
 
 
 
401799d
4ff8224
 
 
401799d
4ff8224
 
401799d
4ff8224
 
 
401799d
4ff8224
 
 
 
 
401799d
4ff8224
401799d
4ff8224
 
401799d
4ff8224
 
401799d
4ff8224
 
401799d
4ff8224
 
 
 
401799d
4ff8224
 
 
 
401799d
4ff8224
 
 
401799d
4ff8224
 
401799d
 
 
4ff8224
401799d
 
4ff8224
 
 
 
401799d
4ff8224
 
 
 
 
401799d
4ff8224
 
 
 
 
 
 
 
 
 
401799d
4ff8224
 
 
 
401799d
4ff8224
401799d
 
 
 
 
 
 
 
 
4ff8224
401799d
 
 
 
 
4ff8224
 
401799d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ff8224
 
401799d
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import logging

import pytest
import requests
from langgraph.types import Command

from agent import AgentRunner

# Configure test logger
test_logger = logging.getLogger("test_agent")
test_logger.setLevel(logging.INFO)

# Suppress specific warnings
pytestmark = pytest.mark.filterwarnings("ignore::DeprecationWarning:httpx._models")

# Constants
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
QUESTIONS_URL = f"{DEFAULT_API_URL}/questions"


@pytest.fixture(scope="session")
def agent():
    """Fixture to create and return an AgentRunner instance."""
    test_logger.info("Creating AgentRunner instance")
    return AgentRunner()


# @pytest.fixture(scope="session")
# def questions_data():
#     """Fixture to fetch questions from the API."""
#     test_logger.info(f"Fetching questions from: {QUESTIONS_URL}")
#     try:
#         response = requests.get(QUESTIONS_URL, timeout=15)
#         response.raise_for_status()
#         data = response.json()
#         if not data:
#             test_logger.error("Fetched questions list is empty.")
#             return []
#         test_logger.info(f"Fetched {len(data)} questions.")
#         return data
#     except requests.exceptions.RequestException as e:
#         test_logger.error(f"Error fetching questions: {e}")
#         return []
#     except requests.exceptions.JSONDecodeError as e:
#         test_logger.error(f"Error decoding JSON response from questions endpoint: {e}")
#         return []
#     except Exception as e:
#         test_logger.error(f"An unexpected error occurred fetching questions: {e}")
#         return []
#
# class TestAppQuestions:
#     """Test cases for questions from the app."""
#
#     def test_first_app_question(self, agent, questions_data):
#         """Test the agent's response to the first app question."""
#         if not questions_data:
#             pytest.skip("No questions available from API")
#
#         first_question = questions_data[0]
#         question_text = first_question.get("question")
#         task_id = first_question.get("task_id")
#
#         if not question_text or not task_id:
#             pytest.skip("First question is missing required fields")
#
#         test_logger.info(f"Testing with app question: {question_text}")
#
#         response = agent(question_text)
#         test_logger.info(f"Agent response: {response}")
#
#         # Check that the response contains the expected information
#         assert "Mercedes Sosa" in response, "Response should mention Mercedes Sosa"
#         assert "studio albums" in response.lower(), "Response should mention studio albums"
#         assert "2000" in response and "2009" in response, "Response should mention the year range"
#
#         # Verify that a number is mentioned (either as word or digit)
#         import re
#         number_pattern = r'\b(one|two|three|four|five|six|seven|eight|nine|ten|\d+)\b'
#         has_number = bool(re.search(number_pattern, response.lower()))
#         assert has_number, "Response should include the number of albums"
#
#         # Check for album names in the response
#         known_albums = [
#             "Corazón Libre",
#             "Cantora",
#             "Hermano",
#             "Acústico",
#             "Argentina quiere cantar"
#         ]
#         found_albums = [album for album in known_albums if album in response]
#         assert len(found_albums) > 0, "Response should mention at least some of the known albums"
#
#         # Check for a structured response
#         assert re.search(r'\d+\.\s+[^(]+\(\d{4}\)', response), \
#             "Response should list albums with years"


class TestBasicCodeAgentCapabilities:
    """Test basic capabilities of the code agent."""

    def setup_method(self):
        """Setup method to initialize the agent before each test."""
        test_logger.info("Creating AgentRunner instance")
        self.agent = AgentRunner()

    def test_simple_math_calculation_with_steps(self):
        """Test that the agent can perform basic math calculations and log steps."""
        question = "What is the result of the following operation: 5 + 3 + 1294.678?"
        test_logger.info(f"Testing math calculation with question: {question}")

        # Run the agent and get the response
        response = self.agent(question)

        # Verify the response contains the correct result
        expected_result = str(5 + 3 + 1294.678)
        assert (
            expected_result in response
        ), f"Response should contain the result {expected_result}"

        # Verify step logs exist and have required fields
        assert self.agent.last_state is not None, "Agent should store last state"
        assert "step_logs" in self.agent.last_state, "State should contain step_logs"
        assert (
            len(self.agent.last_state["step_logs"]) > 0
        ), "Should have at least one step logged"

        # Verify each step has required fields
        for step in self.agent.last_state["step_logs"]:
            assert "step_number" in step, "Each step should have a step_number"
            assert any(
                key in step for key in ["thought", "code", "observation"]
            ), "Each step should have at least one of: thought, code, or observation"

        # Verify the final answer is indicated
        assert (
            "final_answer" in response.lower()
        ), "Response should indicate it's providing an answer"

    def test_document_qa_and_image_generation_with_steps(self):
        """Test that the agent can search for information and generate images, with step logging."""
        question = (
            "Search for information about the Mona Lisa and generate an image of it."
        )
        test_logger.info(
            f"Testing document QA and image generation with question: {question}"
        )

        # Run the agent and get the response
        response = self.agent(question)

        # Verify the response contains both search and image generation
        assert "mona lisa" in response.lower(), "Response should mention Mona Lisa"
        assert "image" in response.lower(), "Response should mention image generation"

        # Verify step logs exist and show logical progression
        assert self.agent.last_state is not None, "Agent should store last state"
        assert "step_logs" in self.agent.last_state, "State should contain step_logs"
        assert (
            len(self.agent.last_state["step_logs"]) > 1
        ), "Should have multiple steps logged"

        # Verify steps show logical progression
        steps = self.agent.last_state["step_logs"]
        search_steps = [step for step in steps if "search" in str(step).lower()]
        image_steps = [step for step in steps if "image" in str(step).lower()]

        assert len(search_steps) > 0, "Should have search steps"
        assert len(image_steps) > 0, "Should have image generation steps"

        # Verify each step has required fields
        for step in steps:
            assert "step_number" in step, "Each step should have a step_number"
            assert any(
                key in step for key in ["thought", "code", "observation"]
            ), "Each step should have at least one of: thought, code, or observation"


def test_simple_math_calculation_with_steps():
    """Test that the agent can perform a simple math calculation and verify intermediate steps."""
    agent = AgentRunner()
    question = "What is the result of the following operation: 5 + 3 + 1294.678?"

    # Process the question
    response = agent(question)

    # Verify step logs exist and have required fields
    assert agent.last_state is not None, "Last state should be stored"
    step_logs = agent.last_state.get("step_logs", [])
    assert len(step_logs) > 0, "Should have recorded step logs"

    for step in step_logs:
        assert "step_number" in step, "Each step should have a step number"
        assert any(
            key in step for key in ["thought", "code", "observation"]
        ), "Each step should have at least one of thought/code/observation"

    # Verify final answer
    expected_result = 1302.678
    assert (
        str(expected_result) in response
    ), f"Response should contain the result {expected_result}"
    assert (
        "final_answer" in response.lower()
    ), "Response should indicate it's using final_answer"


def test_document_qa_and_image_generation_with_steps():
    """Test document QA and image generation with step verification."""
    agent = AgentRunner()
    question = "Can you search for information about the Mona Lisa and generate an image inspired by it?"

    # Process the question
    response = agent(question)

    # Verify step logs exist and demonstrate logical progression
    assert agent.last_state is not None, "Last state should be stored"
    step_logs = agent.last_state.get("step_logs", [])
    assert len(step_logs) > 0, "Should have recorded step logs"

    # Check for search and image generation steps
    has_search_step = False
    has_image_step = False

    for step in step_logs:
        assert "step_number" in step, "Each step should have a step number"
        assert any(
            key in step for key in ["thought", "code", "observation"]
        ), "Each step should have at least one of thought/code/observation"

        # Look for search and image steps in thoughts or code
        step_content = str(step.get("thought", "")) + str(step.get("code", ""))
        if "search" in step_content.lower():
            has_search_step = True
        if "image" in step_content.lower() or "dalle" in step_content.lower():
            has_image_step = True

    assert has_search_step, "Should include a search step"
    assert has_image_step, "Should include an image generation step"
    assert (
        "final_answer" in response.lower()
    ), "Response should indicate it's using final_answer"


if __name__ == "__main__":
    pytest.main([__file__, "-s", "-v", "-x"])