| 1 |
"""Tests for SensorsMenu: sensor visibility management and callbacks.""" |
| 2 |
|
| 3 |
from unittest.mock import MagicMock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from s_tui.sensors_menu import SensorsMenu |
| 8 |
|
| 9 |
|
| 10 |
def _make_mock_source(name, sensors): |
| 11 |
"""Create a MockSource-like object with a name and sensor list.""" |
| 12 |
src = MagicMock() |
| 13 |
src.get_source_name.return_value = name |
| 14 |
src.get_sensor_list.return_value = sensors |
| 15 |
return src |
| 16 |
|
| 17 |
|
| 18 |
@pytest.fixture |
| 19 |
def simple_sources(): |
| 20 |
"""Two sources with 2 and 3 sensors respectively.""" |
| 21 |
return [ |
| 22 |
_make_mock_source("CPU Util", ["Avg", "Core 0"]), |
| 23 |
_make_mock_source("Temp", ["Core 0", "Core 1", "Core 2"]), |
| 24 |
] |
| 25 |
|
| 26 |
|
| 27 |
@pytest.fixture |
| 28 |
def default_conf(): |
| 29 |
"""Default config: all sensors visible (dict keyed by lowercase sensor name).""" |
| 30 |
return { |
| 31 |
"CPU Util": {"avg": True, "core 0": True}, |
| 32 |
"Temp": {"core 0": True, "core 1": True, "core 2": True}, |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
@pytest.fixture |
| 37 |
def menu(simple_sources, default_conf): |
| 38 |
return_fn = MagicMock() |
| 39 |
return SensorsMenu(return_fn, simple_sources, default_conf) |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class TestSensorsMenuInit: |
| 48 |
def test_active_sensors_match_defaults(self, menu, default_conf): |
| 49 |
"""active_sensors should mirror the default config values.""" |
| 50 |
for name, conf_dict in default_conf.items(): |
| 51 |
assert menu.active_sensors[name] == list(conf_dict.values()) |
| 52 |
|
| 53 |
def test_sensor_button_dict_populated(self, menu): |
| 54 |
"""Each source should have checkbox entries.""" |
| 55 |
assert "CPU Util" in menu.sensor_button_dict |
| 56 |
assert "Temp" in menu.sensor_button_dict |
| 57 |
assert len(menu.sensor_button_dict["CPU Util"]) == 2 |
| 58 |
assert len(menu.sensor_button_dict["Temp"]) == 3 |
| 59 |
|
| 60 |
def test_get_size(self, menu): |
| 61 |
"""get_size returns a (height, width) tuple.""" |
| 62 |
size = menu.get_size() |
| 63 |
assert isinstance(size, tuple) |
| 64 |
assert len(size) == 2 |
| 65 |
assert size[1] == SensorsMenu.MAX_TITLE_LEN |
| 66 |
|
| 67 |
def test_main_window_is_urwid_widget(self, menu): |
| 68 |
"""main_window should be a urwid LineBox.""" |
| 69 |
import urwid |
| 70 |
|
| 71 |
assert isinstance(menu.main_window, urwid.LineBox) |
| 72 |
|
| 73 |
def test_no_default_conf_defaults_to_all_true(self, simple_sources): |
| 74 |
"""When default_source_conf entry is falsy, all sensors default True.""" |
| 75 |
from collections import defaultdict |
| 76 |
|
| 77 |
conf = defaultdict(dict) |
| 78 |
return_fn = MagicMock() |
| 79 |
m = SensorsMenu(return_fn, simple_sources, conf) |
| 80 |
assert m.active_sensors["CPU Util"] == [True, True] |
| 81 |
assert m.active_sensors["Temp"] == [True, True, True] |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
class TestSensorsMenuCancel: |
| 90 |
def test_on_cancel_calls_return_fn_with_update_false(self, menu): |
| 91 |
"""on_cancel should call return_fn(update=False).""" |
| 92 |
menu.on_cancel(None) |
| 93 |
menu.return_fn.assert_called_once_with(update=False) |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
class TestSensorsMenuApply: |
| 102 |
def test_on_apply_no_changes(self, menu): |
| 103 |
"""on_apply with no changes calls return_fn(update=False).""" |
| 104 |
menu.on_apply(None) |
| 105 |
menu.return_fn.assert_called_once_with(update=False) |
| 106 |
|
| 107 |
def test_on_apply_with_changes(self, menu): |
| 108 |
"""on_apply after toggling a checkbox calls return_fn(update=True).""" |
| 109 |
|
| 110 |
menu.sensor_button_dict["Temp"][0].set_state(False) |
| 111 |
menu.on_apply(None) |
| 112 |
menu.return_fn.assert_called_once_with(update=True) |
| 113 |
assert menu.active_sensors["Temp"][0] is False |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
class TestSensorsMenuCheckAll: |
| 122 |
def test_setall_cb_col(self, menu): |
| 123 |
"""setall_cb_col sets all checkboxes in a named column.""" |
| 124 |
|
| 125 |
menu.setall_cb_col(None, "Temp", False) |
| 126 |
for cb in menu.sensor_button_dict["Temp"]: |
| 127 |
assert cb.get_state() is False |
| 128 |
|
| 129 |
|
| 130 |
menu.setall_cb_col(None, "Temp", True) |
| 131 |
for cb in menu.sensor_button_dict["Temp"]: |
| 132 |
assert cb.get_state() is True |
| 133 |
|
| 134 |
def test_setall_ignores_other_columns(self, menu): |
| 135 |
"""setall_cb_col should only affect the matching source.""" |
| 136 |
menu.setall_cb_col(None, "Temp", False) |
| 137 |
|
| 138 |
for cb in menu.sensor_button_dict["CPU Util"]: |
| 139 |
assert cb.get_state() is True |