cmvr-visual/backend/tests/test_native_dialogue_example_script.py

95 lines
3.4 KiB
Python

"""演示脚本必须构造有效的原生单镜意图,并在超时后重放冻结命令。"""
from copy import deepcopy
import importlib.util
from pathlib import Path
from types import SimpleNamespace
import sys
import pytest
ROOT = Path(__file__).resolve().parents[2]
def script():
sys.path.insert(0, str(ROOT / "scripts"))
try:
spec = importlib.util.spec_from_file_location(
"native_example", ROOT / "scripts/create_native_dialogue_example.py"
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
finally:
sys.path.pop(0)
def test_example_author_intent_is_valid_and_has_one_visible_speaking_person():
from app.domain.dialogue import upgrade_dialogue_manifest
from app.domain.editors import manifest_from_draft, validate_manifest
from app.domain.stories import StorySpecV3
from app.domain.native_dialogue import compile_native_shots
module = script()
initial = StorySpecV3.model_validate(module.initial_story()).model_dump(mode="json")
m = upgrade_dialogue_manifest(
manifest_from_draft(
SimpleNamespace(
id="a" * 32,
kind="story_v3",
spec=initial,
editor_manifest=None,
)
)
)
native = validate_manifest(module.native_manifest({"manifest": m}))
result = compile_native_shots(native)
assert not result["issues"]
assert len(native["spec"]["shots"]) == 1
assert result["shots"][0]["parameters"]["num_frames"] == 158
assert result["shots"][0]["parameters"]["profile"] == module.PROFILE
assert module.TEXT in result["shots"][0]["prompt"]
assert native["spec"]["shots"][0]["performance"]["visible_speaker_ids"] == [
module.stable_id("speaker")
]
def test_saved_command_is_replayed_without_changing_payload_after_timeout(tmp_path, monkeypatch):
module = script()
monkeypatch.setattr(module, "STATE", tmp_path / "state.json")
state, calls = {}, []
class Client:
def post(self, path, **kwargs):
calls.append((path, deepcopy(kwargs)))
if len(calls) == 1:
raise TimeoutError("unknown outcome")
return SimpleNamespace(is_success=True, json=lambda: {"run": {"id": "a" * 32}})
with pytest.raises(TimeoutError):
module.post_once(Client(), state, "frame", "/editors/story/runs", lambda: {"seed": 261127})
module.post_once(Client(), state, "frame", "/editors/story/runs", lambda: {"seed": 99})
assert calls[0] == calls[1]
assert calls[1][1]["json"]["seed"] == 261127
assert module.STATE.exists()
def test_unknown_story_creation_does_not_post_a_second_story(tmp_path, monkeypatch):
module = script()
monkeypatch.setattr(module, "STATE", tmp_path / "state.json")
monkeypatch.setattr(module, "find_story", lambda client: None)
state = {"story_create": "pending"}
with pytest.raises(RuntimeError, match="暂停重复创建"):
module.prepare(SimpleNamespace(), state)
def test_demo_writer_lock_prevents_concurrent_creation(tmp_path, monkeypatch):
module = script()
monkeypatch.setattr(module, "OUTPUT", tmp_path)
with module.production_lock():
with pytest.raises(RuntimeError, match="其他进程"):
with module.production_lock():
pytest.fail("second writer obtained the business state lock")
with module.production_lock():
pass