sh2/d2ypp2/tests/test_youtube_relay_slot.py
eric 278b48497e Add YouTube relay log tooling and optional hub service controls.
This captures the current live stack updates across the API, Linux service scripts, tests, diagnostics, and exported console assets so the Ubuntu deployment can pull a consistent test3 build.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-30 05:10:09 -05:00

81 lines
2.8 KiB
Python

from __future__ import annotations
import time
import unittest
from unittest.mock import patch
from src.youtube_relay_slot import (
claim_relay_slot,
prune_orphan_relay_recording,
relay_slot_blocks,
release_relay_slot,
youtube_stream_fallback_urls,
)
class YoutubeRelaySlotTests(unittest.TestCase):
def setUp(self) -> None:
release_relay_slot("序号1 主播A")
release_relay_slot("序号2 主播B")
def test_relay_slot_blocks_other_channel_only(self) -> None:
recording = {"序号1 主播A"}
self.assertTrue(relay_slot_blocks("序号2 主播B", recording))
self.assertFalse(relay_slot_blocks("序号1 主播A", recording))
def test_prune_only_when_recording_without_heartbeat(self) -> None:
recording = {"序号1 主播A"}
times = {"序号1 主播A": ["now", "原画"]}
with patch("src.youtube_relay_slot.read_relay_heartbeat", return_value={}):
prune_orphan_relay_recording(
recording,
times,
pm2="youtube2__lane1",
base_dir="/tmp",
)
self.assertEqual(recording, set())
self.assertEqual(times, {})
def test_prune_keeps_slot_when_heartbeat_exists(self) -> None:
recording = {"序号1 主播A"}
times = {"序号1 主播A": ["now", "原画"]}
with patch(
"src.youtube_relay_slot.read_relay_heartbeat",
return_value={"state": "streaming", "updated_at": time.time()},
):
prune_orphan_relay_recording(
recording,
times,
pm2="youtube2__lane1",
base_dir="/tmp",
)
self.assertEqual(recording, {"序号1 主播A"})
def test_claim_and_release_slot(self) -> None:
with patch("src.youtube_relay_slot.write_relay_heartbeat"):
self.assertTrue(
claim_relay_slot("序号1 主播A", pm2="youtube2__lane1", base_dir="/tmp"),
)
self.assertFalse(
claim_relay_slot("序号2 主播B", pm2="youtube2__lane1", base_dir="/tmp"),
)
release_relay_slot("序号1 主播A", pm2="youtube2__lane1", base_dir="/tmp")
self.assertTrue(
claim_relay_slot("序号2 主播B", pm2="youtube2__lane1", base_dir="/tmp"),
)
def test_stream_fallback_urls_same_room_only(self) -> None:
urls = youtube_stream_fallback_urls(
{
"flv_url": "http://pull-flv.example.com/a.flv",
"m3u8_url": "http://pull-hls.example.com/a.m3u8",
"record_url": "https://live.douyin.com/123",
},
"http://pull-flv.example.com/a.flv",
)
self.assertEqual(urls, ["http://pull-hls.example.com/a.m3u8"])
if __name__ == "__main__":
unittest.main()