271 lines
14 KiB
Python
271 lines
14 KiB
Python
"""图2 节点 2「导入分镜图片与脚本」+ 模块②「分镜与脚本管理」的接口测试。"""
|
||
from __future__ import annotations
|
||
|
||
import io
|
||
import json
|
||
import zipfile
|
||
|
||
|
||
def _import(api, pid, *, script_text="", data=None, images=(), archive=None):
|
||
form = {"script_text": script_text, "default_duration": 2}
|
||
form.update(data or {})
|
||
files = []
|
||
for name, payload in images:
|
||
files.append(("images", (name, payload, "image/png")))
|
||
if archive is not None:
|
||
files.append(("archive", ("storyboard.zip", archive, "application/zip")))
|
||
return api.post(f"/api/projects/{pid}/import", data=form, files=files or None)
|
||
|
||
|
||
def test_import_markdown_binds_roles_and_keyframes(api, studio, script_text, make_image_png):
|
||
pid = studio["project_id"]
|
||
images = [("s01.png", make_image_png()), ("s02.png", make_image_png(480, 270, "0x123456"))]
|
||
body = _import(api, pid, script_text=script_text, images=images)
|
||
|
||
result = body["result"]
|
||
assert result["dry_run"] is False and result["parsed"] == 2 and result["created"] == 2
|
||
assert result["bound_characters"] == 2 and result["bound_scenes"] == 1
|
||
assert result["images"] == 2 and result["unmatched_images"] == []
|
||
assert body["summary"]["stats"]["shots"] == 2
|
||
assert body["summary"]["current_stage"] != "create_project"
|
||
|
||
shots = api.get(f"/api/projects/{pid}/shots")["shots"]
|
||
assert [s["order_index"] for s in shots] == [1, 2]
|
||
first = shots[0]
|
||
assert first["code"] == "1" and first["duration"] == 2.0
|
||
assert first["shot_size"] == "medium" and first["camera_movement"] == "push_in"
|
||
assert first["dialogue"] == "我们出发吧" and first["subtitle_text"] == "我们出发吧"
|
||
assert first["speaker_name"] == "林澈" and [c["name"] for c in first["characters"]] == ["林澈"]
|
||
# 提示词由风格指南 + 场景 + 角色 + 画面 + 镜头语言 + 台词自动组装
|
||
assert "林澈" in first["prompt"] and "我们出发吧" in first["prompt"]
|
||
assert "冷色调" in first["prompt"] and first["negative_prompt"]
|
||
# 关键帧按镜号自动匹配为镜头资产
|
||
kinds = [a["kind"] for a in first["assets"]]
|
||
assert "keyframe" in kinds and first["assets"][0]["url"].startswith("/media/")
|
||
|
||
# 导入后立刻重跑前置检查:素材齐备的镜头 draft → ready
|
||
assert body["summary"]["stats"]["shot_status"].get("ready") == 2
|
||
|
||
|
||
def test_import_preview_and_dry_run_leave_database_untouched(api, studio, script_text):
|
||
pid = studio["project_id"]
|
||
preview = api.post(f"/api/projects/{pid}/shots/preview",
|
||
json={"script_text": script_text, "default_duration": 2,
|
||
"image_names": ["s01.png", "s02.png", "多余.png"]})
|
||
assert preview["parsed"] == 2
|
||
assert preview["shots"][0]["images"] == ["s01.png"]
|
||
assert preview["unmatched_images"] == ["多余.png"]
|
||
|
||
dry = _import(api, pid, script_text=script_text, data={"dry_run": "true"},
|
||
images=[("s01.png", b"x"), ("s02.png", b"y")])
|
||
assert dry["result"]["dry_run"] is True and "created" not in dry["result"]
|
||
assert api.get(f"/api/projects/{pid}/shots")["count"] == 0
|
||
|
||
# 纯文本走「一行一镜」兜底,不会解析失败
|
||
plain = api.post(f"/api/projects/{pid}/shots/preview",
|
||
json={"script_text": "完全没有镜头的一行普通文字"})
|
||
assert plain["parsed"] == 1 and plain["shots"][0]["description"]
|
||
broken = api.call("POST", f"/api/projects/{pid}/shots/preview", expect=400,
|
||
json={"script_text": '{"shots": [ 坏掉的 JSON'})
|
||
assert "shots" in broken["detail"] or "JSON" in broken["detail"]
|
||
|
||
|
||
def test_import_supports_csv_tsv_json_and_images_only(api, api2=None):
|
||
"""CSV / JSON 与「只有图片素材」都能导入,列名中英文与中文标签都能识别。"""
|
||
pid = api.project(name="格式测试")
|
||
csv_text = ("镜号,画面,景别,运镜,时长,台词,转场\n"
|
||
"01,雨夜巷口的侦探,特写,推镜,3,别动,淡入\n"
|
||
"02,雨水汇入下水道,大特写,static,1.5,,cut\n")
|
||
body = _import(api, pid, script_text=csv_text, data={"default_duration": 4})
|
||
assert body["result"]["created"] == 2
|
||
shots = api.get(f"/api/projects/{pid}/shots")["shots"]
|
||
assert shots[0]["shot_size"] == "close_up" and shots[0]["transition_out"] == "fade_in"
|
||
assert shots[1]["shot_size"] == "extreme_close_up"
|
||
assert shots[1]["duration"] == 1.5 and shots[0]["duration"] == 3.0
|
||
|
||
pid2 = api.project(name="JSON分镜")
|
||
payload = [{"code": "A1", "description": "无人机掠过雪山山脊", "shot_size": "wide", "duration": 3},
|
||
{"code": "A2", "description": "雪崩从山顶倾泻而下", "duration": 4}]
|
||
body2 = _import(api, pid2, script_text=json.dumps(payload, ensure_ascii=False), data={"default_duration": 9})
|
||
assert [s["duration"] for s in body2["result"]["shots"]] == [3, 4]
|
||
assert body2["result"]["shots"][0]["shot_size"] == "wide"
|
||
|
||
# 无脚本、只有图片:一图一镜
|
||
pid3 = api.project(name="纯图片导入")
|
||
body3 = _import(api, pid3, images=[("cat.png", b"1"), ("dog.png", b"2")], data={"default_duration": 2})
|
||
assert body3["result"]["created"] == 2
|
||
codes = [s["code"] for s in api.get(f"/api/projects/{pid3}/shots")["shots"]]
|
||
assert codes == ["S01", "S02"]
|
||
|
||
|
||
def test_import_zip_archive_mixes_script_and_images(api, studio):
|
||
pid = studio["project_id"]
|
||
script = ("# 镜头 7:破晓\n画面: 晨光穿透云层照亮舰桥\n景别: 远景\n运镜: 摇\n时长: 2\n"
|
||
"出镜角色: 林澈\n场景: 星舰舰桥\n关键帧: 07.png\n")
|
||
buf = io.BytesIO()
|
||
with zipfile.ZipFile(buf, "w") as zf:
|
||
zf.writestr("storyboard.md", script)
|
||
zf.writestr("07.png", b"\x89PNG\r\n\x1a\n" + b"0" * 32)
|
||
body = _import(api, pid, archive=buf.getvalue())
|
||
assert body["result"]["created"] == 1
|
||
assert body["result"]["script_name"].endswith("storyboard.md")
|
||
shot = api.get(f"/api/projects/{pid}/shots")["shots"][0]
|
||
assert shot["code"] == "7" and [a["kind"] for a in shot["assets"]] == ["keyframe"]
|
||
|
||
|
||
def test_reimport_respects_keep_existing_and_replace(api, studio, script_text):
|
||
pid = studio["project_id"]
|
||
_import(api, pid, script_text=script_text)
|
||
again = _import(api, pid, script_text=script_text)
|
||
assert again["result"]["created"] == 0
|
||
assert all(row["skipped_reason"] == "镜号已存在" for row in again["result"]["shots"])
|
||
assert api.get(f"/api/projects/{pid}/shots")["count"] == 2
|
||
|
||
replaced = _import(api, pid, script_text=script_text, data={"replace": "true"})
|
||
assert replaced["result"]["created"] == 2
|
||
assert api.get(f"/api/projects/{pid}/shots")["count"] == 2
|
||
|
||
|
||
def test_shot_crud_reorder_and_asset_slots(api, studio, make_image_png):
|
||
pid = studio["project_id"]
|
||
shot = api.post(f"/api/projects/{pid}/shots",
|
||
json={"title": "空镜", "description": "晨雾中的舰桥外骨骼装甲", "duration": 2,
|
||
"mode": "t2v", "shot_size": "wide"})["result"]
|
||
sid = shot["id"]
|
||
assert shot["status"] == "ready" and shot["stage"] == "preflight_passed"
|
||
|
||
patched = api.patch(f"/api/projects/{pid}/shots/{sid}",
|
||
json={"dialogue": "保持航向", "transition_out": "dissolve",
|
||
"transition_duration_ms": 700})["result"]
|
||
assert patched["subtitle_text"] == "保持航向" and patched["transition_out"] == "dissolve"
|
||
|
||
other = api.post(f"/api/projects/{pid}/shots",
|
||
json={"title": "第二镜", "description": "虫洞内部的蓝色漩涡旋转", "duration": 2,
|
||
"mode": "t2v"})["result"]
|
||
order = api.post(f"/api/projects/{pid}/shots/reorder", json={"shot_ids": [other["id"], sid]})
|
||
assert [s["order_index"] for s in order["result"]["shots"]] == [1, 2]
|
||
|
||
for kind in ("keyframe", "last_frame", "reference_image"):
|
||
body = api.post(f"/api/projects/{pid}/shots/{sid}/assets",
|
||
files={"file": (f"{kind}.png", make_image_png(), "image/png")},
|
||
data={"kind": kind, "name": f"{kind} 素材"})
|
||
assert body["result"]["asset"]["url"].startswith("/media/")
|
||
bad = api.call("POST", f"/api/projects/{pid}/shots/{sid}/assets", expect=400,
|
||
files={"file": ("x.png", make_image_png(), "image/png")}, data={"kind": "portrait"})
|
||
assert "portrait" in bad["detail"]
|
||
|
||
assert api.delete(f"/api/projects/{pid}/shots/{other['id']}")["ok"] is True
|
||
assert api.get(f"/api/projects/{pid}/shots")["count"] == 1
|
||
|
||
|
||
def test_bindings_and_prompt_override_keep_history(api, studio, script_text):
|
||
pid, cid, vid = studio["project_id"], studio["character_id"], studio["voice_id"]
|
||
_import(api, pid, script_text=script_text)
|
||
scene = api.get(f"/api/projects/{pid}")["result"]["scenes"][0]["id"]
|
||
shots = api.get(f"/api/projects/{pid}/shots")["shots"]
|
||
sid = shots[1]["id"]
|
||
|
||
bound = api.post(f"/api/projects/{pid}/shots/{sid}/bindings",
|
||
json={"character_ids": [cid], "scene_ids": [scene],
|
||
"speaker_character_id": cid, "voice_asset_id": vid})["result"]
|
||
bound = bound["shot"]
|
||
assert bound and [c["id"] for c in bound["characters"]] == [cid]
|
||
assert [s["id"] for s in bound["scenes"]] == [scene]
|
||
assert bound["voice_asset_id"] == vid and bound["speaker_character_id"] == cid
|
||
assert bound["voice_name"] == "低沉男声"
|
||
|
||
edited = api.post(f"/api/projects/{pid}/shots/{sid}/prompt",
|
||
json={"prompt": "赛博朋克霓虹风格的虫洞,粒子飞溅",
|
||
"negative_prompt": "低饱和", "reason": "导演要求更强对比"})["result"]
|
||
assert "赛博朋克" in edited["prompt"] and edited["negative_prompt"] == "低饱和"
|
||
assert edited["overrides"]["prompt_override"] == "赛博朋克霓虹风格的虫洞,粒子飞溅"
|
||
|
||
revisions = api.get(f"/api/projects/{pid}/shots/{sid}/prompt-revisions")["revisions"]
|
||
assert revisions[0]["reason"] == "导演要求更强对比"
|
||
|
||
synced = api.post(f"/api/projects/{pid}/shots/{sid}/prompt",
|
||
json={"auto_sync": True, "reason": "恢复自动组装"})["result"]
|
||
assert "赛博朋克" not in synced["prompt"] and "虫洞" in synced["prompt"]
|
||
assert synced["overrides"]["prompt_override"] == ""
|
||
|
||
|
||
def test_preflight_surfaces_blockers_and_unblocks_after_fix(api, studio):
|
||
pid = studio["project_id"]
|
||
sid = api.post(f"/api/projects/{pid}/shots",
|
||
json={"title": "缺素材", "description": "黑暗中的呼吸声", "mode": "i2v",
|
||
"duration": 2})["result"]["id"]
|
||
report = api.get(f"/api/projects/{pid}/preflight")["preflight"]
|
||
check = next(c for c in report["checks"] if c["shot_id"] == sid)
|
||
codes = {i["code"] for i in check["issues"]}
|
||
assert "missing_keyframe" in codes
|
||
assert report["blocked"] >= 1 and report["can_generate"] is False
|
||
|
||
plan = api.get(f"/api/projects/{pid}/tasklist")["plan"]
|
||
blocked = next(s for s in plan["shots"] if s["shot_id"] == sid)
|
||
assert blocked["action"] != "enqueue" and blocked["blocking"]
|
||
assert plan["blocked_count"] >= 1
|
||
|
||
api.post(f"/api/projects/{pid}/shots/{sid}/assets",
|
||
files={"file": ("k.png", b"\x89PNG\r\n\x1a\n" + b"1" * 32, "image/png")},
|
||
data={"kind": "keyframe"})
|
||
fixed = api.get(f"/api/projects/{pid}/preflight")
|
||
after = next(c for c in fixed["preflight"]["checks"] if c["shot_id"] == sid)
|
||
assert after["ok"] is True and fixed["preflight"]["can_generate"] is True
|
||
assert fixed["summary"]["stats"]["shot_status"].get("ready") == 1
|
||
|
||
|
||
def test_import_binds_the_speaker_named_by_the_script(api, studio):
|
||
"""三人同框时说话人按脚本绑定,不再拿「出镜角色的第一个」顶替。
|
||
|
||
这一条钉住的是真实翻车:斗地主的分镜里三人轮流开口,
|
||
旧实现把每一镜的说话人都绑成第一个出镜角色,于是小燕子的台词
|
||
用郭靖的嗓子配音,观众一听就知道换了个人。
|
||
"""
|
||
pid = studio["project_id"]
|
||
for name in ("小燕子", "阿拉蕾"):
|
||
api.post(f"/api/projects/{pid}/characters",
|
||
json={"name": name, "role": "客串", "appearance": "古装"})
|
||
script = """# 镜头 1:抢地主
|
||
画面: 三人围桌,小燕子一拍桌子站起
|
||
时长: 2
|
||
台词: 那我抢啦
|
||
说话人: 小燕子(拍桌)
|
||
出镜角色: 林澈、小燕子、阿拉蕾
|
||
|
||
# 镜头 2:举桌
|
||
画面: 阿拉蕾把整张牌桌举过头顶
|
||
时长: 2
|
||
台词: 阿拉蕾举着打嘛
|
||
说话人: 阿拉蕾
|
||
出镜角色: 林澈、小燕子、阿拉蕾
|
||
|
||
# 镜头 3:没写说话人
|
||
画面: 林澈皱眉看着手里的两张牌
|
||
时长: 2
|
||
台词: 这对三是最后一对
|
||
出镜角色: 林澈、小燕子
|
||
"""
|
||
result = _import(api, pid, script_text=script)["result"]
|
||
assert result["created"] == 3 and result["speakers_bound"] == 2
|
||
|
||
shots = api.get(f"/api/projects/{pid}/shots")["shots"]
|
||
assert [s["speaker_name"] for s in shots] == ["小燕子", "阿拉蕾", "林澈"]
|
||
# 只写进「说话人」、没列进出镜角色的人也要挂上镜头:配音和身份参考图都要用到他
|
||
assert "小燕子" in [c["name"] for c in shots[0]["characters"]]
|
||
|
||
|
||
def test_import_leaves_speaker_unbound_when_the_script_names_a_stranger(api, studio):
|
||
"""脚本里的说话人在项目里不存在时留空,而不是随手安给同框的某个人。"""
|
||
pid = studio["project_id"]
|
||
script = """# 镜头 1
|
||
画面: 林澈听着门外传来的声音
|
||
时长: 2
|
||
台词: 谁在外面
|
||
说话人: 神秘人
|
||
出镜角色: 林澈
|
||
"""
|
||
_import(api, pid, script_text=script)
|
||
shot = api.get(f"/api/projects/{pid}/shots")["shots"][0]
|
||
assert shot["speaker_name"] in (None, "")
|
||
assert [c["name"] for c in shot["characters"]] == ["林澈"]
|