1- import uuid
21from typing import cast
3- from unittest .mock import MagicMock , Mock
2+ from unittest .mock import Mock
43
54import pytest
65import pytest_mock
7- from polyfactory .factories .pydantic_factory import ModelFactory
86from streamdeck .actions import Action
97from streamdeck .manager import PluginManager
108from streamdeck .models .events import DialRotate , EventBase , event_adapter
11- from streamdeck .websocket import WebSocketClient
129
13-
14- class DialRotateEventFactory (ModelFactory [DialRotate ]):
15- """Polyfactory factory for creating a fake event message based on our Pydantic model."""
16-
17-
18- @pytest .fixture
19- def plugin_manager (port_number : int ) -> PluginManager :
20- """Fixture that provides a configured instance of PluginManager for testing.
21-
22- Returns:
23- PluginManager: An instance of PluginManager with test parameters.
24- """
25- plugin_uuid = "test-plugin-uuid"
26- plugin_registration_uuid = str (uuid .uuid1 ())
27- register_event = "registerPlugin"
28- info = {"some" : "info" }
29-
30- return PluginManager (
31- port = port_number ,
32- plugin_uuid = plugin_uuid ,
33- plugin_registration_uuid = plugin_registration_uuid ,
34- register_event = register_event ,
35- info = info ,
36- )
10+ from tests .test_utils .fake_event_factories import DialRotateEventFactory
3711
3812
3913@pytest .fixture
40- def patch_websocket_client ( monkeypatch : pytest . MonkeyPatch ) -> tuple [MagicMock , EventBase ]:
41- """Fixture that uses pytest's MonkeyPatch to mock WebSocketClient and StreamDeckCommandSender for the PluginManager run method .
14+ def mock_websocket_client_with_event ( patch_websocket_client : Mock ) -> tuple [Mock , EventBase ]:
15+ """Fixture that mocks the WebSocketClient and provides a fake DialRotateEvent message .
4216
4317 Args:
44- monkeypatch: pytest's monkeypatch fixture .
18+ patch_websocket_client: Mocked instance of the patched WebSocketClient .
4519
4620 Returns:
4721 tuple: Mocked instance of WebSocketClient, and a fake DialRotateEvent.
4822 """
49- mock_websocket_client = MagicMock (spec = WebSocketClient )
50-
51- mock_websocket_client .__enter__ .return_value = mock_websocket_client
52-
5323 # Create a fake event message, and convert it to a json string to be passed back by the client.listen_forever() method.
5424 fake_event_message : DialRotate = DialRotateEventFactory .build ()
55- mock_websocket_client .listen_forever .return_value = [fake_event_message .model_dump_json ()]
25+ patch_websocket_client .listen_forever .return_value = [fake_event_message .model_dump_json ()]
5626
57- monkeypatch . setattr ( "streamdeck.manager.WebSocketClient" , lambda port : mock_websocket_client )
27+ return patch_websocket_client , fake_event_message
5828
59- return mock_websocket_client , fake_event_message
60-
61-
62- @pytest .fixture
63- def mock_command_sender (mocker : pytest_mock .MockerFixture ) -> Mock :
64- """Fixture that patches the StreamDeckCommandSender.
65-
66- Args:
67- mocker: pytest-mock's mocker fixture.
68-
69- Returns:
70- Mock: Mocked instance of StreamDeckCommandSender
71- """
72- mock_cmd_sender = Mock ()
73- mocker .patch ("streamdeck.manager.StreamDeckCommandSender" , return_value = mock_cmd_sender )
74- return mock_cmd_sender
7529
7630
7731@pytest .fixture
@@ -114,7 +68,7 @@ def test_plugin_manager_register_action(plugin_manager: PluginManager):
11468 assert plugin_manager ._registry ._plugin_actions [0 ] == action
11569
11670
117- @pytest .mark .usefixtures ("patch_websocket_client " )
71+ @pytest .mark .usefixtures ("mock_websocket_client_with_event " )
11872def test_plugin_manager_sends_registration_event (
11973 mock_command_sender : Mock , plugin_manager : PluginManager
12074):
@@ -130,10 +84,10 @@ def test_plugin_manager_sends_registration_event(
13084@pytest .mark .usefixtures ("_spy_action_registry_get_action_handlers" )
13185@pytest .mark .usefixtures ("_spy_event_adapter_validate_json" )
13286def test_plugin_manager_process_event (
133- patch_websocket_client : tuple [MagicMock , EventBase ], plugin_manager : PluginManager
87+ mock_websocket_client_with_event : tuple [Mock , EventBase ], plugin_manager : PluginManager
13488):
13589 """Test that PluginManager processes events correctly, calling event_adapter.validate_json and action_registry.get_action_handlers."""
136- mock_websocket_client , fake_event_message = patch_websocket_client
90+ mock_websocket_client , fake_event_message = mock_websocket_client_with_event
13791
13892 plugin_manager .run ()
13993
0 commit comments