from copy import deepcopy import pytest from test_dialogue_domain import A, B, C, S, U, ASSET, manifest from app.domain.audio_timeline import compile_audio_timeline from app.domain.dialogue import prepare_dialogue from app.services.editor_media import compile_captions, materialize_cues, shot_ranges def media(): m = manifest() m["captions"]["mode"] = "script" bindings = { "normalized_speech:" + U: dict(asset_id=ASSET, sample_count=290001, sample_rate=48000) } return m, bindings def test_cross_cut_caption_once_uses_frozen_global_sample_endpoints(): m, bindings = media() p = prepare_dialogue(m, bindings, {"max_clip_frames": 64}) assert len(p["placements"]) > 2 effective = p["effective_manifest"] # 外部口型模型的25fps工作副本不得影响成片的24fps时钟。 p["work_fps"] = 25 assert compile_captions(effective, {}, prepared=p) == [ dict(start_frame=5, end_frame=150, text="Hello") ] cues = materialize_cues(effective, {}, prepared=p) assert len(cues) == 1 and cues[0]["utterance_id"] == U assert compile_audio_timeline(effective, bindings)["segments"] == [] def test_pending_speech_keeps_draft_editable_and_final_compile_requires_measurement(): m, _ = media() assert materialize_cues(m, {}) == [] with pytest.raises(ValueError, match="配音"): compile_captions(m, {}) m["spec"]["dialogue"]["utterances"][0]["text"] = "" assert materialize_cues(m, {}) == [] def test_caption_overrides_are_independent_and_absolute_time_does_not_move(): m, bindings = media() m["captions"]["cues"] = [ dict( id=C, anchor="utterance", utterance_id=U, start_frame=2, end_frame=9, text="人工文字", text_override=True, timing_override=True, ), dict(id="a" * 32, anchor="absolute", start_frame=2, end_frame=4, text="片名"), ] before = deepcopy(m) assert compile_captions(m, bindings) == [ dict(start_frame=2, end_frame=4, text="片名"), dict(start_frame=7, end_frame=14, text="人工文字"), ] assert m == before m["captions"]["cues"][0]["timing_override"] = False assert compile_captions(m, bindings)[1] == dict(start_frame=5, end_frame=150, text="人工文字") def test_v3_dedup_applies_to_relative_caption_and_audio_layer(): m, _ = media() m["spec"]["shots"][1]["continuity"]["transition"] = "continue" m["spec"]["shots"][1]["edit"]["dedup_frames"] = 1 m["spec"]["render"]["total_frames"] = 239 m["captions"] = { "mode": "manual", "cues": [dict(id=C, anchor="shot", shot_id=B, start_frame=0, end_frame=2, text="x")], } assert shot_ranges(m) == ({A: (0, 120), B: (120, 239)}, 239) m["audio"]["layers"] = [ dict(id=C, asset_id=ASSET, anchor="shot", shot_id=B, source_end_sample=100) ] projection = compile_audio_timeline(m, {}, source_samples={ASSET: 100}) assert projection["total_samples"] == 239 * 2000 assert projection["segments"][0]["offset_sample"] == 120 * 2000 assert compile_captions(m, {}) == [dict(start_frame=120, end_frame=122, text="x")] def test_timing_without_override_follows_shortened_line(): m, bindings = media() m["captions"]["cues"] = [ dict( id=C, anchor="utterance", utterance_id=U, start_frame=0, end_frame=200, text="before", timing_override=False, ) ] assert compile_captions(m, bindings) == [dict(start_frame=5, end_frame=150, text="Hello")] def test_narration_and_dialogue_with_same_author_id_get_independent_cues(): m, bindings = media() m["audio"]["mode"] = "generated_narration" m["audio"]["segments"] = [dict(id=U, shot_id=A, text="原旁白")] bindings["audio:" + U] = dict(asset_id=ASSET, sample_count=12000, sample_rate=48000) cues = materialize_cues(m, bindings) assert len({cue["id"] for cue in cues}) == 2 assert {c["text"] for c in cues} == {"Hello", "原旁白"}