Buckets:
| """ | |
| Test suite for CA24 Multi-Agent Systems | |
| مجموعه تست برای سیستمهای چند عاملی CA24 | |
| """ | |
| import pytest | |
| import asyncio | |
| import os | |
| import sys | |
| from unittest.mock import Mock, patch | |
| # Add paths | |
| sys.path.append("./src") | |
| sys.path.append("./config") | |
| class TestGeminiConfig: | |
| """تست پیکربندی Gemini""" | |
| def test_gemini_config_creation(self): | |
| """تست ایجاد پیکربندی Gemini""" | |
| from config.gemini_config import GeminiConfig | |
| config = GeminiConfig( | |
| api_key="test_key", model_name="gemini-1.5-pro", temperature=0.7 | |
| ) | |
| assert config.api_key == "test_key" | |
| assert config.model_name == "gemini-1.5-pro" | |
| assert config.temperature == 0.7 | |
| assert config.max_tokens == 2048 | |
| def test_gemini_config_defaults(self): | |
| """تست مقادیر پیشفرض""" | |
| from config.gemini_config import GeminiConfig | |
| config = GeminiConfig(api_key="test_key") | |
| assert config.model_name == "gemini-1.5-pro" | |
| assert config.temperature == 0.7 | |
| assert config.top_p == 0.9 | |
| assert config.top_k == 40 | |
| class TestMultiAgentSystem: | |
| """تست سیستم چند عاملی""" | |
| async def test_multi_agent_system_initialization(self): | |
| """تست راهاندازی سیستم چند عاملی""" | |
| from config.gemini_config import GeminiConfig | |
| from src.core.multi_agent_system import MultiAgentSystem | |
| config = GeminiConfig(api_key="test_key") | |
| with patch("src.core.multi_agent_system.GeminiManager"): | |
| mas = MultiAgentSystem(config) | |
| await mas.initialize() | |
| assert mas.system_status == "running" | |
| assert len(mas.agents) > 0 | |
| async def test_task_submission(self): | |
| """تست ارسال وظیفه""" | |
| from config.gemini_config import GeminiConfig | |
| from src.core.multi_agent_system import MultiAgentSystem | |
| config = GeminiConfig(api_key="test_key") | |
| with patch("src.core.multi_agent_system.GeminiManager"): | |
| mas = MultiAgentSystem(config) | |
| await mas.initialize() | |
| task_id = await mas.submit_task("test_task", {"param": "value"}) | |
| assert task_id is not None | |
| assert isinstance(task_id, str) | |
| class TestRAGSystem: | |
| """تست سیستم RAG""" | |
| def test_rag_config_creation(self): | |
| """تست ایجاد پیکربندی RAG""" | |
| from src.core.rag_system import RAGConfig | |
| config = RAGConfig(chunk_size=500, chunk_overlap=100, top_k=3) | |
| assert config.chunk_size == 500 | |
| assert config.chunk_overlap == 100 | |
| assert config.top_k == 3 | |
| assert config.vector_store_type == "chroma" | |
| def test_document_processor(self): | |
| """تست پردازشگر اسناد""" | |
| from src.core.rag_system import RAGConfig, DocumentProcessor | |
| config = RAGConfig(chunk_size=100, chunk_overlap=20) | |
| processor = DocumentProcessor(config) | |
| # تست تقسیم متن | |
| test_text = "این یک متن تست است. " * 10 | |
| from langchain_core.documents import Document | |
| docs = [Document(page_content=test_text)] | |
| split_docs = processor.split_documents(docs) | |
| assert len(split_docs) > 1 | |
| assert all(len(doc.page_content) <= config.chunk_size for doc in split_docs) | |
| class TestCommunicationSystem: | |
| """تست سیستم ارتباطی""" | |
| async def test_message_creation(self): | |
| """تست ایجاد پیام""" | |
| from src.core.communication_system import ( | |
| Message, | |
| MessagePriority, | |
| CommunicationProtocol, | |
| ) | |
| message = Message( | |
| sender="agent1", | |
| receiver="agent2", | |
| message_type="test", | |
| content={"data": "test"}, | |
| priority=MessagePriority.HIGH, | |
| ) | |
| assert message.sender == "agent1" | |
| assert message.receiver == "agent2" | |
| assert message.message_type == "test" | |
| assert message.priority == MessagePriority.HIGH | |
| assert message.id is not None | |
| async def test_communication_manager(self): | |
| """تست مدیر ارتباطات""" | |
| from src.core.communication_system import CommunicationManager | |
| comm_manager = CommunicationManager("memory") | |
| # تست ارسال پیام | |
| success = await comm_manager.send_message( | |
| sender="test_sender", | |
| receiver="test_receiver", | |
| message_type="test", | |
| content={"message": "test"}, | |
| ) | |
| assert success is True | |
| async def test_broadcast_message(self): | |
| """تست پیام عمومی""" | |
| from src.core.communication_system import CommunicationManager | |
| comm_manager = CommunicationManager("memory") | |
| success = await comm_manager.broadcast_message( | |
| sender="coordinator", | |
| message_type="announcement", | |
| content={"message": "test broadcast"}, | |
| ) | |
| assert success is True | |
| class TestCoordinationSystem: | |
| """تست سیستم هماهنگی""" | |
| def test_task_creation(self): | |
| """تست ایجاد وظیفه""" | |
| from src.core.coordination_system import Task, TaskStatus | |
| task = Task(description="test task", parameters={"param": "value"}, priority=1) | |
| assert task.description == "test task" | |
| assert task.parameters == {"param": "value"} | |
| assert task.priority == 1 | |
| assert task.status == TaskStatus.PENDING | |
| assert task.id is not None | |
| def test_resource_creation(self): | |
| """تست ایجاد منبع""" | |
| from src.core.coordination_system import Resource, ResourceType | |
| resource = Resource( | |
| resource_type=ResourceType.CPU, | |
| total_capacity=100.0, | |
| available_capacity=80.0, | |
| ) | |
| assert resource.resource_type == ResourceType.CPU | |
| assert resource.total_capacity == 100.0 | |
| assert resource.available_capacity == 80.0 | |
| assert resource.allocated_capacity == 0.0 | |
| def test_resource_allocation(self): | |
| """تست تخصیص منبع""" | |
| from src.core.coordination_system import Resource, ResourceType | |
| resource = Resource( | |
| resource_type=ResourceType.CPU, | |
| total_capacity=100.0, | |
| available_capacity=80.0, | |
| ) | |
| # تست تخصیص موفق | |
| success = resource.allocate(20.0) | |
| assert success is True | |
| assert resource.available_capacity == 60.0 | |
| assert resource.allocated_capacity == 20.0 | |
| # تست تخصیص ناموفق | |
| success = resource.allocate(70.0) | |
| assert success is False | |
| assert resource.available_capacity == 60.0 | |
| assert resource.allocated_capacity == 20.0 | |
| def test_agent_capability(self): | |
| """تست قابلیت Agent""" | |
| from src.core.coordination_system import AgentCapability, ResourceType | |
| capability = AgentCapability( | |
| capability_name="analysis", | |
| proficiency_level=0.8, | |
| supported_task_types=["analysis", "evaluation"], | |
| resource_requirements={ResourceType.CPU: 30.0}, | |
| ) | |
| assert capability.capability_name == "analysis" | |
| assert capability.proficiency_level == 0.8 | |
| assert "analysis" in capability.supported_task_types | |
| assert capability.resource_requirements[ResourceType.CPU] == 30.0 | |
| class TestIntegration: | |
| """تستهای یکپارچگی""" | |
| async def test_full_system_integration(self): | |
| """تست یکپارچگی کامل سیستم""" | |
| from config.gemini_config import GeminiConfig | |
| from src.core.communication_system import CommunicationManager | |
| from src.core.coordination_system import ( | |
| CoordinationManager, | |
| CoordinationStrategy, | |
| ) | |
| # راهاندازی سیستمها | |
| comm_manager = CommunicationManager("memory") | |
| coord_manager = CoordinationManager( | |
| comm_manager, CoordinationStrategy.HIERARCHICAL | |
| ) | |
| with patch.object(coord_manager, "initialize"): | |
| await coord_manager.initialize() | |
| # تست هماهنگی | |
| result = await coord_manager.coordinate_agents( | |
| {"type": "task_distribution", "tasks": [], "strategy": "balanced"} | |
| ) | |
| assert result is not None | |
| # تستهای عملکرد | |
| class TestPerformance: | |
| """تستهای عملکرد""" | |
| async def test_message_processing_performance(self): | |
| """تست عملکرد پردازش پیام""" | |
| from src.core.communication_system import CommunicationManager | |
| import time | |
| comm_manager = CommunicationManager("memory") | |
| # تست ارسال پیامهای متعدد | |
| start_time = time.time() | |
| for i in range(100): | |
| await comm_manager.send_message( | |
| sender=f"sender_{i}", | |
| receiver=f"receiver_{i}", | |
| message_type="test", | |
| content={"index": i}, | |
| ) | |
| end_time = time.time() | |
| processing_time = end_time - start_time | |
| # بررسی اینکه پردازش در زمان معقول انجام شود | |
| assert processing_time < 5.0 # کمتر از 5 ثانیه | |
| def test_memory_usage(self): | |
| """تست استفاده از حافظه""" | |
| import psutil | |
| import os | |
| process = psutil.Process(os.getpid()) | |
| initial_memory = process.memory_info().rss | |
| # ایجاد اشیاء متعدد | |
| from src.core.communication_system import Message | |
| messages = [] | |
| for i in range(1000): | |
| message = Message( | |
| sender=f"sender_{i}", | |
| receiver=f"receiver_{i}", | |
| message_type="test", | |
| content={"data": f"test_data_{i}"}, | |
| ) | |
| messages.append(message) | |
| final_memory = process.memory_info().rss | |
| memory_increase = final_memory - initial_memory | |
| # بررسی اینکه افزایش حافظه معقول باشد (کمتر از 100MB) | |
| assert memory_increase < 100 * 1024 * 1024 | |
| # تستهای خطا | |
| class TestErrorHandling: | |
| """تست مدیریت خطا""" | |
| async def test_invalid_api_key(self): | |
| """تست کلید API نامعتبر""" | |
| from config.gemini_config import GeminiConfig, GeminiManager | |
| config = GeminiConfig(api_key="invalid_key") | |
| with pytest.raises(Exception): | |
| gemini_manager = GeminiManager(config) | |
| async def test_message_processing_error(self): | |
| """تست خطا در پردازش پیام""" | |
| from src.core.communication_system import CommunicationManager | |
| comm_manager = CommunicationManager("memory") | |
| # تست ارسال پیام با دادههای نامعتبر | |
| success = await comm_manager.send_message( | |
| sender="", receiver="", message_type="", content={} | |
| ) | |
| # باید موفق باشد حتی با دادههای نامعتبر | |
| assert success is True | |
| if __name__ == "__main__": | |
| pytest.main([__file__, "-v"]) | |
Xet Storage Details
- Size:
- 11.6 kB
- Xet hash:
- 373674092dc7a33eba56738c3e1bd4f1e01e0f788c7f5920dc806170fc216436
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.