test(hook): styles contract, bitmap-emoji scaling, layer walk-back

- test_hooks.py: the 6-style contract the frontend mirrors (boxed vs
  outline), fixed-bitmap emoji rescaling (_emoji_scale/_measure_width),
  and create_hook_image rendering every style (emoji included) with the
  unknown-style fallback.
- test_hook_layers.py: _strip_burned_hook walk-back (hooked_/legacy hook_,
  missing-file stop), the two-step subtitled->hooked->clean strip /api/hook
  replaces through, and _canonical_clip_file resolving hook chains with
  newest-derivation-wins.
This commit is contained in:
juancarlos.cavero 2026-08-19 16:22:38 +02:00
parent 2b2e287a9b
commit f8535b2584
2 changed files with 183 additions and 2 deletions

99
tests/test_hook_layers.py Normal file
View file

@ -0,0 +1,99 @@
"""Layer bookkeeping for burned hooks (app.py helpers).
The hook is a derived file (hooked_<ts>_<clean>) exactly like captions
(subtitled_<ts>_<clean>): replacing or removing a hook walks the prefix back
to the clean file instead of stacking a second overlay — the bug this design
replaced. These tests pin the walk-back and the canonical-file resolution.
"""
import os
import time
import pytest
app_module = pytest.importorskip("app")
_strip_burned_captions = app_module._strip_burned_captions
_strip_burned_hook = app_module._strip_burned_hook
_canonical_clip_file = app_module._canonical_clip_file
def _touch(directory, name, mtime=None):
path = os.path.join(str(directory), name)
with open(path, "wb") as f:
f.write(b"x")
if mtime is not None:
os.utime(path, (mtime, mtime))
return path
CLEAN = "base_clip_1.mp4"
HOOKED = f"hooked_123_{CLEAN}"
CAPTIONED_HOOKED = f"subtitled_456_{HOOKED}"
class TestStripBurnedHook:
def test_walks_hooked_prefix_back_to_clean(self, tmp_path):
_touch(tmp_path, CLEAN)
assert _strip_burned_hook(str(tmp_path), HOOKED) == CLEAN
def test_walks_legacy_hook_prefix(self, tmp_path):
_touch(tmp_path, CLEAN)
assert _strip_burned_hook(str(tmp_path), f"hook_{CLEAN}") == CLEAN
def test_no_prefix_is_returned_unchanged(self, tmp_path):
_touch(tmp_path, CLEAN)
assert _strip_burned_hook(str(tmp_path), CLEAN) == CLEAN
def test_missing_underlying_file_stops_the_walk(self, tmp_path):
# Restored project that only kept the current version: never point at
# a file that does not exist.
assert _strip_burned_hook(str(tmp_path), HOOKED) == HOOKED
class TestLayerChain:
def test_captioned_hook_strips_in_two_steps(self, tmp_path):
"""subtitled_<ts>_hooked_<ts>_<clean> -> hooked -> clean, the exact
order /api/hook uses to REPLACE a hook without stacking."""
_touch(tmp_path, CLEAN)
_touch(tmp_path, HOOKED)
_touch(tmp_path, CAPTIONED_HOOKED)
no_captions = _strip_burned_captions(str(tmp_path), CAPTIONED_HOOKED)
assert no_captions == HOOKED
assert _strip_burned_hook(str(tmp_path), no_captions) == CLEAN
class TestCanonicalClipFile:
def test_captioned_hook_chain_resolves(self, tmp_path):
now = time.time()
_touch(tmp_path, CLEAN, mtime=now - 60)
_touch(tmp_path, HOOKED, mtime=now - 30)
_touch(tmp_path, CAPTIONED_HOOKED, mtime=now)
assert _canonical_clip_file(str(tmp_path), "base", 0) == CAPTIONED_HOOKED
def test_uncaptioned_hook_resolves(self, tmp_path):
now = time.time()
_touch(tmp_path, CLEAN, mtime=now - 60)
_touch(tmp_path, HOOKED, mtime=now)
assert _canonical_clip_file(str(tmp_path), "base", 0) == HOOKED
def test_legacy_manual_hook_resolves(self, tmp_path):
now = time.time()
_touch(tmp_path, CLEAN, mtime=now - 60)
_touch(tmp_path, f"hook_{CLEAN}", mtime=now)
assert _canonical_clip_file(str(tmp_path), "base", 0) == f"hook_{CLEAN}"
def test_newest_derivation_wins(self, tmp_path):
"""After a hook replacement both chains exist on disk; the newer one
must be served or the replace would appear to do nothing."""
now = time.time()
_touch(tmp_path, CLEAN, mtime=now - 90)
_touch(tmp_path, HOOKED, mtime=now - 60)
_touch(tmp_path, CAPTIONED_HOOKED, mtime=now - 40)
new_chain = f"subtitled_900_hooked_800_{CLEAN}"
_touch(tmp_path, f"hooked_800_{CLEAN}", mtime=now - 20)
_touch(tmp_path, new_chain, mtime=now)
assert _canonical_clip_file(str(tmp_path), "base", 0) == new_chain
def test_clean_only_returns_clean(self, tmp_path):
_touch(tmp_path, CLEAN)
assert _canonical_clip_file(str(tmp_path), "base", 0) == CLEAN

