from __future__ import annotations import tempfile import unittest from pathlib import Path from src.youtube_live_store import ( close_live_event, get_live_event, ingest_browser_telemetry, list_live_events, record_control_action, record_process_observation, youtube_live_summary, ) class YoutubeLiveStoreTests(unittest.TestCase): 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"]) if __name__ == "__main__": unittest.main()