douyinWEB/backend/tests/test_youtube_live.py
2026-09-13 22:10:49 -05:00

135 lines
4.1 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import annotations
import time
from app.youtube_live import (
_live_entry,
_parse_batch_result,
_studio_receiving_from_snap,
_studio_snap_belongs_to_assign_group,
)
def test_parse_batch_live():
row = {
"ok": True,
"is_live": True,
"channel_id": "UCabc",
"title": "Channel",
"live": {"id": "vid123456789", "title": "Stream Title", "url": "https://www.youtube.com/watch?v=vid123456789"},
}
entry = _parse_batch_result(row)
assert entry["is_live"] is True
assert entry["source"] == "youtubei"
assert "watch?v=vid123456789" in entry["live_url"]
assert entry["title"] == "Stream Title"
def test_parse_batch_offline():
row = {"ok": True, "is_live": False, "channel_id": "UCabc"}
entry = _parse_batch_result(row)
assert entry["is_live"] is False
assert "live_url" not in entry
def test_parse_batch_error():
row = {"ok": False, "error": "timeout"}
entry = _parse_batch_result(row)
assert entry["is_live"] is False
assert entry.get("error")
def test_live_entry_minimal():
entry = _live_entry(is_live=True, source="oauth", live_url="https://youtube.com/watch?v=x", title="T")
assert entry["is_live"]
assert entry["source"] == "oauth"
def test_parse_broadcast_shaped_as_search_not_required():
"""Regression: offline oauth entry keeps channel_id."""
entry = _live_entry(is_live=False, source="oauth-broadcast", channel_id="UCabc")
assert entry["is_live"] is False
assert entry["channel_id"] == "UCabc"
assert entry["source"] == "oauth-broadcast"
def test_studio_receiving_rejects_edge_mirror_and_preparing():
assert not _studio_receiving_from_snap(
{
"on_air": True,
"encoder_receiving": True,
"live_status": "live",
"detect_source": "edge_assign_live",
"received_at": time.time(),
}
)
assert not _studio_receiving_from_snap(
{
"on_air": True,
"preparing_stream": True,
"live_status": "preparing",
"detect_source": "cdp_dom",
"received_at": time.time(),
}
)
assert not _studio_receiving_from_snap(
{
"on_air": True,
"live_status": "live",
"detect_source": "chrome_extension",
"received_at": time.time() - 120,
}
)
assert _studio_receiving_from_snap(
{
"on_air": True,
"encoder_receiving": True,
"live_status": "live",
"detect_source": "chrome_extension",
"received_at": time.time() - 10,
}
)
def test_studio_snap_shared_neko_requires_ownership():
snap = {
"on_air": True,
"encoder_receiving": True,
"live_status": "live",
"url": "https://studio.youtube.com/video/pMQedt4HENo/livestreaming",
"detect_source": "chrome_extension",
"received_at": time.time() - 5,
}
# Sibling card on shared Neko, not pushing, wrong UC → must not claim receiving.
assert not _studio_snap_belongs_to_assign_group(
snap,
youtube_channel_id="UCtVfMFdLTN4ap__8g7MFtVw",
neko_shared=True,
group_is_pushing=False,
)
# Owner pushing on that Neko may claim it.
assert _studio_snap_belongs_to_assign_group(
snap,
youtube_channel_id="UCO9wMc9tj9mD221Gu0rLBeA",
neko_shared=True,
group_is_pushing=True,
)
# Channel path in Studio URL binds unambiguously.
assert _studio_snap_belongs_to_assign_group(
{
**snap,
"url": "https://studio.youtube.com/channel/UCtVfMFdLTN4ap__8g7MFtVw/livestreaming",
},
youtube_channel_id="UCtVfMFdLTN4ap__8g7MFtVw",
neko_shared=True,
group_is_pushing=False,
)
# Saved studio video hint matches snap URL.
assert _studio_snap_belongs_to_assign_group(
snap,
youtube_channel_id="UCtVfMFdLTN4ap__8g7MFtVw",
neko_shared=True,
group_is_pushing=False,
studio_url_hint="https://studio.youtube.com/video/pMQedt4HENo/livestreaming",
)