|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | +from unittest.mock import create_autospec |
| 5 | + |
| 6 | +import pytest |
| 7 | +from polyfactory.factories.pydantic_factory import ModelFactory |
| 8 | +from streamdeck.actions import Action, ActionRegistry, GlobalAction |
| 9 | +from streamdeck.models import events |
| 10 | + |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from unittest.mock import Mock |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def mock_event_handler() -> Mock: |
| 19 | + def dummy_handler(event: events.EventBase) -> None: |
| 20 | + """Dummy event handler function that matches the EventHandlerFunc TypeAlias.""" |
| 21 | + |
| 22 | + return create_autospec(dummy_handler, spec_set=True) |
| 23 | + |
| 24 | + |
| 25 | +class ApplicationDidLaunchEventFactory(ModelFactory[events.ApplicationDidLaunchEvent]): |
| 26 | + """Polyfactory factory for creating fake applicationDidLaunch event message based on our Pydantic model. |
| 27 | +
|
| 28 | + ApplicationDidLaunchEvent's hold no unique identifier properties, besides the almost irrelevant `event` name property. |
| 29 | + """ |
| 30 | + |
| 31 | +class DeviceDidConnectFactory(ModelFactory[events.DeviceDidConnectEvent]): |
| 32 | + """Polyfactory factory for creating fake deviceDidConnect event message based on our Pydantic model. |
| 33 | +
|
| 34 | + DeviceDidConnectEvent's have `device` unique identifier property. |
| 35 | + """ |
| 36 | + |
| 37 | +class KeyDownEventFactory(ModelFactory[events.KeyDownEvent]): |
| 38 | + """Polyfactory factory for creating fake keyDown event message based on our Pydantic model. |
| 39 | +
|
| 40 | + KeyDownEvent's have the unique identifier properties: |
| 41 | + `device`: Identifies the Stream Deck device that this event is associated with. |
| 42 | + `action`: Identifies the action that caused the event. |
| 43 | + `context`: Identifies the *instance* of an action that caused the event. |
| 44 | + """ |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize(("event_name","event_factory"), [ |
| 48 | + ("keyDown", KeyDownEventFactory), |
| 49 | + ("deviceDidConnect", DeviceDidConnectFactory), |
| 50 | + ("applicationDidLaunch", ApplicationDidLaunchEventFactory) |
| 51 | +]) |
| 52 | +def test_global_action_gets_triggered_by_event( |
| 53 | + mock_event_handler: Mock, |
| 54 | + event_name: str, |
| 55 | + event_factory: ModelFactory[events.EventBase], |
| 56 | +): |
| 57 | + """Test that a global action's event handlers are triggered by an event. |
| 58 | +
|
| 59 | + Global actions should be triggered by any event type that is registered with them, |
| 60 | + regardless of the event's unique identifier properties (or whether they're even present). |
| 61 | + """ |
| 62 | + fake_event_data = event_factory.build() |
| 63 | + |
| 64 | + global_action = GlobalAction() |
| 65 | + |
| 66 | + global_action.on(event_name)(mock_event_handler) |
| 67 | + |
| 68 | + for handler in global_action.get_event_handlers(event_name): |
| 69 | + handler(fake_event_data) |
| 70 | + |
| 71 | + assert mock_event_handler.call_count == 1 |
| 72 | + assert fake_event_data in mock_event_handler.call_args.args |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize(("event_name","event_factory"), [ |
| 76 | + ("keyDown", KeyDownEventFactory), |
| 77 | + ("deviceDidConnect", DeviceDidConnectFactory), |
| 78 | + ("applicationDidLaunch", ApplicationDidLaunchEventFactory) |
| 79 | +]) |
| 80 | +def test_action_gets_triggered_by_event(mock_event_handler: Mock, event_name: str, event_factory: ModelFactory[events.EventBase]): |
| 81 | + # Create a fake event model instance |
| 82 | + fake_event_data: events.EventBase = event_factory.build() |
| 83 | + # Extract the action UUID from the fake event data, or use a default value |
| 84 | + action_uuid: str = fake_event_data.action if fake_event_data.is_action_specific() else "my-fake-action-uuid" |
| 85 | + |
| 86 | + action = Action(uuid=action_uuid) |
| 87 | + |
| 88 | + # Register the mock event handler with the action |
| 89 | + action.on(event_name)(mock_event_handler) |
| 90 | + |
| 91 | + # Get the action's event handlers for the event and call them |
| 92 | + for handler in action.get_event_handlers(event_name): |
| 93 | + handler(fake_event_data) |
| 94 | + |
| 95 | + # For some reason, assert_called_once() and assert_called_once_with() are returning False here... |
| 96 | + # assert mock_event_handler.assert_called_once(fake_event_data) |
| 97 | + assert mock_event_handler.call_count == 1 |
| 98 | + assert fake_event_data in mock_event_handler.call_args.args |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize(("event_name","event_factory"), [ |
| 103 | + ("keyDown", KeyDownEventFactory), |
| 104 | + ("deviceDidConnect", DeviceDidConnectFactory), |
| 105 | + ("applicationDidLaunch", ApplicationDidLaunchEventFactory) |
| 106 | +]) |
| 107 | +def test_global_action_registry_get_action_handlers_filtering(mock_event_handler: Mock, event_name: str, event_factory: ModelFactory[events.EventBase]): |
| 108 | + # Create a fake event model instance |
| 109 | + fake_event_data: events.EventBase = event_factory.build() |
| 110 | + # Extract the action UUID from the fake event data, or use a default value |
| 111 | + action_uuid: str = fake_event_data.action if fake_event_data.is_action_specific() else None |
| 112 | + |
| 113 | + registry = ActionRegistry() |
| 114 | + # Create an Action instance, without an action UUID as global actions aren't associated with a specific action |
| 115 | + global_action = GlobalAction() |
| 116 | + |
| 117 | + global_action.on(event_name)(mock_event_handler) |
| 118 | + |
| 119 | + # Register the global action with the registry |
| 120 | + registry.register(global_action) |
| 121 | + |
| 122 | + for handler in registry.get_action_handlers( |
| 123 | + event_name=fake_event_data.event, |
| 124 | + event_action_uuid=action_uuid, |
| 125 | + ): |
| 126 | + handler(fake_event_data) |
| 127 | + |
| 128 | + assert mock_event_handler.call_count == 1 |
| 129 | + assert fake_event_data in mock_event_handler.call_args.args |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.parametrize(("event_name","event_factory"), [ |
| 134 | + ("keyDown", KeyDownEventFactory), |
| 135 | + ("deviceDidConnect", DeviceDidConnectFactory), |
| 136 | + ("applicationDidLaunch", ApplicationDidLaunchEventFactory) |
| 137 | +]) |
| 138 | +def test_action_registry_get_action_handlers_filtering(mock_event_handler: Mock, event_name: str, event_factory: ModelFactory[events.EventBase]): |
| 139 | + # Create a fake event model instance |
| 140 | + fake_event_data: events.EventBase = event_factory.build() |
| 141 | + # Extract the action UUID from the fake event data, or use a default value |
| 142 | + action_uuid: str = fake_event_data.action if fake_event_data.is_action_specific() else None |
| 143 | + |
| 144 | + registry = ActionRegistry() |
| 145 | + # Create an Action instance, using either the fake event's action UUID or a default value |
| 146 | + action = Action(uuid=action_uuid or "my-fake-action-uuid") |
| 147 | + |
| 148 | + action.on(event_name)(mock_event_handler) |
| 149 | + |
| 150 | + # Register the action with the registry |
| 151 | + registry.register(action) |
| 152 | + |
| 153 | + for handler in registry.get_action_handlers( |
| 154 | + event_name=fake_event_data.event, |
| 155 | + event_action_uuid=action_uuid, # This will be None if the event is not action-specific (i.e. doesn't have an action UUID property) |
| 156 | + ): |
| 157 | + handler(fake_event_data) |
| 158 | + |
| 159 | + assert mock_event_handler.call_count == 1 |
| 160 | + assert fake_event_data in mock_event_handler.call_args.args |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +def test_multiple_actions_filtering(): |
| 165 | + registry = ActionRegistry() |
| 166 | + action = Action("my-fake-action-uuid-1") |
| 167 | + global_action = GlobalAction() |
| 168 | + |
| 169 | + global_action_event_handler_called = False |
| 170 | + action_event_handler_called = False |
| 171 | + |
| 172 | + @global_action.on("applicationDidLaunch") |
| 173 | + def _global_app_did_launch_action_handler(event: events.EventBase): |
| 174 | + nonlocal global_action_event_handler_called |
| 175 | + global_action_event_handler_called = True |
| 176 | + |
| 177 | + @action.on("keyDown") |
| 178 | + def _action_key_down_event_handler(event: events.EventBase): |
| 179 | + nonlocal action_event_handler_called |
| 180 | + action_event_handler_called = True |
| 181 | + |
| 182 | + # Register both actions with the registry |
| 183 | + registry.register(global_action) |
| 184 | + registry.register(action) |
| 185 | + |
| 186 | + # Create a fake event model instances |
| 187 | + fake_app_did_launch_event_data: events.ApplicationDidLaunchEvent = ApplicationDidLaunchEventFactory.build() |
| 188 | + fake_key_down_event_data: events.KeyDownEvent = KeyDownEventFactory.build(action=action.uuid) |
| 189 | + |
| 190 | + for handler in registry.get_action_handlers(event_name=fake_app_did_launch_event_data.event): |
| 191 | + handler(fake_app_did_launch_event_data) |
| 192 | + |
| 193 | + assert global_action_event_handler_called |
| 194 | + assert not action_event_handler_called |
| 195 | + |
| 196 | + # Reset the flag for global action event handler |
| 197 | + global_action_event_handler_called = False |
| 198 | + |
| 199 | + # Get the action handlers for the event and call them |
| 200 | + for handler in registry.get_action_handlers(event_name=fake_key_down_event_data.event, event_action_uuid=fake_key_down_event_data.action): |
| 201 | + handler(fake_key_down_event_data) |
| 202 | + |
| 203 | + assert action_event_handler_called |
| 204 | + assert not global_action_event_handler_called |
0 commit comments