from copy import deepcopy import pytest from pydantic import ValidationError from app.domain.dialogue import ( has_continuity, has_dialogue, prepare_dialogue, supports_sample_timing, upgrade_dialogue_manifest, ) from app.domain.editors import validate_manifest from app.domain.stories import parse_story_spec from app.schemas.editors import SaveEditor from app.schemas.stories import StoryCreate A, B, S, U, ASSET, C = (f"{i:032x}" for i in range(1, 7)) def manifest(): old = validate_manifest( dict(source_kind="story", spec={"shots": [{"id": A}, {"id": B}]}, shot_ids=[A, B]) ) saved = deepcopy(old) m = upgrade_dialogue_manifest(old) assert old == saved and "dialogue" not in old["spec"] d = m["spec"]["dialogue"] # 此夹具保存旧 TTS/Muse 图的作者合同;新模式另有专用原生夹具。 d.pop("generation", None) d.pop("language", None) seq, other = d["sequences"] seq.update(shot_ids=[A, B], utterance_ids=[U]) d.update( speakers=[dict(id=S, name="Narrator", voice={})], utterances=[dict(id=U, speaker_id=S, text="Hello")], sequences=[seq], ) m["spec"]["shots"][1]["performance"]["sequence_id"] = seq["id"] return validate_manifest(m) def prepare(m, count, **caps): return prepare_dialogue( m, {U: dict(asset_id=ASSET, sample_count=count, sample_rate=48000)}, caps ) def test_upgrade_dispatch_and_invalid_references(): m = manifest() assert has_dialogue(m) and supports_sample_timing(m) and not has_continuity(m) assert ( SaveEditor(expected_version=1, manifest=m).model_dump()["manifest"]["schema_version"] == 3 ) assert StoryCreate(name="story", spec=m["spec"]).model_dump()["spec"]["dialogue"]["enabled"] m["spec"]["dialogue"]["utterances"][0]["speaker_id"] = C with pytest.raises(ValidationError): parse_story_spec(m["spec"]) def test_long_recording_placed_once_across_cuts_and_stable_split(): m = manifest() saved = deepcopy(m) result = prepare(m, 290001, max_clip_frames=64) assert m == saved assert not result["issues"] assert len(result["utterances"]) == 1 (line,) = result["utterances"] assert (line["start_sample"], line["end_sample"]) == (9600, 299601) slices = result["placements"] assert len(slices) >= 3 and all(not p["visible"] for p in slices) assert sum(p["end_sample"] - p["start_sample"] for p in slices) == 290001 assert slices[-1]["source_end_sample"] == 290001 assert prepare(m, 290001, max_clip_frames=64) == result effective = result["effective_manifest"] assert len(effective["shot_ids"]) > 2 and not effective["audio"]["segments"] second = prepare(effective, 290001, max_clip_frames=64) assert second["effective_manifest"] == effective def test_locked_duration_and_absolute_cue_conflicts(): m = manifest() for shot in m["spec"]["shots"]: shot["performance"]["duration_mode"] = "fixed" assert prepare(m, 700000)["issues"] m = manifest() m["captions"]["cues"] = [ dict(id=C, anchor="absolute", start_frame=180, end_frame=190, text="x") ] result = prepare(m, 50000) assert result["issues"] assert result["effective_manifest"]["captions"]["cues"][0]["start_frame"] == 180 def test_tts_hash_follows_spoken_content_not_timeline(): m = manifest() (first,) = prepare(m, 100000)["utterances"] seq = m["spec"]["dialogue"]["sequences"][0] seq["shot_ids"] = [B] m["spec"]["shots"][0]["performance"]["sequence_id"] = None (second,) = prepare(m, 100000)["utterances"] assert first["content_hash"] == second["content_hash"] assert first["placement_hash"] != second["placement_hash"] def test_rounded_sequence_endpoints_use_one_global_sample_clock(): m = manifest() d = m["spec"]["dialogue"] second_uid = "e" * 32 (seq,) = d["sequences"] seq["shot_ids"] = [A] seq2 = dict(seq, id=C, shot_ids=[B], utterance_ids=[second_uid]) d["sequences"] = [seq2, seq] d["utterances"].append(dict(d["utterances"][0], id=second_uid)) m["spec"]["shots"][1]["performance"]["sequence_id"] = C bindings = {uid: dict(asset_id=ASSET, sample_count=100001) for uid in (U, second_uid)} result = prepare_dialogue(m, bindings, {}) assert not result["issues"] assert result["timing"]["total_frames"] == 125 first, second = result["utterances"] assert second["start_sample"] - first["end_sample"] == 24000 def test_provider_defaults_and_derived_binding_keys(): from app.domain.dialogue import DialogueVoice, speech_stem_id assert DialogueVoice().model == "kokoro-82m" assert DialogueVoice().revision == 1 with pytest.raises(ValidationError): DialogueVoice(style="angrily") with pytest.raises(ValidationError): DialogueVoice(model="https://example.com/private") m = manifest() stem = speech_stem_id(A, S) m["bindings"]["speech_stem:" + stem] = dict(asset_id=ASSET, source="generated", kind="audio") m["bindings"]["dialogue_report:" + A] = dict(asset_id=ASSET, source="generated", kind="json") assert validate_manifest(m)["bindings"] == validate_manifest(validate_manifest(m))["bindings"] def test_v5_continue_deducts_exactly_one_frame_and_fixed_sequence_stays_fixed(): m = manifest() s1, s2 = m["spec"]["shots"] s2["continuity"]["transition"] = "continue" s2["edit"]["dedup_frames"] = 1 m["spec"]["dialogue"]["sequences"][0]["timing_mode"] = "fixed" assert has_continuity(m) result = prepare(m, 270000) assert not result["issues"] and result["timing"]["total_frames"] == 239 assert len(result["placements"]) == 2 assert result["placements"][1]["start_sample"] == 240000 assert result["placements"][1]["shot_start_sample"] == 0 def test_prepared_children_can_expand_again_without_reusing_existing_ids(): first = prepare(manifest(), 290001, max_clip_frames=64)["effective_manifest"] old_ids = set(first["shot_ids"]) result = prepare(first, 750000, max_clip_frames=64) ids = result["effective_manifest"]["shot_ids"] assert not result["issues"] and old_ids < set(ids) and len(set(ids)) == len(ids) def test_split_moves_relative_caption_and_layer_to_matching_child(): m = manifest() m["captions"]["cues"] = [ dict(id=C, anchor="shot", shot_id=A, start_frame=32, end_frame=50, text="abc") ] m["audio"]["layers"] = [ dict(id="a" * 32, anchor="shot", shot_id=A, offset_sample=96000, asset_id=ASSET) ] result = prepare(m, 290001, max_clip_frames=64) assert not result["issues"] clips = result["provenance"]["shot_children"][A] assert len(clips) == 2 first_clip = result["timing"]["shots"][0]["contribution_frames"] cues = result["effective_manifest"]["captions"]["cues"] assert [(c["shot_id"], c["start_frame"], c["end_frame"]) for c in cues] == [ (A, 32, first_clip), (clips[1], 0, 50 - first_clip), ] (layer,) = result["effective_manifest"]["audio"]["layers"] assert (layer["shot_id"], layer["offset_sample"]) == (clips[1], 96000 - first_clip * 2000) def test_sample_timing_is_editor_feature_independent_of_story_continuity(): raw = {"schema_version": 2, "source_kind": "workflow", "spec": {"schema_version": 2}} assert supports_sample_timing(raw) assert not has_continuity(raw) and not has_dialogue(raw) def test_manual_shift_default_preserves_historical_shot_bytes(): from app.domain.dialogue import ShotPerformance assert ShotPerformance().audio_context_shift_frames == 0 for value in [-6, 3, 6]: assert ( ShotPerformance(audio_context_shift_frames=value).model_dump()[ "audio_context_shift_frames" ] == value ) assert ( "audio_context_shift_frames" not in ShotPerformance(audio_context_shift_frames=0).model_dump() ) for value in [-7, 7, True, "3", 1.5]: with pytest.raises(ValidationError): ShotPerformance(audio_context_shift_frames=value)