sh2/d2ypp2/tests/test_ops_center.py
eric 9888f1daad Harden YouTube unmanned live stack and hide advanced console panels.
Fix Pro config SSOT, event FSM, FFmpeg error tiers, relay heartbeat, and ingest auth while keeping relay snapshot, quality guard, and event hub hidden in the UI.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-23 06:31:31 -05:00

99 lines
4.5 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,
get_ops_playbook,
ops_playbooks,
ops_playbooks_with_service_state,
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_ops_playbooks_define_executable_service_actions(self) -> None:
rows = ops_playbooks()
by_id = {item["id"]: item for item in rows}
self.assertIn("youtube-live-ready", by_id)
youtube = get_ops_playbook("youtube-live-ready")
self.assertIn("shellcrash", youtube["services"])
self.assertIn("srs", youtube["services"])
self.assertIn("neko", youtube["services"])
self.assertTrue(youtube["snapshot"])
self.assertTrue(all(step.get("service") and step.get("action") for step in youtube["actions"]))
def test_ops_playbooks_include_dependency_runtime_state(self) -> None:
rows = ops_playbooks_with_service_state(
[
{"service_id": "shellcrash", "available": True, "running": True, "status": "online"},
{"service_id": "srs", "available": True, "running": False, "status": "stopped"},
{"service_id": "neko", "available": False, "running": False, "status": "unavailable"},
]
)
youtube = next(item for item in rows if item["id"] == "youtube-live-ready")
self.assertEqual(youtube["required_count"], 3)
self.assertEqual(youtube["running_count"], 1)
self.assertEqual(youtube["available_count"], 2)
self.assertFalse(youtube["ready"])
self.assertEqual(youtube["status"], "partial")
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["playbooks"])
self.assertTrue(summary["recipes"])
if __name__ == "__main__":
unittest.main()