Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /CIRISAI_CIRISAgent /tests /test_initialization_fixes.py
| """ | |
| Unit tests for initialization bug fixes. | |
| Tests cover three fixes: | |
| 1. database_maintenance_service property alias | |
| 2. Communication service initialization order | |
| 3. Removed dead payload code in UpdatedStatusConscience | |
| """ | |
| from unittest.mock import AsyncMock, MagicMock, Mock, patch | |
| import pytest | |
| from ciris_engine.logic.conscience.updated_status_conscience import UpdatedStatusConscience | |
| from ciris_engine.logic.runtime.ciris_runtime import CIRISRuntime | |
| from ciris_engine.schemas.runtime.models import Thought, ThoughtStatus, ThoughtType | |
| class TestDatabaseMaintenanceServiceAlias: | |
| """Test that database_maintenance_service property alias works correctly.""" | |
| def test_database_maintenance_service_alias_returns_maintenance_service(self): | |
| """Test that database_maintenance_service returns the same object as maintenance_service.""" | |
| # Create a mock runtime with service_initializer | |
| runtime = Mock(spec=CIRISRuntime) | |
| mock_service = Mock() | |
| # Mock the service_initializer | |
| runtime.service_initializer = Mock() | |
| runtime.service_initializer.maintenance_service = mock_service | |
| # Use the actual property implementation | |
| type(runtime).maintenance_service = property( | |
| lambda self: self.service_initializer.maintenance_service if self.service_initializer else None | |
| ) | |
| type(runtime).database_maintenance_service = property(lambda self: self.maintenance_service) | |
| # Verify both properties return the same service | |
| assert runtime.maintenance_service is mock_service | |
| assert runtime.database_maintenance_service is mock_service | |
| assert runtime.database_maintenance_service is runtime.maintenance_service | |
| def test_database_maintenance_service_alias_returns_none_when_no_initializer(self): | |
| """Test that database_maintenance_service returns None when service_initializer is None.""" | |
| runtime = Mock(spec=CIRISRuntime) | |
| runtime.service_initializer = None | |
| # Use the actual property implementation | |
| type(runtime).maintenance_service = property( | |
| lambda self: self.service_initializer.maintenance_service if self.service_initializer else None | |
| ) | |
| type(runtime).database_maintenance_service = property(lambda self: self.maintenance_service) | |
| # Verify both properties return None | |
| assert runtime.maintenance_service is None | |
| assert runtime.database_maintenance_service is None | |
| def test_database_maintenance_service_accessible_via_hasattr(self): | |
| """Test that hasattr correctly detects database_maintenance_service property.""" | |
| runtime = Mock(spec=CIRISRuntime) | |
| mock_service = Mock() | |
| runtime.service_initializer = Mock() | |
| runtime.service_initializer.maintenance_service = mock_service | |
| # Use the actual property implementation | |
| type(runtime).maintenance_service = property( | |
| lambda self: self.service_initializer.maintenance_service if self.service_initializer else None | |
| ) | |
| type(runtime).database_maintenance_service = property(lambda self: self.maintenance_service) | |
| # Verify hasattr works correctly (this is what adapter.py uses) | |
| assert hasattr(runtime, "database_maintenance_service") | |
| assert hasattr(runtime, "maintenance_service") | |
| class TestCommunicationServiceInitializationOrder: | |
| """Test that adapter services are registered before components are built.""" | |
| async def test_register_adapter_services_step_exists(self): | |
| """Test that the 'Register Adapter Services' initialization step is registered.""" | |
| # This test verifies the _register_adapter_services method exists and can be called | |
| # The method is now called in Phase 5 right after adapters start | |
| # Just verify the method exists on CIRISRuntime | |
| assert hasattr(CIRISRuntime, "_register_adapter_services") | |
| # Create a mock async function to test | |
| mock_method = AsyncMock() | |
| # Call it to verify it's async | |
| await mock_method() | |
| mock_method.assert_called_once() | |
| async def test_register_adapter_services_before_build_components(self): | |
| """Test that adapter services would be registered before components are built.""" | |
| # This is a simple test that verifies the initialization order by simulating calls | |
| # In the actual code, init_manager.register_step ensures the order: | |
| # 1. Start Adapters (Phase 5) | |
| # 2. Register Adapter Services (Phase 5) <- NEW STEP | |
| # 3. Initialize Maintenance Service (Phase 5) | |
| # 4. Build Components (Phase 6) | |
| call_order = [] | |
| # Mock methods to track call order | |
| async def mock_start_adapters(): | |
| call_order.append("start_adapters") | |
| async def mock_register_adapter_services(): | |
| call_order.append("register_adapter_services") | |
| async def mock_build_components(): | |
| call_order.append("build_components") | |
| # Simulate initialization sequence | |
| await mock_start_adapters() | |
| await mock_register_adapter_services() | |
| await mock_build_components() | |
| # Verify correct order | |
| assert call_order == ["start_adapters", "register_adapter_services", "build_components"] | |
| assert call_order.index("register_adapter_services") < call_order.index("build_components") | |
| class TestUpdatedStatusConscienceNoPayload: | |
| """Test that UpdatedStatusConscience no longer attempts to access thought.payload.""" | |
| def test_no_payload_attribute_access(self): | |
| """Test that the dead payload code has been removed.""" | |
| # Read the updated_status_conscience.py source to verify no payload access | |
| import inspect | |
| from ciris_engine.logic.conscience.updated_status_conscience import UpdatedStatusConscience | |
| source = inspect.getsource(UpdatedStatusConscience) | |
| # Verify the dead code patterns are NOT present | |
| assert 'if hasattr(thought, "payload")' not in source, "Dead payload code should be removed" | |
| assert "thought.payload[" not in source, "Dead payload code should be removed" | |
| assert "has no payload attribute" not in source, "Dead payload warning should be removed" | |
| # Verify the explanatory comment IS present | |
| assert ( | |
| "ConscienceCheckResult.CIRIS_OBSERVATION_UPDATED_STATUS" in source | |
| ), "Explanatory comment about where observation is stored should exist" | |
| def test_conscience_check_stores_observation_in_result(self): | |
| """Test that observation data is stored in ConscienceCheckResult, not thought.payload.""" | |
| # Create test data | |
| thought = Thought( | |
| thought_id="test_thought", | |
| source_task_id="test_task", | |
| created_at="2025-01-01T00:00:00Z", | |
| updated_at="2025-01-01T00:00:00Z", | |
| content="Test content", | |
| status=ThoughtStatus.PENDING, | |
| thought_type=ThoughtType.STANDARD, | |
| ) | |
| # Verify thought does NOT have payload attribute | |
| assert not hasattr(thought, "payload"), "Thought should not have payload attribute" | |
| # The data flow is: observation → ConscienceCheckResult.CIRIS_OBSERVATION_UPDATED_STATUS | |
| # NOT: observation → thought.payload["CIRIS_OBSERVATION_UPDATED_STATUS"] | |
| # This is verified by the source code inspection test above | |
| def test_thought_schema_has_no_payload_field(self): | |
| """Test that the Thought schema does not define a payload field.""" | |
| from ciris_engine.schemas.runtime.models import Thought | |
| # Get all field names from the Thought model | |
| field_names = list(Thought.model_fields.keys()) | |
| # Verify 'payload' is NOT in the field names | |
| assert "payload" not in field_names, "Thought schema should not have payload field" | |
| # Verify expected fields ARE present | |
| expected_fields = ["thought_id", "source_task_id", "content", "status", "thought_type"] | |
| for field in expected_fields: | |
| assert field in field_names, f"Thought schema should have {field} field" | |
| def test_thought_instance_cannot_have_payload_attribute(self): | |
| """Test that Thought instances reject dynamic payload attribute due to extra='forbid'.""" | |
| from pydantic import ValidationError | |
| # Create a valid thought | |
| thought = Thought( | |
| thought_id="test", | |
| source_task_id="task", | |
| created_at="2025-01-01T00:00:00Z", | |
| updated_at="2025-01-01T00:00:00Z", | |
| content="content", | |
| status=ThoughtStatus.PENDING, | |
| thought_type=ThoughtType.STANDARD, | |
| ) | |
| # Verify we cannot set payload attribute (Pydantic extra='forbid') | |
| with pytest.raises(ValidationError): | |
| Thought( | |
| thought_id="test", | |
| source_task_id="task", | |
| created_at="2025-01-01T00:00:00Z", | |
| updated_at="2025-01-01T00:00:00Z", | |
| content="content", | |
| status=ThoughtStatus.PENDING, | |
| thought_type=ThoughtType.STANDARD, | |
| payload={"test": "data"}, # This should fail | |
| ) | |
Xet Storage Details
- Size:
- 9.28 kB
- Xet hash:
- 5cd625ed33176753d2ac0839a7c1d0d5f2ca381b72d94c575cc77d66ad6e4fb2
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.