"""状态规划只能补作者留空的槽;模型不能重写镜头身份与已有意图。""" from copy import deepcopy import json from types import SimpleNamespace from unittest.mock import AsyncMock import pytest from app.adapters.provider.story_planning import plan_story, validate from app.domain.errors import MediaError A, B, SCENE = "a" * 32, "b" * 32, "c" * 32 def story(): return { "schema_version": 4, "scenes": [{"id": SCENE, "name": "海边"}], "shots": [ { "id": A, "scene_id": SCENE, "visual_prompt": "旅人站在沙滩上", "motion_prompt": "旅人朝海边走两步", "continuity": {"group_id": "g", "state_in": {"summary": "作者锁定的开场"}}, }, { "id": B, "scene_id": SCENE, "visual_prompt": "旅人走向海边", "motion_prompt": "继续向前走", "continuity": {"transition": "continue", "group_id": "g"}, }, ], } def response(): return { "shots": [ { "shot_id": A, "state_in": {"summary": "模型试图改开场", "location": "海边"}, "state_out": { "summary": "旅人走完两步,朝向大海", "location": "海边", "motion": "向海边前行", }, "end_prompt": "旅人面朝大海,身体前倾,右脚落地", }, { "shot_id": B, "state_in": { "summary": "旅人走完两步,朝向大海", "location": "海边", "motion": "向海边前行", }, "state_out": {"summary": "旅人接近海浪边缘", "location": "海边"}, "end_prompt": "旅人接近海浪边缘,面朝大海", }, ] } @pytest.fixture def provider(): def json_file(work, name, value): path = work / name path.write_text(json.dumps(value, ensure_ascii=False), encoding="utf-8") return path return SimpleNamespace( _chat=AsyncMock(return_value=json.dumps(response(), ensure_ascii=False)), _json_file=json_file, settings=SimpleNamespace(qwen_model="vision"), ) async def test_planner_fills_empty_state_with_global_context_preserving_authored_intent( provider, tmp_path ): raw = story() before = deepcopy(raw) result = await plan_story(provider, {"spec": raw}, {}, tmp_path) assert raw == before planned = result.result["spec"] assert [s["id"] for s in planned["shots"]] == [A, B] assert planned["shots"][0]["continuity"]["state_in"]["summary"] == "作者锁定的开场" assert planned["shots"][0]["motion_prompt"] == raw["shots"][0]["motion_prompt"] assert ( planned["shots"][1]["continuity"]["state_in"] == planned["shots"][0]["continuity"]["state_out"] ) assert "海边" in provider._chat.call_args.args[0][-1]["content"] @pytest.mark.parametrize("kind", ["missing", "duplicate", "empty_state"]) async def test_invalid_planning_result_fails_without_mutating_input(provider, tmp_path, kind): value = response() if kind == "missing": value["shots"].pop() elif kind == "duplicate": value["shots"][1]["shot_id"] = A else: value["shots"][1]["state_out"] = {} provider._chat.return_value = json.dumps(value) with pytest.raises(MediaError): await plan_story(provider, {"spec": story()}, {}, tmp_path) def test_planning_contract_rejects_unsupported_fields_and_old_specs(): with pytest.raises(MediaError): validate({"spec": story(), "path": "C:/secret"}, {}) with pytest.raises(MediaError): validate({"spec": {**story(), "schema_version": 3}}, {})