clipwright/tests/test_generation_controls.py
juancarlos.cavero 4cc2fe32e0 fix(selection): score every window and enforce the clip floor
A 9:10 source delivered 3 clips in prod where the pipeline had asked
itself for 6-12. Two independent leaks, both in the chain that derives
the clip count.

The scoring pass SELECTED instead of ranking: "choose up to 3 windows
from this batch" put the shortlist ceiling at 3 * n_batches rather than
shortlist_target, so 9 windows batched 8 + 1 shortlisted 4 of a target
of 8, and clip_count_targets then asked the detail pass for 4-8 clips
instead of 6-12. Every source under ~30 min was starved the same way,
which is the mechanism behind "95% of jobs deliver 3 clips or fewer" —
the clip count D2 retention hangs on (0.4% for 1-3 clips, 16.1% for
4-9). It also discarded the ranking it was computing: a batch holding
five great moments could still contribute only three, while a batch of
filler contributed its own three. The prompt now scores every window it
is given and the shortlist is the global top N.

Batches are near-equal instead of "full ones plus the remainder": a
trailing batch of one window came back with that window whatever its
score, because a cap of 3 cannot filter a batch of 1, so the tail of the
video entered the shortlist by arithmetic. Same call count as before.

And the floor was only a sentence in the detail prompt, with nothing in
code checking it, so a pass that returned half shipped half. When it
comes back under min_clips the shortlist windows that produced nothing
get one more call for the difference; an empty answer is accepted,
because padding is worse than a short list.

Measured on that source, same transcript, three runs: shortlist 4 -> 8,
clips 4 -> 6, cost unchanged (~$0.005, 3 calls).
2026-09-23 00:27:14 +02:00

84 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Unit tests for the manual generation controls (discussion #65): the
clip-duration band helper and the prompt templates it feeds.
Pure logic only — no API calls, no heavy imports (gemini_worker's templates are
read via ast so the ML stack never loads in CI).
"""
import ast
import os
import pytest
import clip_selection
@pytest.fixture(autouse=True)
def _clean_env(monkeypatch):
monkeypatch.delenv("CLIP_MIN_SECONDS", raising=False)
monkeypatch.delenv("CLIP_MAX_SECONDS", raising=False)
class TestClipDurationBounds:
def test_defaults_are_the_classic_band(self):
assert clip_selection.clip_duration_bounds() == (15.0, 60.0)
def test_env_overrides_apply(self, monkeypatch):
monkeypatch.setenv("CLIP_MIN_SECONDS", "20")
monkeypatch.setenv("CLIP_MAX_SECONDS", "30")
assert clip_selection.clip_duration_bounds() == (20.0, 30.0)
def test_clamped_to_platform_limits(self, monkeypatch):
monkeypatch.setenv("CLIP_MIN_SECONDS", "1")
monkeypatch.setenv("CLIP_MAX_SECONDS", "9999")
assert clip_selection.clip_duration_bounds() == (5.0, 180.0)
def test_degenerate_band_keeps_a_real_range(self, monkeypatch):
# max below min must widen, not invert or collapse.
monkeypatch.setenv("CLIP_MIN_SECONDS", "60")
monkeypatch.setenv("CLIP_MAX_SECONDS", "20")
lo, hi = clip_selection.clip_duration_bounds()
assert lo == 60.0 and hi >= lo + 5.0
def test_garbage_falls_back_to_defaults(self, monkeypatch):
monkeypatch.setenv("CLIP_MIN_SECONDS", "garbage")
monkeypatch.setenv("CLIP_MAX_SECONDS", "")
assert clip_selection.clip_duration_bounds() == (15.0, 60.0)
def _templates():
"""The prompt template constants, read without importing gemini_worker
(which pulls google-genai — absent in the thin CI env)."""
mod = ast.parse(open(os.path.join(os.path.dirname(__file__), "..",
"gemini_worker.py")).read())
return {
node.targets[0].id: node.value.value
for node in mod.body
if isinstance(node, ast.Assign) and isinstance(node.value, ast.Constant)
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id.endswith("_PROMPT_TEMPLATE")
}
class TestPromptTemplates:
def test_detail_template_carries_the_band(self):
text = _templates()["DETAIL_PROMPT_TEMPLATE"].format(
video_duration=100, language="es", min_clips=2, max_clips=4,
min_secs=10.0, max_secs=20.0, windows_json="[]")
assert "10 to 20 seconds" in text
assert "return 2 to 4 clips" in text
def test_visual_template_carries_band_and_count(self):
text = _templates()["VISUAL_PROMPT_TEMPLATE"].format(
video_duration=100, language="es", min_clips=2, max_clips=4,
min_secs=10.0, max_secs=20.0)
assert "2–4 MOST engaging" in text
assert "10 to 20 seconds" in text
def test_score_template_ranks_every_window(self):
# The pass used to SELECT ("choose up to 3 windows from this batch"),
# which capped the shortlist at 3 * n_batches and threw away the
# ranking it was computing. It scores everything now.
text = _templates()["SCORE_PROMPT_TEMPLATE"].format(
video_duration=100, language="es", windows_json="[]")
assert "Score EVERY window" in text
assert "up to 3 windows" not in text