sh2/d2ypp2/tests/test_youtube_live_store.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

150 lines
6.0 KiB
Python

from __future__ import annotations
import tempfile
import unittest
from pathlib import Path
from src.youtube_live_store import (
close_live_event,
_connect,
get_live_event,
ingest_browser_telemetry,
list_live_events,
record_control_action,
record_process_observation,
youtube_live_summary,
)
class YoutubeLiveStoreTests(unittest.TestCase):
def test_connection_enables_foreign_keys_and_wal_when_supported(self) -> None:
with tempfile.TemporaryDirectory() as td:
db = Path(td) / "hub_state.sqlite"
conn = _connect(db)
try:
self.assertEqual(conn.execute("PRAGMA foreign_keys").fetchone()[0], 1)
self.assertEqual(conn.execute("PRAGMA busy_timeout").fetchone()[0], 12000)
self.assertIn(conn.execute("PRAGMA journal_mode").fetchone()[0].lower(), {"wal", "memory"})
finally:
conn.close()
def test_control_action_creates_traceable_event(self) -> None:
with tempfile.TemporaryDirectory() as td:
db = Path(td) / "hub_state.sqlite"
result = record_control_action(
"youtube",
"start",
script="youtube.py",
url_lines="https://live.douyin.com/12345678\n",
youtube_ini_text="[youtube]\nkey = abcd-efgh-ijkl\n",
db_path=db,
)
event = result["event"]
self.assertEqual(event["process"], "youtube")
self.assertEqual(event["source_room_id"], "12345678")
self.assertEqual(event["youtube_key_suffix"], "h-ijkl")
self.assertEqual(event["status"], "active")
closed = close_live_event(event["id"], db_path=db)
self.assertEqual(closed["event"]["status"], "ended")
self.assertIsNotNone(closed["event"]["ended_at"])
def test_browser_telemetry_adds_samples_and_alerts(self) -> None:
with tempfile.TemporaryDirectory() as td:
db = Path(td) / "hub_state.sqlite"
result = ingest_browser_telemetry(
{
"process": "youtube2__lane1",
"url": "https://studio.youtube.com/video/abc/livestreaming",
"page_title": "Live Control Room",
"stream_title": "Relay: Douyin room",
"source_url": "https://live.douyin.com/9988",
"live_status": "live",
"health": "excellent",
"viewer_count": 42,
"popups": ["Stream is healthy"],
},
db_path=db,
)
event = result["event"]
self.assertEqual(event["source_room_id"], "9988")
self.assertEqual(event["last_viewer_count"], 42)
self.assertGreaterEqual(event["quality_score"], 80)
detail = get_live_event(event["id"], db_path=db)
self.assertEqual(detail["sample_count"], 1)
self.assertEqual(len(detail["samples"]), 1)
self.assertEqual(len(detail["alerts"]), 1)
listing = list_live_events(limit=10, db_path=db)
self.assertEqual(listing["total"], 1)
summary = youtube_live_summary(db_path=db)
self.assertEqual(summary["stats"]["active_events"], 1)
self.assertIn("browser_extension_telemetry", summary["capabilities"])
def test_inactive_process_observation_closes_active_event(self) -> None:
with tempfile.TemporaryDirectory() as td:
db = Path(td) / "hub_state.sqlite"
created = record_control_action(
"youtube",
"restart",
url_lines="https://live.douyin.com/456789\n",
youtube_ini_text="[youtube]\nkey = test-key-456789\n",
db_path=db,
)
self.assertEqual(created["event"]["status"], "active")
result = record_process_observation(
"youtube",
{
"raw_status": "Process is configured but not running",
"process_status": "configured",
"business_status": "ready",
"business_note": "Process is configured but not running",
"ffmpeg_active": False,
"is_pushing": False,
},
url_lines="https://live.douyin.com/456789\n",
db_path=db,
)
self.assertEqual(result["count"], 1)
summary = youtube_live_summary(db_path=db)
self.assertEqual(summary["stats"]["active_events"], 0)
detail = get_live_event(created["event"]["id"], db_path=db)
self.assertEqual(detail["status"], "ended")
self.assertIsNotNone(detail["ended_at"])
def test_relay_starting_process_observation_keeps_active_event(self) -> None:
with tempfile.TemporaryDirectory() as td:
db = Path(td) / "hub_state.sqlite"
created = record_control_action(
"youtube",
"start",
url_lines="https://live.douyin.com/456789\n",
youtube_ini_text="[youtube]\nkey = test-key-456789\n",
db_path=db,
)
self.assertEqual(created["event"]["status"], "active")
result = record_process_observation(
"youtube",
{
"process_status": "online",
"business_status": "relay_starting",
"business_note": "FFmpeg worker is present, but no video progress has been observed yet",
"ffmpeg_active": True,
"is_pushing": False,
},
url_lines="https://live.douyin.com/456789\n",
db_path=db,
)
self.assertNotIn("count", result)
summary = youtube_live_summary(db_path=db)
self.assertEqual(summary["stats"]["active_events"], 1)
if __name__ == "__main__":
unittest.main()