File size: 12,990 Bytes
460ec88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
Tests for the integrated GAIA agent.

These tests verify that all components of the integrated agent work correctly
and that the agent correctly handles all types of questions in the GAIA assessment.
"""

import os
import sys
import unittest
from unittest.mock import patch, MagicMock
import logging

# Add parent directory to path to import the module
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# Import the integrated agent
from agent_integrated import GAIAIntegratedAgent

# Disable logging for tests
logging.disable(logging.CRITICAL)

class TestIntegratedAgent(unittest.TestCase):
    """Test cases for the integrated GAIA agent."""
    
    def setUp(self):
        """Set up the test environment."""
        # Use a minimal configuration for testing
        self.test_config = {
            "verbose": True,
            "memory": {
                "use_supabase": False,
                "cache_enabled": True
            }
        }
        
        # Create the agent
        self.agent = GAIAIntegratedAgent(self.test_config)
    
    def tearDown(self):
        """Clean up after tests."""
        if hasattr(self, 'agent'):
            self.agent.reset()
    
    def test_agent_initialization(self):
        """Test that the agent initializes correctly."""
        self.assertTrue(self.agent.state["initialized"])
        self.assertIsNotNone(self.agent.multimodal_processor)
        self.assertIsNotNone(self.agent.search_manager)
        self.assertIsNotNone(self.agent.memory_manager)
        self.assertIsNotNone(self.agent.text_analyzer)
        self.assertIsNotNone(self.agent.tools_registry)
    
    def test_reversed_text_questions(self):
        """Test that the agent correctly handles reversed text questions."""
        questions = [
            "What does this reversed text say: ELPPA",
            "Decode the following: .ANANAB si sihT"
        ]
        
        for question in questions:
            with self.subTest(question=question):
                answer = self.agent.process_question(question)
                self.assertIsNotNone(answer)
                self.assertTrue(len(answer) > 0)
                
                # Check for expected phrases in the answer
                if "ELPPA" in question:
                    self.assertIn("APPLE", answer.upper())
                if "ANANAB" in question:
                    self.assertIn("BANANA", answer.upper())
    
    def test_unscramble_word_questions(self):
        """Test that the agent correctly handles word unscrambling questions."""
        questions = [
            "Unscramble this word: ELPPA",
            "What is the correct spelling of ANANAB?"
        ]
        
        for question in questions:
            with self.subTest(question=question):
                answer = self.agent.process_question(question)
                self.assertIsNotNone(answer)
                self.assertTrue(len(answer) > 0)
                
                # Check for expected phrases in the answer
                if "ELPPA" in question:
                    self.assertIn("APPLE", answer.upper())
                if "ANANAB" in question:
                    self.assertIn("BANANA", answer.upper())
    
    @patch('src.gaia.agent.components.video_analyzer.VideoAnalyzer.analyze_video_content')
    def test_youtube_video_questions(self, mock_analyze):
        """Test that the agent correctly handles YouTube video questions."""
        # Mock the video analyzer to return a known result
        mock_analyze.return_value = {
            "success": True,
            "content": "The video shows 3 bird species.",
            "metadata": {
                "title": "Bird Video",
                "duration": 120
            }
        }
        
        questions = [
            "In this YouTube video https://www.youtube.com/watch?v=dQw4w9WgXcQ, how many bird species are shown?",
            "What is the content of this video: https://youtu.be/dQw4w9WgXcQ"
        ]
        
        for question in questions:
            with self.subTest(question=question):
                answer = self.agent.process_question(question)
                self.assertIsNotNone(answer)
                self.assertTrue(len(answer) > 0)
                self.assertIn("3", answer)
    
    @patch('src.gaia.agent.components.search_manager.SearchManager.search')
    def test_factual_questions(self, mock_search):
        """Test that the agent correctly handles factual questions."""
        # Mock the search manager to return a known result
        mock_search.return_value = {
            "success": True,
            "answer": "Paris is the capital of France.",
            "sources": ["Wikipedia"]
        }
        
        questions = [
            "What is the capital of France?",
            "Who wrote the novel '1984'?"
        ]
        
        for question in questions:
            with self.subTest(question=question):
                answer = self.agent.process_question(question)
                self.assertIsNotNone(answer)
                self.assertTrue(len(answer) > 0)
                if "France" in question:
                    self.assertIn("Paris", answer)
    
    @patch('src.gaia.agent.graph.run_agent_graph')
    def test_complex_questions(self, mock_graph):
        """Test that the agent correctly handles complex questions using LangGraph."""
        # Mock the graph to return a known result
        mock_graph.return_value = {
            "answer": "The answer requires multiple steps of reasoning.",
            "reasoning": "Step 1...\nStep 2...\nStep 3...",
            "tool_results": []
        }
        
        questions = [
            "Compare and contrast renewable and non-renewable energy sources.",
            "What are the implications of quantum computing for cryptography?"
        ]
        
        for question in questions:
            with self.subTest(question=question):
                answer = self.agent.process_question(question)
                self.assertIsNotNone(answer)
                self.assertTrue(len(answer) > 0)
    
    @patch('src.gaia.agent.components.image_analyzer.ImageAnalyzer.process_image')
    def test_image_questions(self, mock_process):
        """Test that the agent correctly handles image analysis questions."""
        # Mock the image analyzer to return a known result
        mock_process.return_value = {
            "success": True,
            "description": "The image shows a mountain landscape.",
            "elements": ["mountain", "sky", "trees"],
            "analysis_type": "general"
        }
        
        questions = [
            "Analyze this image: /path/to/image.jpg",
            "What is shown in this picture: /path/to/photo.png?"
        ]
        
        for question in questions:
            with self.subTest(question=question):
                # Patch os.path.exists to return True for our test path
                with patch('os.path.exists', return_value=True):
                    answer = self.agent.process_question(question)
                    self.assertIsNotNone(answer)
                    self.assertTrue(len(answer) > 0)
                    self.assertIn("mountain", answer.lower())
    
    @patch('src.gaia.agent.components.audio_analyzer.AudioAnalyzer.process_audio')
    def test_audio_questions(self, mock_process):
        """Test that the agent correctly handles audio analysis questions."""
        # Mock the audio analyzer to return a known result
        mock_process.return_value = {
            "success": True,
            "transcription": "This is a test audio recording.",
            "audio_type": "speech",
            "speakers": ["Speaker 1"]
        }
        
        questions = [
            "Transcribe this audio file: /path/to/recording.mp3",
            "What is said in this audio: /path/to/audio.wav?"
        ]
        
        for question in questions:
            with self.subTest(question=question):
                # Patch os.path.exists to return True for our test path
                with patch('os.path.exists', return_value=True):
                    answer = self.agent.process_question(question)
                    self.assertIsNotNone(answer)
                    self.assertTrue(len(answer) > 0)
                    self.assertIn("test audio", answer.lower())
    
    def test_error_handling(self):
        """Test that the agent correctly handles errors."""
        # Create a scenario that would cause an error in processing
        with patch.object(self.agent, '_initialize_components', side_effect=RuntimeError("Test error")):
            with self.assertRaises(RuntimeError):
                agent = GAIAIntegratedAgent(self.test_config)
        
        # Test graceful error handling in process_question
        with patch.object(self.agent.text_analyzer, 'process_text_question', side_effect=Exception("Test error")):
            answer = self.agent.process_question("What does this reversed text say: ELPPA")
            self.assertIsNotNone(answer)
            self.assertTrue(len(answer) > 0)
            # Check that we got an error message (since verbose=True in test_config)
            self.assertIn("Error", answer)
    
    def test_memory_caching(self):
        """Test that the agent correctly caches and retrieves answers."""
        # First process a question to cache the answer
        question = "What is 2+2?"
        
        # Mock the memory manager's methods
        with patch.object(self.agent.memory_manager, 'get_cached_answer', return_value=None):
            with patch.object(self.agent.memory_manager, 'cache_question_answer') as mock_cache:
                answer = self.agent.process_question(question)
                self.assertIsNotNone(answer)
                # Verify the cache was called
                mock_cache.assert_called_once()
        
        # Now process the same question again and verify it uses the cache
        with patch.object(self.agent.memory_manager, 'get_cached_answer', return_value="The answer is 4.") as mock_get:
            answer = self.agent.process_question(question)
            self.assertEqual(answer, "The answer is 4.")
            # Verify the cache was checked
            mock_get.assert_called_once()
    
    def test_evaluation_questions(self):
        """Test the agent against mock evaluation questions."""
        # Sample evaluation questions
        evaluation_questions = [
            "What is the capital of France?",  # Factual
            "What does this reversed text say: ELPPA",  # Reversed text
            "Unscramble this word: ANANAB",  # Word unscrambling
            "In this YouTube video https://www.youtube.com/watch?v=dQw4w9WgXcQ, what's happening?",  # Video
            "How many bird species are visible in this YouTube video: https://youtu.be/dQw4w9WgXcQ?",  # Assessment question
            "How many studio albums did Mercedes Sosa release between 2000 and 2009?",  # Assessment question
            "What's shown in this image: /path/to/image.jpg?",  # Image analysis
            "What is the content of this audio file: /path/to/audio.mp3?",  # Audio analysis
            "What is the position evaluation of this chess position: /path/to/chess.png?",  # Chess analysis
            "Compare and contrast renewable and non-renewable energy sources."  # Complex reasoning
        ]
        
        # Setup necessary mocks
        with patch('src.gaia.agent.components.video_analyzer.VideoAnalyzer.analyze_video_content', 
                  return_value={"success": True, "content": "Birds flying"}), \
             patch('src.gaia.agent.components.image_analyzer.ImageAnalyzer.process_image',
                  return_value={"success": True, "description": "An image"}), \
             patch('src.gaia.agent.components.audio_analyzer.AudioAnalyzer.process_audio',
                  return_value={"success": True, "transcription": "Audio content"}), \
             patch('os.path.exists', return_value=True), \
             patch('src.gaia.agent.components.search_manager.SearchManager.search',
                  return_value={"success": True, "answer": "A factual answer"}), \
             patch('src.gaia.agent.graph.run_agent_graph',
                  return_value={"answer": "A complex answer"}):
            
            # Test each question
            for question in evaluation_questions:
                with self.subTest(question=question):
                    answer = self.agent.process_question(question)
                    self.assertIsNotNone(answer)
                    self.assertTrue(len(answer) > 0)
                    # Check for specific content in assessment questions
                    if "Mercedes Sosa" in question:
                        self.assertIn("7", answer)
                    if "bird species" in question and "YouTube" in question:
                        self.assertIn("3", answer)


if __name__ == "__main__":
    unittest.main()