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