clipwright/tests/test_handoff.py
pedro-bento 79341f0a38
All checks were successful
CI / unit (pull_request) Successful in 45s
CI / backend-e2e (pull_request) Successful in 1m20s
CI / dashboard-e2e (pull_request) Successful in 2m28s
feat(pipeline): integrate profiles, layout, handoff, and provenance
2026-10-05 17:01:08 +01:00

166 lines
6.6 KiB
Python

"""Cadence handoff ingest (PLAN #8): manifest validation + word merging.
These exercise ``main.load_handoff_transcript`` directly. The CLI/e2e wiring
(real ffmpeg, handoff actually skipping ASR) lives under ``tests/e2e``.
"""
import json
import pytest
main = pytest.importorskip("clipwright.pipeline.main") # needs cv2/mediapipe stubs
def _write(path, data):
path.write_text(json.dumps(data), encoding="utf-8")
return path
def _transcript(words):
return {
"text": " So the -Kanal.",
"language": "en",
"segments": [{"start": 0.0, "end": 0.8, "text": " So the -Kanal.",
"words": words}],
}
WORDS = [
{"word": " So", "start": 0.0, "end": 0.3},
{"word": " the", "start": 0.3, "end": 0.5},
{"word": "-Kanal.", "start": 0.5, "end": 0.8},
]
def _manifest(tmp_path, *, duration=6.0, transcript_rel="recording.transcript.json",
schema="clipwright.handoff/1", transcript_ref=None):
return {
"schema": schema,
"generator": {"name": "cadence", "version": "0.1.0"},
"edited": {"timeline": "voice-audio", "duration": duration, "master": None},
"sources": [{"index": 0, "path": "/abs/recordings/rec.mkv", "duration": 612.3}],
"transcript": transcript_ref if transcript_ref is not None
else {"path": transcript_rel},
"candidates": [],
"chapters": [],
}
# --- happy path ------------------------------------------------------------
def test_handoff_loads_and_merges_continuation_words(tmp_path):
_write(tmp_path / "recording.handoff.json", _manifest(tmp_path))
_write(tmp_path / "recording.transcript.json", _transcript(WORDS))
result = main.load_handoff_transcript(
tmp_path / "recording.handoff.json", tmp_path / "master.mp4", 6.0)
words = result["segments"][0]["words"]
# The leading-space tokens stay boundaries; "-Kanal." (no leading space)
# glues onto " the" and extends its end.
assert [w["word"] for w in words] == [" So", " the-Kanal."]
assert words[1]["end"] == 0.8
def test_transcript_path_resolves_relative_to_the_manifest(tmp_path):
sub = tmp_path / "bundle"
sub.mkdir()
_write(sub / "rec.handoff.json", _manifest(tmp_path, transcript_rel="rec.transcript.json"))
_write(sub / "rec.transcript.json", _transcript(WORDS))
result = main.load_handoff_transcript(sub / "rec.handoff.json", "master.mp4", 5.99)
assert result["segments"]
def test_missing_words_list_is_tolerated(tmp_path):
transcript = {"language": "en",
"segments": [{"start": 0.0, "end": 1.0, "text": " no words"}]}
_write(tmp_path / "recording.handoff.json", _manifest(tmp_path))
_write(tmp_path / "recording.transcript.json", transcript)
result = main.load_handoff_transcript(
tmp_path / "recording.handoff.json", "master.mp4", 6.0)
assert "words" not in result["segments"][0]
# --- failure modes ---------------------------------------------------------
def test_missing_manifest_is_refused(tmp_path):
with pytest.raises(main.HandoffError) as exc:
main.load_handoff_transcript(tmp_path / "nope.json", "master.mp4", 6.0)
assert "not found" in str(exc.value)
def test_malformed_manifest_json_is_refused(tmp_path):
(tmp_path / "bad.handoff.json").write_text("{ not json", encoding="utf-8")
with pytest.raises(main.HandoffError) as exc:
main.load_handoff_transcript(tmp_path / "bad.handoff.json", "master.mp4", 6.0)
assert "malformed handoff manifest" in str(exc.value)
def test_missing_transcript_is_refused(tmp_path):
_write(tmp_path / "recording.handoff.json", _manifest(tmp_path))
with pytest.raises(main.HandoffError) as exc:
main.load_handoff_transcript(
tmp_path / "recording.handoff.json", "master.mp4", 6.0)
assert "transcript not found" in str(exc.value)
def test_manifest_without_transcript_path_is_refused(tmp_path):
_write(tmp_path / "recording.handoff.json", _manifest(tmp_path, transcript_ref={}))
with pytest.raises(main.HandoffError) as exc:
main.load_handoff_transcript(
tmp_path / "recording.handoff.json", "master.mp4", 6.0)
assert "transcript.path" in str(exc.value)
def test_transcript_without_segments_is_refused(tmp_path):
_write(tmp_path / "recording.handoff.json", _manifest(tmp_path))
_write(tmp_path / "recording.transcript.json", {"language": "en", "segments": []})
with pytest.raises(main.HandoffError) as exc:
main.load_handoff_transcript(
tmp_path / "recording.handoff.json", "master.mp4", 6.0)
assert "no segments" in str(exc.value)
def test_unknown_schema_is_refused(tmp_path):
_write(tmp_path / "recording.handoff.json",
_manifest(tmp_path, schema="something.else/9"))
_write(tmp_path / "recording.transcript.json", _transcript(WORDS))
with pytest.raises(main.HandoffError) as exc:
main.load_handoff_transcript(
tmp_path / "recording.handoff.json", "master.mp4", 6.0)
assert "unsupported handoff schema" in str(exc.value)
# --- duration gate ---------------------------------------------------------
@pytest.mark.parametrize("measured", [5.96, 6.0, 6.04])
def test_duration_within_one_percent_passes(tmp_path, measured):
_write(tmp_path / "recording.handoff.json", _manifest(tmp_path, duration=6.0))
_write(tmp_path / "recording.transcript.json", _transcript(WORDS))
assert main.load_handoff_transcript(
tmp_path / "recording.handoff.json", "master.mp4", measured)["segments"]
def test_duration_mismatch_refuses_and_points_at_transcript(tmp_path):
_write(tmp_path / "recording.handoff.json", _manifest(tmp_path, duration=6.0))
_write(tmp_path / "recording.transcript.json", _transcript(WORDS))
with pytest.raises(main.HandoffError) as exc:
main.load_handoff_transcript(
tmp_path / "recording.handoff.json", "master.mp4", 7.5)
message = str(exc.value)
assert "refusing the handoff" in message
assert "--transcript" in message
# --- never opens source paths ----------------------------------------------
def test_source_paths_are_never_opened(tmp_path):
# A remote manifest may name any path; the loader must not touch it.
manifest = _manifest(tmp_path)
manifest["sources"] = [{"index": 0, "path": str(tmp_path / "does-not-exist.mkv")}]
manifest["edited"]["master"] = {"path": str(tmp_path / "also-missing.mp4")}
_write(tmp_path / "recording.handoff.json", manifest)
_write(tmp_path / "recording.transcript.json", _transcript(WORDS))
assert main.load_handoff_transcript(
tmp_path / "recording.handoff.json", "master.mp4", 6.0)["segments"]