cadence/tests/test_handoff.py
pedro-bento 314aa99777
Some checks failed
CI / lint-and-test (push) Successful in 17s
CI / lint-and-test (pull_request) Successful in 12s
Version bump / version-bump (pull_request) Failing after 6s
feat(handoff): emit clipwright transcript + handoff manifest
2026-10-05 16:38:02 +01:00

227 lines
7.6 KiB
Python

import json
import cadence
from cadence import Segment, Word
def _timeline_with_drop():
return [
Segment(0.0, 2.0, "keep"),
Segment(2.0, 3.0, "drop", reason="filler"),
Segment(3.0, 5.0, "keep"),
]
def test_build_mapped_transcript_returns_subs_and_words(make_track):
td = make_track(
name="video.mp4",
duration=2.0,
timeline=[Segment(0.0, 2.0, "keep")],
words=[Word("Hello.", 0.2, 0.5), Word("world.", 0.6, 0.9)],
)
subs, mapped_words = cadence.build_mapped_transcript([td])
assert [w[2] for w in mapped_words] == ["Hello.", "world."]
assert len(subs) == 1
start, end, text, words = subs[0]
assert text == "Hello. world."
assert [w[2] for w in words] == ["Hello.", "world."]
assert start == 0.2
assert end >= 0.9
# Pure: a second call yields the same structure.
assert cadence.build_mapped_transcript([td]) == (subs, mapped_words)
def test_build_mapped_transcript_empty_when_no_words(make_track):
td = make_track(name="video.mp4", duration=2.0)
assert cadence.build_mapped_transcript([td]) == ([], [])
def test_generate_handoff_writes_valid_json(make_track, tmp_path):
td = make_track(
name="video.mp4",
duration=2.0,
timeline=[Segment(0.0, 2.0, "keep")],
words=[Word("Hello.", 0.2, 0.5), Word("world.", 0.6, 0.9)],
)
out = tmp_path / "recording.kdenlive"
cadence.generate_handoff([td], out, jl_mode="j", jl_frames=2, language="en")
transcript_path = tmp_path / "recording.transcript.json"
handoff_path = tmp_path / "recording.handoff.json"
assert transcript_path.exists()
assert handoff_path.exists()
assert not list(tmp_path.glob("*.tmp"))
transcript = json.loads(transcript_path.read_text(encoding="utf-8"))
handoff = json.loads(handoff_path.read_text(encoding="utf-8"))
# transcribe_media contract shape.
assert transcript["language"] == "en"
assert transcript["text"] == "Hello. world."
assert transcript["source"] == {
"kind": "cadence-edited",
"fps": 30.0,
"width": 1920,
"height": 1080,
"duration": 2.0,
"generator": f"cadence {cadence.__version__}",
}
assert len(transcript["segments"]) == 1
segment = transcript["segments"][0]
assert segment["text"] == "Hello. world."
assert set(segment) == {"start", "end", "text", "words"}
# clipwright.handoff/1 manifest.
assert handoff["schema"] == "clipwright.handoff/1"
assert handoff["generator"] == {"name": "cadence", "version": cadence.__version__}
assert handoff["edited"]["duration"] == 2.0
assert handoff["edited"]["master"] is None
assert handoff["edited"]["timeline"] == "voice-audio"
assert handoff["transcript"] == {"path": "recording.transcript.json"}
assert handoff["candidates"] == []
assert handoff["chapters"] == []
assert len(handoff["sources"]) == 1
assert handoff["sources"][0]["path"] == str((tmp_path / "video.mp4").resolve())
assert handoff["sources"][0]["audio_track"] == 1
assert [e["type"] for e in handoff["edit"]] == ["keep"]
# JSON round-trip.
assert json.loads(json.dumps(transcript)) == transcript
assert json.loads(json.dumps(handoff)) == handoff
def test_transcript_words_carry_leading_space(make_track, tmp_path):
td = make_track(
name="video.mp4",
duration=2.0,
timeline=[Segment(0.0, 2.0, "keep")],
words=[Word("Hello.", 0.2, 0.5), Word("world.", 0.6, 0.9)],
)
out = tmp_path / "recording.kdenlive"
cadence.generate_handoff([td], out)
transcript = json.loads(
(tmp_path / "recording.transcript.json").read_text(encoding="utf-8")
)
words = [w for seg in transcript["segments"] for w in seg["words"]]
assert words # non-empty
for word in words:
assert word["word"].startswith(" ")
assert word["word"].strip() == word["word"].lstrip()
assert [w["word"] for w in words] == [" Hello.", " world."]
def test_edited_duration_and_edit_reasons(make_track, tmp_path):
td = make_track(
name="video.mp4",
duration=5.0,
timeline=_timeline_with_drop(),
words=[
Word("a", 0.5, 0.8),
Word("dropped", 2.2, 2.5),
Word("b", 3.5, 3.8),
],
)
out = tmp_path / "recording.kdenlive"
cadence.generate_handoff([td], out, jl_mode="j", jl_frames=2)
handoff = json.loads(
(tmp_path / "recording.handoff.json").read_text(encoding="utf-8")
)
assert handoff["edited"]["duration"] == 4.0 # (2-0) + (5-3)
assert handoff["edited"]["jl_mode"] == "j"
assert handoff["edited"]["jl_frames"] == 2
edit = handoff["edit"]
assert [e["type"] for e in edit] == ["keep", "drop", "keep"]
assert edit[1]["reason"] == "filler"
assert edit[1]["start"] == 2.0
assert edit[1]["end"] == 3.0
# J/L applies: both keeps get a shift (the last is always 0).
assert edit[0]["video_shift_frames"] == 2
assert edit[2]["video_shift_frames"] == 0
def test_jl_off_omits_video_shift_frames(make_track, tmp_path):
td = make_track(
name="video.mp4",
duration=2.0,
timeline=[Segment(0.0, 2.0, "keep")],
words=[Word("Hello.", 0.2, 0.5)],
)
out = tmp_path / "recording.kdenlive"
cadence.generate_handoff([td], out, jl_mode="off", jl_frames=2)
handoff = json.loads(
(tmp_path / "recording.handoff.json").read_text(encoding="utf-8")
)
assert all("video_shift_frames" not in e for e in handoff["edit"])
def test_multi_input_concat_handoff(make_track, tmp_path):
td1 = make_track(
name="a.mp4",
duration=4.0,
timeline=[
Segment(0.0, 2.0, "keep"),
Segment(2.0, 3.0, "drop"),
Segment(3.0, 4.0, "keep"),
],
words=[Word("a", 1.0, 1.2), Word("dropped", 2.1, 2.3), Word("b", 3.1, 3.3)],
)
td2 = make_track(
name="b.mp4",
duration=3.0,
timeline=[Segment(0.0, 3.0, "keep")],
words=[Word("c", 0.5, 0.7)],
)
out = tmp_path / "recording.kdenlive"
cadence.generate_handoff([td1, td2], out)
handoff = json.loads(
(tmp_path / "recording.handoff.json").read_text(encoding="utf-8")
)
assert [s["index"] for s in handoff["sources"]] == [0, 1]
assert [s["duration"] for s in handoff["sources"]] == [4.0, 3.0]
assert handoff["edited"]["duration"] == 6.0 # (2+1) + 3
assert [e["source"] for e in handoff["edit"]] == [0, 0, 0, 1]
transcript = json.loads(
(tmp_path / "recording.transcript.json").read_text(encoding="utf-8")
)
assert transcript["source"]["duration"] == 6.0
all_words = [w["word"] for seg in transcript["segments"] for w in seg["words"]]
assert all_words == [" a", " b", " c"]
def test_srt_regression_unchanged(make_track, tmp_path):
td = make_track(
name="video.mp4",
duration=2.0,
timeline=[Segment(0.0, 2.0, "keep")],
words=[Word("Hello.", 0.2, 0.5), Word("world.", 0.6, 0.9)],
)
out = tmp_path / "recording.kdenlive"
cadence.generate_combined_transcripts([td], out)
srt = (tmp_path / "recording.srt").read_text(encoding="utf-8")
assert srt == (
"1\n"
"00:00:00,200 --> 00:00:01,000\n"
"Hello. world.\n"
)
assert (tmp_path / "recording_llm_transcript.txt").exists()
assert not list(tmp_path.glob("*.tmp"))
def test_parse_args_handoff_does_not_imply_captions(monkeypatch):
monkeypatch.setattr("sys.argv", ["cadence", "in.mp4", "--handoff"])
args = cadence.parse_args()
assert args.handoff is True
assert args.captions is False