# -*- coding: utf-8 -*- from __future__ import annotations import time from app.shorts_upload import is_upload_quota_error from app.shorts_yt_quota import ( YT_DAILY_UPLOAD_LIMIT_DEFAULT, blocked_profile_ids, clear_all_upload_self_blocks, pause_uploads_until_reset, profile_upload_blocked, project_uploads_available, reserve_yt_upload_slot, yt_upload_budget, ) from app.timeutil import pacific_day_from_ts def test_is_upload_quota_error_matches_429(): err = 'Quota exceeded for quota metric "Video Uploads" and limit "Video Uploads per day"' assert is_upload_quota_error(err) def test_yt_upload_budget_and_reserve(temp_db): cfg = temp_db.get_settings_map() limit, used, remaining = yt_upload_budget(cfg) assert limit == YT_DAILY_UPLOAD_LIMIT_DEFAULT assert used >= 0 assert remaining == limit - used assert reserve_yt_upload_slot(cfg) is True _, used2, remaining2 = yt_upload_budget(temp_db.get_settings_map()) assert used2 == used + 1 assert remaining2 == remaining - 1 def test_never_self_blocks_uploads(temp_db): """Local counter / pause helpers must not gate uploads.""" cfg = temp_db.get_settings_map() for _ in range(YT_DAILY_UPLOAD_LIMIT_DEFAULT + 5): assert reserve_yt_upload_slot(cfg) is True cfg = temp_db.get_settings_map() assert project_uploads_available(cfg, profile_id="A") is True assert profile_upload_blocked(cfg, profile_id="A") == (False, 0.0) assert blocked_profile_ids(cfg) == set() ch = temp_db.create_shorts_channel(name="Y", enabled=True) job = temp_db.create_shorts_job(session_id="", room_id="r1", channel_pk=ch["id"]) temp_db.update_shorts_job(job["id"], status="scheduled", clip_path="/tmp/x.mp4", upload_at=time.time()) until = pause_uploads_until_reset(temp_db.get_settings_map(), reason="test pause") refreshed = temp_db.get_shorts_job(job["id"]) assert refreshed is not None assert refreshed["status"] == "scheduled" # Must NOT defer the job into the future as a self-pause assert float(refreshed["upload_at"]) <= time.time() + 5 cfg = temp_db.get_settings_map() assert float(cfg.get("shorts_yt_upload_blocked_until") or 0) == 0 assert until > time.time() # reset timestamp still returned for UI hint clear_all_upload_self_blocks(cfg) assert profile_upload_blocked(temp_db.get_settings_map(), profile_id="A") == (False, 0.0) def test_pacific_day_from_ts(): day = pacific_day_from_ts(time.time()) assert len(day) == 10 and day[4] == "-"