Tighten http/youtubei live probes, classify Studio encoder_lost vs waiting, and keep dy/dylive public live counts aligned via edge assign_lives. Co-authored-by: Cursor <cursoragent@cursor.com>
155 lines
6.1 KiB
Python
155 lines
6.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
from app import youtube_neko
|
|
|
|
|
|
def _reset_events() -> None:
|
|
youtube_neko._studio_events.clear() # noqa: SLF001
|
|
|
|
|
|
def test_payload_stream_confirmed_live_status() -> None:
|
|
assert youtube_neko._payload_stream_confirmed({"live_status": "live"}) is True
|
|
# health alone / streaming (page-open mirror) must NOT count as on-air
|
|
assert youtube_neko._payload_stream_confirmed({"health": "excellent"}) is False
|
|
assert youtube_neko._payload_stream_confirmed({"live_status": "streaming"}) is False
|
|
assert youtube_neko._payload_stream_confirmed({"live_status": "waiting"}) is False
|
|
assert youtube_neko._payload_stream_confirmed({"live_status": "ready"}) is False
|
|
assert youtube_neko._payload_stream_confirmed({"viewer_count": 0, "url": "https://studio.youtube.com/x/livestreaming"}) is False
|
|
assert youtube_neko._payload_stream_confirmed({"viewer_count": 3, "url": "https://studio.youtube.com/x/livestreaming"}) is True
|
|
assert (
|
|
youtube_neko._payload_stream_confirmed(
|
|
{"text": "No data\nConnect your encoder to go live", "live_status": "waiting"}
|
|
)
|
|
is False
|
|
)
|
|
assert youtube_neko._payload_nodata_or_pending({"live_status": "waiting"}) is True
|
|
assert youtube_neko._payload_nodata_or_pending({"signals": {"nodata": True}}) is True
|
|
assert youtube_neko._payload_nodata_or_pending({"live_status": "live", "text": "You're live"}) is False
|
|
|
|
|
|
def test_studio_encoder_lost_is_duanbo_not_pending() -> None:
|
|
"""LIVE timer + No data + will end shortly = 断播, not 待开播."""
|
|
text = (
|
|
"LIVE\n8:15\nEnd Stream\nLive\nNo data\nKey:\nConnect your encoder to go live\n"
|
|
"No data - This stream will end shortly, unless you restart it in your streaming software\n"
|
|
"Concurrent viewers\n0"
|
|
)
|
|
payload = {
|
|
"url": "https://studio.youtube.com/video/1cQY6jHytxQ/livestreaming",
|
|
"live_status": "encoder_lost",
|
|
"live_duration": "8:15",
|
|
"live_duration_sec": 495,
|
|
"viewer_count": 0,
|
|
"text": text,
|
|
"signals": {
|
|
"path": "/livestreaming",
|
|
"nodata": True,
|
|
"encoder_lost": True,
|
|
"session_live": True,
|
|
"pending_go_live": False,
|
|
},
|
|
}
|
|
assert youtube_neko._payload_session_live(payload) is True
|
|
assert youtube_neko._payload_encoder_lost(payload) is True
|
|
assert youtube_neko._payload_stream_confirmed(payload) is False
|
|
assert youtube_neko._payload_nodata_or_pending(payload) is False
|
|
|
|
# Infer from text alone (no explicit status)
|
|
inferred = {
|
|
"url": "https://studio.youtube.com/video/1cQY6jHytxQ/livestreaming",
|
|
"text": text,
|
|
"live_duration": "8:15",
|
|
"signals": {"path": "/livestreaming", "nodata": True},
|
|
}
|
|
assert youtube_neko._payload_session_live(inferred) is True
|
|
assert youtube_neko._payload_encoder_lost(inferred) is True
|
|
assert youtube_neko._payload_nodata_or_pending(inferred) is False
|
|
|
|
# True 待开播: nodata without LIVE session
|
|
pending = {
|
|
"url": "https://studio.youtube.com/video/x/livestreaming",
|
|
"live_status": "waiting",
|
|
"text": "No data\nConnect your encoder to go live\nStart sending us your video",
|
|
"signals": {"nodata": True, "pending_go_live": True},
|
|
}
|
|
assert youtube_neko._payload_session_live(pending) is False
|
|
assert youtube_neko._payload_encoder_lost(pending) is False
|
|
assert youtube_neko._payload_nodata_or_pending(pending) is True
|
|
|
|
|
|
|
|
def test_ingest_remote_studio_mirror_does_not_fake_streaming() -> None:
|
|
youtube_neko._studio_events.clear() # noqa: SLF001
|
|
youtube_neko._studio_latest_by_ref.clear() # noqa: SLF001
|
|
out = youtube_neko.ingest_remote_studio_mirror(
|
|
2,
|
|
{
|
|
"live_page_open": True,
|
|
"on_air": False,
|
|
"live_status": "",
|
|
"url": "https://studio.youtube.com/video/L1sF_kZvzIk/livestreaming",
|
|
},
|
|
)
|
|
assert out.get("ok") is not False or out # ingest returns dict
|
|
snap = youtube_neko.studio_snapshot_for_ref("ops:2", max_age_sec=60.0)
|
|
assert snap.get("live_page_open") is True
|
|
assert snap.get("on_air") is False
|
|
assert str(snap.get("live_status") or "") in {"waiting", ""}
|
|
|
|
|
|
def test_latest_live_studio_event_since_ts() -> None:
|
|
_reset_events()
|
|
now = time.time()
|
|
youtube_neko.ingest_studio_telemetry(
|
|
{
|
|
"url": "https://studio.youtube.com/channel/abc/livestreaming",
|
|
"live_status": "waiting",
|
|
"received_at": now - 10,
|
|
}
|
|
)
|
|
assert youtube_neko.latest_live_studio_event(max_age_sec=60.0, since_ts=now) is None
|
|
youtube_neko.ingest_studio_telemetry(
|
|
{
|
|
"url": "https://studio.youtube.com/channel/abc/livestreaming",
|
|
"live_status": "live",
|
|
"received_at": now + 1,
|
|
}
|
|
)
|
|
ev = youtube_neko.latest_live_studio_event(max_age_sec=60.0, since_ts=now)
|
|
assert ev is not None
|
|
assert youtube_neko.youtube_live_confirmed(max_age_sec=60.0, since_ts=now)["live"] is True
|
|
|
|
|
|
def test_youtube_live_confirmed_no_event() -> None:
|
|
_reset_events()
|
|
out = youtube_neko.youtube_live_confirmed(max_age_sec=30.0)
|
|
assert out["ok"] is False
|
|
assert out["live"] is False
|
|
|
|
|
|
def test_maybe_append_minute_report_persists_source(tmp_path, monkeypatch) -> None:
|
|
_reset_events()
|
|
youtube_neko._studio_minute_reports.clear() # noqa: SLF001
|
|
youtube_neko._studio_minute_last_at.clear() # noqa: SLF001
|
|
monkeypatch.setattr(youtube_neko, "_neko_root", lambda: tmp_path)
|
|
now = time.time()
|
|
youtube_neko.ingest_studio_telemetry(
|
|
{
|
|
"neko_instance_id": 3,
|
|
"url": "https://studio.youtube.com/video/abc/livestreaming",
|
|
"live_status": "live",
|
|
"health": "excellent",
|
|
"viewer_count": 12,
|
|
"source": "cdp",
|
|
"received_at": now,
|
|
}
|
|
)
|
|
rows = youtube_neko.list_studio_minute_reports(3, limit=5)
|
|
assert rows and rows[0]["on_air"] is True
|
|
assert rows[0].get("source") == "cdp"
|
|
disk = tmp_path / "studio_minute" / "3.jsonl"
|
|
assert disk.is_file()
|