Files
the_kurgan/tests/test_alerts.py
T
paul ba39016d52 Sensor health in the API and UI; balloon tips when something starts going wrong
- telemetry: _source() now remembers each failing source's error message, not
  just the log timestamp; health() reports them and every snapshot carries a
  "health" key, so /api/state shows which sensor source is failing and since
  when. A source that reads again clears its entry.
- Web UI: a header pill counts the failing sources; hovering lists the errors.
- tray: notify() shows a balloon tip from any thread (queued on the tray
  window, NIF_INFO). app.py grows an alert monitor: every 5 s it diffs the
  current bad states (fan failsafes/control errors, AI backend errors, failing
  sensor sources) against the last look and balloons only what is new. The
  first look is the baseline: problems that predate the tray icon stay quiet.
- Tests: test_telemetry.py (5) and test_alerts.py (7); README updated.
2026-12-05 17:51:00 +13:00

98 lines
4.0 KiB
Python

import unittest
from unittest.mock import Mock, patch
from kurgan import tray
from kurgan.app import alert_states, new_alerts
def fake_fans(gpu_status="GPU 50 C, 40% PWM", top_status="55 C, 45%"):
group = Mock()
group.name = "GPU"
group.status = gpu_status
top = Mock()
top.status = top_status
fans = Mock()
fans.groups = [group]
fans.top = top
return fans
def fake_ai(*errors):
ai = Mock()
ai.snapshot.return_value = {"backends": [{"key": key, "label": key.capitalize(), "error": error}
for key, error in errors]}
return ai
def fake_health(entries=None):
telemetry = Mock()
telemetry.health.return_value = entries or {}
return telemetry
class AlertStatesTest(unittest.TestCase):
def test_healthy_everything_is_quiet(self):
self.assertEqual(alert_states(fake_fans(), fake_ai(("strata", None), ("llama", None)),
fake_health()), {})
def test_failsafes_errors_and_failing_sources_are_collected(self):
states = alert_states(
fake_fans(gpu_status="FAILSAFE 100%: GPU temperature unavailable",
top_status="UNAVAILABLE (lost); BIOS curve keeps control"),
fake_ai(("strata", "exited (code 3); restarting."), ("llama", None)),
fake_health({"GPU sensors": {"error": "NVMLError: uninitialised", "age_s": 3.0}}))
self.assertEqual(states, {
"fans/GPU": "GPU case fans: FAILSAFE 100%: GPU temperature unavailable",
"fans/top": "Top fans: UNAVAILABLE (lost); BIOS curve keeps control",
"ai/strata": "Strata: exited (code 3); restarting.",
"telemetry/GPU sensors": "GPU sensors read failed (NVMLError: uninitialised); continuing without it",
})
def test_fans_or_ai_being_off_is_not_an_error(self):
self.assertEqual(alert_states(None, None, fake_health()), {})
class NewAlertsTest(unittest.TestCase):
def test_only_new_keys_alert_and_recovery_is_quiet(self):
known = {"fans/GPU": "an earlier message"}
states = {"fans/GPU": "still failing", "ai/strata": "exited"}
self.assertEqual(new_alerts(known, states), [("ai/strata", "exited")])
self.assertEqual(new_alerts(states, {}), []) # everything recovered: nothing new
self.assertEqual(new_alerts({}, states), [("ai/strata", "exited"), ("fans/GPU", "still failing")])
class TrayNotifyTest(unittest.TestCase):
def make_icon(self):
return tray.TrayIcon("The Kurgan", "unused.ico", on_exit=lambda: None)
def test_notify_queues_a_balloon_and_pings_the_tray_thread(self):
icon = self.make_icon()
icon.notify("The Kurgan", "Top fans: FAILSAFE 100%", warning=True) # no window yet: just stored
self.assertEqual(icon._info, ("The Kurgan", "Top fans: FAILSAFE 100%", tray.NIIF_WARNING))
icon._hwnd = 123
with patch.object(tray, "_u32") as u32:
icon.notify("The Kurgan", "hello")
u32.PostMessageW.assert_called_once_with(123, tray.WM_TRAY_INFO, 0, 0)
self.assertEqual(icon._info[2], tray.NIIF_NONE)
def test_messages_are_cut_to_what_the_structure_holds(self):
icon = self.make_icon()
icon.notify("t" * 100, "m" * 300)
title, message, _ = icon._info
self.assertEqual((len(title), len(message)), (63, 255))
def test_the_balloon_fields_are_filled_and_shown(self):
icon = self.make_icon()
icon._hwnd = 1
icon._info = ("The Kurgan", "GPU case fans: FAILSAFE 100%", tray.NIIF_WARNING)
nid = icon._info_nid()
self.assertEqual((nid.uFlags, nid.szInfoTitle, nid.szInfo, nid.dwInfoFlags),
(tray.NIF_INFO, "The Kurgan", "GPU case fans: FAILSAFE 100%", tray.NIIF_WARNING))
with patch.object(tray, "_shell32") as shell:
icon._show_info()
self.assertEqual(shell.Shell_NotifyIconW.call_args.args[0], tray.NIM_MODIFY)
if __name__ == "__main__":
unittest.main()