Skip to content

Commit c5969ba

Browse files
committed
test_context: Add test suite for event contexts
1 parent d01bf08 commit c5969ba

File tree

2 files changed

+258
-0
lines changed

2 files changed

+258
-0
lines changed
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
extends SentryTestSuite
2+
## Test contexts with events.
3+
4+
5+
signal callback_processed
6+
7+
8+
func after_test() -> void:
9+
SentrySDK._unset_before_send()
10+
11+
12+
func test_set_context_basic() -> void:
13+
SentrySDK._set_before_send(func (event: SentryEvent) -> SentryEvent:
14+
var json: String = event.to_json()
15+
16+
# Verify contexts section exists
17+
assert_json(json).at("/").must_contain("contexts").verify()
18+
19+
# Verify our custom gamedev context appears with correct data
20+
assert_json(json).at("/contexts/game_session") \
21+
.must_contain("match_id", "match_abc123") \
22+
.must_contain("game_mode", "battle_royale") \
23+
.must_contain("players_remaining", 15) \
24+
.exactly(1)
25+
26+
callback_processed.emit.call_deferred()
27+
return null
28+
)
29+
30+
SentrySDK.set_context("game_session", {
31+
"match_id": "match_abc123",
32+
"game_mode": "battle_royale",
33+
"players_remaining": 15
34+
})
35+
SentrySDK.capture_event(SentrySDK.create_event())
36+
37+
var monitor := monitor_signals(self, false)
38+
await assert_signal(monitor).is_emitted("callback_processed")
39+
40+
41+
func test_set_context_overwrite() -> void:
42+
SentrySDK._set_before_send(func (event: SentryEvent) -> SentryEvent:
43+
var json: String = event.to_json()
44+
45+
# Verify the context was overwritten with new data
46+
assert_json(json).at("/contexts/game_state") \
47+
.must_contain("level", "forest_2") \
48+
.must_contain("score", 2500) \
49+
.must_not_contain("checkpoint") \
50+
.exactly(1)
51+
52+
callback_processed.emit.call_deferred()
53+
return null
54+
)
55+
56+
# Set initial context
57+
SentrySDK.set_context("game_state", {
58+
"level": "forest_1",
59+
"score": 1000,
60+
"checkpoint": "start"
61+
})
62+
63+
# Overwrite with new data
64+
SentrySDK.set_context("game_state", {
65+
"level": "forest_2",
66+
"score": 2500
67+
})
68+
69+
SentrySDK.capture_event(SentrySDK.create_event())
70+
71+
var monitor := monitor_signals(self, false)
72+
await assert_signal(monitor).is_emitted("callback_processed")
73+
74+
75+
func test_set_context_complex_data() -> void:
76+
SentrySDK._set_before_send(func (event: SentryEvent) -> SentryEvent:
77+
var json: String = event.to_json()
78+
79+
# Verify nested object structure
80+
assert_json(json).at("/contexts/player_data/profile") \
81+
.must_contain("name", "TestPlayer") \
82+
.must_contain("level", 42) \
83+
.must_contain("active", true) \
84+
.exactly(1)
85+
86+
# Verify array contents
87+
assert_json(json).at("/contexts/player_data/inventory") \
88+
.is_array() \
89+
.must_contain("/0", "sword") \
90+
.must_contain("/1", "potion") \
91+
.must_contain("/2", "magic_key") \
92+
.exactly(1)
93+
94+
# Verify mixed types in nested structure
95+
assert_json(json).at("/contexts/player_data/stats") \
96+
.must_contain("health", 85.5) \
97+
.must_contain("mana", 60) \
98+
.must_contain("experience", 1250.75) \
99+
.exactly(1)
100+
101+
callback_processed.emit.call_deferred()
102+
return null
103+
)
104+
105+
SentrySDK.set_context("player_data", {
106+
"profile": {
107+
"name": "TestPlayer",
108+
"level": 42,
109+
"active": true
110+
},
111+
"inventory": ["sword", "potion", "magic_key"],
112+
"stats": {
113+
"health": 85.5,
114+
"mana": 60,
115+
"experience": 1250.75
116+
}
117+
})
118+
119+
SentrySDK.capture_event(SentrySDK.create_event())
120+
121+
var monitor := monitor_signals(self, false)
122+
await assert_signal(monitor).is_emitted("callback_processed")
123+
124+
125+
func test_set_context_edge_cases() -> void:
126+
SentrySDK._set_before_send(func (event: SentryEvent) -> SentryEvent:
127+
var json: String = event.to_json()
128+
129+
# Verify empty context is present
130+
assert_json(json).at("/contexts/") \
131+
.must_contain("empty_context", {}) \
132+
.verify()
133+
134+
# Verify special characters in context data
135+
assert_json(json).at("/contexts/special_chars") \
136+
.must_contain("unicode_text", "Hello 世界! 🚀") \
137+
.must_contain("symbols", "!@#$%^&*()") \
138+
.must_contain("quotes", "He said \"Hello\"") \
139+
.exactly(1)
140+
141+
# Verify boolean and string values
142+
assert_json(json).at("/contexts/edge_data") \
143+
.must_contain("empty_string", "") \
144+
.must_contain("zero_value", 0) \
145+
.must_contain("false_value", false) \
146+
.exactly(1)
147+
148+
callback_processed.emit.call_deferred()
149+
return null
150+
)
151+
152+
# Test empty context
153+
SentrySDK.set_context("empty_context", {})
154+
155+
# Test special characters and Unicode
156+
SentrySDK.set_context("special_chars", {
157+
"unicode_text": "Hello 世界! 🚀",
158+
"symbols": "!@#$%^&*()",
159+
"quotes": "He said \"Hello\""
160+
})
161+
162+
# Test edge values
163+
SentrySDK.set_context("edge_data", {
164+
"empty_string": "",
165+
"zero_value": 0,
166+
"false_value": false
167+
})
168+
169+
SentrySDK.capture_event(SentrySDK.create_event())
170+
171+
var monitor := monitor_signals(self, false)
172+
await assert_signal(monitor).is_emitted("callback_processed")
173+
174+
175+
func test_multiple_contexts_in_single_event() -> void:
176+
SentrySDK._set_before_send(func (event: SentryEvent) -> SentryEvent:
177+
var json: String = event.to_json()
178+
179+
# Verify gameplay context
180+
assert_json(json).at("/contexts/gameplay") \
181+
.must_contain("level", "dungeon_3") \
182+
.must_contain("difficulty", "hard") \
183+
.must_contain("score", 15750) \
184+
.exactly(1)
185+
186+
# Verify player progress context
187+
assert_json(json).at("/contexts/player_progress") \
188+
.must_contain("character_level", 25) \
189+
.must_contain("xp", 45000) \
190+
.must_contain("gold", 1250) \
191+
.exactly(1)
192+
193+
# Verify game settings context
194+
assert_json(json).at("/contexts/game_settings") \
195+
.must_contain("graphics_quality", "high") \
196+
.must_contain("sound_enabled", true) \
197+
.must_contain("language", "en") \
198+
.exactly(1)
199+
200+
callback_processed.emit.call_deferred()
201+
return null
202+
)
203+
204+
# Set multiple different gamedev contexts
205+
SentrySDK.set_context("gameplay", {
206+
"level": "dungeon_3",
207+
"difficulty": "hard",
208+
"score": 15750
209+
})
210+
211+
SentrySDK.set_context("player_progress", {
212+
"character_level": 25,
213+
"xp": 45000,
214+
"gold": 1250
215+
})
216+
217+
SentrySDK.set_context("game_settings", {
218+
"graphics_quality": "high",
219+
"sound_enabled": true,
220+
"language": "en"
221+
})
222+
223+
SentrySDK.capture_event(SentrySDK.create_event())
224+
225+
var monitor := monitor_signals(self, false)
226+
await assert_signal(monitor).is_emitted("callback_processed")
227+
228+
229+
func test_default_contexts_presence() -> void:
230+
SentrySDK._set_before_send(func (event: SentryEvent) -> SentryEvent:
231+
var json: String = event.to_json()
232+
233+
# Test standard contexts presence
234+
assert_json(json).at("/") \
235+
.must_contain("/contexts/app") \
236+
.must_contain("/contexts/culture") \
237+
.must_contain("/contexts/device") \
238+
.must_contain("/contexts/gpu") \
239+
.must_contain("/contexts/os") \
240+
.verify()
241+
242+
# Test Godot contexts presence
243+
assert_json(json).at("/") \
244+
.must_contain("/contexts/godot_engine") \
245+
.must_contain("/contexts/display") \
246+
.must_contain("/contexts/environment") \
247+
.must_contain("/contexts/godot_performance") \
248+
.verify()
249+
250+
callback_processed.emit.call_deferred()
251+
return null
252+
)
253+
254+
SentrySDK.capture_event(SentrySDK.create_event())
255+
256+
var monitor := monitor_signals(self, false)
257+
await assert_signal(monitor).is_emitted("callback_processed")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://bbifbkevlscdi

0 commit comments

Comments
 (0)