creative-brain-os / tests /test_edit_split.py
kpranav022's picture
Claude Opus 5
Fix two wiring bugs, stage the hook's own shot, keep every spoken word
b3cbda7
Raw History Blame Contribute Delete
5.14 kB
"""Splitting a clip sends the SAME manifest index twice, with a different `in`,
`out` and share of the spoken line each time. That's the whole reason a split is
free — no regeneration, just the source file trimmed twice — and it's also the
one shape `apply_edit` never saw before, so it gets a real end-to-end run.
"""
import subprocess
import pytest
from app.services import edit as edit_mod
from app.services.video import media_duration
pytestmark = pytest.mark.skipif(
subprocess.run(["which", "ffmpeg"], capture_output=True, check=False).returncode != 0,
reason="ffmpeg not installed",
)
def clip(seconds: float, hue: str) -> bytes:
"""A short mp4 with audio, which stitch_clips needs on every input."""
return subprocess.run(
["ffmpeg", "-v", "error", "-y",
"-f", "lavfi", "-t", str(seconds), "-i", f"color=c={hue}:s=240x426:r=12",
"-f", "lavfi", "-t", str(seconds), "-i", "sine=frequency=300",
"-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac",
"-f", "mp4", "-movflags", "frag_keyframe+empty_moov", "pipe:1"],
capture_output=True, check=True,
).stdout
@pytest.fixture
def fake_store(monkeypatch):
"""Storage and Supabase stubbed out; ffmpeg is left real — the point is to
exercise the trim/stitch/caption chain, not the mocks."""
files = {"clip0.mp4": clip(4.0, "red"), "clip1.mp4": clip(3.0, "blue")}
manifest = [
{"index": 0, "file": "clip0.mp4", "duration": 4.0, "spoken": "save four hundred dollars"},
{"index": 1, "file": "clip1.mp4", "duration": 3.0, "spoken": "on cover you already have"},
]
saved: dict = {}
monkeypatch.setattr(edit_mod, "load_manifest", lambda rid: manifest)
monkeypatch.setattr(edit_mod, "load_media", lambda key: files[key])
monkeypatch.setattr(
edit_mod, "save_clips", lambda rid, pairs, niche="": saved.update(clips=pairs)
)
monkeypatch.setattr(
edit_mod, "save_render",
lambda name, data, niche="": saved.update(video=data, name=name) or name,
)
class Store:
def select(self, *a, **k):
return [{"id": "r1", "niche": "insurance", "asset_id": "a1", "candidate_number": 1}]
def insert(self, *a, **k):
return None
monkeypatch.setattr(edit_mod, "SupabaseStore", Store)
# Captions are best-effort in apply_edit, so a silent failure would hide a
# real bug. Record what it was asked to burn in instead.
burned: list[list[dict]] = []
real = edit_mod.apply_overlays
monkeypatch.setattr(
edit_mod, "apply_overlays",
lambda video, overlays: burned.append(overlays) or real(video, overlays),
)
return saved, burned
def test_split_trims_one_source_twice(fake_store):
saved, burned = fake_store
edit_mod.apply_edit(
"r1",
[
{"index": 0, "in": 0.0, "out": 2.5, "spoken": "save four"},
{"index": 0, "in": 2.5, "out": 4.0, "spoken": "hundred dollars"},
{"index": 1, "in": 0.0, "out": 3.0, "spoken": "on cover you already have"},
],
captions=True,
caption_source="script",
caption_cues=None, # force apply_edit to derive cues from the kept pieces
)
# Three pieces out of two source files, at the lengths asked for.
pieces = saved["clips"]
assert len(pieces) == 3
assert [round(m["duration"], 1) for _, m in pieces] == [2.5, 1.5, 3.0]
assert [m["spoken"] for _, m in pieces] == [
"save four", "hundred dollars", "on cover you already have",
]
# Hard cuts (video_crossfade_seconds = 0), so the export keeps the full 7.0s of
# its three pieces rather than losing the overlap a dissolve would have eaten.
assert 6.9 <= media_duration(saved["video"], ".mp4") <= 7.3
# Deduped: karaoke captions emit one overlay per spoken word, all carrying the
# same window text, so counting raw overlays would count word states.
texts = " ".join(dict.fromkeys(o["text"] for o in burned[0]))
assert "save four" in texts and "hundred dollars" in texts
# The whole sentence must not appear under BOTH halves — that was the bug the
# per-piece `spoken` exists to prevent.
assert texts.count("save") == 1, texts
def test_a_piece_without_spoken_falls_back_to_the_manifest(fake_store):
"""An unsplit clip omits `spoken`; it must still be captioned with its line."""
saved, burned = fake_store
edit_mod.apply_edit(
"r1",
[{"index": 1, "in": 0.0, "out": 3.0}],
captions=True,
caption_source="script",
caption_cues=None,
)
assert saved["clips"][0][1]["spoken"] == "on cover you already have"
assert "cover" in " ".join(o["text"] for o in burned[0])
def test_dragged_caption_placement_reaches_the_renderer(fake_store):
_, burned = fake_store
edit_mod.apply_edit(
"r1",
[{"index": 1, "in": 0.0, "out": 3.0}],
captions=True,
caption_source="script",
caption_cues=None,
caption_x=0.3,
caption_y=0.7,
)
assert all(o["x"] == 0.3 and o["y"] == 0.7 for o in burned[0])