Files
CenaForge/clipper/tests/test_editor.py
T
MateuusandClaude Opus 5.5 84f30bbc2d CenaForge: primeira versão pública
Gerador de vídeos narrativos (Godot + FFmpeg), painel Next.js, serviço de voz,
Clipador de lives e Publicador para YouTube, TikTok e Instagram.

Inclui a leitura das visualizações dos posts para o Dashboard e o botão
"Publicar no <rede>" nos cartões das publicações.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:35:09 -03:00

314 lines
19 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Editor de cortes: validador do projeto, contas de tempo, legenda animada (ASS), filtergraph e rotas."""
import json
import shutil
import subprocess
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from app import editor, editor_render, layout
from app.config import Settings
from app.db import Database
from app.main import create_app
from app.scoring import Word
from app.worker import Engine
from .test_sources_and_api import KEY, FakeJudge
FFMPEG = shutil.which("ffmpeg")
def make_project(**over) -> dict:
p = {
"schema": 1, "source": {"sessionId": 1, "lo": 0, "hi": 60}, "settings": {},
"cuts": [{"start": 10, "end": 20, "mode": None}, {"start": 25, "end": 30, "mode": "game_only"}],
"captions": {"enabled": True, "preset": "pop", "style": {}, "emphColor": "#22E36B",
"words": [{"s": 11, "e": 11.4, "w": "olha"}, {"s": 11.5, "e": 12, "w": "isso", "emph": True},
{"s": 21, "e": 22, "w": "removida"}, {"s": 26, "e": 26.5, "w": "voltou"}]},
"overlays": [{"id": "hook", "kind": "hook", "text": "ELE NÃO ACREDITOU", "start": 0, "end": 3, "y": 0.2, "size": 70,
"color": "#FFFFFF", "box": "#E11D48", "anim": "pop"},
{"id": "z1", "kind": "zoom", "start": 12, "end": 15, "scale": 1.4, "x": 0.5, "y": 0.4, "ramp": 0.2}],
"progress": {"enabled": True, "color": "#FF7A1A", "height": 12},
"audio": {"volume": 1, "narration": [], "music": None, "sfx": []},
"cover": {"enabled": True, "t": 12, "text": "LAG"},
}
p.update(over)
return p
def validate(data, tmp_path: Path, duration=100.0):
return editor.validate_project(data, session_id=1, session_duration=duration, repo_root=tmp_path,
tts_dir=tmp_path / "tts", assets_dir=tmp_path / "assets")
def test_time_mapping():
cuts = [{"start": 10, "end": 20}, {"start": 25, "end": 30}]
assert editor.output_duration(cuts) == 15
assert editor.to_output(15, cuts) == (5, True)
assert editor.to_output(22, cuts) == (10, False) # trecho removido gruda no começo do próximo
assert editor.to_output(26, cuts) == (11, True)
assert editor.to_session(11, cuts) == 26
sl = editor.slice_pieces(cuts, 8, 12)
assert [(p.start, p.end, p.out) for p in sl] == [(18, 20, 0), (25, 27, 2)]
assert editor.normalize_cuts([{"start": 5, "end": 8}, {"start": 1, "end": 5.01}]) == [{"start": 1, "end": 8}]
def test_validate_accepts_and_normalizes(tmp_path):
p = validate(make_project(), tmp_path)
assert p["cuts"][1]["mode"] == "game_only" and p["captions"]["words"][1]["emph"] is True
assert editor.output_duration(p["cuts"]) == 15
@pytest.mark.parametrize("change,message", [
({"schema": 2}, "versão"),
({"cuts": []}, "pelo menos um trecho"),
({"cuts": [{"start": 10, "end": 11}]}, "de 3 a 180"),
({"cuts": [{"start": 10, "end": 70}]}, r"cuts\[0\]\.end vai de 0 a 60"), # fora da janela (hi = 60)
({"source": {"sessionId": 9, "lo": 0, "hi": 60}}, "outra sessão"),
({"source": {"sessionId": 1, "lo": 0, "hi": 900}}, "source.hi"), # além do fim da gravação
({"cuts": [{"start": 10, "end": 20, "mode": "rm -rf"}]}, "mode"),
({"overlays": [{"id": "a b", "kind": "text", "text": "x", "start": 10, "end": 12}]}, "id curto"),
({"overlays": [{"id": "z1", "kind": "zoom", "start": 10, "end": 14, "scale": 1.3},
{"id": "z2", "kind": "zoom", "start": 12, "end": 16, "scale": 1.3}]}, "dois zooms"),
({"overlays": [{"id": "t", "kind": "text", "text": "x" * 200, "start": 10, "end": 12}]}, "120"),
({"audio": {"music": {"file": "../voice/.env"}}}, "música"),
({"audio": {"music": {"file": "C:/Windows/win.ini"}}}, "música"),
({"audio": {"narration": [{"id": "n", "file": "output/x.wav", "at": 10}]}}, "narração"),
])
def test_validate_rejects(tmp_path, change, message):
with pytest.raises(editor.EditError, match=message):
validate(make_project(**change), tmp_path)
def test_zero_length_word_is_tolerated(tmp_path):
data = make_project()
data["captions"]["words"].append({"s": 13.0, "e": 13.0, "w": "queria"}) # Whisper real (corte 9 do skipnho)
words = validate(data, tmp_path)["captions"]["words"]
assert {"s": 13.0, "e": 13.05, "w": "queria"} in words
def test_window_limit(tmp_path):
with pytest.raises(editor.EditError, match="janela"):
validate(make_project(source={"sessionId": 1, "lo": 0, "hi": 900}), tmp_path, duration=2000)
def test_audio_only_from_allowed_folders(tmp_path):
(tmp_path / "assets").mkdir()
(tmp_path / "assets" / "beat.mp3").write_bytes(b"x")
(tmp_path / "tts").mkdir()
(tmp_path / "tts" / "tts-0123456789ab.wav").write_bytes(b"x")
p = validate(make_project(audio={"volume": 1, "music": {"file": "assets/beat.mp3", "volume": 0.2},
"narration": [{"id": "n1", "file": "tts/tts-0123456789ab.wav", "at": 12}]}), tmp_path)
assert p["audio"]["music"]["file"] == "assets/beat.mp3" and p["audio"]["narration"][0]["at"] == 12
def test_output_words_and_ass_presets(tmp_path):
p = validate(make_project(), tmp_path)
words = editor.output_words(p)
assert [w.word for w in words] == ["olha", "isso", "voltou"] # "removida" caiu no trecho cortado
assert words[2].start == 11.0 # 26 s da sessão = 10 s + 1 s
cfg = editor.effective_settings(layout.merge(), p)
ass = editor_render.build_project_ass(p, cfg, "twitch.tv/x")
assert "\\fscx118" in ass # pop na palavra falada
assert "&H6BE322&" in ass # cor da ênfase (#22E36B em BGR) na palavra marcada
assert "ELE NÃO ACREDITOU" in ass and "Box" in ass # gancho com caixa
assert "\\p1" in ass and "\\fscx100.00" in ass # barra de progresso até 100%
assert "\\C" not in ass # gotcha 58: tag nunca em maiúscula
for preset, mark in (("karaoke", "\\kf"), ("single", "\\fscx70"), ("minimal", "OLHA")):
q = json.loads(json.dumps(p))
q["captions"]["preset"] = preset
text = editor_render.build_project_ass(q, editor.effective_settings(layout.merge(), q), "")
assert mark in text if preset != "minimal" else mark not in text # mínima não usa maiúsculas
def test_ass_escapes_user_text(tmp_path):
p = make_project(overlays=[{"id": "t", "kind": "text", "text": "{\\b1}oi\\N", "start": 11, "end": 13}])
q = validate(p, tmp_path)
ass = editor_render.build_project_ass(q, editor.effective_settings(layout.merge(), q), "")
assert "{\\b1}" not in ass and "(/b1)oi/N" in ass
def test_filter_has_pieces_layouts_and_zoom(tmp_path):
p = validate(make_project(), tmp_path)
cfg = editor.effective_settings(layout.merge({"layout": {"cam": {"x": 0, "y": 0.5, "w": 0.2, "h": 0.3}}}), p)
graph = editor_render.build_filter(p, cfg, editor.pieces(p["cuts"]), 10.0, "e.ass", [])
assert "trim=start=0.000:end=10.000" in graph and "trim=start=15.000:end=20.000" in graph
assert "vstack" in graph and "[c_0]" in graph # 1º trecho câmera em cima (rótulos únicos por trecho)
assert "concat=n=2:v=1:a=1" in graph and "zoompan=z='1+0.4000*" in graph and "ass=e.ass" in graph
# Prévia real a partir de 8 s: o zoom continua no mesmo instante do corte (it + 8).
assert "(it+8.000)" in editor_render.zoom_filter(p, 8.0)
def test_suggestion_is_clamped():
s = editor.clean_suggestion({"hook": "x" * 200, "title": "t", "start_s": -50, "end_s": 999,
"remove": [{"start_s": 20, "end_s": 20.1, "why": ""}, {"start_s": 30, "end_s": 34, "why": "silêncio"}],
"zooms": [{"t": 40, "why": "grito"}, {"t": 500, "why": "fora"}], "reason": "r"}, 10, 80)
assert (s["start"], s["end"]) == (10, 80) and len(s["hook"]) == 80
assert s["remove"] == [{"start": 30, "end": 34, "why": "silêncio"}] and [z["t"] for z in s["zooms"]] == [40]
# ---------------------------------------------------------------- rotas e render real
def _lavfi_segments(folder: Path, n: int = 3, seconds: int = 4) -> list[Path]:
folder.mkdir(parents=True, exist_ok=True)
files = []
for i in range(n):
f = folder / f"seg_{i:05d}.ts"
subprocess.run([FFMPEG, "-y", "-v", "error", "-f", "lavfi", "-i", f"testsrc2=s=640x360:r=30:d={seconds}",
"-f", "lavfi", "-i", f"sine=frequency={300 + 100 * i}:d={seconds}", "-c:v", "libx264", "-preset",
"ultrafast", "-g", "15", "-c:a", "aac", "-shortest", "-f", "mpegts", str(f)], check=True, capture_output=True)
files.append(f)
return files
@pytest.fixture
def api(tmp_path, monkeypatch):
import app.editor_api as editor_api
import app.worker as worker
monkeypatch.setattr(editor_api, "REPO_ROOT", tmp_path)
monkeypatch.setattr(worker, "REPO_ROOT", tmp_path)
settings = Settings.load(env_file=tmp_path / "none.env", service_key=KEY, data_dir=tmp_path / "data",
clips_dir=tmp_path / "output" / "clipper", judge_provider="codex", voice_url=None, voice_key=None)
db = Database(settings.data_dir / "clipper.db")
engine = Engine(settings, db, judge=FakeJudge())
client = TestClient(create_app(settings, engine, start_engine=False))
db.run("INSERT INTO sessions (id, platform, kind, url, status, dir, duration_s, started_at) "
"VALUES (1, 'twitch', 'live', 'https://www.twitch.tv/skipnho', 'done', 'data/s1', 12, 'x')")
words = [[1.0, 1.4, "olha"], [1.5, 2.0, "isso"], [9.0, 9.5, "fim"]]
for i in range(3):
db.run("INSERT INTO segments (session_id, idx, file, start_s, end_s, wall_end, analyzed, words_json) VALUES (1, ?, ?, ?, ?, 0, 1, ?)",
i, f"data/s1/seg_{i:05d}.ts", i * 4.0, i * 4.0 + 4, json.dumps([w for w in words if i * 4 <= w[0] < i * 4 + 4]))
db.run("INSERT INTO clips (id, session_id, peak_s, start_s, end_s, score, source, status, title, created_at, updated_at) "
"VALUES (7, 1, 5, 2, 10, 8, 'manual', 'ready', 'Título', 'x', 'x')")
return client, engine, tmp_path
def test_edit_routes_roundtrip(api):
client, engine, root = api
h = {"X-Service-Key": KEY}
r = client.get("/v1/clips/7/edit", headers=h)
assert r.status_code == 200, r.text
body = r.json()
assert body["rev"] == 0 and body["project"]["cuts"] == [{"start": 2.0, "end": 10.0, "mode": None}]
assert [w["w"] for w in body["words"]] == ["olha", "isso", "fim"] and body["window"] == {"lo": 0.0, "hi": 12.0}
project = body["project"]
project["cuts"] = [{"start": 0.0, "end": 3.0, "mode": None}, {"start": 6.0, "end": 10.0, "mode": None}]
saved = client.put("/v1/clips/7/edit", headers=h, json={"project": project, "rev": 0})
assert saved.status_code == 200 and saved.json()["rev"] == 1 and saved.json()["durationS"] == 7
stale = client.put("/v1/clips/7/edit", headers=h, json={"project": project, "rev": 0})
assert stale.status_code == 409 # autosave de outra aba não atropela
bad = client.put("/v1/clips/7/edit", headers=h, json={"project": {**project, "cuts": []}, "rev": 1})
assert bad.status_code == 400 and "trecho" in bad.json()["error"]
assert client.get("/v1/clips/7", headers=h).json()["edit"]["updatedAt"]
exp = client.post("/v1/clips/7/export", headers=h)
assert exp.status_code == 200 and exp.json()["status"] == "pending" and exp.json()["editedDurationS"] == 7
assert (exp.json()["startS"], exp.json()["endS"]) == (0.0, 10.0)
# Ajuste do cartão (−2 s no início não cabe: a sessão começa em 0) → as pontas do projeto acompanham.
client.patch("/v1/clips/7", headers=h, json={"endS": 11.0})
assert client.get("/v1/clips/7/edit", headers=h).json()["project"]["cuts"][-1]["end"] == 11.0
assert client.delete("/v1/clips/7/edit", headers=h).status_code == 204
assert client.get("/v1/clips/7", headers=h).json()["edit"] is None
def test_tts_and_suggest_need_services(api):
client, _engine, _root = api
h = {"X-Service-Key": KEY}
assert "voz" in client.post("/v1/clips/7/tts", headers=h, json={"text": "oi", "voice": "pt-br-x"}).json()["error"]
assert client.post("/v1/clips/7/tts", headers=h, json={"text": "oi", "voice": "../x"}).status_code == 422
@pytest.mark.skipif(not FFMPEG, reason="FFmpeg não instalado")
def test_real_render_proxy_and_waveform(api):
client, engine, root = api
_lavfi_segments(root / "data" / "s1")
h = {"X-Service-Key": KEY}
project = client.get("/v1/clips/7/edit", headers=h).json()["project"]
project["cuts"] = [{"start": 1.0, "end": 4.0, "mode": None}, {"start": 6.0, "end": 9.0, "mode": "game_only"}]
project["overlays"].append({"id": "z", "kind": "zoom", "start": 7, "end": 8.5, "scale": 1.5, "x": 0.5, "y": 0.5, "ramp": 0.2})
project["progress"]["enabled"] = True
assert client.put("/v1/clips/7/edit", headers=h, json={"project": project, "rev": 0}).status_code == 200
proxy = client.post("/v1/clips/7/proxy", headers=h, json={"lo": 0, "hi": 12})
assert proxy.status_code == 200, proxy.text
wave = client.get("/v1/clips/7/waveform?lo=0&hi=12", headers=h).json()
assert 500 <= len(wave["peaks"]) <= 610 and max(wave["peaks"]) == 1.0
prev = client.post("/v1/clips/7/preview-render", headers=h, json={"fromS": 2, "durationS": 3})
assert prev.status_code == 200, prev.text
client.post("/v1/clips/7/export", headers=h)
engine.render_pending()
clip = client.get("/v1/clips/7", headers=h).json()
assert clip["status"] == "ready", clip["error"]
out = root / clip["file"]
assert clip["edit"]["cover"] and (root / clip["edit"]["cover"]).is_file() # capa (etapa 7) gerada no export
probe = subprocess.run([shutil.which("ffprobe") or "ffprobe", "-v", "error", "-show_entries", "stream=width,height:format=duration",
"-of", "json", str(out)], capture_output=True, text=True, check=True)
info = json.loads(probe.stdout)
assert {"width": 1080, "height": 1920} in [{k: s[k] for k in ("width", "height")} for s in info["streams"] if "width" in s]
assert abs(float(info["format"]["duration"]) - 6.0) < 0.25 # 3 s + 3 s, sem o meio removido
# ---------------------------------------------------------------- projeto entregue pela IA
def test_auto_project_uses_pipeline_knowledge():
clip = {"session_id": 1, "start_s": 100.0, "end_s": 140.0, "peak_s": 120.0, "title": "Título do juiz"}
analysis = {"action_start_s": 95.0, "reaction_end_s": 143.0, "hook": "Caiu a 1 m da chegada"}
words = [Word(94.0, 95.3, "olha"), Word(96.0, 96.4, "vai"), Word(103.0, 103.5, "silêncio"),
Word(110.0, 110.4, "depois"), Word(118.0, 118.5, "NÃO"), Word(141.0, 141.4, "kkkk")]
loudness = [-20.0] * 200
for t in range(103, 111):
loudness[t] = -55.0 # quieto entre 103,5 e 110 s → a IA corta; perto do pico (120 s) não corta
# Mínimo de 15 s (configuração do canal): sobra espaço para tirar o silêncio. Com o padrão novo (60 s) o corte de
# 50 s já está abaixo do mínimo e nenhum silêncio sai (conferido no fim).
cfg = layout.merge({"layout": {"cam": {"x": 0, "y": 0.5, "w": 0.2, "h": 0.3}}, "duration": {"minS": 15}})
p = editor.auto_project(clip, analysis, words, loudness, 300.0, cfg, model="m", generated_at="agora")
assert p["cuts"][0]["start"] == 93.9 # ação em 95 − 0,8 s caiu no meio de "olha" → recua para antes da palavra
assert p["cuts"][-1]["end"] == 143.8 # fim da reação + 0,8 s
assert len(p["cuts"]) == 2 and p["cuts"][0]["end"] == 103.75 and p["cuts"][1]["start"] == 109.75
hook = next(o for o in p["overlays"] if o["kind"] == "hook")
zoom = next(o for o in p["overlays"] if o["kind"] == "zoom")
assert hook["text"] == "Caiu a 1 m da chegada" and zoom["start"] == 119.8 and zoom["y"] == 0.17 # rosto (câmera em cima)
assert p["captions"]["preset"] == "pop" and p["meta"]["origin"] == "ai" and p["cover"]["t"] == 120.0
assert any("silêncio" in s for s in p["meta"]["steps"])
# Sem análise e sem som medido: trecho do juiz, gancho = título.
q = editor.auto_project(clip, None, words, [], 300.0, layout.merge())
# Mínimo de verdade (17/11/2026): o trecho de 40 s do juiz cresce PARA TRÁS até mínimo + 1 s (+0,1 de folga).
assert (q["cuts"][0]["start"], q["cuts"][-1]["end"]) == (78.9, 140.0)
assert editor.output_duration(q["cuts"]) >= 61.0 and any("estendido" in s for s in q["meta"]["steps"])
assert q["overlays"][0]["text"] == "Título do juiz"
# Gancho do analista longo (frase descritiva) → usa o título do editor.
long_hook = {"hook": "A provocação sobre ficar moscando começa e, logo depois, ele solta vários gritos"}
assert editor.auto_project(clip, long_hook, words, [], 300.0, layout.merge())["overlays"][0]["text"] == "Título do juiz"
default_min = editor.auto_project(clip, analysis, words, loudness, 300.0, layout.merge())
assert len(default_min["cuts"]) == 1 and not any("silêncio" in s for s in default_min["meta"]["steps"])
def test_render_always_goes_through_project(api):
"""Um caminho de render: corte sem projeto ganha o da IA (origem ai); salvar no editor vira origem user."""
client, engine, root = api
h = {"X-Service-Key": KEY}
body = client.get("/v1/clips/7/edit", headers=h).json()
assert body["origin"] == "ai" and body["aiProject"]["meta"]["origin"] == "ai" and not body["saved"]
if FFMPEG:
_lavfi_segments(root / "data" / "s1")
engine.db.run("UPDATE clips SET status = 'pending' WHERE id = 7")
engine.render_pending()
clip = client.get("/v1/clips/7", headers=h).json()
assert clip["status"] == "ready", clip["error"]
assert clip["edit"]["origin"] == "ai"
project = client.get("/v1/clips/7/edit", headers=h).json()
saved = client.put("/v1/clips/7/edit", headers=h, json={"project": project["project"], "rev": project["rev"]})
assert saved.status_code == 200
again = client.get("/v1/clips/7/edit", headers=h).json()
assert again["origin"] == "user" and again["aiProject"]["meta"]["origin"] == "ai"
def test_pick_hook_never_shows_session_time_or_cuts_a_word():
from app.editor import pick_hook
# Medido no corte 25 do skipnho: o analista devolveu uma frase com o tempo da gravação e ela saía cortada.
raw = "Aos 5085s, ele solta que não veio de família rica e imediatamente começa a se desculpar"
text, from_analyst = pick_hook(raw, "“Não vim de família rica” — e vêm os pedidos de desculpa")
assert not from_analyst and text == "“Não vim de família rica” — e vêm os pedidos de desculpa"
assert pick_hook("O lag jogou ele na água", "título") == ("O lag jogou ele na água", True)
long_title = "Ele tenta explicar o que aconteceu com o carro e acaba piorando tudo na frente do chat"
text, _ = pick_hook("", long_title)
assert len(text) <= 60 and long_title.startswith(text) and not text.endswith(" ")
assert pick_hook("Aos 120s, vem o susto", None)[0] == "Vem o susto"