"""JUIZ de contexto (doc/clipador/EQUIPE-IA.md): decisão pelo código (aprovar / ajustar e conferir uma vez / frase de contexto / descartar), o fluxo no motor, a linha do tempo JSON dentro do orçamento e o mosaico de quadros.""" import json import shutil import subprocess from pathlib import Path import pytest from app import context_judge, editor_api, media, timeline from app import roles as roles_module from app.judge import Judge, Verdict from app.scoring import ChatMessage, Word from .test_render_and_duplicates import add_clip from .test_sources_and_api import auth, env, seed_session # noqa: F401 - fixture reaproveitada FFMPEG = shutil.which("ffmpeg") FFPROBE = shutil.which("ffprobe") def result(failed=(), fix=None, line="", reason="motivo"): return {"viewer_summary": "resumo", "confidence": 0.8, "reason": reason, "pt": {"reason": reason}, "checks": {c: {"ok": c not in failed, "t": None, "why": ""} for c in roles_module.CHECKS}, "fix": {"start_s": None, "end_s": None, "context_line": line, **(fix or {})}} LIMITS = {"start_s": 200.0, "end_s": 265.0, "lo": 80.0, "hi": 280.0, "min_s": 61.0, "max_s": 90.0} CONF = roles_module.default_roles()["judge"] # ---------------------------------------------------------------- decisão pelo código def test_all_checks_ok_approves(): d = context_judge.decide(result(), conf=CONF, score=8, min_score=6, **LIMITS) assert d.action == "approve" and d.failed == [] and d.window is None and d.score_after == 8 def test_missing_setup_with_fix_adjusts_inside_the_limits(): d = context_judge.decide(result(["setup"], {"start_s": 150}), conf=CONF, score=8, **LIMITS) assert d.action == "adjust" and d.failed_critical == ["setup"] # Recuou o início para pegar o começo; com máximo de 90 s o fim encolhe (fit_window mantém o início). assert d.window == (150.0, 240.0) and "começo (setup)" in d.reason_pt def test_fix_outside_the_window_is_clamped_and_no_change_means_discard(): d = context_judge.decide(result(["payoff"], {"end_s": 999}), conf=CONF, **LIMITS) assert d.action == "adjust" and d.window[1] == 280.0 # nunca passa do limite same = context_judge.decide(result(["payoff"], {"end_s": 265.2}), conf=CONF, **LIMITS) assert same.action == "discard" # "ajuste" que não muda nada def test_critical_failure_without_fix_discards_with_portuguese_reason(): d = context_judge.decide(result(["payoff"], reason="ends before the fight is resolved"), conf=CONF, **LIMITS) assert d.action == "discard" and d.reason_pt.startswith("Descartado pelo juiz: sem desfecho.") no_fix = context_judge.decide(result(["setup"], {"start_s": 150}), conf={**CONF, "allowFix": False}, **LIMITS) assert no_fix.action == "discard" def test_context_line_solves_standalone(): d = context_judge.decide(result(["standalone"], line="Tfue tries to talk his way out of a robbery"), conf=CONF, score=8, min_score=6, **LIMITS) assert d.action == "approve" and d.solved_by_line == ["standalone"] and d.context_line.startswith("Tfue") off = context_judge.decide(result(["standalone"], line="Tfue tries"), conf={**CONF, "allowContextLine": False}, **LIMITS) assert off.action == "discard" too_long = context_judge.decide(result(["standalone"], line=" ".join(["word"] * 14)), conf=CONF, **LIMITS) assert too_long.action == "discard" def test_non_critical_failures_only_lower_the_score(): d = context_judge.decide(result(["strong_open"]), conf=CONF, score=7, min_score=6, **LIMITS) assert d.action == "approve" and d.score_after == 6.5 low = context_judge.decide(result(["strong_open", "faithful", "intelligible"]), conf=CONF, score=7, min_score=6, **LIMITS) assert low.action == "discard" and low.score_after == 5.5 and "nota caiu" in low.reason_pt custom = context_judge.decide(result(["strong_open"]), conf={**CONF, "critical": ["strong_open"]}, **LIMITS) assert custom.action == "discard" # as críticas são calibráveis no painel def test_manual_clip_only_warns(): d = context_judge.decide(result(["payoff"], {"end_s": 280}), conf=CONF, manual=True, **LIMITS) assert d.action == "warn" and d.window is None and d.reason_pt.startswith("Aviso do juiz") # ---------------------------------------------------------------- fluxo no motor class ContextFakeJudge(Judge): """Analista, editor e juiz falsos (nunca chama CLI). `answers` = respostas do juiz, em ordem.""" def __init__(self, answers, keep=True, score=8.0): super().__init__("codex", "fake", Path(".")) self.answers = list(answers) self.keep, self.score_value = keep, score self.context_prompts: list[str] = [] self.editor_prompt = "" def analyze(self, prompt, images, lang="pt"): return {"situation": "treta", "what_happened": "Ele tenta negociar e leva um tiro.", "screen": "", "speech": "", "chat_meaning": "", "confidence": 0.9, "action_start_s": 270, "reaction_end_s": 330, "hook": "", "lines": [{"n": 1, "v": "streamer aponta rifle para mascarado"}]} def judge(self, prompt, start_s, end_s, images=None, lang="pt", **_limits): self.editor_prompt = prompt return Verdict(self.keep, self.score_value, 270.0, 340.0, "Tentou negociar e levou tiro", "c", "r", "fake") def check_context(self, prompt, lang="pt"): self.context_prompts.append(prompt) answer = self.answers.pop(0) if isinstance(answer, Exception): raise answer return answer def run_clip(engine, judge, source="chat"): engine.judge = judge # Sem ffmpeg nos blocos falsos: um "mosaico" de um quadro só (em 275 s), que o analista descreve. engine.intel.visual = lambda *_a, **_k: ([Path("mosaico-falso.jpg")], [275.0], True) sid = seed_session(engine) cid = (engine.db.get("SELECT MAX(id) AS n FROM clips")["n"] or 0) + 1 add_clip(engine, sid, cid, "candidate", 300, 280, 345, source=source) engine.judge_clip(engine.db.get("SELECT * FROM clips WHERE id = ?", cid)) row = engine.db.get("SELECT * FROM clips WHERE id = ?", cid) return row, json.loads(row["judge_json"]) def test_engine_approves_and_records_the_context(env): # noqa: F811 _client, engine = env row, info = run_clip(engine, ContextFakeJudge([result(line="Tenta negociar com o assaltante")])) assert row["status"] == "pending" and info["context"]["decision"] == "approve" assert info["context"]["contextLine"] == "Tenta negociar com o assaltante" prompt = engine.judge.context_prompts[0] # O juiz vê só o que vai no corte: o trecho e os 20 s antes marcados out=1. assert "Corte que o espectador verá: 270s a 340s" in prompt and '"out":1' in prompt assert "História pretendida (analista): Ele tenta negociar e leva um tiro." in prompt # O que o analista viu no quadro virou o `v` da linha do tempo do editor. assert '"v":"streamer aponta rifle para mascarado"' in engine.judge.editor_prompt def test_engine_adjusts_once_then_approves(env): # noqa: F811 _client, engine = env row, info = run_clip(engine, ContextFakeJudge([result(["setup"], {"start_s": 250}), result()])) assert row["status"] == "pending" and (row["start_s"], row["end_s"]) == (250.0, 340.0) ctx = info["context"] assert ctx["decision"] == "adjust" and ctx["window"] == [250.0, 340.0] and ctx["first"]["failedCritical"] == ["setup"] assert len(engine.judge.context_prompts) == 2 and "250s a 340s" in engine.judge.context_prompts[1] def test_engine_adjusts_then_discards_when_still_failing(env): # noqa: F811 client, engine = env row, info = run_clip(engine, ContextFakeJudge([result(["payoff"], {"end_s": 355}), result(["payoff"], {"end_s": 360})])) assert row["status"] == "discarded" and info["discardedBy"] == "context" assert info["context"]["decision"] == "discard" and "depois do ajuste" in info["context"]["reasonPt"] assert len(engine.judge.context_prompts) == 2 # confere UMA vez de novo, nunca mais # Descartado pelo juiz fica visível e dá para forçar o render, como o descarte de hoje. forced = client.post("/v1/clips/1/render", headers=auth()).json() assert forced["status"] == "pending" and forced["judge"]["context"]["decision"] == "discard" def test_engine_discard_without_fix_and_judge_failure_keeps_editor(env): # noqa: F811 _client, engine = env row, info = run_clip(engine, ContextFakeJudge([result(["standalone"])])) assert row["status"] == "discarded" and info["context"]["failedCritical"] == ["standalone"] from app.judge import JudgeError row, info = run_clip(engine, ContextFakeJudge([JudgeError("cota")])) assert row["status"] == "pending" and info["context"] == {"error": "cota"} def test_judge_role_off_and_editor_discard_skip_the_judge(env): # noqa: F811 _client, engine = env judge = ContextFakeJudge([result(["payoff"])]) judge.roles["judge"]["enabled"] = False row, info = run_clip(engine, judge) assert row["status"] == "pending" and "context" not in info and judge.context_prompts == [] kept_out = ContextFakeJudge([result()], keep=False) row, info = run_clip(engine, kept_out) assert row["status"] == "discarded" and kept_out.context_prompts == [] # o editor já descartou def test_manual_clip_is_never_discarded_by_the_judge(env): # noqa: F811 _client, engine = env row, info = run_clip(engine, ContextFakeJudge([result(["payoff"], {"end_s": 360})]), source="manual") assert row["status"] == "pending" and info["context"]["decision"] == "warn" assert (row["start_s"], row["end_s"]) == (280.0, 345.0) def test_context_line_becomes_the_opening_text_of_the_edit(env): # noqa: F811 _client, engine = env row, _info = run_clip(engine, ContextFakeJudge([result(["setup"], {"start_s": 250}), result(line="Ele tenta negociar com o assaltante")])) session = engine.db.get("SELECT * FROM sessions WHERE id = ?", row["session_id"]) project = editor_api.auto_for(engine, row, session) hook = next(o for o in project["overlays"] if o["kind"] == "hook") assert hook["text"] == "Ele tenta negociar com o assaltante" # O ajuste do juiz não é desfeito pelo início da ação do analista (270 s). assert project["cuts"][0]["start"] <= 250.5 # ---------------------------------------------------------------- linha do tempo JSON def _evidence(seconds=240): words, chat = [], [] for t in range(0, seconds, 2): words.append(Word(float(t), t + 1.5, f"palavra{t}", "other1" if 60 <= t < 64 else "")) for t in range(0, seconds, 3): chat.append(ChatMessage(float(t), "KEKW que isso" if t % 2 else "lol", emotes=("KEKW",) if t % 2 else ())) loudness = [-30.0] * seconds loudness[100] = -10.0 return timeline.Evidence(words=words, chat=chat, loudness=loudness, face=[(101, "laugh", 6.4)], scenes=[(100.0, "explosão na garagem")]) def test_timeline_entries_are_short_split_by_speaker_and_marked(): ev = _evidence() items = timeline.entries(ev, 40, 130, clip=(60, 120)) assert all(i["d"] <= 8 for i in items) and items[0]["t"] == 40 assert any(i.get("sp", "").startswith("[other1]") for i in items) # troca de falante abre entrada nova # Fora do corte (antes e depois) sai marcado; nenhuma entrada atravessa o início ou o fim do corte. assert all(i.get("out") == 1 for i in items if i["t"] < 60 or i["t"] >= 120) assert not any("out" in i for i in items if 60 <= i["t"] < 120) assert {60, 120} <= {i["t"] for i in items} and all(i["t"] + i["d"] <= 60 for i in items if i["t"] < 60) hit = next(i for i in items if i["t"] <= 100 < i["t"] + i["d"]) assert hit["v"] == "explosão na garagem" and hit["face"] == "laugh.6" and hit["lvl"] > 3 assert hit["chat"].split("|")[0].isdigit() and "KEKW×" in hit["chat"] def test_timeline_fits_the_budget(): ev = _evidence(600) full = timeline.build(ev, 0, 600, {"win": [0, 600]}, budget_chars=100_000) small = timeline.build(ev, 0, 600, {"win": [0, 600]}, budget_chars=6000) assert len(small) <= 6000 < len(full) assert all(json.loads(line) for line in small.splitlines()) # cada linha continua JSON válido coarse = timeline.build(ev, 0, 600, {"win": [480, 600]}, budget_chars=100_000, coarse_until=480) first = json.loads(coarse.splitlines()[1]) assert first["d"] == 20 and "face" not in first # a "volta" da varredura vai resumida def test_scene_lines_are_saved_by_tile_number(env): # noqa: F811 _client, engine = env sid = seed_session(engine) n = timeline.save_scene_lines(engine, sid, "scan", [{"n": 2, "v": " carro capotando "}, {"n": 9, "v": "x"}, {"n": "a"}], [10.0, 40.0, 70.0]) assert n == 1 and timeline.scene_lines(engine, sid, 0, 100) == [(40.0, "carro capotando")] @pytest.mark.skipif(not (FFMPEG and FFPROBE), reason="FFmpeg não instalado") @pytest.mark.parametrize("count,size", [(8, "1536x432"), (6, "1536x432"), (3, "1152x216")]) def test_mosaic_puts_frames_in_one_image(tmp_path, count, size): frames = [] for i in range(count): out = tmp_path / f"f{i}.jpg" subprocess.run([FFMPEG, "-y", "-v", "error", "-f", "lavfi", "-i", f"color=c=0x{i * 30:02x}4080:s=768x432", "-frames:v", "1", str(out)], check=True) frames.append(out) target = media.mosaic(FFMPEG, frames, tmp_path / "m.jpg") probe = subprocess.run([FFPROBE, "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height", "-of", "csv=s=x:p=0", str(target)], capture_output=True, text=True, check=True) assert probe.stdout.strip() == size