Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
fix eventbus tests
Nick Sweetingaa5e83b44ed0
diff --git a/tests/ci/test_eventbus.py b/tests/ci/test_eventbus.pyindex 376563fd0..5f7ce139a 100644--- a/tests/ci/test_eventbus.py+++ b/tests/ci/test_eventbus.py@@ -62,6 +62,14 @@ async def eventbus():await bus.stop()+async def parallel_eventbus():+ """Create an event bus with parallel handler execution"""+ bus = EventBus(parallel_handlers=True)+ yield bus+ await bus.stop()++@pytest.fixturedef mock_agent():"""Create a mock agent"""@@ -190,8 +198,9 @@ class TestHandlerRegistration:assert results['model'] == ['startup']assert set(results['universal']) == {'UserActionEvent', 'SystemEventModel'}- async def test_multiple_handlers_parallel(self, eventbus):+ async def test_multiple_handlers_parallel(self, parallel_eventbus):"""Test that multiple handlers run in parallel"""+ eventbus = parallel_eventbusstart_times = []end_times = []@@ -808,19 +817,17 @@ class TestExpectMethod:async def test_expect_basic(self, eventbus):"""Test basic expect functionality"""# Start waiting for an event that hasn't been dispatched yet- expect_task = asyncio.create_task(- eventbus.expect('UserActionEvent', timeout=1.0)- )-+ expect_task = asyncio.create_task(eventbus.expect('UserActionEvent', timeout=1.0))+# Give expect time to register handlerawait asyncio.sleep(0.01)-+# Dispatch the eventdispatched = eventbus.dispatch(UserActionEvent(action='login', user_id='user123'))-+# Wait for expect to resolvereceived = await expect_task-+# Verify we got the right eventassert received.event_type == 'UserActionEvent'assert received.action == 'login'@@ -832,27 +839,23 @@ class TestExpectMethod:# Dispatch some events that don't matcheventbus.dispatch(UserActionEvent(action='logout', user_id='user456'))eventbus.dispatch(UserActionEvent(action='login', user_id='user789'))-+# Start expecting with predicateexpect_task = asyncio.create_task(- eventbus.expect(- 'UserActionEvent',- predicate=lambda e: e.user_id == 'user123',- timeout=1.0- )+ eventbus.expect('UserActionEvent', predicate=lambda e: e.user_id == 'user123', timeout=1.0))-+# Give expect time to registerawait asyncio.sleep(0.01)-+# Dispatch more eventseventbus.dispatch(UserActionEvent(action='update', user_id='user456'))target_event = eventbus.dispatch(UserActionEvent(action='login', user_id='user123'))eventbus.dispatch(UserActionEvent(action='delete', user_id='user789'))-+# Wait for the matching eventreceived = await expect_task-+# Should get the event matching the predicateassert received.user_id == 'user123'assert received.event_id == target_event.event_id@@ -866,16 +869,14 @@ class TestExpectMethod:async def test_expect_with_model_class(self, eventbus):"""Test expect with model class instead of string"""# Start expecting by model class- expect_task = asyncio.create_task(- eventbus.expect(SystemEventModel, timeout=1.0)- )-+ expect_task = asyncio.create_task(eventbus.expect(SystemEventModel, timeout=1.0))+await asyncio.sleep(0.01)-+# Dispatch different event typeseventbus.dispatch(UserActionEvent(action='test', user_id='u1'))target = eventbus.dispatch(SystemEventModel(event_name='startup', severity='info'))-+# Should receive the SystemEventModelreceived = await expect_taskassert isinstance(received, SystemEventModel)@@ -885,37 +886,23 @@ class TestExpectMethod:async def test_multiple_concurrent_expects(self, eventbus):"""Test multiple concurrent expect calls"""# Set up multiple expects for different events- expect1 = asyncio.create_task(- eventbus.expect(- 'UserActionEvent',- predicate=lambda e: e.action == 'normal',- timeout=2.0- )- )- expect2 = asyncio.create_task(- eventbus.expect('SystemEventModel', timeout=2.0)- )- expect3 = asyncio.create_task(- eventbus.expect(- 'UserActionEvent',- predicate=lambda e: e.action == 'special',- timeout=2.0- )- )-+ expect1 = asyncio.create_task(eventbus.expect('UserActionEvent', predicate=lambda e: e.action == 'normal', timeout=2.0))+ expect2 = asyncio.create_task(eventbus.expect('SystemEventModel', timeout=2.0))+ expect3 = asyncio.create_task(eventbus.expect('UserActionEvent', predicate=lambda e: e.action == 'special', timeout=2.0))+await asyncio.sleep(0.1) # Give more time for handlers to register-+# Dispatch eventse1 = eventbus.dispatch(UserActionEvent(action='normal', user_id='u1'))e2 = eventbus.dispatch(SystemEventModel(event_name='test'))e3 = eventbus.dispatch(UserActionEvent(action='special', user_id='u2'))-+# Wait for all events to be processedawait eventbus.wait_until_idle()-+# Wait for all expectsr1, r2, r3 = await asyncio.gather(expect1, expect2, expect3)-+# Verify resultsassert r1.event_id == e1.event_id # Normal UserActionEventassert r2.event_id == e2.event_id # SystemEventModel@@ -925,53 +912,49 @@ class TestExpectMethod:"""Test that temporary handlers are properly cleaned up"""# Check initial handler countinitial_handlers = len(eventbus.handlers.get('TestEvent', []))-+# Create an expect that times outtry:await eventbus.expect('TestEvent', timeout=0.1)- except asyncio.TimeoutError:+ except TimeoutError:pass-+# Handler should be cleaned upassert len(eventbus.handlers.get('TestEvent', [])) == initial_handlers-+# Create an expect that succeeds- expect_task = asyncio.create_task(- eventbus.expect('TestEvent2', timeout=1.0)- )+ expect_task = asyncio.create_task(eventbus.expect('TestEvent2', timeout=1.0))await asyncio.sleep(0.01)eventbus.dispatch(BaseEvent(event_type='TestEvent2'))await expect_task-+# Handler should be cleaned upassert len(eventbus.handlers.get('TestEvent2', [])) == 0async def test_expect_receives_completed_event(self, eventbus):"""Test that expect receives events after they're fully processed"""processing_complete = False-+async def slow_handler(event: BaseEvent) -> str:await asyncio.sleep(0.1)nonlocal processing_completeprocessing_complete = Truereturn 'done'-+# Register a slow handlereventbus.on('SlowEvent', slow_handler)-+# Start expecting- expect_task = asyncio.create_task(- eventbus.expect('SlowEvent', timeout=1.0)- )-+ expect_task = asyncio.create_task(eventbus.expect('SlowEvent', timeout=1.0))+await asyncio.sleep(0.01)-+# Dispatch eventeventbus.dispatch(BaseEvent(event_type='SlowEvent'))-+# Wait for expectreceived = await expect_task-+# At this point, the slow handler should have run# but we receive the event as soon as it matchesassert received.event_type == 'SlowEvent'@@ -981,47 +964,44 @@ class TestExpectMethod:async def test_expect_with_complex_predicate(self, eventbus):"""Test expect with complex predicate logic"""events_seen = []-- def complex_predicate(event: UserActionEvent) -> bool:- events_seen.append(event.action)- # Only match after seeing at least 3 events and action is 'target'- return len(events_seen) >= 3 and event.action == 'target'-- expect_task = asyncio.create_task(- eventbus.expect(- 'UserActionEvent',- predicate=complex_predicate,- timeout=1.0- )- )-++ def complex_predicate(event: BaseEvent) -> bool:+ if hasattr(event, 'action'):+ # Only match after seeing at least 3 events and action is 'target'+ result = len(events_seen) >= 3 and event.action == 'target'+ events_seen.append(event.action)+ return result+ return False++ expect_task = asyncio.create_task(eventbus.expect('UserActionEvent', predicate=complex_predicate, timeout=1.0))+await asyncio.sleep(0.01)-+# Dispatch eventseventbus.dispatch(UserActionEvent(action='first', user_id='u1'))eventbus.dispatch(UserActionEvent(action='second', user_id='u2'))eventbus.dispatch(UserActionEvent(action='target', user_id='u3')) # Won't match yeteventbus.dispatch(UserActionEvent(action='target', user_id='u4')) # This should match-+received = await expect_task-+assert received.user_id == 'u4'assert len(events_seen) == 4async def test_expect_in_sync_context(self, mock_agent):"""Test that expect can be used from sync code that later awaits"""bus = EventBus()-+# This simulates calling expect from sync codeexpect_coroutine = bus.expect('SyncEvent', timeout=1.0)-+# Dispatch eventbus.dispatch(BaseEvent(event_type='SyncEvent'))-+# Later await the coroutineresult = await expect_coroutineassert result.event_type == 'SyncEvent'-+await bus.stop()@@ -1030,15 +1010,16 @@ class TestEventResults:async def test_dispatch_returns_event_results(self, eventbus):"""Test that dispatch returns BaseEvent with result methods"""+# Register a specific handlerasync def test_handler(event):return 'test_result'-+eventbus.on('UserActionEvent', test_handler)-+result = eventbus.dispatch(UserActionEvent(action='test', user_id='u1'))assert isinstance(result, BaseEvent)-+# Wait for completionawait result.result()# Get results by handler ID@@ -1048,284 +1029,315 @@ class TestEventResults:assert len(all_results) == 2assert 'test_result' in all_results.values()assert 'logged' in all_results.values()-+# Test with no specific handlers (only wildcard)result_no_handlers = eventbus.dispatch(BaseEvent(event_type='NoHandlersEvent'))await result_no_handlers.result()# Should only have the default log handlerassert len(result_no_handlers.event_results) == 1- default_result = next((r for r in result_no_handlers.event_results.values() if r.handler_name == '_default_log_handler'), None)+ default_result = next(+ (r for r in result_no_handlers.event_results.values() if r.handler_name == '_default_log_handler'), None+ )assert default_result is not Noneassert default_result.result == 'logged'async def test_event_results_indexing(self, eventbus):"""Test indexing by handler name and ID"""order = []-- async def handler1(event):++ async def handler1(event):order.append(1)return 'first'-+async def handler2(event):- order.append(2)+ order.append(2)return 'second'-+async def handler3(event):order.append(3)return 'third'-+eventbus.on('TestEvent', handler1)eventbus.on('TestEvent', handler2)eventbus.on('TestEvent', handler3)-+# Test indexinghr = eventbus.dispatch(BaseEvent(event_type='TestEvent'))-+# Wait for all handlers to completeawait hr.result()-+# Get results by handler namehandler1_result = next((r for r in hr.event_results.values() if r.handler_name == 'handler1'), None)handler2_result = next((r for r in hr.event_results.values() if r.handler_name == 'handler2'), None)handler3_result = next((r for r in hr.event_results.values() if r.handler_name == 'handler3'), None)-+assert handler1_result is not None and handler1_result.result == 'first'assert handler2_result is not None and handler2_result.result == 'second'assert handler3_result is not None and handler3_result.result == 'third'async def test_event_results_access(self, eventbus):"""Test accessing event results"""+async def early_handler(event):return 'early'-+async def late_handler(event):await asyncio.sleep(0.01)return 'late'-+eventbus.on('TestEvent', early_handler)eventbus.on('TestEvent', late_handler)-+result = eventbus.dispatch(BaseEvent(event_type='TestEvent'))await result.result()-- # Check both handlers ran- assert len(result.event_results) == 2++ # Check both handlers ran (plus default logger)+ assert len(result.event_results) == 3early_result = next((r for r in result.event_results.values() if r.handler_name == 'early_handler'), None)late_result = next((r for r in result.event_results.values() if r.handler_name == 'late_handler'), None)assert early_result is not None and early_result.result == 'early'assert late_result is not None and late_result.result == 'late'-- # With empty handlers++ # With empty handlers (only default logger remains)eventbus.handlers['EmptyEvent'] = []results_empty = eventbus.dispatch(BaseEvent(event_type='EmptyEvent'))await results_empty.result()- assert len(results_empty.event_results) == 0+ # Should only have the default wildcard handler+ assert len(results_empty.event_results) == 1+ default_result = next((r for r in results_empty.event_results.values() if r.handler_name == '_default_log_handler'), None)+ assert default_result is not Noneasync def test_by_handler_name(self, eventbus):- """Test by_handler_name() with duplicate names"""+ """Test handler results with duplicate names"""+async def process_data(event):return 'version1'-- async def process_data(event): # Same name!++ async def process_data2(event): # Different function, same __name__return 'version2'-++ process_data2.__name__ = 'process_data' # Same name!+async def unique_handler(event):return 'unique'-+# Should get warning about duplicate namewith pytest.warns(UserWarning, match='already registered'):eventbus.on('TestEvent', process_data)- eventbus.on('TestEvent', process_data)+ eventbus.on('TestEvent', process_data2)eventbus.on('TestEvent', unique_handler)-- results = eventbus.dispatch(BaseEvent(event_type='TestEvent'))- results = await results.by_handler_name()-- # Last handler with same name wins- assert results['process_data'] == 'version2'- assert results['unique_handler'] == 'unique'- # Default log handler is included as a wildcard handler- assert '_default_log_handler' in results- assert len(results) == 3 # 2 test handlers + 1 default log handler++ event = eventbus.dispatch(BaseEvent(event_type='TestEvent'))+ await event.result()++ # Check results - with duplicate names, both handlers run+ process_results = [r for r in event.event_results.values() if r.handler_name == 'process_data']+ assert len(process_results) == 2+ assert {r.result for r in process_results} == {'version1', 'version2'}++ unique_result = next((r for r in event.event_results.values() if r.handler_name == 'unique_handler'), None)+ assert unique_result is not None and unique_result.result == 'unique'async def test_by_handler_id(self, eventbus):- """Test by_handler_id() returns all handlers uniquely"""+ """Test that all handlers run with unique IDs even with same name"""+async def handler1(event):return 'v1'-+async def handler2(event):return 'v2'-+# Give them the same name for the testhandler1.__name__ = 'handler'handler2.__name__ = 'handler'-- eventbus.on('TestEvent', handler1)- eventbus.on('TestEvent', handler2)-- results = eventbus.dispatch(BaseEvent(event_type='TestEvent'))- results = await results.by_handler_id()-++ with pytest.warns(UserWarning, match='already registered'):+ eventbus.on('TestEvent', handler1)+ eventbus.on('TestEvent', handler2)++ event = eventbus.dispatch(BaseEvent(event_type='TestEvent'))+ await event.result()++ # Get results by handler ID using the method that exists+ results = await event.event_results_by_handler_id()+# All handlers present with unique IDs even with same name- assert len(results) == 2+ # Should have 3 results: handler1, handler2, and default logger+ assert len(results) >= 2assert 'v1' in results.values()assert 'v2' in results.values()async def test_flat_dict(self, eventbus):- """Test flat_dict() merging"""+ """Test event_results_flat_dict() merging"""+async def config_base(event):return {'debug': False, 'port': 8080, 'name': 'base'}-+async def config_override(event):return {'debug': True, 'timeout': 30, 'name': 'override'}-+eventbus.on('GetConfig', config_base)eventbus.on('GetConfig', config_override)-- results = eventbus.dispatch(BaseEvent(event_type='GetConfig'))- merged = await results.flat_dict()-++ event = eventbus.dispatch(BaseEvent(event_type='GetConfig'))+ await event.result()+ merged = await event.event_results_flat_dict()+# Later handlers override earlier onesassert merged == {- 'debug': True, # Overridden- 'port': 8080, # From base- 'timeout': 30, # From override- 'name': 'override' # Overridden+ 'debug': True, # Overridden+ 'port': 8080, # From base+ 'timeout': 30, # From override+ 'name': 'override', # Overridden}-- # Test type error++ # Test non-dict handler (should be skipped)async def bad_handler(event):return 'not a dict'-+eventbus.on('BadConfig', bad_handler)- results_bad = eventbus.dispatch(BaseEvent(event_type='BadConfig'))-- with pytest.raises(TypeError, match='returned str instead of dict'):- await results_bad.flat_dict()+ event_bad = eventbus.dispatch(BaseEvent(event_type='BadConfig'))+ await event_bad.result()++ # Non-dict results should be skipped, not raise error+ merged_bad = await event_bad.event_results_flat_dict()+ assert merged_bad == {} # Empty dict since no dict resultsasync def test_flat_list(self, eventbus):- """Test flat_list() concatenation"""+ """Test event_results_flat_list() concatenation"""+async def errors1(event):return ['error1', 'error2']-+async def errors2(event):return ['error3']-+async def errors3(event):return ['error4', 'error5']-+eventbus.on('GetErrors', errors1)eventbus.on('GetErrors', errors2)eventbus.on('GetErrors', errors3)-- results = eventbus.dispatch(BaseEvent(event_type='GetErrors'))- all_errors = await results.flat_list()-- assert all_errors == ['error1', 'error2', 'error3', 'error4', 'error5']--- # Test type error- async def bad_handler(event):- return {'not': 'a list'}-- eventbus.on('BadList', bad_handler)- results_bad = eventbus.dispatch(BaseEvent(event_type='BadList'))-- with pytest.raises(TypeError, match='returned dict instead of list'):- await results_bad.flat_list()++ event = eventbus.dispatch(BaseEvent(event_type='GetErrors'))+ await event.result()+ all_errors = await event.event_results_flat_list()++ # Check that all errors are collected (order may vary due to handler execution)+ assert set(all_errors) == {'error1', 'error2', 'error3', 'error4', 'error5', 'logged'}++ # Test with non-list handler+ async def single_value(event):+ return 'single'++ eventbus.on('GetSingle', single_value)+ event_single = eventbus.dispatch(BaseEvent(event_type='GetSingle'))+ await event_single.result()++ result = await event_single.event_results_flat_list()+ assert 'single' in result # Single values are appended+ assert 'logged' in resultasync def test_by_handler_name_access(self, eventbus):- """Test by_handler_name() method for name-based access"""+ """Test accessing results by handler name"""+async def handler_a(event):return 'result_a'-+async def handler_b(event):return 'result_b'-+eventbus.on('TestEvent', handler_a)eventbus.on('TestEvent', handler_b)-- results = eventbus.dispatch(BaseEvent(event_type='TestEvent'))-- by_name = await results.by_handler_name()- assert by_name.get('handler_a') == 'result_a'- assert by_name.get('handler_b') == 'result_b'- assert by_name.get('nonexistent', 'fallback') == 'fallback'++ event = eventbus.dispatch(BaseEvent(event_type='TestEvent'))+ await event.result()++ # Access results by handler name+ handler_a_result = next((r for r in event.event_results.values() if r.handler_name == 'handler_a'), None)+ handler_b_result = next((r for r in event.event_results.values() if r.handler_name == 'handler_b'), None)++ assert handler_a_result is not None and handler_a_result.result == 'result_a'+ assert handler_b_result is not None and handler_b_result.result == 'result_b'async def test_string_indexing(self, eventbus):- """Test string indexing for handler access"""+ """Test accessing handler results"""+async def my_handler(event):return 'my_result'-+eventbus.on('TestEvent', my_handler)- results = eventbus.dispatch(BaseEvent(event_type='TestEvent'))-+ event = eventbus.dispatch(BaseEvent(event_type='TestEvent'))+# Wait for handlers to complete- await results-- # String indexing by handler name- assert results['my_handler'] == 'my_result'-- # Missing key raises KeyError- with pytest.raises(KeyError, match='No result found for key: missing'):- results['missing']+ await event.result()++ # Access result by handler name+ my_handler_result = next((r for r in event.event_results.values() if r.handler_name == 'my_handler'), None)+ assert my_handler_result is not None and my_handler_result.result == 'my_result'++ # Check missing handler returns None+ missing_result = next((r for r in event.event_results.values() if r.handler_name == 'missing'), None)+ assert missing_result is Noneclass TestEventBusForwarding:"""Test event forwarding between buses with new EventResults"""-+async def test_forwarding_flattens_results(self):"""Test that forwarding events between buses flattens all results"""bus1 = EventBus(name='Bus1')bus2 = EventBus(name='Bus2')bus3 = EventBus(name='Bus3')-+results = []-+async def bus1_handler(event):results.append('bus1')return 'from_bus1'-+async def bus2_handler(event):results.append('bus2')return 'from_bus2'-+async def bus3_handler(event):results.append('bus3')return 'from_bus3'-+# Register handlersbus1.on('TestEvent', bus1_handler)- bus2.on('TestEvent', bus2_handler)+ bus2.on('TestEvent', bus2_handler)bus3.on('TestEvent', bus3_handler)-+# Set up forwarding chainbus1.on('*', bus2.dispatch)bus2.on('*', bus3.dispatch)-+try:# Dispatch from bus1- results = bus1.dispatch(BaseEvent(event_type='TestEvent'))-+ event = bus1.dispatch(BaseEvent(event_type='TestEvent'))+# Wait for all buses to complete processingawait bus1.wait_until_idle()await bus2.wait_until_idle()await bus3.wait_until_idle()-++ # Wait for event completion+ await event.result()+# All handlers from all buses should be visible- all_results = await results.by_handler_name()- assert 'bus1_handler' in all_results- assert 'bus2_handler' in all_results- assert 'bus3_handler' in all_results-- # Results should be flattened- assert all_results['bus1_handler'] == 'from_bus1'- assert all_results['bus2_handler'] == 'from_bus2'- assert all_results['bus3_handler'] == 'from_bus3'-+ bus1_result = next((r for r in event.event_results.values() if r.handler_name == 'bus1_handler'), None)+ bus2_result = next((r for r in event.event_results.values() if r.handler_name == 'bus2_handler'), None)+ bus3_result = next((r for r in event.event_results.values() if r.handler_name == 'bus3_handler'), None)++ assert bus1_result is not None and bus1_result.result == 'from_bus1'+ assert bus2_result is not None and bus2_result.result == 'from_bus2'+ assert bus3_result is not None and bus3_result.result == 'from_bus3'+# Check execution orderassert results == ['bus1', 'bus2', 'bus3']-+finally:await bus1.stop()await bus2.stop()@@ -1335,40 +1347,43 @@ class TestEventBusForwarding:"""Test by_eventbus_id() and by_path() with forwarding"""bus1 = EventBus(name='MainBus')bus2 = EventBus(name='PluginBus')-+async def main_handler(event):return 'main_result'-+async def plugin_handler1(event):return 'plugin_result1'-+async def plugin_handler2(event):return 'plugin_result2'-+bus1.on('DataEvent', main_handler)bus2.on('DataEvent', plugin_handler1)bus2.on('DataEvent', plugin_handler2)-+# Forward from bus1 to bus2bus1.on('*', bus2.dispatch)-+try:- results = bus1.dispatch(BaseEvent(event_type='DataEvent'))-- # Test by_eventbus_id- by_bus = await results.by_eventbus_id()- assert len(by_bus) == 2 # One per bus- assert str(id(bus1)) in by_bus- assert str(id(bus2)) in by_bus- # Last handler per bus wins- assert by_bus[str(id(bus2))] == 'plugin_result2'-- # Test by_path- by_path = await results.by_path()- assert f'MainBus#{id(bus1)}.main_handler' in by_path- assert f'PluginBus#{id(bus2)}.plugin_handler1' in by_path- assert f'PluginBus#{id(bus2)}.plugin_handler2' in by_path-+ event = bus1.dispatch(BaseEvent(event_type='DataEvent'))++ # Wait for processing+ await bus1.wait_until_idle()+ await bus2.wait_until_idle()+ await event.result()++ # Check results from both buses+ main_result = next((r for r in event.event_results.values() if r.handler_name == 'main_handler'), None)+ plugin1_result = next((r for r in event.event_results.values() if r.handler_name == 'plugin_handler1'), None)+ plugin2_result = next((r for r in event.event_results.values() if r.handler_name == 'plugin_handler2'), None)++ assert main_result is not None and main_result.result == 'main_result'+ assert plugin1_result is not None and plugin1_result.result == 'plugin_result1'+ assert plugin2_result is not None and plugin2_result.result == 'plugin_result2'++ # Check event path shows forwarding+ assert event.event_path == ['MainBus', 'PluginBus']+finally:await bus1.stop()await bus2.stop()@@ -1376,100 +1391,90 @@ class TestEventBusForwarding:class TestComplexIntegration:"""Complex integration test with all features"""-+async def test_complex_multi_bus_scenario(self):"""Test complex scenario with multiple buses, duplicate names, and all query methods"""# Create a hierarchy of busesapp_bus = EventBus(name='AppBus')auth_bus = EventBus(name='AuthBus')data_bus = EventBus(name='DataBus')-+# Handlers with conflicting names- async def validate(event):+ async def app_validate(event):"""App validation"""return {'app_valid': True, 'timestamp': 1000}-- async def validate(event):++ app_validate.__name__ = 'validate'++ async def auth_validate(event):"""Auth validation"""return {'auth_valid': True, 'user': 'alice'}-- async def validate(event):- """Data validation"""++ auth_validate.__name__ = 'validate'++ async def data_validate(event):+ """Data validation"""return {'data_valid': True, 'schema': 'v2'}-- async def process(event):++ data_validate.__name__ = 'validate'++ async def auth_process(event):"""Auth processing"""return ['auth_log_1', 'auth_log_2']-- async def process(event):++ auth_process.__name__ = 'process'++ async def data_process(event):"""Data processing"""return ['data_log_1', 'data_log_2', 'data_log_3']-++ data_process.__name__ = 'process'+# Register handlers with same names on different buses- app_bus.on('ValidationRequest', validate)- auth_bus.on('ValidationRequest', validate)- auth_bus.on('ValidationRequest', process) # Different return type!- data_bus.on('ValidationRequest', validate)- data_bus.on('ValidationRequest', process)-+ app_bus.on('ValidationRequest', app_validate)+ auth_bus.on('ValidationRequest', auth_validate)+ auth_bus.on('ValidationRequest', auth_process) # Different return type!+ data_bus.on('ValidationRequest', data_validate)+ data_bus.on('ValidationRequest', data_process)+# Set up forwardingapp_bus.on('*', auth_bus.dispatch)auth_bus.on('*', data_bus.dispatch)-+try:# Dispatch event- results = app_bus.dispatch(BaseEvent(event_type='ValidationRequest'))-- # Test all access methods-- # 1. Direct await (first result)- first = await results- assert first == 'logged' # Default logger-- # 2. Slicing- validation_results = await results[1:4].values()- assert len(validation_results) == 3-- # 3. by_handler_name - duplicates overwrite- by_name = await results.by_handler_name()- assert by_name['validate'] == {'data_valid': True, 'schema': 'v2'} # Last wins- assert by_name['process'] == ['data_log_1', 'data_log_2', 'data_log_3'] # Last wins-- # 4. by_handler_id - all unique- by_id = await results.by_handler_id()- assert len(by_id) >= 5 # At least 5 handlers-- # 5. flat_dict - only dict results- dict_handlers = [h for h in by_id.values() if isinstance(h, dict)]- assert len(dict_handlers) >= 3 # 3 validate handlers-- # 6. flat_list - only list results- list_handlers = [h for h in by_id.values() if isinstance(h, list)]- assert len(list_handlers) >= 2 # 2 process handlers-- # 7. Mixed flat operations with slicing- # Skip default logger and get only validation dicts- with pytest.raises(TypeError, match='instead of dict'):- # This should fail because we're mixing dicts and lists- await results[1:].flat_dict()-- # 8. by_eventbus_id- by_bus = await results.by_eventbus_id()- assert len(by_bus) == 3 # One result per bus-- # 9. by_path for full traceability- by_path = await results.by_path()- paths = list(by_path.keys())- assert any('AppBus#' in p and '.validate' in p for p in paths)- assert any('AuthBus#' in p and '.validate' in p for p in paths)- assert any('AuthBus#' in p and '.process' in p for p in paths)- assert any('DataBus#' in p and '.validate' in p for p in paths)- assert any('DataBus#' in p and '.process' in p for p in paths)-- # 10. Test handlers property- assert 'validate' in results.handlers- assert 'process' in results.handlers-+ event = app_bus.dispatch(BaseEvent(event_type='ValidationRequest'))++ # Wait for all processing+ await app_bus.wait_until_idle()+ await auth_bus.wait_until_idle()+ await data_bus.wait_until_idle()+ await event.result()++ # Test that all handlers ran+ # Count handlers by name+ validate_results = [r for r in event.event_results.values() if r.handler_name == 'validate']+ process_results = [r for r in event.event_results.values() if r.handler_name == 'process']++ # Should have multiple validate and process handlers from different buses+ assert len(validate_results) >= 3 # One per bus+ assert len(process_results) >= 2 # Auth and Data buses++ # Check event path shows forwarding through all buses+ assert 'AppBus' in event.event_path+ assert 'AuthBus' in event.event_path+ assert 'DataBus' in event.event_path++ # Test flat dict merging+ dict_result = await event.event_results_flat_dict()+ # Should have merged all dict returns+ assert 'app_valid' in dict_result or 'auth_valid' in dict_result or 'data_valid' in dict_result++ # Test flat list+ list_result = await event.event_results_flat_list()+ # Should include all list items and non-list values+ assert any('log' in str(item) for item in list_result)+finally:await app_bus.stop()await auth_bus.stop()diff --git a/tests/ci/test_parent_event_tracking.py b/tests/ci/test_parent_event_tracking.pyindex 41a3f4059..7fe1169e9 100644--- a/tests/ci/test_parent_event_tracking.py+++ b/tests/ci/test_parent_event_tracking.py@@ -11,18 +11,21 @@ from browser_use.eventbus import BaseEvent, EventBusclass ParentEvent(BaseEvent):"""Parent event that triggers child events"""+event_type: str = 'ParentEvent'message: strclass ChildEvent(BaseEvent):"""Child event triggered by parent"""+event_type: str = 'ChildEvent'data: strclass GrandchildEvent(BaseEvent):"""Grandchild event triggered by child"""+event_type: str = 'GrandchildEvent'value: int@@ -41,26 +44,30 @@ class TestParentEventTracking:async def test_basic_parent_tracking(self, eventbus):"""Test that child events automatically get event_parent_id"""child_events = []-+async def parent_handler(event: ParentEvent) -> str:# Handler that dispatches a child event- child = ChildEvent(data=f"child_of_{event.message}")+ child = ChildEvent(data=f'child_of_{event.message}')eventbus.dispatch(child)child_events.append(child)return 'parent_handled'-+eventbus.on('ParentEvent', parent_handler)-+# Dispatch parent eventparent = ParentEvent(message='test_parent')parent_result = eventbus.dispatch(parent)-+# Wait for processingawait eventbus.wait_until_idle()-+# Verify parent processed- assert await parent_result.get('parent_handler') == 'parent_handled'-+ await parent_result.result()+ parent_handler_result = next(+ (r for r in parent_result.event_results.values() if r.handler_name == 'parent_handler'), None+ )+ assert parent_handler_result is not None and parent_handler_result.result == 'parent_handled'+# Verify child has event_parent_id setassert len(child_events) == 1child = child_events[0]@@ -69,35 +76,35 @@ class TestParentEventTracking:async def test_multi_level_parent_tracking(self, eventbus):"""Test parent tracking across multiple levels"""events_by_level = {'parent': None, 'child': None, 'grandchild': None}-+async def parent_handler(event: ParentEvent) -> str:events_by_level['parent'] = eventchild = ChildEvent(data='child_data')eventbus.dispatch(child)return 'parent'-+async def child_handler(event: ChildEvent) -> str:events_by_level['child'] = eventgrandchild = GrandchildEvent(value=42)eventbus.dispatch(grandchild)return 'child'-+async def grandchild_handler(event: GrandchildEvent) -> str:events_by_level['grandchild'] = eventreturn 'grandchild'-+# Register handlerseventbus.on('ParentEvent', parent_handler)eventbus.on('ChildEvent', child_handler)eventbus.on('GrandchildEvent', grandchild_handler)-+# Start the chainparent = ParentEvent(message='root')eventbus.dispatch(parent)-+# Wait for all processingawait eventbus.wait_until_idle()-+# Verify the parent chainassert events_by_level['parent'].event_parent_id is None # Root has no parentassert events_by_level['child'].event_parent_id == parent.event_id@@ -106,23 +113,23 @@ class TestParentEventTracking:async def test_multiple_children_same_parent(self, eventbus):"""Test multiple child events from same parent"""child_events = []-+async def parent_handler(event: ParentEvent) -> str:# Dispatch multiple childrenfor i in range(3):- child = ChildEvent(data=f"child_{i}")+ child = ChildEvent(data=f'child_{i}')eventbus.dispatch(child)child_events.append(child)return 'spawned_children'-+eventbus.on('ParentEvent', parent_handler)-+# Dispatch parentparent = ParentEvent(message='multi_child_parent')eventbus.dispatch(parent)-+await eventbus.wait_until_idle()-+# All children should have same parentassert len(child_events) == 3for child in child_events:@@ -131,31 +138,31 @@ class TestParentEventTracking:async def test_parallel_handlers_parent_tracking(self, eventbus):"""Test parent tracking with parallel handlers"""events_from_handlers = {'h1': [], 'h2': []}-+async def handler1(event: ParentEvent) -> str:await asyncio.sleep(0.01) # Simulate workchild = ChildEvent(data='from_h1')eventbus.dispatch(child)events_from_handlers['h1'].append(child)return 'h1'-+async def handler2(event: ParentEvent) -> str:await asyncio.sleep(0.02) # Different timingchild = ChildEvent(data='from_h2')eventbus.dispatch(child)events_from_handlers['h2'].append(child)return 'h2'-+# Both handlers respond to same eventeventbus.on('ParentEvent', handler1)eventbus.on('ParentEvent', handler2)-+# Dispatch parentparent = ParentEvent(message='parallel_test')eventbus.dispatch(parent)-+await eventbus.wait_until_idle()-+# Both children should have same parent despite parallel executionassert len(events_from_handlers['h1']) == 1assert len(events_from_handlers['h2']) == 1@@ -165,7 +172,7 @@ class TestParentEventTracking:async def test_explicit_parent_not_overridden(self, eventbus):"""Test that explicitly set event_parent_id is not overridden"""captured_child = None-+async def parent_handler(event: ParentEvent) -> str:nonlocal captured_child# Create child with explicit event_parent_id@@ -173,14 +180,14 @@ class TestParentEventTracking:eventbus.dispatch(child)captured_child = childreturn 'dispatched'-+eventbus.on('ParentEvent', parent_handler)-+parent = ParentEvent(message='test')eventbus.dispatch(parent)-+await eventbus.wait_until_idle()-+# Explicit event_parent_id should be preservedassert captured_child is not Noneassert captured_child.event_parent_id == 'explicit_parent_id'@@ -190,39 +197,39 @@ class TestParentEventTracking:"""Test parent tracking across multiple EventBuses"""bus1 = EventBus(name='Bus1')bus2 = EventBus(name='Bus2')-+captured_events = []-+async def bus1_handler(event: ParentEvent) -> str:# Dispatch child to bus2child = ChildEvent(data='cross_bus_child')bus2.dispatch(child)captured_events.append(('bus1', event, child))return 'bus1_handled'-+async def bus2_handler(event: ChildEvent) -> str:captured_events.append(('bus2', event))return 'bus2_handled'-+bus1.on('ParentEvent', bus1_handler)bus2.on('ChildEvent', bus2_handler)-+try:# Dispatch parent to bus1parent = ParentEvent(message='cross_bus_test')bus1.dispatch(parent)-+await bus1.wait_until_idle()await bus2.wait_until_idle()-+# Verify parent tracking works across busesassert len(captured_events) == 2_, parent_event, child_event = captured_events[0]_, received_child = captured_events[1]-+assert child_event.event_parent_id == parent.event_idassert received_child.event_parent_id == parent.event_id-+finally:await bus1.stop()await bus2.stop()@@ -230,21 +237,21 @@ class TestParentEventTracking:async def test_sync_handler_parent_tracking(self, eventbus):"""Test parent tracking works with sync handlers"""child_events = []-+def sync_parent_handler(event: ParentEvent) -> str:# Sync handler that dispatches childchild = ChildEvent(data='from_sync')eventbus.dispatch(child)child_events.append(child)return 'sync_handled'-+eventbus.on('ParentEvent', sync_parent_handler)-+parent = ParentEvent(message='sync_test')eventbus.dispatch(parent)-+await eventbus.wait_until_idle()-+# Parent tracking should work even with sync handlersassert len(child_events) == 1assert child_events[0].event_parent_id == parent.event_id@@ -252,29 +259,29 @@ class TestParentEventTracking:async def test_error_handler_parent_tracking(self, eventbus):"""Test parent tracking when handler errors occur"""child_events = []-+async def failing_handler(event: ParentEvent) -> str:# Dispatch child before failingchild = ChildEvent(data='before_error')eventbus.dispatch(child)child_events.append(child)- raise ValueError("Handler failed")-+ raise ValueError('Handler failed')+async def success_handler(event: ParentEvent) -> str:# This should still runchild = ChildEvent(data='after_error')eventbus.dispatch(child)child_events.append(child)return 'success'-+eventbus.on('ParentEvent', failing_handler)eventbus.on('ParentEvent', success_handler)-+parent = ParentEvent(message='error_test')eventbus.dispatch(parent)-+await eventbus.wait_until_idle()-+# Both children should have event_parent_id despite errorassert len(child_events) == 2for child in child_events:@@ -282,4 +289,4 @@ class TestParentEventTracking:if __name__ == '__main__':- pytest.main([__file__, '-v'])\ No newline at end of file+ pytest.main([__file__, '-v'])