Skip to content

Commit 32d554e

Browse files
Copilotfriggeri
andcommitted
Add unit tests for unknown/malformed session event handling
Co-authored-by: friggeri <106686+friggeri@users.noreply.github.com>
1 parent c4f5944 commit 32d554e

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

python/test_unknown_events.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""
2+
Test that unknown/malformed session events are handled gracefully.
3+
"""
4+
5+
from datetime import datetime
6+
from uuid import uuid4
7+
8+
import pytest
9+
10+
from copilot import CopilotClient
11+
12+
13+
class TestUnknownEventHandling:
14+
"""Test graceful handling of unknown and malformed session events."""
15+
16+
def test_event_parsing_with_unknown_type(self):
17+
"""Verify that unknown event types map to UNKNOWN enum value."""
18+
from copilot.generated.session_events import SessionEventType, session_event_from_dict
19+
20+
unknown_event = {
21+
"id": str(uuid4()),
22+
"timestamp": datetime.now().isoformat(),
23+
"parentId": None,
24+
"type": "session.completely_new_event_from_future",
25+
"data": {},
26+
}
27+
28+
event = session_event_from_dict(unknown_event)
29+
assert event.type == SessionEventType.UNKNOWN, \
30+
f"Expected UNKNOWN, got {event.type}"
31+
32+
def test_malformed_data_raises_exception(self):
33+
"""Malformed data should raise exceptions (caught by handler)."""
34+
from copilot.generated.session_events import session_event_from_dict
35+
36+
# Bad UUID format
37+
malformed_event = {
38+
"id": "not-a-uuid",
39+
"timestamp": datetime.now().isoformat(),
40+
"parentId": None,
41+
"type": "session.start",
42+
"data": {},
43+
}
44+
45+
with pytest.raises((ValueError, AssertionError)):
46+
session_event_from_dict(malformed_event)
47+
48+
# Bad timestamp format
49+
malformed_event2 = {
50+
"id": str(uuid4()),
51+
"timestamp": "invalid-timestamp",
52+
"parentId": None,
53+
"type": "session.start",
54+
"data": {},
55+
}
56+
57+
with pytest.raises(Exception):
58+
session_event_from_dict(malformed_event2)
59+
60+
def test_handler_catches_parsing_exceptions(self):
61+
"""The notification handler should catch and ignore parsing exceptions."""
62+
from copilot.generated.session_events import session_event_from_dict
63+
64+
events_dispatched = []
65+
66+
def mock_dispatch(event):
67+
events_dispatched.append(event)
68+
69+
# Test 1: Known event should work
70+
known_event = {
71+
"id": str(uuid4()),
72+
"timestamp": datetime.now().isoformat(),
73+
"parentId": None,
74+
"type": "session.start",
75+
"data": {},
76+
}
77+
78+
try:
79+
event = session_event_from_dict(known_event)
80+
mock_dispatch(event)
81+
except Exception as e:
82+
pytest.fail(f"Known event should not raise: {e}")
83+
84+
assert len(events_dispatched) == 1, "Known event should be dispatched"
85+
86+
# Test 2: Unknown event type should use UNKNOWN enum
87+
unknown_event = {
88+
"id": str(uuid4()),
89+
"timestamp": datetime.now().isoformat(),
90+
"parentId": None,
91+
"type": "session.future_unknown_event",
92+
"data": {},
93+
}
94+
95+
try:
96+
event = session_event_from_dict(unknown_event)
97+
mock_dispatch(event)
98+
except Exception as e:
99+
pytest.fail(f"Unknown event should not raise: {e}")
100+
101+
assert len(events_dispatched) == 2, "Unknown event should be dispatched with UNKNOWN type"
102+
103+
# Test 3: Malformed event - simulate what the handler does
104+
malformed_event = {
105+
"id": "not-a-valid-uuid",
106+
"timestamp": datetime.now().isoformat(),
107+
"parentId": None,
108+
"type": "session.start",
109+
"data": {},
110+
}
111+
112+
# This simulates the try-except in the notification handler
113+
try:
114+
event = session_event_from_dict(malformed_event)
115+
mock_dispatch(event)
116+
except Exception:
117+
# Handler catches and returns, event not dispatched
118+
pass
119+
120+
assert len(events_dispatched) == 2, "Malformed event should not be dispatched"
121+

0 commit comments

Comments
 (0)