""" Test that unknown event types are handled gracefully for forward compatibility. This test verifies that: 1. The session.usage_info event type is recognized 2. Unknown future event types map to UNKNOWN enum value 3. Real parsing errors (malformed data) are NOT suppressed and surface for visibility """ from datetime import datetime from uuid import uuid4 import pytest from copilot.generated.session_events import ( Data, ElicitationCompletedAction, ElicitationRequestedMode, ElicitationRequestedSchema, PermissionRequest, PermissionRequestMemoryAction, SessionEventType, SessionTaskCompleteData, UserMessageAgentMode, UserMessageAttachmentGithubReferenceType, session_event_from_dict, ) class TestEventForwardCompatibility: """Test forward compatibility for unknown event types.""" def test_session_usage_info_is_recognized(self): """The session.usage_info event type should be in the enum.""" assert SessionEventType.SESSION_USAGE_INFO.value == "session.usage_info" def test_unknown_event_type_maps_to_unknown(self): """Unknown event types should map to UNKNOWN enum value for forward compatibility.""" unknown_event = { "id": str(uuid4()), "timestamp": datetime.now().isoformat(), "parentId": None, "type": "session.future_feature_from_server", "data": {}, } event = session_event_from_dict(unknown_event) assert event.type == SessionEventType.UNKNOWN, f"Expected UNKNOWN, got {event.type}" def test_malformed_uuid_raises_error(self): """Malformed UUIDs should raise ValueError for visibility, not be suppressed.""" malformed_event = { "id": "not-a-valid-uuid", "timestamp": datetime.now().isoformat(), "parentId": None, "type": "session.start", "data": {}, } # This should raise an error and NOT be silently suppressed with pytest.raises(ValueError): session_event_from_dict(malformed_event) def test_malformed_timestamp_raises_error(self): """Malformed timestamps should raise an error for visibility.""" malformed_event = { "id": str(uuid4()), "timestamp": "not-a-valid-timestamp", "parentId": None, "type": "session.start", "data": {}, } # This should raise an error and NOT be silently suppressed with pytest.raises((ValueError, TypeError)): session_event_from_dict(malformed_event) def test_explicit_generated_symbols_remain_available(self): """Explicit generated helper symbols should remain importable.""" assert ElicitationCompletedAction.ACCEPT.value == "accept" assert UserMessageAgentMode.INTERACTIVE.value == "interactive" assert ElicitationRequestedMode.FORM.value == "form" assert UserMessageAttachmentGithubReferenceType.PR.value == "pr" schema = ElicitationRequestedSchema( properties={"answer": {"type": "string"}}, type="object" ) assert schema.to_dict()["type"] == "object" def test_data_shim_preserves_raw_mapping_values(self): """Compatibility Data should keep arbitrary nested mappings as plain dicts.""" parsed = Data.from_dict( { "arguments": {"toolCallId": "call-1"}, "input": {"step_name": "build"}, } ) assert parsed.arguments == {"toolCallId": "call-1"} assert isinstance(parsed.arguments, dict) assert parsed.input == {"step_name": "build"} assert isinstance(parsed.input, dict) constructed = Data(arguments={"tool_call_id": "call-1"}) assert constructed.to_dict() == {"arguments": {"tool_call_id": "call-1"}} def test_schema_defaults_are_applied_for_missing_optional_fields(self): """Generated event models should honor primitive schema defaults during parsing.""" request = PermissionRequest.from_dict({"kind": "memory", "fact": "remember this"}) assert request.action == PermissionRequestMemoryAction.STORE task_complete = SessionTaskCompleteData.from_dict({"success": True}) assert task_complete.summary == ""