111 lines
4.3 KiB
Python
111 lines
4.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Unit tests for Shorts front-load pace (90% by BJ 20:00) + gap floors."""
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
from unittest import mock
|
|
|
|
from app.shorts_schedule import (
|
|
CHANNEL_GAP_FLOOR_SEC,
|
|
QUOTA_DEADLINE_HOUR,
|
|
QUOTA_FRONTLOAD_RATIO,
|
|
_deadline_ts,
|
|
_quota_pressure,
|
|
fleet_pace_plan,
|
|
pick_upload_at,
|
|
)
|
|
from app.timeutil import beijing_day_bounds, push_date_from_ts
|
|
|
|
|
|
def test_channel_gap_floor_constant():
|
|
assert CHANNEL_GAP_FLOOR_SEC == 600.0
|
|
assert QUOTA_DEADLINE_HOUR == 20
|
|
assert QUOTA_FRONTLOAD_RATIO == 0.9
|
|
|
|
|
|
def test_deadline_is_beijing_20():
|
|
day = push_date_from_ts(time.time())
|
|
day_start, day_end = beijing_day_bounds(day)
|
|
d = _deadline_ts(day)
|
|
assert day_start < d < day_end
|
|
assert abs(d - (day_start + 20 * 3600)) < 60
|
|
|
|
|
|
def test_pick_upload_at_respects_10min_floor(temp_db):
|
|
ch = temp_db.create_shorts_channel(name="Gap", daily_min=9, daily_max=9, enabled=True)
|
|
day = push_date_from_ts(time.time())
|
|
noon = beijing_day_bounds(day)[0] + 12 * 3600
|
|
# Seed enough uploads so we are NOT behind front-load (avoid emergency <10min).
|
|
for i in range(8):
|
|
j = temp_db.create_shorts_job(session_id="", room_id=f"gap{i}", channel_pk=ch["id"], push_date=day)
|
|
temp_db.update_shorts_job(
|
|
j["id"], status="done", finished_at=noon - 3600 + i * 700, upload_at=noon - 3600 + i * 700
|
|
)
|
|
j = temp_db.create_shorts_job(session_id="", room_id="gapx", channel_pk=ch["id"], push_date=day)
|
|
temp_db.update_shorts_job(j["id"], status="done", finished_at=noon - 30, upload_at=noon - 30)
|
|
|
|
cfg = {
|
|
"shorts_upload_spread": True,
|
|
"shorts_upload_min_gap_sec": 600,
|
|
"shorts_channel_min_gap_sec": 600,
|
|
}
|
|
with mock.patch("app.shorts_schedule.time.time", return_value=noon):
|
|
at = pick_upload_at(channel_pk=ch["id"], push_date=day, cfg=cfg)
|
|
assert at >= noon - 30 + CHANNEL_GAP_FLOOR_SEC
|
|
|
|
|
|
def test_pick_upload_at_stays_before_deadline_when_behind(temp_db):
|
|
ch = temp_db.create_shorts_channel(name="Early", daily_min=9, daily_max=9, enabled=True)
|
|
day = push_date_from_ts(time.time())
|
|
day_start, _ = beijing_day_bounds(day)
|
|
now = day_start + 6 * 3600 # 06:00 BJ — far behind 90% by 20:00
|
|
deadline = _deadline_ts(day)
|
|
cfg = {
|
|
"shorts_upload_spread": True,
|
|
"shorts_upload_min_gap_sec": 600,
|
|
"shorts_channel_min_gap_sec": 600,
|
|
}
|
|
with mock.patch("app.shorts_schedule.time.time", return_value=now):
|
|
at = pick_upload_at(channel_pk=ch["id"], push_date=day, cfg=cfg)
|
|
assert at <= deadline + 60
|
|
assert at < day_start + 22 * 3600 # not dumped near midnight
|
|
|
|
|
|
def test_end_of_day_compresses_below_10min(temp_db):
|
|
ch = temp_db.create_shorts_channel(name="Rush", daily_min=9, daily_max=9, enabled=True)
|
|
day = push_date_from_ts(time.time())
|
|
_, day_end = beijing_day_bounds(day)
|
|
now = day_end - 12 * 60
|
|
j = temp_db.create_shorts_job(session_id="", room_id="r1", channel_pk=ch["id"], push_date=day)
|
|
temp_db.update_shorts_job(j["id"], status="uploaded", finished_at=now - 20, upload_at=now - 20)
|
|
cfg = {
|
|
"shorts_upload_spread": True,
|
|
"shorts_upload_min_gap_sec": 600,
|
|
"shorts_channel_min_gap_sec": 600,
|
|
}
|
|
with mock.patch("app.shorts_schedule.time.time", return_value=now):
|
|
at = pick_upload_at(channel_pk=ch["id"], push_date=day, cfg=cfg)
|
|
assert at < now + CHANNEL_GAP_FLOOR_SEC
|
|
assert at <= day_end
|
|
|
|
|
|
def test_quota_pressure_uses_channel_daily_min(temp_db):
|
|
ch = temp_db.create_shorts_channel(name="P", daily_min=9, daily_max=12, enabled=True)
|
|
day = push_date_from_ts(time.time())
|
|
g, c, behind, urgent = _quota_pressure(channel_pk=ch["id"], push_date=day, now=time.time())
|
|
assert behind is True or urgent is True or g < 1.0
|
|
assert c <= 1.0
|
|
|
|
|
|
def test_fleet_pace_plan_behind_in_morning(temp_db):
|
|
for i in range(3):
|
|
temp_db.create_shorts_channel(name=f"C{i}", daily_min=9, daily_max=9, enabled=True)
|
|
day = push_date_from_ts(time.time())
|
|
day_start, _ = beijing_day_bounds(day)
|
|
now = day_start + 6 * 3600
|
|
with mock.patch("app.shorts_schedule.time.time", return_value=now):
|
|
plan = fleet_pace_plan(day, now=now)
|
|
assert plan["front_target"] == 25 or plan["front_target"] >= 24 # ceil(0.9*27)=25
|
|
assert plan["behind"] is True
|
|
assert plan["recommended_global_gap_sec"] <= 600
|