View file

@ -1,7 +1,18 @@
"""Tests for hook overlay text handling (emoji runs, long-word wrapping)."""
"""Tests for hook overlay text handling (emoji runs, long-word wrapping,
visual styles, bitmap-emoji scaling, image generation)."""
import os
from PIL import Image, ImageDraw, ImageFont
from hooks import _split_emoji_runs, _break_long_word, _EMOJI_RE
from hooks import (
HOOK_STYLES,
_break_long_word,
_emoji_scale,
_measure_width,
_split_emoji_runs,
_EMOJI_RE,
create_hook_image,
)
def _draw_and_font():
@ -42,3 +53,74 @@ class TestLongWordWrap:
def test_short_word_single_piece(self):
draw, font = _draw_and_font()
assert _break_long_word(draw, "kurz", font, None, 1000) == ["kurz"]
class TestHookStyles:
"""The 6 styles are a contract shared with the frontend (HookModal's
picker and HookOverlay's HOOK_LOOKS must mirror these keys)."""
def test_expected_style_names(self):
assert set(HOOK_STYLES) == {
"classic", "dark", "yellow", "red", "outline", "outline_yellow",
}
def test_boxed_styles_have_opaque_box_and_shadow(self):
for name in ("classic", "dark", "yellow", "red"):
look = HOOK_STYLES[name]
assert look["box"][3] > 0, name
assert look["shadow"] is True, name
assert look["outline"] is None, name
def test_outline_styles_have_no_box_but_a_stroke(self):
for name in ("outline", "outline_yellow"):
look = HOOK_STYLES[name]
assert look["box"][3] == 0, name
assert look["shadow"] is False, name
color, px = look["outline"]
assert px > 0 and len(color) == 3, name
class TestEmojiScaling:
"""Fixed-bitmap emoji fonts (NotoColorEmoji only loads at its 109px
strike) are rescaled to the text size instead of being dropped."""
def _text_font(self, size=54):
# A real scalable font so .size is meaningful.
return ImageFont.truetype(os.path.join("fonts", "NotoSerif-Bold.ttf"), size)
def test_scale_is_ratio_of_text_size_to_native_strike(self):
font = self._text_font(54)
assert _emoji_scale(font, (font, 109)) == 54 / 109
def test_scalable_font_needs_no_rescale(self):
font = self._text_font(54)
assert _emoji_scale(font, (font, 54)) == 1.0
def test_measure_width_scales_emoji_runs(self):
draw = ImageDraw.Draw(Image.new("RGBA", (1, 1)))
font = self._text_font(54)
emoji_font = self._text_font(109) # stand-in for a 109px strike
full = _measure_width(draw, "🔥", font, (emoji_font, 109))
native = draw.textlength("🔥", font=emoji_font)
assert abs(full - native * (54 / 109)) < 0.01
def test_measure_width_without_emoji_font(self):
draw = ImageDraw.Draw(Image.new("RGBA", (1, 1)))
font = self._text_font(54)
assert _measure_width(draw, "hola", font, None) > 0
class TestCreateHookImage:
def test_every_style_renders(self, tmp_path):
for style in HOOK_STYLES:
out = str(tmp_path / f"hook_{style}.png")
path, w, h = create_hook_image(
"Did you know? 🤯", 500, out, style=style)
assert os.path.exists(path), style
assert w > 0 and h > 0, style
assert Image.open(path).size == (w, h), style
def test_unknown_style_falls_back_to_classic(self, tmp_path):
out = str(tmp_path / "hook_fallback.png")
path, w, h = create_hook_image("Hola", 500, out, style="nope")
assert os.path.exists(path) and w > 0 and h > 0