clipwright/tests/test_hook_start.py
pedro-bento 2dc1ba1eea
All checks were successful
CI / backend-e2e (push) Successful in 1m20s
CI / unit (push) Successful in 45s
CI / dashboard-e2e (push) Successful in 2m31s
feat/youtube-growth-toolkit (#8)
Reviewed-on: #8
Co-authored-by: pedro-bento <mail@pbento.pt>
Co-committed-by: pedro-bento <mail@pbento.pt>
2026-10-05 20:33:56 +01:00

170 lines
7.3 KiB
Python

"""Hook-first clip boundaries: the pure ``find_hook_start`` pass (#2a).
The first two seconds decide the swipe, so the pass moves a clip's start off
its wind-up and onto the earliest strong spoken token. It is deterministic and
word-based (no LLM) and must fail safe: no clear hook -> no change, and the
trim is capped by seconds, by fraction of the clip, and by the minimum clip
duration.
"""
from clipwright.pipeline.clip_selection import (
find_hook_start,
hook_first_enabled,
)
def _w(word, s, e):
return {"w": word, "s": s, "e": e}
class TestPicksEarliestHook:
def test_picks_the_earliest_hook_after_the_wind_up(self):
words = [
_w(" so", 0.0, 0.3),
_w(" uh", 0.3, 0.6),
_w(" why", 1.2, 1.6), # interrogative — first hook
_w(" do", 1.6, 1.8),
_w(" we", 1.8, 2.0),
]
assert find_hook_start(words, 0.0, 30.0, min_duration=15.0,
max_trim=5.0, max_fraction=0.5) == 1.2
def test_earliest_wins_across_signal_categories(self):
words = [
_w(" okay", 0.0, 0.3),
_w(" 3", 0.8, 1.1), # number
_w(" but", 1.4, 1.7), # contrast
_w(" crash", 2.0, 2.4), # stakes
]
assert find_hook_start(words, 0.0, 30.0, min_duration=15.0,
max_trim=5.0, max_fraction=0.5) == 0.8
def test_start_is_absolute_not_relative_to_the_clip(self):
words = [_w(" huh", 100.0, 100.3), _w(" why", 103.0, 103.4)]
assert find_hook_start(words, 100.0, 140.0, min_duration=15.0,
max_trim=5.0, max_fraction=0.5) == 103.0
def test_a_clip_already_on_its_hook_is_left_alone(self):
# trim ~0 is noise; returning None means "do not touch".
words = [_w(" bug", 10.0, 10.3), _w(" but", 12.0, 12.3)]
assert find_hook_start(words, 10.0, 40.0, min_duration=15.0,
max_trim=5.0, max_fraction=0.5) is None
class TestTrimCaps:
def test_respects_max_trim_seconds(self):
words = [_w(" uh", 0.0, 0.3), _w(" why", 6.0, 6.4)]
assert find_hook_start(words, 0.0, 30.0, min_duration=15.0,
max_trim=4.0, max_fraction=1.0) is None
# Just inside the cap it fires.
words[1] = _w(" why", 3.9, 4.3)
assert find_hook_start(words, 0.0, 30.0, min_duration=15.0,
max_trim=4.0, max_fraction=1.0) == 3.9
def test_respects_max_fraction_of_the_clip(self):
words = [_w(" uh", 0.0, 0.3), _w(" why", 4.0, 4.4)]
# duration 20, 10% -> 2.0s cap, so a 4.0s hook is out of reach.
assert find_hook_start(words, 0.0, 20.0, min_duration=5.0,
max_trim=100.0, max_fraction=0.1) is None
# 50% -> 10s cap, the same hook is reachable.
assert find_hook_start(words, 0.0, 20.0, min_duration=5.0,
max_trim=100.0, max_fraction=0.5) == 4.0
def test_zero_cap_never_moves(self):
words = [_w(" why", 1.0, 1.4)]
assert find_hook_start(words, 0.0, 30.0, min_duration=15.0,
max_trim=0.0, max_fraction=1.0) is None
def test_min_duration_wins_over_the_caps(self):
# Hook at 2.0 is within the trim cap but would leave 14s < 15s.
words = [_w(" why", 2.0, 2.4)]
assert find_hook_start(words, 0.0, 16.0, min_duration=15.0,
max_trim=5.0, max_fraction=0.5) is None
def test_degenerate_bounds_do_not_crash(self):
words = [_w(" why", 1.0, 1.4)]
assert find_hook_start(words, 5.0, 5.0, min_duration=15.0) is None
assert find_hook_start(words, 0.0, 0.0, min_duration=15.0) is None
class TestNoHook:
def test_returns_none_without_a_hook(self):
words = [
_w(" and", 0.0, 0.3),
_w(" then", 0.3, 0.6),
_w(" we", 0.6, 0.9),
_w(" the", 0.9, 1.2),
]
assert find_hook_start(words, 0.0, 30.0, min_duration=15.0,
max_trim=5.0, max_fraction=0.5) is None
def test_filler_alone_is_not_a_hook(self):
words = [_w(" um", 0.0, 0.2), _w(" okay", 0.2, 0.5),
_w(" yeah", 0.5, 0.8)]
assert find_hook_start(words, 0.0, 30.0, min_duration=15.0) is None
def test_empty_or_malformed_words_fail_safe(self):
assert find_hook_start(None, 0.0, 30.0, min_duration=15.0) is None
assert find_hook_start([], 0.0, 30.0, min_duration=15.0) is None
assert find_hook_start(["junk", {"no_s": 1}], 1.0, 30.0,
min_duration=15.0) is None
def test_clip_bounds_that_do_not_parse(self):
assert find_hook_start([_w(" why", 1.0, 1.4)], None, 30.0) is None
class TestTokenNormalisation:
def test_words_without_leading_spaces_still_match(self):
words = [_w("uh", 0.0, 0.3), _w("Why", 1.0, 1.4)]
assert find_hook_start(words, 0.0, 30.0, min_duration=15.0,
max_trim=5.0, max_fraction=0.5) == 1.0
def test_case_and_trailing_punctuation_are_stripped(self):
words = [_w(" Uh,", 0.0, 0.3), _w("CRASH!", 1.0, 1.4)]
assert find_hook_start(words, 0.0, 30.0, min_duration=15.0,
max_trim=5.0, max_fraction=0.5) == 1.0
def test_numeric_tokens_are_hooks(self):
assert find_hook_start([_w(" 2024", 1.0, 1.4)], 0.0, 30.0,
min_duration=15.0, max_trim=5.0,
max_fraction=0.5) == 1.0
def test_empty_word_text_is_ignored(self):
words = [_w("", 0.5, 0.6), _w(" ", 0.6, 0.7), _w(" how", 1.0, 1.4)]
assert find_hook_start(words, 0.0, 30.0, min_duration=15.0,
max_trim=5.0, max_fraction=0.5) == 1.0
class TestEnvConfig:
def test_reads_max_trim_from_env(self, monkeypatch):
monkeypatch.setenv("HOOK_FIRST_MAX_TRIM", "1.0")
monkeypatch.setenv("HOOK_FIRST_MAX_FRACTION", "1.0")
words = [_w(" uh", 0.0, 0.3), _w(" why", 2.0, 2.4)]
assert find_hook_start(words, 0.0, 30.0, min_duration=15.0) is None
words[1] = _w(" why", 0.8, 1.2)
assert find_hook_start(words, 0.0, 30.0, min_duration=15.0) == 0.8
def test_reads_max_fraction_from_env(self, monkeypatch):
monkeypatch.setenv("HOOK_FIRST_MAX_TRIM", "100")
monkeypatch.setenv("HOOK_FIRST_MAX_FRACTION", "0.1")
words = [_w(" uh", 0.0, 0.3), _w(" why", 4.0, 4.4)]
assert find_hook_start(words, 0.0, 20.0, min_duration=5.0) is None
def test_garbage_env_falls_back_to_defaults(self, monkeypatch):
monkeypatch.setenv("HOOK_FIRST_MAX_TRIM", "not-a-number")
monkeypatch.setenv("HOOK_FIRST_MAX_FRACTION", "")
words = [_w(" why", 3.5, 3.9)]
assert find_hook_start(words, 0.0, 30.0, min_duration=15.0) == 3.5
def test_enabled_is_on_by_default(self, monkeypatch):
monkeypatch.delenv("HOOK_FIRST", raising=False)
assert hook_first_enabled() is True
def test_enabled_can_be_switched_off(self, monkeypatch):
for raw in ("0", "false", "FALSE", "no", "off"):
monkeypatch.setenv("HOOK_FIRST", raw)
assert hook_first_enabled() is False
def test_enabled_explicit_true(self, monkeypatch):
monkeypatch.setenv("HOOK_FIRST", "1")
assert hook_first_enabled() is True