Debian Codegraph @

tests/test_temp_source.py

raw symbols SCIP
1 """Tests for TempSource with mocked psutil."""
2
3 from collections import OrderedDict
4
5 import pytest
6
7 from s_tui.sources.temp_source import TempSource
8 from tests.conftest import SensorTemperature
9
10
11 def _make_temp_dict(sensors_list, group_name="coretemp"):
12 return OrderedDict([(group_name, sensors_list)])
13
14
15 @pytest.fixture
16 def basic_temp_mock(mocker):
17 """Mock sensors_temperatures with 2 valid sensors."""
18 sensors = [
19 SensorTemperature(label="Core 0", current=55.0, high=80.0, critical=100.0),
20 SensorTemperature(label="Core 1", current=60.0, high=80.0, critical=100.0),
21 ]
22 temps = _make_temp_dict(sensors)
23 mocker.patch("psutil.sensors_temperatures", return_value=temps)
24 return temps
25
26
27 class TestTempSourceInit:
28 def test_name(self, basic_temp_mock):
29 src = TempSource()
30 assert src.get_source_name() == "Temp"
31
32 def test_measurement_unit(self, basic_temp_mock):
33 src = TempSource()
34 assert src.get_measurement_unit() == "C"
35
36 def test_is_available(self, basic_temp_mock):
37 src = TempSource()
38 assert src.get_is_available() is True
39
40 def test_sensor_count(self, basic_temp_mock):
41 src = TempSource()
42 assert len(src.get_sensor_list()) == 2
43
44 def test_default_threshold(self, basic_temp_mock):
45 src = TempSource()
46 assert src.temp_thresh == 80
47
48 def test_custom_threshold(self, basic_temp_mock):
49 src = TempSource(temp_thresh=90)
50 assert src.temp_thresh == 90
51
52 def test_threshold_list_length(self, basic_temp_mock):
53 src = TempSource()
54 assert len(src.get_threshold_list()) == 2
55
56 def test_pallet(self, basic_temp_mock):
57 src = TempSource()
58 assert "temp" in src.get_pallet()[0]
59
60 def test_alert_pallet(self, basic_temp_mock):
61 src = TempSource()
62 pallet = src.get_alert_pallet()
63 assert pallet is not None
64 assert "high temp" in pallet[0]
65
66
67 class TestTempSourceFiltering:
68 def test_filters_out_low_temp(self, mocker):
69 sensors = [
70 SensorTemperature(label="Good", current=55.0, high=80.0, critical=100.0),
71 SensorTemperature(label="Low", current=0.5, high=80.0, critical=100.0),
72 ]
73 mocker.patch(
74 "psutil.sensors_temperatures", return_value=_make_temp_dict(sensors)
75 )
76 src = TempSource()
77 assert len(src.get_sensor_list()) == 1
78
79 def test_filters_out_high_temp(self, mocker):
80 sensors = [
81 SensorTemperature(label="Good", current=55.0, high=80.0, critical=100.0),
82 SensorTemperature(label="High", current=127.5, high=80.0, critical=100.0),
83 ]
84 mocker.patch(
85 "psutil.sensors_temperatures", return_value=_make_temp_dict(sensors)
86 )
87 src = TempSource()
88 assert len(src.get_sensor_list()) == 1
89
90
91 class TestTempSourceUpdate:
92 def test_update_populates_values(self, basic_temp_mock):
93 src = TempSource()
94 src.update()
95 readings = src.get_reading_list()
96 assert readings[0] == pytest.approx(55.0)
97 assert readings[1] == pytest.approx(60.0)
98
99 def test_update_thresholds(self, basic_temp_mock):
100 src = TempSource()
101 src.update()
102 thresholds = src.get_threshold_list()
103 # Sensors have high=80.0, so thresholds should use that
104 assert all(t == 80.0 for t in thresholds)
105
106
107 class TestTempSourceEdgeTriggered:
108 def test_not_triggered_below_thresh(self, basic_temp_mock):
109 src = TempSource()
110 src.update()
111 assert src.get_edge_triggered() is False
112
113 def test_triggered_above_thresh(self, mocker):
114 sensors = [
115 SensorTemperature(label="Hot", current=85.0, high=80.0, critical=100.0),
116 ]
117 mocker.patch(
118 "psutil.sensors_temperatures", return_value=_make_temp_dict(sensors)
119 )
120 src = TempSource()
121 src.update()
122 assert src.get_edge_triggered() is True
123
124 def test_reset(self, basic_temp_mock):
125 src = TempSource()
126 src.update()
127 src.reset()
128 assert src.max_temp == 10
129
130
131 class TestTempSourceAlerts:
132 def test_no_alerts_below_threshold(self, basic_temp_mock):
133 """Sensors at 55/60 with threshold 80 should have no alerts."""
134 src = TempSource()
135 src.update()
136 alerts = src.get_sensor_alerts()
137 assert all(a is None for a in alerts)
138
139 def test_alert_when_above_threshold(self, mocker):
140 """Sensor above its threshold should get 'high temp txt' alert."""
141 sensors = [
142 SensorTemperature(label="Hot", current=85.0, high=80.0, critical=100.0),
143 SensorTemperature(label="Cool", current=50.0, high=80.0, critical=100.0),
144 ]
145 mocker.patch(
146 "psutil.sensors_temperatures", return_value=_make_temp_dict(sensors)
147 )
148 src = TempSource()
149 src.update()
150 alerts = src.get_sensor_alerts()
151 assert alerts[0] == "high temp txt"
152 assert alerts[1] is None
153
154 def test_alert_matches_graph_coloring(self, mocker):
155 """Alert should trigger for exactly the same sensors as graph coloring."""
156 sensors = [
157 SensorTemperature(label="Core 0", current=82.0, high=80.0, critical=100.0),
158 SensorTemperature(label="Core 1", current=78.0, high=80.0, critical=100.0),
159 SensorTemperature(label="Core 2", current=90.0, high=85.0, critical=100.0),
160 ]
161 mocker.patch(
162 "psutil.sensors_temperatures", return_value=_make_temp_dict(sensors)
163 )
164 src = TempSource()
165 src.update()
166 alerts = src.get_sensor_alerts()
167 # Core 0: 82 > 80 threshold → alert
168 assert alerts[0] == "high temp txt"
169 # Core 1: 78 < 80 threshold, but global triggered (82 > 80) and has per-sensor
170 # threshold, so only per-sensor check applies → no alert
171 assert alerts[1] is None
172 # Core 2: 90 > 85 threshold → alert
173 assert alerts[2] == "high temp txt"