Files
MonitorSwitcher/tests/test_device_watcher.py

63 lines
1.8 KiB
Python

import time
from unittest.mock import MagicMock
import pytest
from config.config_manager import ConfigManager
from core.device_watcher import DeviceWatcher
@pytest.fixture
def config(tmp_path):
return ConfigManager(tmp_path / "config.json")
@pytest.fixture
def switcher():
return MagicMock()
def test_handle_event_connect_triggers_correct_profile(config, switcher):
watcher = DeviceWatcher(config, switcher)
watcher._handle_event(
"HID\\{...}_DEV_VID&0203F0_PID&6343_REV&0413_AABBCCDD", "connect"
)
time.sleep(0.6)
switcher.apply_profile.assert_called_once_with("pc1_dp")
def test_handle_event_disconnect_triggers_correct_profile(config, switcher):
watcher = DeviceWatcher(config, switcher)
watcher._handle_event(
"HID\\{...}_DEV_VID&0203F0_PID&6343_REV&0413_AABBCCDD", "disconnect"
)
time.sleep(0.6)
switcher.apply_profile.assert_called_once_with("pc2_hdmi")
def test_handle_event_ignores_unknown_device(config, switcher):
watcher = DeviceWatcher(config, switcher)
watcher._handle_event("HID\\VID&DEAD_PID&BEEF", "connect")
time.sleep(0.6)
switcher.apply_profile.assert_not_called()
def test_handle_event_ignores_disabled_trigger(config, switcher):
cfg = config.get()
cfg["device_triggers"][0]["enabled"] = False
config.update(cfg)
watcher = DeviceWatcher(config, switcher)
watcher._handle_event(
"HID\\{...}_DEV_VID&0203F0_PID&6343_REV&0413", "connect"
)
time.sleep(0.6)
switcher.apply_profile.assert_not_called()
def test_debounce_collapses_rapid_events(config, switcher):
watcher = DeviceWatcher(config, switcher)
device_id = "HID\\{...}_DEV_VID&0203F0_PID&6343_REV&0413"
for _ in range(5):
watcher._handle_event(device_id, "connect")
time.sleep(0.8)
assert switcher.apply_profile.call_count == 1