cmvr-visual/backend/tests/test_native_dialogue_engine.py

171 lines
6.8 KiB
Python
Raw Normal View History

"""原生对白以整镜音视频为单位,台词分配不能猜测或截断。"""
from copy import deepcopy
from types import SimpleNamespace
import pytest
def manifest():
from app.domain.dialogue import upgrade_dialogue_manifest
from app.domain.editors import manifest_from_draft
from test_dialogue_engine import story
m = upgrade_dialogue_manifest(
manifest_from_draft(
SimpleNamespace(
id="a" * 32,
kind="story_v3",
spec=story(),
editor_manifest=None,
)
)
)
d = m["spec"]["dialogue"]
d["generation"], d["language"] = "native_av", "Chinese"
speaker = d["speakers"][0]
speaker["native_voice"] = "年轻女性,清晰温柔的中音,语速自然"
d["utterances"] = [{"id": "4" * 32, "speaker_id": speaker["id"], "text": "我会在这里等你。"}]
d["sequences"][0]["utterance_ids"] = ["4" * 32]
return m
def test_single_shot_native_lines_are_explicitly_compiled_without_tts():
from app.domain.native_dialogue import compile_native_shots
m = manifest()
result = compile_native_shots(m)
assert not result["issues"]
row = result["shots"][0]
assert row["utterance_ids"] == ["4" * 32]
assert row["parameters"]["num_frames"] in range(124, 346, 17)
assert row["parameters"]["profile"] == "minimax-h3-fl2va-native-av-v1"
assert "<d>[Chinese] 我会在这里等你。</d>" in row["prompt"]
assert "non_diegetic_music: N/A" in row["prompt"]
assert "不做口型" not in row["prompt"]
assert (
result["manifest"]["spec"]["shots"][0]["edit"]["duration_frames"]
== row["parameters"]["num_frames"]
)
assert m == manifest(), "计划不能原地修改作者清单"
def test_shared_sequence_does_not_guess_which_shot_speaks():
from app.domain.native_dialogue import compile_native_shots
m = manifest()
second = deepcopy(m["spec"]["shots"][0])
second["id"] = "5" * 32
m["spec"]["shots"].append(second)
m["shot_ids"].append(second["id"])
m["spec"]["dialogue"]["sequences"][0]["shot_ids"] = m["shot_ids"].copy()
result = compile_native_shots(m)
assert "native_utterance_assignment_required" in {x["code"] for x in result["issues"]}
m["spec"]["shots"][0]["performance"]["native_utterance_ids"] = ["4" * 32]
second["performance"]["native_utterance_ids"] = []
result = compile_native_shots(m)
assert not result["issues"]
assert result["shots"][1]["utterance_ids"] == []
def test_native_plan_rejects_duplicate_or_missing_lines_and_external_recordings():
from app.domain.native_dialogue import compile_native_shots
m = manifest()
m["spec"]["shots"][0]["performance"]["native_utterance_ids"] = []
assert "native_utterance_unassigned" in {x["code"] for x in compile_native_shots(m)["issues"]}
m = manifest()
m["spec"]["dialogue"]["utterances"][0].update(source="asset", asset_id="b" * 32)
assert "native_recording_unsupported" in {x["code"] for x in compile_native_shots(m)["issues"]}
def test_native_plan_keeps_a_whole_line_and_rejects_text_exceeding_capacity():
from app.domain.native_dialogue import compile_native_shots
m = manifest()
m["spec"]["dialogue"]["utterances"][0]["text"] = "很长的台词" * 100
result = compile_native_shots(m)
assert "native_dialogue_too_long" in {x["code"] for x in result["issues"]}
assert result["shots"][0]["utterance_ids"] == ["4" * 32]
def test_only_h3_voice_or_text_changes_invalidate_native_context_not_captions():
from app.domain.native_dialogue import compile_native_shots
m = manifest()
original = compile_native_shots(m)["shots"][0]["context_hash"]
m["captions"]["mode"] = "manual"
assert compile_native_shots(m)["shots"][0]["context_hash"] == original
m["spec"]["dialogue"]["speakers"][0]["native_voice"] = "低沉的年轻女声"
assert compile_native_shots(m)["shots"][0]["context_hash"] != original
def test_uncertified_two_face_and_reference_recording_are_not_ignored():
from app.domain.native_dialogue import compile_native_shots
m = manifest()
m["spec"]["shots"][0]["performance"]["visible_speaker_ids"] = ["1" * 32, "6" * 32]
assert "native_two_faces_unavailable" in {x["code"] for x in compile_native_shots(m)["issues"]}
m = manifest()
m["spec"]["dialogue"]["speakers"][0]["voice"]["reference_asset_id"] = "b" * 32
assert "native_voice_reference_unsupported" in {
x["code"] for x in compile_native_shots(m)["issues"]
}
def test_unicode_language_budget_does_not_treat_russian_as_silence():
from app.domain.native_dialogue import compile_native_shots
m = manifest()
m["spec"]["dialogue"]["language"] = "Russian"
m["spec"]["dialogue"]["utterances"][0]["text"] = "Привет " * 100
assert "native_dialogue_too_long" in {x["code"] for x in compile_native_shots(m)["issues"]}
def test_fixed_native_frames_reject_insufficient_whole_line_capacity():
from app.domain.native_dialogue import compile_native_shots
m = manifest()
m["spec"]["shots"][0]["performance"]["duration_mode"] = "fixed"
m["spec"]["shots"][0]["edit"].update(start_frame=0, duration_frames=124)
m["spec"]["dialogue"]["utterances"][0]["text"] = "不要截断这一句完整台词。" * 4
codes = {x["code"] for x in compile_native_shots(m)["issues"]}
assert "native_fixed_speech_capacity" in codes
def test_real_word_alignment_uses_the_same_chinese_normalization_as_inspection():
from app.domain.native_av import align_native_utterances
report = dict(
sample_rate=48000,
timestamp_source="whisper-word-timestamps",
timestamp_origin="audio_asset",
words=[dict(text="他住5号楼", start_sample=4800, end_sample=72000)],
)
lines = [dict(id="4" * 32, text="她住五号楼。")]
aligned = align_native_utterances(report, lines)
assert aligned[0]["start_sample"] == 4800
assert aligned[0]["end_sample"] == 72000
assert aligned[0]["words"] == report["words"]
with pytest.raises(ValueError, match="不符|缺少"):
align_native_utterances(report, [dict(id="4" * 32, text="她住六号楼。")])
def test_real_words_split_multi_digit_number_without_changing_global_normalization():
from app.domain.native_av import align_native_utterances
report = dict(
sample_rate=48000,
timestamp_source="whisper-word-timestamps",
timestamp_origin="audio_asset",
words=[
dict(text="1", start_sample=4800, end_sample=12000),
dict(text="2", start_sample=12000, end_sample=24000),
],
)
aligned = align_native_utterances(report, [dict(id="4" * 32, text="12")])
assert aligned[0]["words"] == report["words"]
assert aligned[0]["end_sample"] == 24000
with pytest.raises(ValueError, match="不符|缺少"):
align_native_utterances(report, [dict(id="4" * 32, text="13")])