clipwright/tests/test_thumbnail_studio.py
pedro-bento 83ad1feaba
All checks were successful
CI / backend-tests (pull_request) Successful in 30s
CI / frontend-build (pull_request) Successful in 29s
refactor: move backend into the clipwright package
Restructure the 37 root-level Python modules into a proper package:
  clipwright/{core,api,pipeline,layouts,media,integrations,tools}

- Rewrite every local import (module-level and deferred) to absolute
  clipwright.<group>.<mod> form; no legacy root shims or re-exports.
- Add clipwright/core/paths.py and route fonts/assets/cookies through it.
- Entry points: uvicorn clipwright.api.app:app; jobs run as
  python -m clipwright.pipeline.main; probe as -m ...quality_probe.
- child_env() appends REPO_ROOT to PYTHONPATH so the -m workers work.
- transcribe_backends: sys.modules key now clipwright.media.scene_detection.
- Tooling: root pyproject.toml (pytest pythonpath=[.], ruff), shared
  requirements-test.txt, CI installs it, Docker PYTHONPATH=/app.
- Tests: all imports/fakes/source-scans repointed; new tests/test_package_layout.py
  enforces no root modules, docstring-only __init__, and no legacy imports.
- Docs/comments updated for the new layout.

Backend: 744 passed, 19 skipped. Frontend lint+build green.
2026-10-04 20:40:38 +01:00

71 lines
2.7 KiB
Python

"""Thumbnail Studio pieces that run without Gemini or a video."""
import pytest
from PIL import Image
from clipwright.media import thumbnail
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]}