nex_animation/scripts/e2e_selfcheck.py

435 lines
28 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""端到端业务自检脚本:严格按图2 的 12 个节点跑一遍真实 HTTP。
与 pytest 的区别:pytest 用 TestClient 在进程内测契约,本脚本打的是**真实运行的服务**,
并真的用 ffprobe 校验镜头产物与成片(分辨率/时长/编码/音轨),因此它验证的是
「这台机器上这套服务能不能交付」,适合改完代码或换环境后跑一次。
# 1) 起后端(无需 Redis / Postgres / 供应商密钥)
cd backend && DATABASE_URL='sqlite://////tmp/nex_ui/ui.db' \
MEDIA_ROOT=/tmp/nex_ui/media TASK_EXECUTOR=inline \
../.venv/bin/uvicorn app.main:app --port 8000
# 2) 跑自检(默认打 8000;也可 NEX_BASE=http://127.0.0.1:5173 走前端代理)
NEX_BASE=http://127.0.0.1:8000 ../.venv/bin/python ../scripts/e2e_selfcheck.py
脚本只依赖 HTTP 与本机 ffprobe:产物一律通过 /media 或 /api/exports/{id}/download
下载后本地校验,**不需要**知道后端的 MEDIA_ROOT 在哪。
服务端 `AUTH_ENABLED=true`(部署形态的默认值)时 `/api/*` 与 `/media/*` 一律要会话,
脚本开局会自动登录换 Cookie,否则第一步就是 401。账号取 `NEX_USER` / `NEX_PASS`,
缺省用 `/api/auth/state` 回传的初始管理员用户名 + 部署包默认口令;
本机开发实例(`AUTH_ENABLED=false`)不登录也能跑,逻辑同一条。
脚本会新建一个独立项目,跑完 12 个节点(含「生成失败重试」与「审核驳回重做」两条回路),
任一环节不符合预期就打印接口原文并以退出码 1 结束。依赖:httpx + 本机 ffmpeg/ffprobe。
开跑前的 [00] 步会先把模型配置里的 failure_rate 清零:上一轮若在「注入故障 -> 落败」
中途失败退出,注入会残留在数据库里,把这一轮的每个镜头都打失败,白白浪费时间排查。
"""
import json, os, subprocess, sys, pathlib
import httpx
BASE = os.environ.get("NEX_BASE", "http://127.0.0.1:8000").rstrip("/")
WORK = pathlib.Path(os.environ.get("NEX_WORK", "/tmp/nex_e2e/work")); WORK.mkdir(parents=True, exist_ok=True)
# 全程共用一个 Client:登录后 nex_session Cookie 落在它身上,后续 /api 与 /media 直接带上
CLIENT = httpx.Client(base_url=BASE, timeout=1800, trust_env=False, follow_redirects=True)
def login():
"""服务端开了鉴权就先换会话;没开就什么都不做。
不直接猜 /api/auth/login 是否必需,而是问 /api/auth/state(未登录也返回 200),
顺带拿到初始管理员用户名,避免把账号写死在脚本里。
"""
r = CLIENT.get("/api/auth/state")
if r.status_code != 200:
print(" XX %s/api/auth/state -> %s(服务在跑吗?)" % (BASE, r.status_code)); sys.exit(1)
st = r.json()
if not st.get("auth_enabled"):
print(" .. 服务端未开鉴权(AUTH_ENABLED=false),匿名访问")
return
if st.get("authenticated"):
return
user = os.environ.get("NEX_USER") or (st.get("login_hint") or {}).get("username") or "admin"
pwd = os.environ.get("NEX_PASS") or "admin@123"
r = CLIENT.post("/api/auth/login", json={"username": user, "password": pwd})
if r.status_code != 200:
print(" XX 登录失败 %s -> %s\n%s\n 换个账号试试:NEX_USER=xxx NEX_PASS=xxx %s"
% (user, r.status_code, r.text[:300], " ".join(sys.argv)))
sys.exit(1)
print(" ok 已登录 %s(服务端 AUTH_ENABLED=true;覆盖账号用 NEX_USER/NEX_PASS)" % user)
def probe(path):
"""用 ffprobe 实测媒体文件;失败时把 stderr 一起抛出来,避免只剩一句 KeyError。"""
run = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
"format=duration:stream=codec_type,codec_name,width,height,sample_rate",
"-of", "json", str(path)], capture_output=True, text=True)
if run.returncode != 0:
print(" XX ffprobe 读取 %s 失败:%s" % (path, run.stderr.strip()[:400])); sys.exit(1)
info = json.loads(run.stdout)
if "format" not in info:
print(" XX ffprobe 未读到 %s 的 format 信息:%s" % (path, run.stdout[:400])); sys.exit(1)
return info
S = [0]
def step(n):
S[0] += 1
print("\n[%02d] %s" % (S[0], n))
def check(cond, msg):
if not cond:
print(" XX " + msg); sys.exit(1)
print(" ok " + msg)
def call(method, path, expect=200, **kw):
r = CLIENT.request(method, path, **kw)
if r.status_code != expect:
print(" XX %s %s -> %s\n%s" % (method, path, r.status_code, r.text[:1200])); sys.exit(1)
if "json" in r.headers.get("content-type", ""):
b = r.json()
return b.get("result", b) if isinstance(b, dict) else b
return r
def png(name, color):
p = WORK / name
if not p.exists():
subprocess.run(["ffmpeg", "-y", "-loglevel", "error", "-f", "lavfi", "-i",
"color=c=%s:s=1280x720:d=1" % color, "-frames:v", "1", str(p)], check=True)
return p
def up(method, path, kind, file, extra=None):
d = {"kind": kind}; d.update(extra or {})
return call(method, path, data=d, files={"file": (file.name, file.read_bytes(), "image/png" if file.suffix == ".png" else "audio/wav")})
SCRIPT = """# 分镜脚本 · 雨夜站台
## 镜 1
- 景别: 全景
- 运镜: 推镜
- 时长: 4
- 场景: 雨夜站台
- 人物: 林墨
- 镜头描述: 空荡的站台被霓虹染成蓝色,细雨斜斜落下
- 台词: 这座城市从不等人。
## 镜 2
- 景别: 中景
- 运镜: 移镜
- 时长: 5
- 场景: 雨夜站台
- 人物: 林墨
- 镜头描述: 林墨收起伞走进画面,目光落在电子时刻表上
- 台词: 末班列车,还有两分钟。
## 镜 3
- 景别: 特写
- 运镜: 固定
- 时长: 4
- 场景: 雨夜车厢
- 人物: 苏黎
- 镜头描述: 车窗上的雨滴把对面站台扭成光斑,苏黎的倒影浮在上面
- 台词: 有些人一转身就是一辈子。
## 镜 4
- 景别: 近景
- 运镜: 拉镜
- 时长: 5
- 场景: 雨夜车厢
- 人物: 林墨, 苏黎
- 镜头描述: 两人在车厢两端对望,列车缓缓启动
- 台词: 下次,别再迟到了。
"""
print("=" * 76)
print(" Nex Animation Studio · 图2 业务流程 端到端验证")
print("=" * 76)
print(" 目标服务 %s" % BASE)
login()
# ---- 节点 1 创建视频项目
# 自检前环境复原:上一轮如果在「注入故障 → 落败」中途失败退出,failure_rate=1.0
# 会留在模型配置里,把这一轮的每个镜头都打失败,排查起来极其费劲,因此一律先清零。
print("\n[00] 自检前环境复原(清除上一轮可能遗留的故障注入)")
_left = []
for _p in call("GET", "/api/providers")["providers"]:
_cfg = dict(_p.get("config") or {})
if _cfg.get("failure_rate"):
_cfg["failure_rate"] = 0
call("PATCH", "/api/providers/%s" % _p["id"], json={"config": _cfg})
_left.append("%s/%s" % (_p["provider"], _p["model"]))
print(" ok 已清除遗留故障注入:%s" % "、".join(_left) if _left else " ok 模型配置干净,无遗留故障注入")
step("创建视频项目")
proj = call("POST", "/api/projects", json={
"name": "《末班车》第一集", "description": "雨夜站台与末班列车", "genre": "都市情感",
"target_duration": 20, "aspect_ratio": "16:9", "resolution": "720p", "fps": 24,
"style_guide": "冷色调霓虹,胶片颗粒,浅景深", "global_negative_prompt": "文字水印, 畸变手部",
"default_provider": "mock", "default_transition": "dissolve"})
pid = proj["id"]
check(proj["status"] == "draft", "项目 #%s 创建成功,status=%s stage=%s" % (pid, proj["status"], proj["current_stage"]))
# ---- 模块① 资产库
step("资产库:人物 / 场景 / 声线 + 定妆图·三视图·服装·音色样本")
lin = call("POST", "/api/projects/%s/characters" % pid, json={"name": "林墨", "code": "LINMO", "role": "主角",
"personality": "克制冷静", "appearance": "黑色风衣,短发", "persona_prompt": "30岁男性,冷峻克制",
"voice_profile": "低沉男声"})
su = call("POST", "/api/projects/%s/characters" % pid, json={"name": "苏黎", "code": "SULI", "role": "主角",
"personality": "温柔坚定", "appearance": "米色大衣,长发", "persona_prompt": "28岁女性,温柔坚定"})
sc1 = call("POST", "/api/projects/%s/scenes" % pid, json={"name": "雨夜站台", "description": "霓虹空站台",
"prompt": "wet platform, neon night", "lighting": "霓虹冷光", "time_of_day": "深夜", "palette": "蓝紫"})
sc2 = call("POST", "/api/projects/%s/scenes" % pid, json={"name": "雨夜车厢", "description": "空旷地铁车厢",
"prompt": "empty subway carriage, night", "lighting": "顶灯惨白", "time_of_day": "深夜"})
v1 = call("POST", "/api/projects/%s/voices" % pid, json={"name": "低沉男声", "provider": "mock",
"provider_voice_id": "male-low", "language": "zh-CN", "gender": "male", "is_default": True})
v2 = call("POST", "/api/projects/%s/voices" % pid, json={"name": "温柔女声", "provider": "mock",
"provider_voice_id": "female-soft", "language": "zh-CN", "gender": "female"})
check(True, "人物x2(%s,%s) 场景x2(%s,%s) 声线x2(%s,%s)" % (lin["id"], su["id"], sc1["id"], sc2["id"], v1["id"], v2["id"]))
a1 = up("POST", "/api/projects/%s/characters/%s/assets" % (pid, lin["id"]), "portrait", png("lin.png", "0x2b3a55"), {"note": "风衣定妆"})
check(a1["version"] == 1 and a1["size_bytes"] > 0 and a1["checksum"], "林墨定妆图 v1 已入库(%s bytes, checksum=%s)" % (a1["size_bytes"], a1["checksum"][:10]))
a2 = up("POST", "/api/projects/%s/characters/%s/assets" % (pid, lin["id"]), "portrait", png("lin.png", "0x2b3a55"), {"note": "换装后"})
check(a2["version"] == 2, "同名再传 -> 资产 current_version 递增到 v%s" % a2["version"])
up("POST", "/api/projects/%s/characters/%s/assets" % (pid, lin["id"]), "costume", png("cos.png", "0x101820"))
for cid, f in ((lin["id"], "lin3.png"), (su["id"], "su3.png")):
up("POST", "/api/projects/%s/characters/%s/assets" % (pid, cid), "turnaround", png(f, "0x445566"))
up("POST", "/api/projects/%s/scenes/%s/assets" % (pid, sc1["id"]), "scene_image", png("s1.png", "0x16324f"))
up("POST", "/api/projects/%s/scenes/%s/assets" % (pid, sc2["id"]), "scene_image", png("s2.png", "0x3a2f1f"))
wav = WORK / "sample.wav"
if not wav.exists():
subprocess.run(["ffmpeg", "-y", "-loglevel", "error", "-f", "lavfi", "-i", "sine=frequency=180:duration=2", str(wav)], check=True)
up("POST", "/api/projects/%s/voices/%s/assets" % (pid, v1["id"]), "voice_sample", wav)
ast = call("GET", "/api/projects/%s/assets" % pid)
check(ast["count"] == 7, "资产库共 %s 个素材位" % ast["count"])
_pv = [x for x in ast["assets"] if x["id"] == a2["id"]][0]
check(len(_pv["versions"]) == 2 and _pv["versions"][0]["version_no"] == 2, "定妆图历史版本可回溯:%s" % [v["version_no"] for v in _pv["versions"]])
# ---- 节点 2 导入分镜图片与脚本
step("导入分镜图片与脚本(Markdown + 关键帧按镜号自动匹配)")
imgs = [("shot_%02d.png" % i, png("kf%d.png" % i, "0x%da3b1c" % i).read_bytes(), "image/png") for i in range(1, 5)]
res = call("POST", "/api/projects/%s/import" % pid,
data={"script_text": SCRIPT, "script_name": "storyboard.md", "default_duration": 5.0},
files=[("images", t) for t in imgs])
check(res["created"] == 4, "新增 %s 个镜头,关键帧匹配 %s 张,未匹配 %s" % (res["created"], res["images"] - len(res.get("unmatched_images") or []), res.get("unmatched_images")))
shots = call("GET", "/api/projects/%s/shots" % pid)["shots"]
kf = call("GET", "/api/projects/%s/assets?owner_type=shot&kind=keyframe" % pid)
check(kf["count"] == 4, "%s 张关键帧已挂到对应镜头" % kf["count"])
check(shots[0]["duration"] == 4.0 and shots[0]["dialogue"].startswith("这座城市"), "镜号/时长/台词解析正确")
check(shots[0]["camera_movement"] == "push_in", "运镜中文标签映射为枚举 push_in")
# ---- 节点 3 绑定人物/场景/声音
step("绑定人物 / 场景 / 声音资产")
sm = {"雨夜站台": sc1["id"], "雨夜车厢": sc2["id"]}
cm = {"林墨": lin["id"], "苏黎": su["id"]}
for s in shots:
names = [c["name"] for c in s["characters"]]
snames = [x["name"] for x in s["scenes"]]
b = {"scene_ids": [sm[n] for n in snames if n in sm] or [sc1["id"]], "character_ids": [cm[n] for n in names if n in cm],
"voice_asset_id": v2["id"] if "苏黎" in names else v1["id"],
"speaker_character_id": cm.get(names[0]) if names else None}
call("POST", "/api/projects/%s/shots/%s/bindings" % (pid, s["id"]), json=b)
shots = call("GET", "/api/projects/%s/shots" % pid)["shots"]
check(all(s["characters"] and s["scenes"] and s["voice_asset_id"] for s in shots), "4 个镜头人物/场景/声线绑定完成")
# ---- 提示词组装
step("提示词自动组装(角色设定+场景+风格+负面词)与版本留痕")
for s in shots:
r = call("POST", "/api/projects/%s/shots/%s/prompt" % (pid, s["id"]),
json={"negative_prompt": "字幕, 水印", "reason": "初版提示词", "actor": "e2e", "auto_sync": True})
check(True, "镜 %s:%s 字提示词 / %s 字负面词" % (s["code"], len(r["prompt"]), len(r["negative_prompt"])))
rev = call("GET", "/api/projects/%s/shots/%s/prompt-revisions" % (pid, shots[0]["id"]))
check(len(rev["revisions"]) >= 1, "提示词版本留痕 %s 条" % len(rev["revisions"]))
# ---- 节点 5 检查素材与参数
step("检查素材与参数(预检)")
pre = call("GET", "/api/projects/%s/preflight" % pid)["preflight"]
check(pre["can_generate"] is True, "预检 can_generate=%s 镜头=%s 就绪=%s 阻塞=%s 错误=%s 警告=%s" % (pre["can_generate"], pre["total"], pre["ready"], pre["blocked"], pre["errors"], pre["warnings"]))
# ---- 节点 4 生成分镜任务清单
step("生成分镜任务清单")
plan = call("GET", "/api/projects/%s/tasklist" % pid)["plan"]
check(plan["queued_count"] == 4, "计划视频任务 %s 个 / 配音 %s / 跳过 %s / 阻塞 %s" % (plan["queued_count"], plan["voiceover_count"], plan["skipped_count"], plan["blocked_count"]))
check(True, "预估成本 %s %s" % (plan["estimated_cost"], plan["currency"]))
# ---- 节点 6/7/8 队列调度 -> 调用模型 -> 生成成功?
step("入队 -> 任务队列调度(并发/限流)-> 调用视频模型 -> 生成成功?")
enq = call("POST", "/api/projects/%s/tasks" % pid, json={"priority": 5, "actor": "e2e", "with_voiceover": True})
check(enq["created"]["count"] >= 4, "入队 视频 %s + 配音 %s = %s 个任务(跳过 %s)" % (
len(enq["created"]["created"]), len(enq["created"]["voiceovers"]), enq["created"]["count"], len(enq["created"]["skipped"])))
dr = call("POST", "/api/projects/%s/dispatch" % pid, json={"limit": 64, "drain": True, "timeout": 1200})["drained"]
tk = call("GET", "/api/tasks?project_id=%s" % pid)
check(dr["remaining"] == 0 and dr["timed_out"] is False and tk["counts"].get("failed", 0) == 0,
"队列排空:任务 %s 个 %s / 剩余 %s / 轮次 %s / 派发 %s" % (
tk["count"], tk["counts"], dr["remaining"], dr["rounds"], dr["dispatched"]))
shots = call("GET", "/api/projects/%s/shots" % pid)["shots"]
gen = [s for s in shots if s["output_path"]]
check(len(gen) == 4, "%s/4 镜头生成成功并已下载落盘" % len(gen))
check(all(s["status"] in ("generated", "qc_passed", "approved") for s in shots), "镜头状态机 -> " + ",".join(sorted({s["status"] for s in shots})))
# ---- 节点 8 下载并保存视频
step("下载并保存视频")
u = gen[0]["output_url"]
r = CLIENT.get(u, timeout=120)
check(r.status_code == 200 and len(r.content) > 5000, "媒体直出可访问 %s(%.0f KB)" % (u, len(r.content) / 1024))
clip = WORK / "shot-01.mp4"; clip.write_bytes(r.content)
inf = probe(clip)
vid = [s for s in inf["streams"] if s["codec_type"] == "video"][0]
check(float(inf["format"]["duration"]) > 2 and vid["width"] >= 480,
"ffprobe 实测镜头产物 %sx%s %.2fs,真实可播放" % (
vid["width"], vid["height"], float(inf["format"]["duration"])))
# ---- 节点 9 质检 + 人工审核
step("质量检查与人工审核 -> 镜头合格?")
qcs = call("GET", "/api/projects/%s/qc" % pid)["qc"]
for it in (qcs.get("shots") or [])[:6]:
check(True, " 镜头#%s: %s 得分 %s 通过=%s 问题 %s 条" % (it.get("shot_id"), it.get("status"), it.get("score"), it.get("passed"), len(it.get("issues") or [])))
shots = call("GET", "/api/projects/%s/shots" % pid)["shots"]
check(all(s["qc_score"] is not None and s["qc"] for s in shots), "4 个镜头均有自动质检评分 " + str([s["qc_score"] for s in shots]))
for s in shots:
call("POST", "/api/projects/%s/shots/%s/approve" % (pid, s["id"]), json={"notes": "画面合格", "reviewer": "e2e"})
rvs = call("GET", "/api/projects/%s/reviews?kind=manual" % pid)["reviews"]
check(len([x for x in rvs if x["verdict"] == "pass"]) == 4, "人工审核 4 条 pass")
# ---- 节点 9「否」分支:驳回 -> 调整提示词 -> 重新生成
step("审核回路(不合格分支):驳回 -> 调整提示词 -> 重新入队 -> 重跑")
tgt = shots[3]
rej = call("POST", "/api/projects/%s/shots/%s/reject" % (pid, tgt["id"]), json={
"notes": "人物脸部不一致", "reviewer": "e2e", "prompt": tgt["prompt"] + ",正脸清晰,五官稳定",
"regenerate": True, "priority": 4})
check(rej["shot"]["status"] == "rejected", "镜头 %s 置为 rejected,自动重排队 %s 个任务" % (
tgt["code"], rej["requeue"]["created"]["count"]))
dr2 = call("POST", "/api/projects/%s/dispatch" % pid, json={"limit": 32, "drain": True, "timeout": 900})["drained"]
check(dr2["completed_during_drain"] >= 1 and dr2["remaining"] == 0, "重生成完成 %s 个任务,队列已空" % dr2["completed_during_drain"])
after = call("GET", "/api/projects/%s/shots" % pid)["shots"]
ta = [s for s in after if s["id"] == tgt["id"]][0]
check(ta["output_path"] is not None, "重生成后镜头 %s 产物已更新(status=%s)" % (ta["code"], ta["status"]))
call("POST", "/api/projects/%s/shots/%s/approve" % (pid, tgt["id"]), json={"notes": "重做合格", "reviewer": "e2e"})
# ---- 节点 10 按时间线合成
step("按时间线合成")
tl = call("POST", "/api/projects/%s/timeline" % pid, json={"name": "主时间线", "only_approved": True})
t0 = tl["timeline"]
seg0 = t0["total_duration_ms"] / 1000
check(len(t0["segments"]) == 4, "时间线 v%s:%s 段,总时长 %.2fs" % (t0["version"], len(t0["segments"]), seg0))
st = call("PATCH", "/api/projects/%s/timeline/settings" % pid, json={"settings": {
"fps": 24, "width": 1280, "height": 720, "burn_subtitle": False,
"voice_gain_db": 0.0, "bgm_gain_db": -12.0, "duck_bgm": True, "target_lufs": -16.0}})
check(st["timeline"]["width"] == 1280, "时间线参数:1280x720@24fps")
sg = t0["segments"]
call("PATCH", "/api/projects/%s/timeline/segments/%s" % (pid, sg[1]["id"]), json={"transition_out": "dissolve", "transition_duration_ms": 400})
call("PATCH", "/api/projects/%s/timeline/segments/%s" % (pid, sg[2]["id"]), json={"source_in_ms": 400, "duration_ms": 3000})
t1 = call("GET", "/api/projects/%s/timeline" % pid)["timeline"]
check(abs(t1["total_duration_ms"] / 1000 - seg0) > 0.05, "转场/裁剪生效:%.2fs -> %.2fs" % (seg0, t1["total_duration_ms"] / 1000))
rep = call("GET", "/api/projects/%s/timeline/report" % pid)["report"]
check(rep["ready"] is True, "交付预检 ready=%s issues=%s" % (rep["ready"], len(rep.get("issues") or [])))
srt = CLIENT.get("/api/projects/%s/timeline/subtitles?fmt=srt" % pid, timeout=60)
check(srt.status_code == 200 and "-->" in srt.text, "字幕轨导出 SRT:%s 条 cue" % srt.text.count("-->"))
# ---- 节点 11 配音 / 音乐 / 字幕 / 混音
step("配音 / 音乐 / 字幕与混音")
vo = call("POST", "/api/projects/%s/voiceover" % pid, json={"only_missing": False, "priority": 3})
check(vo["count"] == 4, "登记配音任务 %s 个" % vo["count"])
bgm = WORK / "bgm.wav"
if not bgm.exists():
subprocess.run(["ffmpeg", "-y", "-loglevel", "error", "-f", "lavfi", "-i", "sine=frequency=440:duration=30", str(bgm)], check=True)
ac = call("POST", "/api/projects/%s/timeline/audio" % pid, data={"kind": "bgm", "label": "片尾配乐", "gain_db": -18.0, "loop": True, "ducking": True},
files={"file": ("bgm.wav", bgm.read_bytes(), "audio/wav")})
check("clip_id" in ac, "BGM 音轨挂载 clip#%s" % ac["clip_id"])
call("POST", "/api/projects/%s/timeline/subtitles" % pid, json={"shot_id": shots[0]["id"], "text": "这座城市从不等人。", "speaker": "林墨"})
dr3 = call("POST", "/api/projects/%s/dispatch" % pid, json={"limit": 64, "drain": True, "timeout": 900})["drained"]
check(dr3["remaining"] == 0 and dr3["timed_out"] is False, "配音队列排空:本轮完成 %s / 剩余 %s" % (dr3["completed_during_drain"], dr3["remaining"]))
shots = call("GET", "/api/projects/%s/shots" % pid)["shots"]
check(all(s["voiceover_path"] and s["voiceover_duration_ms"] for s in shots), "4 条配音生成并绑定 " + str([s["voiceover_duration_ms"] for s in shots]))
tl2 = call("POST", "/api/projects/%s/timeline" % pid, json={"name": "配音对齐版", "only_approved": True})["timeline"]
check(tl2["total_duration_ms"] > t1["total_duration_ms"], "重建时间线纳入配音时长对齐:%.2fs -> %.2fs" % (t1["total_duration_ms"] / 1000, tl2["total_duration_ms"] / 1000))
# ---- 节点 12 导出完整视频
step("导出完整视频")
comp = call("POST", "/api/projects/%s/compose" % pid, json={"preset": "draft_720p", "name": "第一集_初剪", "burn_subtitle": False, "timeline_id": tl2["id"]})
ex = comp["exports"]
check(len(ex) == 1 and ex[0]["status"] == "succeeded", "导出 %s:%s / %.2f MB / %.1fs / sha256=%s" % (
ex[0]["id"], ex[0]["preset"], ex[0]["size_bytes"] / 1048576, ex[0]["elapsed_ms"] / 1000, (ex[0].get("checksum") or "")[:12]))
dl = CLIENT.get("/api/exports/%s/download" % ex[0]["id"], timeout=300)
out = WORK / "final.mp4"; out.write_bytes(dl.content)
check(len(dl.content) == ex[0]["size_bytes"], "下载完整成片 %.2f MB" % (len(dl.content) / 1048576))
f2 = probe(out)
check(float(f2["format"]["duration"]) > 12, "成片时长 %.2fs" % float(f2["format"]["duration"]))
vt = [s for s in f2["streams"] if s["codec_type"] == "video"]; at = [s for s in f2["streams"] if s["codec_type"] == "audio"]
check(vt and at, "成片视频 %sx%s %s + 音轨 %s@%sHz" % (vt[0].get("width"), vt[0].get("height"), vt[0].get("codec_name"),
at[0].get("codec_name"), at[0].get("sample_rate")))
fin = call("GET", "/api/projects/%s" % pid)
check(fin["status"] == "delivered", "项目状态机 -> %s(stage=%s)" % (fin["status"], fin["stage_label"]))
# ---- 底层:统一编排 + 项目数据库
step("统一任务编排与项目数据库:队列/生成记录/成本/事件")
gens = call("GET", "/api/projects/%s/generations" % pid)
check(gens["count"] >= 5, "生成记录 %s 条(含 1 次驳回重做)" % gens["count"])
cost = call("GET", "/api/projects/%s/costs" % pid)["cost"]
check(True, "成本台账 " + json.dumps(cost, ensure_ascii=False)[:260])
ev = call("GET", "/api/projects/%s/events" % pid)["events"]
check(len(ev) >= 20, "事件流水 %s 条" % len(ev))
summ = call("GET", "/api/projects/%s/summary" % pid)
check(True, "流程进度 " + json.dumps({k: v for k, v in summ["stats"].items() if k in
("shots", "shots_approved", "tasks", "tasks_active", "generated_duration", "progress_ratio")}, ensure_ascii=False))
wf = summ["workflow"]
check(wf[-1]["state"] == "done", "工作流 %s 个节点全部回写:\n %s" % (
len(wf), " → ".join("%s[%s]" % (n["label"], n["state"]) for n in wf)))
# ---- 节点 8「否」分支:生成失败 -> 记录错误并重试 -> 回到队列 -> 修复后恢复
step("生成失败回路(节点8「否」分支):注入故障 -> 记录错误并重试 -> 落败 -> 修复后一键重跑")
spec = next(r for r in call("GET", "/api/providers")["providers"]
if r["provider"] == "mock" and r["media_type"] == "video")
call("PATCH", "/api/providers/%s" % spec["id"], json={"config": {"failure_rate": 1.0}})
check(True, "给 %s/%s 注入 failure_rate=1.0(模拟供应商必失败)" % (spec["provider"], spec["model"]))
victim = call("GET", "/api/projects/%s/shots" % pid)["shots"][0]
call("POST", "/api/projects/%s/tasks" % pid, json={"shot_ids": [victim["id"]], "force": True,
"allow_unready": True, "with_voiceover": False, "priority": 9, "actor": "e2e"})
d4 = call("POST", "/api/projects/%s/dispatch" % pid, json={"limit": 8, "drain": True, "timeout": 900})["drained"]
check(d4["remaining"] == 0 and d4["timed_out"] is False, "队列排空(含退避等待):轮次 %s / 剩余 %s" % (d4["rounds"], d4["remaining"]))
bad = [t for t in call("GET", "/api/tasks?project_id=%s" % pid)["tasks"]
if t["kind"] == "generate_shot" and t["shot_id"] == victim["id"] and t["status"] == "failed"]
check(len(bad) == 1, "节点8 判定为「否」:镜头 %s 的生成任务已落败" % victim["code"])
bt = bad[0]
check(bt["attempts"] == bt["max_attempts"] and bt["attempts"] >= 2,
"按上限重试:尝试 %s/%s 次,末次错误码 %s" % (bt["attempts"], bt["max_attempts"], bt["error_code"]))
check([h["attempt"] for h in bt["error_history"]] == list(range(1, bt["attempts"] + 1)),
"每次失败都留痕:error_history 尝试序号 %s" % [h["attempt"] for h in bt["error_history"]])
vs = next(s for s in call("GET", "/api/projects/%s/shots" % pid)["shots"] if s["id"] == victim["id"])
check(vs["status"] == "failed" and vs["error_code"] == bt["error_code"],
"镜头状态机 -> failed 并记录 error_code=%s(无脏产物)" % vs["error_code"])
wf_bad = {n["key"]: n for n in call("GET", "/api/projects/%s/summary" % pid)["workflow"]}
check(wf_bad["generate"]["state"] == "failed" and "失败" in wf_bad["generate"]["detail"],
"流程节点「调用视频模型」转红并写明原因:%s" % wf_bad["generate"]["detail"])
acts = {e["action"] for e in call("GET", "/api/projects/%s/events" % pid)["events"]}
check({"task.retry_scheduled", "task.failed"} <= acts, "「记录错误并重试」回路留痕(task.retry_scheduled / task.failed)")
call("PATCH", "/api/providers/%s" % spec["id"], json={"config": {"failure_rate": 0.0}})
requeued = call("POST", "/api/projects/%s/tasks/retry-failed" % pid, json={"reset_attempts": True})
check(requeued["count"] >= 1 and bt["id"] in requeued["requeued"],
"故障修复后一键重跑:重新入队 %s 个失败任务(含 #%s)" % (requeued["count"], bt["id"]))
d5 = call("POST", "/api/projects/%s/dispatch" % pid, json={"limit": 8, "drain": True, "timeout": 900})["drained"]
healed = next(t for t in call("GET", "/api/tasks?project_id=%s" % pid)["tasks"] if t["id"] == bt["id"])
check(healed["status"] == "succeeded" and healed["attempts"] == 1 and len(healed["error_history"]) == bt["attempts"],
"重跑成功:attempts 归零重新计数(1 次成功),历史错误仍保留 %s 条" % len(healed["error_history"]))
vs2 = next(s for s in call("GET", "/api/projects/%s/shots" % pid)["shots"] if s["id"] == victim["id"])
check(vs2["status"] in ("generated", "qc_passed") and vs2["output_exists"] is True,
"镜头 %s 重新生成并落盘 %s" % (vs2["code"], vs2["output_url"]))
call("POST", "/api/projects/%s/shots/%s/approve" % (pid, victim["id"]), json={"notes": "重做后复核合格", "reviewer": "e2e"})
wf_ok = {n["key"]: n for n in call("GET", "/api/projects/%s/summary" % pid)["workflow"]}
check(all(n["state"] == "done" for n in wf_ok.values()) and d5["remaining"] == 0,
"恢复后 12 个节点重新全绿(队列剩余 %s)" % d5["remaining"])
check([r for r in call("GET", "/api/providers")["providers"] if r["id"] == spec["id"]][0]["config"].get("failure_rate", 0) == 0,
"模型故障注入已复原,不污染后续运行")
print("\n" + "=" * 76)
print(" 图2 全部 12 节点端到端验证通过(含两条失败回路) 项目#%s 成片 %s" % (pid, out))
print("=" * 76)