# -*- 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, defer_profile_shorts_uploads, mark_api_quota_cooldown, 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_local_counter_does_not_block(temp_db): """Burning the local display counter must not gate uploads by itself.""" 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() def test_api_cooldown_blocks_and_ntfy_once(temp_db): """Real API cool-down blocks auto path; second mark in-window should not re-notify.""" cfg = temp_db.get_settings_map() until1, notify1 = mark_api_quota_cooldown(cfg, profile_id="B", reason="429 test") assert notify1 is True assert until1 > time.time() cfg = temp_db.get_settings_map() blocked, until = profile_upload_blocked(cfg, profile_id="B") assert blocked is True assert until > time.time() assert "B" in blocked_profile_ids(cfg) assert project_uploads_available(cfg, profile_id="B") is False # Other clients stay open assert project_uploads_available(cfg, profile_id="A") is True until2, notify2 = mark_api_quota_cooldown(cfg, profile_id="B", reason="429 again") assert notify2 is False assert until2 >= until1 - 1 def test_api_cooldown_defers_due_jobs(temp_db): ch = temp_db.create_shorts_channel(name="YB", enabled=True, oauth_profile_id="B") job = temp_db.create_shorts_job(session_id="", room_id="r1", channel_pk=ch["id"]) now = time.time() temp_db.update_shorts_job( job["id"], status="scheduled", clip_path="/tmp/x.mp4", upload_at=now - 10 ) until = pause_uploads_until_reset(temp_db.get_settings_map(), profile_id="B", reason="test") moved = defer_profile_shorts_uploads("B", until, reason="cool") assert moved >= 1 refreshed = temp_db.get_shorts_job(job["id"]) assert refreshed is not None assert float(refreshed["upload_at"]) >= until # clear_all only drops expired cool-downs clear_all_upload_self_blocks(temp_db.get_settings_map()) assert profile_upload_blocked(temp_db.get_settings_map(), profile_id="B")[0] is True def test_pacific_day_from_ts(): day = pacific_day_from_ts(time.time()) assert len(day) == 10 and day[4] == "-"