# -*- coding: utf-8 -*- from __future__ import annotations import time from app.timeutil import push_date_from_ts def test_shorts_stats_history(temp_db): ch = temp_db.create_shorts_channel(name="H", enabled=True) day = push_date_from_ts(time.time()) j1 = temp_db.create_shorts_job(session_id="", room_id="h1", channel_pk=ch["id"], push_date=day) j2 = temp_db.create_shorts_job(session_id="", room_id="h2", channel_pk=ch["id"], push_date=day) temp_db.update_shorts_job(j1["id"], status="uploaded", youtube_video_id="v1") temp_db.update_shorts_job(j2["id"], status="failed", error="OAuth refresh failed") hist = temp_db.shorts_stats_history(days=7) assert hist["days"] == 7 assert len(hist["series"]) == 7 today = next(x for x in hist["series"] if x["push_date"] == day) assert today["uploaded"] == 1 assert today["failed"] == 1 def test_shorts_failed_groups(temp_db): ch = temp_db.create_shorts_channel(name="G", enabled=True) day = push_date_from_ts(time.time()) a = temp_db.create_shorts_job(session_id="", room_id="g1", channel_pk=ch["id"], push_date=day) b = temp_db.create_shorts_job(session_id="", room_id="g2", channel_pk=ch["id"], push_date=day) c = temp_db.create_shorts_job(session_id="", room_id="g3", channel_pk=ch["id"], push_date=day) temp_db.update_shorts_job(a["id"], status="failed", error="OAuth 刷新失败: invalid_grant") temp_db.update_shorts_job(b["id"], status="failed", error="成片已丢失,已重新排队录制") temp_db.update_shorts_job(c["id"], status="failed", error="ffmpeg 录制超时") data = temp_db.shorts_failed_groups(push_date=day) assert data["total"] == 3 cats = {g["category"]: g["count"] for g in data["groups"]} assert cats.get("oauth") == 1 assert cats.get("missing_clip") == 1 assert cats.get("record") == 1 def test_shorts_channel_performance(temp_db): ch = temp_db.create_shorts_channel(name="Perf", enabled=True) day = push_date_from_ts(time.time()) a = temp_db.create_shorts_job(session_id="", room_id="p1", channel_pk=ch["id"], push_date=day) b = temp_db.create_shorts_job(session_id="", room_id="p2", channel_pk=ch["id"], push_date=day) temp_db.update_shorts_job(a["id"], status="uploaded", youtube_video_id="x") temp_db.update_shorts_job(b["id"], status="failed", error="boom") data = temp_db.shorts_channel_performance(days=7) row = next(c for c in data["channels"] if c["id"] == ch["id"]) assert row["uploaded"] == 1 assert row["failed"] == 1 assert row["success_rate"] == 0.5 def test_list_failed_shorts_job_ids(temp_db): ch = temp_db.create_shorts_channel(name="FailList", enabled=True) day = push_date_from_ts(time.time()) a = temp_db.create_shorts_job(session_id="", room_id="f1", channel_pk=ch["id"], push_date=day) b = temp_db.create_shorts_job(session_id="", room_id="f2", channel_pk=ch["id"], push_date=day) temp_db.update_shorts_job(a["id"], status="failed", error="x") temp_db.update_shorts_job(b["id"], status="uploaded", youtube_video_id="y") ids = temp_db.list_failed_shorts_job_ids(push_date=day) assert a["id"] in ids assert b["id"] not in ids def test_shorts_upload_heatmap(temp_db): ch = temp_db.create_shorts_channel(name="Heat", enabled=True) day = push_date_from_ts(time.time()) job = temp_db.create_shorts_job(session_id="", room_id="ht1", channel_pk=ch["id"], push_date=day) now = time.time() temp_db.update_shorts_job(job["id"], status="uploaded", youtube_video_id="z", finished_at=now) heat = temp_db.shorts_upload_heatmap(days=1) assert heat["days"] == 1 assert len(heat["uploaded"]) == 24 assert sum(heat["uploaded"]) == 1 assert heat["peak_hour"] is not None def test_list_auto_retry_skips_oauth(temp_db): ch = temp_db.create_shorts_channel(name="AR", enabled=True) day = push_date_from_ts(time.time()) a = temp_db.create_shorts_job(session_id="", room_id="ar1", channel_pk=ch["id"], push_date=day) b = temp_db.create_shorts_job(session_id="", room_id="ar2", channel_pk=ch["id"], push_date=day) now = time.time() - 120 temp_db.update_shorts_job(a["id"], status="failed", error="ffmpeg 录制超时", attempts=1, finished_at=now) temp_db.update_shorts_job( b["id"], status="failed", error="OAuth 刷新失败: invalid_grant", attempts=1, finished_at=now ) rows = temp_db.list_auto_retry_failed_jobs(max_attempts=3, limit=10) ids = {r["id"] for r in rows} assert a["id"] in ids assert b["id"] not in ids