sh2/d2ypp2/tests/test_ops_center.py
2026-06-03 10:53:02 -05:00

62 lines
3.0 KiB
Python

from __future__ import annotations
import tempfile
import unittest
import zipfile
from pathlib import Path
from unittest.mock import patch
from src.ops_center import app_blueprints, create_system_snapshot, studio_ops_summary, system_doctor
class OpsCenterTests(unittest.TestCase):
def test_system_doctor_scores_critical_service_failures(self) -> None:
services = [
{"service_id": "srs", "available": True, "running": False, "detail": "stopped"},
{"service_id": "neko", "available": True, "running": True, "detail": "running"},
{"service_id": "wol", "available": True, "running": False, "detail": "ready"},
]
doctor = system_doctor(services, {"ready": True, "pass_count": 3, "total_count": 3})
self.assertEqual(doctor["critical"], 1)
self.assertLess(doctor["score"], 100)
self.assertIn("srs", "\n".join(doctor["recommendations"]))
def test_app_blueprints_mark_installed_services(self) -> None:
rows = app_blueprints({"homeassistant", "coturn"})
by_id = {item["id"]: item for item in rows}
self.assertTrue(by_id["homeassistant"]["installed"])
self.assertTrue(by_id["coturn"]["installed"])
self.assertFalse(by_id["freshrss"]["installed"])
def test_create_system_snapshot_includes_managed_configs(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
base = Path(tmp)
(base / "config").mkdir()
(base / "config" / "system-stack.env").write_text("PORT=8001\n", encoding="utf-8")
(base / "services" / "ntfy" / "config").mkdir(parents=True)
(base / "services" / "ntfy" / "config" / "server.yml").write_text("base-url: http://live.local:8089\n", encoding="utf-8")
result = create_system_snapshot(base, reason="unit test")
snapshot_path = Path(result["snapshot"]["path"])
with zipfile.ZipFile(snapshot_path) as archive:
names = set(archive.namelist())
self.assertIn("manifest.json", names)
self.assertIn("config/system-stack.env", names)
self.assertIn("services/ntfy/config/server.yml", names)
self.assertGreater(result["snapshot"]["included"], 0)
def test_studio_ops_summary_combines_doctor_inventory_and_recipes(self) -> None:
services = [{"service_id": "wol", "available": True, "running": False, "detail": "ready"}]
with tempfile.TemporaryDirectory() as tmp:
with patch("src.ops_center.list_serial_ports", return_value=[{"path": "/dev/ttyUSB0"}]):
with patch("src.ops_center.list_video_devices", return_value=[{"path": "/dev/video0"}]):
summary = studio_ops_summary(services, {"ready": False, "pass_count": 1, "total_count": 2}, Path(tmp))
self.assertIn("doctor", summary)
self.assertEqual(summary["inventory"]["counts"]["serial"], 1)
self.assertEqual(summary["inventory"]["counts"]["video"], 1)
self.assertTrue(summary["recipes"])
if __name__ == "__main__":
unittest.main()