Buckets:

download
raw
5.75 kB
"""
ACGS-2 Enhanced Agent Bus Tests - Lifecycle
Constitutional Hash: 608508a9bd224290
Comprehensive test coverage for lifecycle functionality in agent_bus.py.
"""
import uuid
from unittest.mock import AsyncMock, MagicMock
import pytest
try:
from enhanced_agent_bus.agent_bus import (
EnhancedAgentBus,
get_agent_bus,
reset_agent_bus,
)
from enhanced_agent_bus.exceptions import (
BusNotStartedError,
ConstitutionalHashMismatchError,
)
from enhanced_agent_bus.models import (
CONSTITUTIONAL_HASH,
AgentMessage,
MessageStatus,
MessageType,
Priority,
)
from enhanced_agent_bus.validators import ValidationResult
except ImportError:
import sys
sys.path.insert(0, "/home/martin/ACGS")
from enhanced_agent_bus.agent_bus import EnhancedAgentBus
from enhanced_agent_bus.models import (
CONSTITUTIONAL_HASH,
AgentMessage,
MessageType,
Priority,
)
from enhanced_agent_bus.validators import ValidationResult
# Test fixtures
@pytest.fixture
def constitutional_hash():
"""Constitutional hash used throughout tests."""
return CONSTITUTIONAL_HASH
@pytest.fixture
def mock_processor():
"""Mock message processor for testing."""
processor = MagicMock()
processor.initialize = AsyncMock()
processor.shutdown = AsyncMock()
processor.process = AsyncMock(return_value=ValidationResult(is_valid=True))
processor.get_metrics = MagicMock(return_value={"processed": 0})
return processor
@pytest.fixture
def mock_registry():
"""Mock agent registry for testing."""
registry = MagicMock()
registry.initialize = AsyncMock()
registry.shutdown = AsyncMock()
registry.register = MagicMock(return_value=True)
registry.unregister = MagicMock(return_value=True)
registry.get = MagicMock(return_value=None)
return registry
@pytest.fixture
def mock_router():
"""Mock message router for testing."""
router = MagicMock()
router.initialize = AsyncMock()
router.shutdown = AsyncMock()
router.route = AsyncMock()
return router
@pytest.fixture
def mock_validator():
"""Mock validation strategy for testing."""
validator = MagicMock()
validator.initialize = AsyncMock()
validator.shutdown = AsyncMock()
validator.validate = AsyncMock(return_value=(True, None))
return validator
@pytest.fixture
async def agent_bus(mock_processor, mock_registry, mock_router, mock_validator):
"""Create an EnhancedAgentBus for testing."""
bus = EnhancedAgentBus(
use_dynamic_policy=False,
use_kafka=False,
use_redis_registry=False,
enable_metering=False,
enable_rate_limiting=False,
processor=mock_processor,
registry=mock_registry,
router=mock_router,
validator=mock_validator,
)
yield bus
# Cleanup
if bus.is_running:
await bus.stop()
@pytest.fixture
async def started_agent_bus(agent_bus):
"""Create and start an EnhancedAgentBus for testing."""
await agent_bus.start()
yield agent_bus
if agent_bus.is_running:
await agent_bus.stop()
@pytest.fixture
def sample_message(constitutional_hash):
"""Create a sample message for testing."""
return AgentMessage(
message_id=str(uuid.uuid4()),
from_agent="agent-sender",
to_agent="agent-receiver",
message_type=MessageType.GOVERNANCE_REQUEST,
content={"action": "test"},
priority=Priority.MEDIUM,
constitutional_hash=constitutional_hash,
tenant_id="tenant-1",
)
@pytest.fixture
def sample_message_no_tenant(constitutional_hash):
"""Create a sample message without tenant for testing."""
return AgentMessage(
message_id=str(uuid.uuid4()),
from_agent="agent-sender",
to_agent="agent-receiver",
message_type=MessageType.GOVERNANCE_REQUEST,
content={"action": "test"},
priority=Priority.MEDIUM,
constitutional_hash=constitutional_hash,
tenant_id=None,
)
# =============================================================================
# LIFECYCLE TESTS
# =============================================================================
class TestLifecycle:
"""Test EnhancedAgentBus lifecycle management."""
async def test_initial_state_not_running(self, agent_bus):
"""Test that bus starts in non-running state."""
assert agent_bus.is_running is False
async def test_start_sets_running_true(self, agent_bus):
"""Test that start() sets running state to True."""
await agent_bus.start()
assert agent_bus.is_running is True
await agent_bus.stop()
async def test_stop_sets_running_false(self, started_agent_bus):
"""Test that stop() sets running state to False."""
await started_agent_bus.stop()
assert started_agent_bus.is_running is False
async def test_double_start_is_safe(self, agent_bus):
"""Test that calling start() twice is safe (idempotent)."""
await agent_bus.start()
await agent_bus.start() # Should not raise
assert agent_bus.is_running is True
await agent_bus.stop()
async def test_double_stop_is_safe(self, started_agent_bus):
"""Test that calling stop() twice is safe (idempotent)."""
await started_agent_bus.stop()
await started_agent_bus.stop() # Should not raise
assert started_agent_bus.is_running is False
# =============================================================================
# AGENT REGISTRATION TESTS
# =============================================================================

Xet Storage Details

Size:
5.75 kB
·
Xet hash:
9a34c3d35abf9958b92f960256a35132295beaa31b238977489a9637367015c0

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.