Spaces:
Sleeping
Sleeping
Download src/gaia/agent/tests/test_integrated_agent.py from jm-vis/Final_Assignment_GAIAAgent: direct link, hf CLI and curl.
- Browser
- Download file 13 kB
-
https://huggingface.co/spaces/jm-vis/Final_Assignment_GAIAAgent/resolve/main/src/gaia/agent/tests/test_integrated_agent.py
- Command line
-
hf download hf://spaces/jm-vis/Final_Assignment_GAIAAgent/src/gaia/agent/tests/test_integrated_agent.py
-
curl -L -o test_integrated_agent.py https://huggingface.co/spaces/jm-vis/Final_Assignment_GAIAAgent/resolve/main/src/gaia/agent/tests/test_integrated_agent.py
13 kB
| """ | |
| 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()) | |
| 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) | |
| 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) | |
| 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) | |
| 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()) | |
| 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() |