clipwright/tests/test_translate.py
juancarlos.cavero 40151e1708 feat: karaoke subtitles, deterministic Auto-Edit v2, emoji hooks, retries (PR #50 block 1)
Ported from PR #50 (Themegaindex) onto current main, adapted to the
billing-mode app.py and the Lumen dashboard:

- subtitles.py: karaoke ASS generation (word-level highlight, glow/pop/box
  effects, dim/uppercase), whisper continuation-word merging, env-configurable
  whisper model, ASS/font input sanitization, faststart
- editor.py + edit_builder.py: Auto-Edit v2 — Gemini returns a structured
  edit-decision list and the FFmpeg filter is built deterministically with
  hard caps (no raw filter strings from the LLM), 2s dry-run + one
  self-repair round, caption-safe mode blocks zooms on subtitled clips
- app.py: /api/subtitle karaoke fields + re-subtitle prefix walk-back
  (fixes double subtitles), has_captions passed to Auto-Edit
- hooks.py: emoji rendering (color-emoji font with strip fallback),
  char-level hard wrap, unique temp PNGs, ffmpeg timeouts; keeps the
  duration/enable overlay from b1113d7
- translate.py: exponential-backoff retries for transient ElevenLabs errors
- dashboard: 11 caption presets in SubtitleModal; karaoke burns server-side
- tests/: 50 pytest tests; .github/workflows/ci.yml runs them + fe build

Encode settings kept at CRF 18 (medium for post-process steps) per f791a74.

Co-authored-by: Themegaindex <themegaindex@users.noreply.github.com>
2026-07-17 21:26:04 +02:00

43 lines
1 KiB
Python

"""Tests for the ElevenLabs retry helper."""
import pytest
import translate
from translate import _with_retry, _TransientHTTPError
@pytest.fixture(autouse=True)
def no_sleep(monkeypatch):
monkeypatch.setattr(translate.time, "sleep", lambda seconds: None)
def test_succeeds_after_transient_failures():
calls = {"n": 0}
def flaky():
calls["n"] += 1
if calls["n"] < 3:
raise _TransientHTTPError("HTTP 502")
return "ok"
assert _with_retry(flaky, "test op") == "ok"
assert calls["n"] == 3
def test_gives_up_after_max_attempts():
def always_fails():
raise _TransientHTTPError("HTTP 503")
with pytest.raises(Exception, match="failed after 3 attempts"):
_with_retry(always_fails, "test op")
def test_non_transient_fails_fast():
calls = {"n": 0}
def bad_request():
calls["n"] += 1
raise ValueError("HTTP 400 - bad request")
with pytest.raises(ValueError):
_with_retry(bad_request, "test op")
assert calls["n"] == 1