-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add tests to verify forward compatibility for unknown session event types #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SteveSandersonMS
merged 7 commits into
main
from
copilot/fix-valueerror-session-usage-info
Jan 19, 2026
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
014f926
Initial plan
Copilot c4f5944
Add graceful error handling for unknown/malformed session events in P…
Copilot 32d554e
Add unit tests for unknown/malformed session event handling
Copilot 5b1c175
Improve test specificity for exception types
Copilot 39d47dc
Revert broad error suppression - let parsing errors surface for visib…
Copilot e1cc9a7
Use specific exception types in test instead of broad Exception
Copilot dc88aef
Run Python formatter (ruff format)
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| """ | ||
| Test that unknown/malformed session events are handled gracefully. | ||
| """ | ||
|
|
||
| from datetime import datetime | ||
| from uuid import uuid4 | ||
|
|
||
| import pytest | ||
|
|
||
| from copilot import CopilotClient | ||
|
|
||
|
|
||
| class TestUnknownEventHandling: | ||
| """Test graceful handling of unknown and malformed session events.""" | ||
|
|
||
| def test_event_parsing_with_unknown_type(self): | ||
| """Verify that unknown event types map to UNKNOWN enum value.""" | ||
| from copilot.generated.session_events import SessionEventType, session_event_from_dict | ||
|
|
||
| unknown_event = { | ||
| "id": str(uuid4()), | ||
| "timestamp": datetime.now().isoformat(), | ||
| "parentId": None, | ||
| "type": "session.completely_new_event_from_future", | ||
| "data": {}, | ||
| } | ||
|
|
||
| event = session_event_from_dict(unknown_event) | ||
| assert event.type == SessionEventType.UNKNOWN, \ | ||
| f"Expected UNKNOWN, got {event.type}" | ||
|
|
||
| def test_malformed_data_raises_exception(self): | ||
| """Malformed data should raise exceptions (caught by handler).""" | ||
| from copilot.generated.session_events import session_event_from_dict | ||
|
|
||
| # Bad UUID format | ||
| malformed_event = { | ||
| "id": "not-a-uuid", | ||
| "timestamp": datetime.now().isoformat(), | ||
| "parentId": None, | ||
| "type": "session.start", | ||
| "data": {}, | ||
| } | ||
|
|
||
| with pytest.raises((ValueError, AssertionError)): | ||
| session_event_from_dict(malformed_event) | ||
|
|
||
| # Bad timestamp format | ||
| malformed_event2 = { | ||
| "id": str(uuid4()), | ||
| "timestamp": "invalid-timestamp", | ||
| "parentId": None, | ||
| "type": "session.start", | ||
| "data": {}, | ||
| } | ||
|
|
||
| with pytest.raises(Exception): | ||
| session_event_from_dict(malformed_event2) | ||
|
|
||
| def test_handler_catches_parsing_exceptions(self): | ||
| """The notification handler should catch and ignore parsing exceptions.""" | ||
| from copilot.generated.session_events import session_event_from_dict | ||
|
|
||
| events_dispatched = [] | ||
|
|
||
| def mock_dispatch(event): | ||
| events_dispatched.append(event) | ||
|
|
||
| # Test 1: Known event should work | ||
| known_event = { | ||
| "id": str(uuid4()), | ||
| "timestamp": datetime.now().isoformat(), | ||
| "parentId": None, | ||
| "type": "session.start", | ||
| "data": {}, | ||
| } | ||
|
|
||
| try: | ||
| event = session_event_from_dict(known_event) | ||
| mock_dispatch(event) | ||
| except Exception as e: | ||
| pytest.fail(f"Known event should not raise: {e}") | ||
|
|
||
| assert len(events_dispatched) == 1, "Known event should be dispatched" | ||
|
|
||
| # Test 2: Unknown event type should use UNKNOWN enum | ||
| unknown_event = { | ||
| "id": str(uuid4()), | ||
| "timestamp": datetime.now().isoformat(), | ||
| "parentId": None, | ||
| "type": "session.future_unknown_event", | ||
| "data": {}, | ||
| } | ||
|
|
||
| try: | ||
| event = session_event_from_dict(unknown_event) | ||
| mock_dispatch(event) | ||
| except Exception as e: | ||
| pytest.fail(f"Unknown event should not raise: {e}") | ||
|
|
||
| assert len(events_dispatched) == 2, "Unknown event should be dispatched with UNKNOWN type" | ||
|
|
||
| # Test 3: Malformed event - simulate what the handler does | ||
| malformed_event = { | ||
| "id": "not-a-valid-uuid", | ||
| "timestamp": datetime.now().isoformat(), | ||
| "parentId": None, | ||
| "type": "session.start", | ||
| "data": {}, | ||
| } | ||
|
|
||
| # This simulates the try-except in the notification handler | ||
| try: | ||
| event = session_event_from_dict(malformed_event) | ||
| mock_dispatch(event) | ||
| except Exception: | ||
| # Handler catches and returns, event not dispatched | ||
| pass | ||
|
|
||
| assert len(events_dispatched) == 2, "Malformed event should not be dispatched" | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.