clipwright/tests/test_thumbnail_studio.py
juancarlos.cavero eb6ef13bba feat(thumbnail): concept-driven thumbnails, crisp burned text, face frames from the video, scored titles
Titles: transcript + 10 frames instead of uploading the whole video (an hour
was ~1M tokens), a brainstorm across fixed styles then a critic that scores,
dedupes by angle and pairs each title with a 1-4 word thumbnail hook. Mobile
rules: payoff inside 50 chars, keyword first. Text model moves to
GEMINI_MODEL_THUMBNAIL (default gemini-3.5-flash); flash-lite is the weakest
tier and this is the one creative step.

Thumbnails: a text call designs N different concepts, one image call each in
parallel. Default burns the hook text with PIL (Anton, black stroke) over
negative space the image model is told to leave, so accents are never wrong;
'AI painted' toggle keeps the old behaviour. Outputs cover-cropped to
1280x720 under YouTube's 2 MB. GET /api/thumbnail/frames/{session} picks
frames with a large, sharp face spread across the runtime and the dashboard
offers them as the person reference. Phone-size preview row in the UI.
JSON parsing now uses raw_decode: flash appended a stray ]} once.
2026-08-27 10:57:34 +02:00

76 lines
2.8 KiB
Python

"""Thumbnail Studio pieces that run without Gemini or a video."""
import io
import os
import sys
import pytest
from PIL import Image
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import thumbnail # noqa: E402
def test_burn_text_wraps_and_stays_in_its_half():
img = Image.new("RGB", (1280, 720), (20, 20, 20))
out = thumbnail.burn_thumbnail_text(img, "0 A 10K EN 30 DÍAS", "left", "yellow")
px = out.convert("RGB")
# Something got drawn on the left half...
left = px.crop((0, 0, 640, 720))
assert max(left.getextrema()[0]) > 200
# ...and the right half (where the subject lives) is untouched.
right = px.crop((700, 0, 1280, 720))
assert max(right.getextrema()[0]) < 60
def test_burn_text_no_text_is_a_noop():
img = Image.new("RGB", (1280, 720), (0, 0, 0))
assert thumbnail.burn_thumbnail_text(img, " ", "left") is img
def test_finalize_cover_crops_to_1280x720_under_2mb(tmp_path):
# A 2K-ish image with a different aspect ratio, full of noise so JPEG is heavy.
import random
noisy = Image.effect_noise((2048, 1536), 80).convert("RGB")
out = tmp_path / "t.jpg"
thumbnail.finalize_thumbnail(noisy, str(out))
with Image.open(out) as saved:
assert saved.size == (1280, 720)
assert out.stat().st_size <= thumbnail.THUMB_MAX_BYTES
def test_rank_frame_rejects_tiny_faces():
assert thumbnail.rank_frame(0, 0.0, None, [0, 0, 50, 50], (1920, 1080), 100) is None
big = thumbnail.rank_frame(0, 0.0, None, [0, 0, 400, 400], (1920, 1080), 100)
assert big and big["score"] > 0
def test_pick_spread_keeps_picks_apart_in_time():
total = 1000
# Three near-identical best frames at 500/505/510 and a weaker one far away.
scored = [
{"idx": 500, "score": 1.0}, {"idx": 505, "score": 0.99}, {"idx": 510, "score": 0.98},
{"idx": 100, "score": 0.5}, {"idx": 900, "score": 0.4},
]
picked = thumbnail.pick_spread(scored, 3, total)
assert [p["idx"] for p in picked] == [100, 500, 900]
def test_normalise_concepts_clamps_and_pads():
raw = [
{"text": "esto funciona", "text_position": "diagonal", "text_color": "red", "scene": "a desk"},
{"scene": ""}, # dropped
"garbage",
]
out = thumbnail.normalise_concepts(raw, 3, "My title", thumbnail_text_hint="hint")
assert len(out) == 3
assert out[0] == {"text": "ESTO FUNCIONA", "text_position": "left", "text_color": "white",
"scene": "a desk", "why": ""}
assert out[1]["why"] == "fallback" and out[1]["text"] == "HINT"
def test_parse_json_tolerates_fences_and_prose():
assert thumbnail._parse_json('Sure!\n```json\n{"a": 1}\n```') == {"a": 1}
def test_parse_json_ignores_trailing_garbage():
assert thumbnail._parse_json('{"a": [1]}\n ]\n}') == {"a": [1]}