videogen/tests/reelforge/test_scheduling_policy.py
caelum0x d56639a594
Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

79 lines
2.7 KiB
Python

"""Unit tests for the pure publishing scheduling policy (``decide``).
Focus: the min-gap self-cannibalization guard must clear EVERY recent post, not
just the first conflicting one, and a min-gap deferral must still honor the
daily cap on its landing day.
"""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from app.domains.publishing import scheduling_policy
UTC = timezone.utc
NOW = datetime(2026, 8, 19, 12, 0, 0, tzinfo=UTC)
def _min_gap_s(platform: str) -> float:
return scheduling_policy.platform_config(platform)["min_gap_h"] * 3600.0
def test_min_gap_deferral_clears_every_recent_post():
# tiktok min_gap 3h; two recent posts (12:01 and 16:00). A candidate at 12:01
# must land >= 3h from BOTH — snapping past only the first (-> 16:00) would
# collide with the second (self-cannibalization).
posts = [
datetime(2026, 8, 19, 12, 1, tzinfo=UTC),
datetime(2026, 8, 19, 16, 0, tzinfo=UTC),
]
decision = scheduling_policy.decide(
"tiktok",
datetime(2026, 8, 19, 12, 1, tzinfo=UTC),
now=NOW,
recent_posts=posts,
)
assert decision.allow is False
run_at = decision.run_at.astimezone(UTC)
gap = _min_gap_s("tiktok")
# Clears every existing post by the full min-gap.
assert all(abs((run_at - p).total_seconds()) >= gap - 1 for p in posts)
def test_successive_deferrals_do_not_collapse_onto_same_slot():
# Simulate scheduling three clips in one pass (accumulating history): each
# decided slot must stay >= min-gap from all previously chosen slots.
history: list[datetime] = []
chosen: list[datetime] = []
for _ in range(3):
decision = scheduling_policy.decide(
"tiktok",
NOW + timedelta(minutes=1),
now=NOW,
recent_posts=list(history),
)
run_at = decision.run_at.astimezone(UTC)
chosen.append(run_at)
history.append(run_at)
gap = _min_gap_s("tiktok")
for i in range(len(chosen)):
for j in range(i + 1, len(chosen)):
assert abs((chosen[i] - chosen[j]).total_seconds()) >= gap - 1
def test_min_gap_deferral_respects_daily_cap_on_landing_day():
# youtube cap 2, min_gap 6h. Two posts already today AND a near candidate:
# the min-gap deferral must NOT land a 3rd post on a day already at cap.
posts = [
datetime(2026, 8, 19, 15, 0, tzinfo=UTC),
datetime(2026, 8, 19, 20, 0, tzinfo=UTC),
]
decision = scheduling_policy.decide(
"youtube",
datetime(2026, 8, 19, 20, 1, tzinfo=UTC),
now=NOW,
recent_posts=posts,
)
assert decision.allow is False
assert decision.run_at.astimezone(UTC).date() > NOW.date()