-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_events_bus.py
More file actions
545 lines (447 loc) · 15.4 KB
/
Copy pathtest_events_bus.py
File metadata and controls
545 lines (447 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
"""Tests for the event bus module (REQ-SPEC-001)."""
from __future__ import annotations
import sys
from pathlib import Path
from typing import List
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from runtime.events.bus import ( # noqa: E402
CallbackHandler,
EventBus,
EventFilter,
EventHandler,
get_default_bus,
reset_default_bus,
)
from runtime.events.handlers import ( # noqa: E402
AggregatingHandler,
CompositeHandler,
FilteringHandler,
)
from runtime.events.types import EVENT_CATEGORIES, BusEvent, EventType # noqa: E402
class TestBusEvent:
def test_create_event(self) -> None:
event = BusEvent(
event_type=EventType.WORKFLOW_STARTED.value,
run_id="run-123",
timestamp="2026-01-04T10:00:00-05:00",
payload={"module": "bmm", "workflow": "prd"},
step_id="step-01",
source="engine",
)
assert event.event_type == "WorkflowStarted"
assert event.run_id == "run-123"
assert event.step_id == "step-01"
assert event.payload["module"] == "bmm"
def test_event_to_dict(self) -> None:
event = BusEvent(
event_type="TestEvent",
run_id="run-456",
timestamp="2026-01-04T10:00:00-05:00",
payload={"key": "value"},
)
data = event.to_dict()
assert data["event_type"] == "TestEvent"
assert data["run_id"] == "run-456"
assert data["payload"] == {"key": "value"}
assert "step_id" not in data # None values excluded
def test_event_from_dict(self) -> None:
data = {
"event_type": "TestEvent",
"run_id": "run-789",
"timestamp": "2026-01-04T10:00:00-05:00",
"payload": {"test": True},
"step_id": "step-02",
"source": "test",
}
event = BusEvent.from_dict(data)
assert event.event_type == "TestEvent"
assert event.step_id == "step-02"
assert event.payload["test"] is True
class TestEventFilter:
def test_filter_by_event_type(self) -> None:
filter = EventFilter.for_types("WorkflowStarted", "WorkflowCompleted")
event1 = BusEvent(
event_type="WorkflowStarted",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
event2 = BusEvent(
event_type="WorkflowFailed",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
assert filter.matches(event1) is True
assert filter.matches(event2) is False
def test_filter_by_category(self) -> None:
filter = EventFilter.for_categories("gate")
gate_event = BusEvent(
event_type="HumanGateRequired",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
step_event = BusEvent(
event_type="WorkflowStepStarted",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
assert filter.matches(gate_event) is True
assert filter.matches(step_event) is False
def test_filter_by_run_id(self) -> None:
filter = EventFilter.for_run("run-specific")
event1 = BusEvent(
event_type="TestEvent",
run_id="run-specific",
timestamp="2026-01-04T10:00:00-05:00",
)
event2 = BusEvent(
event_type="TestEvent",
run_id="run-other",
timestamp="2026-01-04T10:00:00-05:00",
)
assert filter.matches(event1) is True
assert filter.matches(event2) is False
def test_filter_by_source(self) -> None:
filter = EventFilter(sources={"plugin", "guardrail"})
event1 = BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
source="plugin",
)
event2 = BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
source="engine",
)
assert filter.matches(event1) is True
assert filter.matches(event2) is False
def test_empty_filter_matches_all(self) -> None:
filter = EventFilter()
event = BusEvent(
event_type="AnyEvent",
run_id="any-run",
timestamp="2026-01-04T10:00:00-05:00",
)
assert filter.matches(event) is True
class TestEventBus:
def test_subscribe_and_publish(self) -> None:
bus = EventBus()
received: List[BusEvent] = []
def handler(event: BusEvent) -> None:
received.append(event)
bus.subscribe_callback(handler)
event = BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
delivered = bus.publish(event)
assert delivered == 1
assert len(received) == 1
assert received[0].event_type == "TestEvent"
def test_subscribe_with_filter(self) -> None:
bus = EventBus()
received: List[BusEvent] = []
def handler(event: BusEvent) -> None:
received.append(event)
filter = EventFilter.for_types("TargetEvent")
bus.subscribe_callback(handler, filter)
bus.publish(
BusEvent(
event_type="OtherEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
)
bus.publish(
BusEvent(
event_type="TargetEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
)
assert len(received) == 1
assert received[0].event_type == "TargetEvent"
def test_unsubscribe(self) -> None:
bus = EventBus()
received: List[BusEvent] = []
def handler(event: BusEvent) -> None:
received.append(event)
sub_id = bus.subscribe_callback(handler)
event = BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
bus.publish(event)
assert len(received) == 1
result = bus.unsubscribe(sub_id)
assert result is True
bus.publish(event)
assert len(received) == 1 # No new events
def test_unsubscribe_unknown_returns_false(self) -> None:
bus = EventBus()
result = bus.unsubscribe("unknown-id")
assert result is False
def test_pause_and_resume(self) -> None:
bus = EventBus()
received: List[BusEvent] = []
def handler(event: BusEvent) -> None:
received.append(event)
bus.subscribe_callback(handler)
event = BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
bus.pause()
delivered = bus.publish(event)
assert delivered == 0
assert len(received) == 0
bus.resume()
delivered = bus.publish(event)
assert delivered == 1
assert len(received) == 1
def test_handler_error_does_not_block(self) -> None:
bus = EventBus()
received: List[BusEvent] = []
def failing_handler(event: BusEvent) -> None:
raise ValueError("Handler error")
def good_handler(event: BusEvent) -> None:
received.append(event)
bus.subscribe_callback(failing_handler)
bus.subscribe_callback(good_handler)
event = BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
delivered = bus.publish(event)
assert delivered == 1 # Only good handler succeeded
assert len(received) == 1
def test_handler_on_error_called(self) -> None:
bus = EventBus()
errors: List[Exception] = []
class ErrorTrackingHandler(EventHandler):
def handle(self, event: BusEvent) -> None:
raise ValueError("Test error")
def on_error(self, event: BusEvent, error: Exception) -> None:
errors.append(error)
bus.subscribe(ErrorTrackingHandler())
event = BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
bus.publish(event)
assert len(errors) == 1
assert str(errors[0]) == "Test error"
def test_multiple_subscribers(self) -> None:
bus = EventBus()
received1: List[BusEvent] = []
received2: List[BusEvent] = []
bus.subscribe_callback(lambda e: received1.append(e))
bus.subscribe_callback(lambda e: received2.append(e))
event = BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
delivered = bus.publish(event)
assert delivered == 2
assert len(received1) == 1
assert len(received2) == 1
def test_clear_subscriptions(self) -> None:
bus = EventBus()
bus.subscribe_callback(lambda e: None)
bus.subscribe_callback(lambda e: None)
assert len(bus.get_subscriptions()) == 2
count = bus.clear()
assert count == 2
assert len(bus.get_subscriptions()) == 0
def test_get_subscriptions(self) -> None:
bus = EventBus()
sub_id = bus.subscribe_callback(lambda e: None)
subs = bus.get_subscriptions()
assert len(subs) == 1
assert subs[0].subscription_id == sub_id
class TestDefaultBus:
def test_get_default_bus(self) -> None:
reset_default_bus()
bus1 = get_default_bus()
bus2 = get_default_bus()
assert bus1 is bus2
def test_reset_default_bus(self) -> None:
reset_default_bus()
bus1 = get_default_bus()
reset_default_bus()
bus2 = get_default_bus()
assert bus1 is not bus2
class TestCallbackHandler:
def test_callback_handler(self) -> None:
received: List[BusEvent] = []
handler = CallbackHandler(lambda e: received.append(e))
event = BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
handler.handle(event)
assert len(received) == 1
def test_callback_handler_with_error_callback(self) -> None:
errors: List[Exception] = []
handler = CallbackHandler(
lambda e: (_ for _ in ()).throw(ValueError("test")),
lambda e, err: errors.append(err),
)
event = BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
try:
handler.handle(event)
except ValueError:
pass
handler.on_error(event, ValueError("on_error test"))
assert len(errors) == 1
class TestAggregatingHandler:
def test_collects_events(self) -> None:
handler = AggregatingHandler()
for i in range(5):
event = BusEvent(
event_type=f"Event{i}",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
handler.handle(event)
assert len(handler.events) == 5
def test_max_events_limit(self) -> None:
handler = AggregatingHandler(max_events=3)
for i in range(10):
event = BusEvent(
event_type=f"Event{i}",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
handler.handle(event)
assert len(handler.events) == 3
def test_clear_returns_events(self) -> None:
handler = AggregatingHandler()
event = BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
handler.handle(event)
events = handler.clear()
assert len(events) == 1
assert len(handler.events) == 0
def test_get_by_type(self) -> None:
handler = AggregatingHandler()
handler.handle(
BusEvent(
event_type="TypeA",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
)
handler.handle(
BusEvent(
event_type="TypeB",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
)
handler.handle(
BusEvent(
event_type="TypeA",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
)
type_a = handler.get_by_type("TypeA")
assert len(type_a) == 2
class TestCompositeHandler:
def test_dispatches_to_all(self) -> None:
received1: List[BusEvent] = []
received2: List[BusEvent] = []
handler = CompositeHandler(
[
CallbackHandler(lambda e: received1.append(e)),
CallbackHandler(lambda e: received2.append(e)),
]
)
event = BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
handler.handle(event)
assert len(received1) == 1
assert len(received2) == 1
def test_add_and_remove(self) -> None:
received: List[BusEvent] = []
inner = CallbackHandler(lambda e: received.append(e))
handler = CompositeHandler()
handler.add(inner)
event = BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
)
handler.handle(event)
assert len(received) == 1
result = handler.remove(inner)
assert result is True
handler.handle(event)
assert len(received) == 1 # No new events
class TestFilteringHandler:
def test_filters_events(self) -> None:
received: List[BusEvent] = []
inner = CallbackHandler(lambda e: received.append(e))
handler = FilteringHandler(
inner,
lambda e: e.payload.get("important", False),
)
handler.handle(
BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
payload={"important": False},
)
)
handler.handle(
BusEvent(
event_type="TestEvent",
run_id="run-1",
timestamp="2026-01-04T10:00:00-05:00",
payload={"important": True},
)
)
assert len(received) == 1
assert received[0].payload["important"] is True
class TestEventCategories:
def test_all_event_types_categorized(self) -> None:
all_categorized = set()
for events in EVENT_CATEGORIES.values():
all_categorized.update(events)
for event_type in EventType:
assert event_type.value in all_categorized, f"{event_type.value} not in any category"
def test_categories_exist(self) -> None:
expected = {
"workflow",
"step",
"gate",
"tool",
"guardrail",
"validation",
"evidence",
"artifact",
"context",
"plugin",
}
assert set(EVENT_CATEGORIES.keys()) == expected