1027 lines
52 KiB
Python
1027 lines
52 KiB
Python
"""ComfyUI 适配器(自建大模型,如 MiniMax H3)契约测试。
|
||
|
||
这些用例**不需要真的连 GPU**:只验证「平台请求 → ComfyUI 节点图」的翻译层,
|
||
以及 ComfyUI 响应 → 平台错误语义的分类。真机链路交给 scripts/comfyui_e2e.py。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import threading
|
||
import time
|
||
from contextlib import contextmanager, nullcontext
|
||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||
from pathlib import Path
|
||
from urllib.parse import parse_qs, unquote, urlparse
|
||
|
||
import pytest
|
||
|
||
from app.constants import RESOLUTIONS
|
||
from app.providers.base import (AssetGenRequest, FatalProviderError, RateLimitedError,
|
||
RetryableProviderError, VideoGenRequest, VoiceGenRequest)
|
||
from app.providers.comfy_assets import (
|
||
ComfyImageProvider, ComfyTTSProvider, _fit_generated_canvas,
|
||
_fit_reference_canvas, _image_seed,
|
||
)
|
||
from app.providers.comfyui import (DEFAULT_MODELS, WORKFLOW_TEMPLATES, ComfyVideoProvider,
|
||
WorkflowBuilder, _failure_message, _file_choices, _first_file,
|
||
_raise_for, reference_inputs)
|
||
from app.providers.registry import comfyui_config
|
||
from tests.test_delivery import _make_wav
|
||
|
||
|
||
def builder(**overrides) -> WorkflowBuilder:
|
||
config = comfyui_config(**overrides)
|
||
return WorkflowBuilder(config, WORKFLOW_TEMPLATES[config["workflow"]])
|
||
|
||
|
||
# ---------------------------------------------------------------- 画布与时长
|
||
def test_canvas_snaps_to_multiple_and_respects_aspect_ratio():
|
||
b = builder()
|
||
for ratio in ("16:9", "9:16", "1:1", "4:3", "3:4", "21:9"):
|
||
width, height = b.canvas(ratio)
|
||
assert width % 32 == 0 and height % 32 == 0, (ratio, width, height)
|
||
assert max(width, height) <= 1344, (ratio, width, height)
|
||
wide_w, wide_h = b.canvas("16:9")
|
||
tall_w, tall_h = b.canvas("9:16")
|
||
assert wide_w > wide_h and tall_h > tall_w
|
||
assert (wide_w, wide_h) == (tall_h, tall_w) # 竖屏是横屏的转置
|
||
|
||
|
||
def test_h3_declares_1080p_as_upscaled_delivery():
|
||
"""H3 的 1080p 是「放大交付」,不是「原生出片」——能力字段必须把这件事说清楚。
|
||
|
||
官方(ComfyCloud 节点的 resolution 只有 480p/720p、Turbo LoRA 文件名带 768p)
|
||
只到 768p;本地节点虽不钳像素,但 2MP 画布实测卡时 ×2.3 且官方未训这一档。
|
||
所以交付 1080p 走「原生像素出片 + 合成端 lanczos 放大」,不额外花卡时。
|
||
"""
|
||
caps = ComfyVideoProvider().capabilities({"workflow": "minimax_h3"})
|
||
assert "1080p" not in caps.resolutions
|
||
assert caps.upscale_to == ("1080p",)
|
||
assert caps.delivery_resolutions == ("480p", "720p", "1080p")
|
||
assert caps.as_dict()["upscale_to"] == ["1080p"]
|
||
# 模型配置写空列表 = 明确关闭放大交付(例如换成了原生就能出 1080p 的机型)
|
||
off = ComfyVideoProvider().capabilities(
|
||
{"workflow": "minimax_h3", "capabilities": {"upscale_to": []}})
|
||
assert off.upscale_to == () and off.delivery_resolutions == ("480p", "720p")
|
||
# 未知 workflow 按既有口径回落到 minimax_h3 模板,因此也带放大交付档;
|
||
# 要关闭就在模型配置里显式写空列表,别让适配器替机型猜。
|
||
fallback = ComfyVideoProvider().capabilities({"workflow": "not_a_workflow"})
|
||
assert fallback.upscale_to == ("1080p",)
|
||
# 放大档不能把原生档位顺序打乱,也不能凭空造出没声明过的 2k/4k
|
||
wide = ComfyVideoProvider().capabilities(
|
||
{"workflow": "minimax_h3", "capabilities": {"resolutions": ["720p", "480p"]}})
|
||
assert wide.delivery_resolutions == ("480p", "720p", "1080p")
|
||
|
||
|
||
def test_default_policy_honours_the_configured_canvas_verbatim():
|
||
"""默认策略(config):配置写多少就出多少,代码既不抬档也不压档。
|
||
|
||
分辨率是「机型 + 任务」参数,控制台和别的进程都会直接改 model_specs;
|
||
适配器里写死任何修正(不管是 max 还是 min)都是在静默覆盖别人的决定,
|
||
而且从接口/质检上看不出来。档位与配置不一致要提示,不能代替它做主。
|
||
"""
|
||
low = WorkflowBuilder({"megapixels": 0.4, "multiple": 32, "max_edge": 1344, "fps": 24},
|
||
WORKFLOW_TEMPLATES["minimax_h3"])
|
||
assert low.canvas("16:9", "720p") == (832, 480) # 不被顶回 720p
|
||
assert low.canvas("9:16", "720p") == (480, 832)
|
||
high = WorkflowBuilder({"megapixels": 2.5, "multiple": 32, "max_edge": 1344, "fps": 24},
|
||
WORKFLOW_TEMPLATES["minimax_h3"])
|
||
assert high.canvas("16:9", "480p") == (1344, 768) # 也不被压到 480p(长边封顶而已)
|
||
|
||
|
||
def test_tier_policy_lifts_canvas_to_the_declared_resolution():
|
||
"""显式 resolution_policy=tier 时才抬档:720p 项目不会悄悄出 480p 糊片。
|
||
|
||
这是「分镜 1 完全是糊的」的根因回归:megapixels 被当提速开关调到 0.4 时,
|
||
画布从 1312x736 掉到 832x480,而档位声明、接口、质检全都看不出来。
|
||
修法不是在代码里无条件抬档,而是让「要不要以档位为下限」变成显式配置。
|
||
"""
|
||
small = WorkflowBuilder({"megapixels": 0.4, "multiple": 32, "max_edge": 1344, "fps": 24,
|
||
"resolution_policy": "tier"},
|
||
WORKFLOW_TEMPLATES["minimax_h3"])
|
||
assert small.canvas("16:9") == (832, 480) # 只看配置:确实只够 480p
|
||
assert small.canvas("16:9", "720p") == (1312, 736) # 声明 720p → 真 720p
|
||
assert small.canvas("9:16", "720p") == (736, 1312)
|
||
assert small.canvas("16:9", "480p") == (864, 480) # 预算高于档位时不无脑放大
|
||
for ar in ("16:9", "9:16", "1:1", "4:3", "3:4"):
|
||
for res in ("360p", "480p", "720p"):
|
||
w, h = small.canvas(ar, res)
|
||
assert w % 32 == 0 and h % 32 == 0, (ar, res, w, h)
|
||
assert min(w, h) >= RESOLUTIONS[res][1] - 1, (ar, res, w, h)
|
||
|
||
|
||
def test_budget_policy_trades_canvas_for_speed():
|
||
"""resolution_policy=budget:像素预算变成上限,档位只当面积天花板。
|
||
|
||
这是 Spark 这类小机的「草案档」:720p 项目按 832x480 生成(≈92s),
|
||
放大到交付尺寸由合成端负责;同时 480p 项目不会被 0.98 的预算无谓拖慢。
|
||
"""
|
||
small = WorkflowBuilder({"megapixels": 0.4, "multiple": 32, "max_edge": 1344, "fps": 24,
|
||
"resolution_policy": "budget"}, WORKFLOW_TEMPLATES["minimax_h3"])
|
||
assert small.canvas("16:9", "720p") == (832, 480) # 预算封顶,不抬档
|
||
assert small.canvas("9:16", "720p") == (480, 832)
|
||
big = WorkflowBuilder({"megapixels": 0.98, "multiple": 32, "max_edge": 1344, "fps": 24,
|
||
"resolution_policy": "budget"}, WORKFLOW_TEMPLATES["minimax_h3"])
|
||
assert big.canvas("16:9", "480p") == (864, 480) # 交付只有 480p,别按 720p 烧卡
|
||
assert big.canvas("16:9", "720p") == (1312, 736)
|
||
|
||
|
||
def test_task_lora_follows_the_conditioning_node():
|
||
"""H3 只有一个基座,fl2v(首帧)与 ref2v(参考图)靠任务 LoRA 区分:LoRA 必须跟着节点走。"""
|
||
cfg = comfyui_config(lora_i2v="minimax_h3_fl2v_turbo_4step_v1.0_768p_comfyui_bf16.safetensors",
|
||
lora_ref2v="minimax_h3_ref2v_turbo_4step_v0.1_comfyui_bf16.safetensors",
|
||
unet_loader="UNETLoader")
|
||
b = WorkflowBuilder(cfg, WORKFLOW_TEMPLATES["minimax_h3"])
|
||
g_key, _ = b.build(prompt="p", negative="", seed=1, width=1344, height=768, length=124,
|
||
first_frame="nex/key.png")
|
||
assert g_key["10"]["class_type"] == "MiniMaxH3ImageToVideo"
|
||
assert g_key["90"]["inputs"]["lora_name"] == cfg["lora_i2v"]
|
||
|
||
g_ref, _ = b.build(prompt="p", negative="", seed=1, width=1344, height=768, length=124,
|
||
refs=["nex/ref.png"])
|
||
assert g_ref["10"]["class_type"] == "MiniMaxH3ReferenceToVideo"
|
||
assert g_ref["90"]["inputs"]["lora_name"] == cfg["lora_ref2v"]
|
||
assert g_ref["10"]["inputs"]["ref_image_size"] == "match" # max 会让参考图拖慢数倍
|
||
|
||
# 只配了通用 lora(老配置)时两条路径都回落到它,行为不变
|
||
legacy = WorkflowBuilder({**cfg, "lora_i2v": "", "lora_ref2v": "", "lora": "old.safetensors"},
|
||
WORKFLOW_TEMPLATES["minimax_h3"])
|
||
for kwargs in ({"first_frame": "nex/key.png"}, {"refs": ["nex/ref.png"]}):
|
||
g, _ = legacy.build(prompt="p", negative="", seed=1, width=1344, height=768,
|
||
length=124, **kwargs)
|
||
assert g["90"]["inputs"]["lora_name"] == "old.safetensors"
|
||
|
||
|
||
def test_canvas_over_model_limit_shrinks_proportionally():
|
||
"""超过模型上限要等比收缩,不能宽高各减一档把 16:9 拉成扁画面。"""
|
||
big = WorkflowBuilder({"megapixels": 2.5, "multiple": 32, "max_edge": 1344, "fps": 24},
|
||
WORKFLOW_TEMPLATES["minimax_h3"])
|
||
assert big.canvas("16:9", "1080p") == (1344, 768) # H3 官方上限画布
|
||
assert big.canvas("16:9") == (1344, 768)
|
||
|
||
|
||
def test_generation_sends_project_resolution_to_the_canvas(monkeypatch):
|
||
"""生成阶段必须把项目档位透传给画布换算(否则 tier 策略形同虚设)。"""
|
||
seen: dict[str, object] = {}
|
||
original = WorkflowBuilder.canvas
|
||
|
||
def spy(self, aspect_ratio, resolution=None):
|
||
seen["args"] = (aspect_ratio, resolution)
|
||
return original(self, aspect_ratio, resolution)
|
||
|
||
def boom(self, **kwargs): # 拿到画布即可,不必真跑图
|
||
seen["graph_kwargs"] = kwargs
|
||
raise FatalProviderError("stop-after-canvas")
|
||
|
||
monkeypatch.setattr(WorkflowBuilder, "canvas", spy)
|
||
monkeypatch.setattr(WorkflowBuilder, "build", boom)
|
||
# 本用例只验证画布换算,不测探活:地址是死端口,开工前闸门会先拦下来。
|
||
monkeypatch.setattr("app.providers.comfyui.require_reachable", lambda *_a, **_kw: None)
|
||
with pytest.raises(FatalProviderError, match="stop-after-canvas"):
|
||
ComfyVideoProvider().generate(
|
||
VideoGenRequest(prompt="雾里的渡船靠岸", duration_seconds=5, mode="t2v",
|
||
aspect_ratio="16:9", resolution="720p"),
|
||
{"base_url": "http://127.0.0.1:9199", "workflow": "minimax_h3",
|
||
"megapixels": 0.4, "resolution_policy": "tier"})
|
||
assert seen["args"] == ("16:9", "720p")
|
||
graph = seen["graph_kwargs"]
|
||
assert (graph["width"], graph["height"]) == (1312, 736)
|
||
|
||
|
||
def test_frame_length_snaps_to_model_grid():
|
||
"""H3 要求帧数落在 17k+5 网格上,否则采样直接报错。"""
|
||
b = builder()
|
||
for seconds in (4, 4.8, 5, 5.2, 8, 10, 15):
|
||
frames = b.length(seconds)
|
||
assert frames >= 5 and frames % 17 == 5, (seconds, frames)
|
||
assert b.length(5) == 124 # 官方模板默认值:5s → 124 帧
|
||
|
||
|
||
# ---------------------------------------------------------------- 节点图
|
||
def test_i2v_graph_uses_first_frame_and_native_audio_chain():
|
||
g, save = builder().build(prompt="雨夜站台", negative="", seed=7, width=1344, height=768,
|
||
length=124, first_frame="nex/kf.png")
|
||
node = g["10"]["inputs"]
|
||
assert node["first_frame"] == ["50", 0] and g["50"]["inputs"]["image"] == "nex/kf.png"
|
||
assert node["clip"] == ["2", 0] and node["vae"] == ["3", 0]
|
||
assert node["prompt"] == "雨夜站台"
|
||
# 原生音画同步:VAEDecode + VAEDecodeAudio → CreateVideo → SaveVideo
|
||
assert g["40"]["inputs"] == {"images": ["30", 0], "audio": ["31", 0], "fps": 24.0}
|
||
assert g["41"]["inputs"]["video"] == ["40", 0] and save == "41"
|
||
assert all("class_type" in n and "inputs" in n for n in g.values())
|
||
|
||
|
||
def test_reference_images_switch_to_r2v_node():
|
||
"""没有关键帧时改用参考图节点(定妆图/场景图当一致性锚点)。"""
|
||
g, _ = builder().build(prompt="橘猫", negative="", seed=1, width=1344, height=768, length=124,
|
||
refs=["nex/cat.png", "nex/scene.png"])
|
||
node = g["10"]
|
||
assert node["class_type"] == "MiniMaxH3ReferenceToVideo"
|
||
# ComfyUI 3.x 的 autogrow 槽位使用父输入前缀的扁平键。
|
||
assert node["inputs"]["ref_images.ref_image_0"] == ["50", 0]
|
||
assert node["inputs"]["ref_images.ref_image_1"] == ["51", 0]
|
||
assert "ref_images" not in node["inputs"]
|
||
assert "ref_image_0" not in node["inputs"] and "ref_image_1" not in node["inputs"]
|
||
assert node["inputs"]["audio_vae"] == ["4", 0]
|
||
assert "first_frame" not in node["inputs"]
|
||
assert g["51"]["inputs"]["image"] == "nex/scene.png"
|
||
|
||
|
||
def test_two_voice_references_own_two_slots_in_order():
|
||
"""两个说话人 = 两条 ``ref_audio``,槽位号必须跟 ``<Audio n>`` 的序号一一对应。
|
||
|
||
caption 里的 ``<Audio 2><d>…</d>`` 是「用第 2 条参考音色说这句」,序号由节点输入
|
||
顺序决定(``ref_audios.ref_audio_{0,1}``)。适配器要是只挂一条、或者换了顺序,
|
||
标签就把第二个人的台词安到第一个人的嗓子上,画面听感直接是「配音接错人」。
|
||
"""
|
||
g, _ = builder().build(prompt="橘猫", negative="", seed=1, width=1344, height=768, length=124,
|
||
refs=["nex/portrait.png"], audios=["nex/v-male.wav", "nex/v-female.wav"])
|
||
node = g["10"]["inputs"]
|
||
assert node["ref_audios.ref_audio_0"] == ["60", 0]
|
||
assert node["ref_audios.ref_audio_1"] == ["61", 0]
|
||
assert g["60"]["inputs"] == {"audio": "nex/v-male.wav"}
|
||
assert g["61"]["inputs"] == {"audio": "nex/v-female.wav"}
|
||
|
||
|
||
def test_partial_voice_upload_drops_every_audio_slot(monkeypatch):
|
||
"""一条音色传上去、一条失败时必须整套作废:槽位号会整体前移,标签就指错人。"""
|
||
seen: dict[str, object] = {}
|
||
|
||
def boom(self, **kwargs):
|
||
seen["audios"] = kwargs.get("audios")
|
||
raise FatalProviderError("stop-after-build")
|
||
|
||
def fake_upload(self, client, base, src, subfolder, title): # noqa: ARG001
|
||
if "bad" in str(src):
|
||
raise RuntimeError("连接被重置")
|
||
return f"up/{Path(str(src)).name}"
|
||
|
||
monkeypatch.setattr(WorkflowBuilder, "build", boom)
|
||
monkeypatch.setattr(ComfyVideoProvider, "_upload", fake_upload)
|
||
monkeypatch.setattr("app.providers.comfyui.require_reachable", lambda *_a, **_kw: None)
|
||
with pytest.raises(FatalProviderError, match="stop-after-build"):
|
||
ComfyVideoProvider().generate(
|
||
VideoGenRequest(prompt="两个人在工位前对话", duration_seconds=5, mode="i2v",
|
||
reference_images=["/media/portrait.png"],
|
||
reference_audios=["/media/v-male.wav", "/media/v-bad.wav"]),
|
||
{"base_url": "http://127.0.0.1:9199", "workflow": "minimax_h3", "megapixels": 0.4})
|
||
assert seen["audios"] == [], "半套音色会错位,宁可不喂"
|
||
|
||
# 单条音色的老行为不变:传不上去就是没有参考音频
|
||
monkeypatch.setattr(ComfyVideoProvider, "_upload",
|
||
lambda self, c, b, s, sub, t: (_ for _ in ()).throw(RuntimeError("x"))
|
||
if "bad" in str(s) else f"up/{Path(str(s)).name}")
|
||
with pytest.raises(FatalProviderError, match="stop-after-build"):
|
||
ComfyVideoProvider().generate(
|
||
VideoGenRequest(prompt="俊杰在工位前说话", duration_seconds=5, mode="i2v",
|
||
reference_images=["/media/portrait.png"],
|
||
reference_audios=["/media/v-male.wav"]),
|
||
{"base_url": "http://127.0.0.1:9199", "workflow": "minimax_h3", "megapixels": 0.4})
|
||
assert seen["audios"] == ["up/v-male.wav"]
|
||
|
||
|
||
def test_reference_images_are_capped_at_model_limit():
|
||
"""节点最多 9 个参考图槽位,超出部分必须丢弃,否则提交即失败。"""
|
||
g, _ = builder().build(prompt="橘猫", negative="", seed=1, width=1344, height=768, length=124,
|
||
refs=[f"nex/r{i}.png" for i in range(12)])
|
||
slots = {key: value for key, value in g["10"]["inputs"].items()
|
||
if key.startswith("ref_images.")}
|
||
assert len(slots) == WORKFLOW_TEMPLATES["minimax_h3"]["max_refs"] == 6
|
||
assert sorted(slots) == [f"ref_images.ref_image_{i}" for i in range(6)]
|
||
|
||
|
||
def test_explicit_shot_reference_is_kept_when_first_frame_exists():
|
||
"""i2v 关键帧不能把用户显式选择的参考图静默挤掉,且首帧仍是第 1 张条件图。"""
|
||
request = VideoGenRequest(
|
||
prompt="同一角色走进雨夜站台",
|
||
duration_seconds=5,
|
||
first_frame="/media/keyframe.png",
|
||
reference_images=["/media/shot-ref.png", "/media/portrait.png"],
|
||
explicit_reference_images=["/media/shot-ref.png"],
|
||
)
|
||
assert reference_inputs(request, "nex/keyframe.png", 6) == [
|
||
"nex/keyframe.png", "/media/shot-ref.png", "/media/portrait.png",
|
||
]
|
||
|
||
|
||
def test_auto_character_reference_does_not_replace_i2v_first_frame():
|
||
"""自动带入的定妆图也要进模型(一致性靠它),但关键帧仍排在第 1 张压住构图。"""
|
||
request = VideoGenRequest(
|
||
prompt="保持角色一致", duration_seconds=5, first_frame="/media/keyframe.png",
|
||
reference_images=["/media/portrait.png"],
|
||
)
|
||
assert reference_inputs(request, "nex/keyframe.png", 6) == [
|
||
"nex/keyframe.png", "/media/portrait.png",
|
||
]
|
||
|
||
|
||
def test_frame_only_reference_mode_keeps_strict_first_frame():
|
||
"""reference_mode=frame_only 保留旧行为:有首帧就不再传参考图(必须复刻关键帧的镜头用)。"""
|
||
request = VideoGenRequest(
|
||
prompt="保持角色一致", duration_seconds=5, first_frame="/media/keyframe.png",
|
||
reference_images=["/media/portrait.png"],
|
||
)
|
||
assert reference_inputs(request, "nex/keyframe.png", 6, mode="frame_only") == []
|
||
# 没有首帧时参考图照常生效,否则图生视频会退化成纯文生视频
|
||
assert reference_inputs(
|
||
VideoGenRequest(prompt="保持角色一致", duration_seconds=5,
|
||
reference_images=["/media/portrait.png"]),
|
||
"", 6, mode="frame_only") == ["/media/portrait.png"]
|
||
|
||
|
||
def test_qwen_image_reference_graph_uses_autogrow_and_reference_latent():
|
||
request = AssetGenRequest(kind="portrait", prompt="保持角色一致性",
|
||
reference_images=["/tmp/local.png"])
|
||
graph = ComfyImageProvider.graph(
|
||
request, {"max_reference_images": 2}, 7,
|
||
references=["nex_animation/portrait-a.png", "nex_animation/portrait-b.png"],
|
||
)
|
||
|
||
assert graph["4"]["inputs"]["images.image_1"] == ["21", 0]
|
||
assert graph["4"]["inputs"]["images.image_2"] == ["22", 0]
|
||
assert "images" not in graph["4"]["inputs"]
|
||
assert graph["4"]["inputs"]["vae"] == ["3", 0]
|
||
assert graph["11"]["inputs"]["latent_image"] == ["4", 2]
|
||
assert graph["21"]["inputs"]["image"] == "nex_animation/portrait-a.png"
|
||
assert graph["22"]["inputs"]["image"] == "nex_animation/portrait-b.png"
|
||
assert graph["4"]["inputs"]["prompt"] == (
|
||
"以<image1>、<image2>为参考。"
|
||
"参考图只用于人物身份、空间结构、材质和美术风格的一致性,不代表最终构图。"
|
||
"必须重新组织画面,生成一张与参考图明显不同的新画面;"
|
||
"改变机位、景别或光线氛围,不要直接复刻、描摹或输出参考图本身。"
|
||
"按以下要求生成:保持角色一致性"
|
||
)
|
||
|
||
|
||
def test_qwen_reference_canvas_is_reframed_to_requested_output(make_image_png, tmp_path):
|
||
"""Reference latent must not override the requested 9:16 output canvas."""
|
||
from app.services import ffmpeg as ff
|
||
|
||
source = tmp_path / "portrait.png"
|
||
source.write_bytes(make_image_png(896, 1184, "0x335577"))
|
||
target = tmp_path / "reference-canvas.png"
|
||
prepared = _fit_reference_canvas(source, 720, 1280, target)
|
||
|
||
assert prepared == target
|
||
assert ff.image_size(prepared) == (720, 1280)
|
||
|
||
|
||
def test_qwen_generated_canvas_is_normalized_to_exact_project_size(make_image_png, tmp_path):
|
||
"""Latent grid rounding must not leak 704x1280 keyframes into a 720x1280 project."""
|
||
from app.services import ffmpeg as ff
|
||
|
||
output = tmp_path / "keyframe.png"
|
||
output.write_bytes(make_image_png(704, 1280, "0x335577"))
|
||
_fit_generated_canvas(output, 720, 1280)
|
||
|
||
assert ff.image_size(output) == (720, 1280)
|
||
|
||
|
||
def test_qwen_image_generation_without_references_still_uses_requested_canvas(
|
||
monkeypatch, make_image_png, tmp_path):
|
||
"""A text-only image request must not hit an uninitialized canvas branch."""
|
||
from app.services import ffmpeg as ff
|
||
|
||
output = tmp_path / "text-only.png"
|
||
|
||
def fake_download(_client, _base, _item, target):
|
||
Path(target).write_bytes(make_image_png(704, 1280, "0x335577"))
|
||
|
||
monkeypatch.setattr("app.providers.comfy_assets.gpu_slot", lambda *_a, **_kw: nullcontext())
|
||
monkeypatch.setattr("app.providers.comfy_assets.httpx.Client", lambda *_a, **_kw: nullcontext())
|
||
# 同上:http://fake-comfy 不可解析,本用例只关心画布分支,探活闸门先放行。
|
||
monkeypatch.setattr("app.providers.comfy_assets.require_reachable", lambda *_a, **_kw: None)
|
||
monkeypatch.setattr("app.providers.comfy_assets.submit_graph",
|
||
lambda *_a, **_kw: "prompt-text-only")
|
||
monkeypatch.setattr("app.providers.comfy_assets.wait_history", lambda *_a, **_kw: {})
|
||
monkeypatch.setattr("app.providers.comfy_assets.first_output",
|
||
lambda *_a, **_kw: {"filename": "text-only.png", "subfolder": "", "type": "output"})
|
||
monkeypatch.setattr("app.providers.comfy_assets.download_output", fake_download)
|
||
|
||
result = ComfyImageProvider().generate(AssetGenRequest(
|
||
kind="scene_image", prompt="空无一人的海边卧室,9:16 竖屏",
|
||
width=720, height=1280, output_path=output,
|
||
), {"base_url": "http://fake-comfy"})
|
||
|
||
assert result.file_path == str(output)
|
||
assert ff.image_size(output) == (720, 1280)
|
||
|
||
|
||
def test_qwen_image_reference_graph_keeps_explicit_marker_and_requests_new_composition():
|
||
request = AssetGenRequest(kind="scene_image", prompt="把<image1>改成室内视角",
|
||
reference_images=["/tmp/local.png"])
|
||
graph = ComfyImageProvider.graph(
|
||
request, {}, 7, references=["nex_animation/scene.png"],
|
||
)
|
||
|
||
prompt = graph["4"]["inputs"]["prompt"]
|
||
assert prompt.endswith("把<image1>改成室内视角")
|
||
assert "将参考图中的镜头移到建筑内部" in prompt
|
||
assert "画面完全位于室内" in prompt
|
||
assert "必须重新组织画面" in prompt
|
||
assert prompt.count("<image1>") == 1
|
||
|
||
|
||
def test_qwen_image_short_interior_prompt_relocates_camera():
|
||
request = AssetGenRequest(kind="scene_image", prompt="室内场景,光线温暖。",
|
||
reference_images=["/tmp/local.png"])
|
||
graph = ComfyImageProvider.graph(
|
||
request, {}, 7, references=["nex_animation/scene.png"],
|
||
)
|
||
|
||
prompt = graph["4"]["inputs"]["prompt"]
|
||
assert prompt.startswith("将<image1>中的镜头移到建筑内部")
|
||
assert "不要展示建筑外立面、屋顶或室外庭院" in prompt
|
||
assert "生成一张与参考图明显不同的新画面" in prompt
|
||
assert prompt.endswith("室内场景,光线温暖。")
|
||
|
||
|
||
def test_qwen_image_reference_respects_explicit_composition_lock():
|
||
request = AssetGenRequest(kind="scene_image", prompt="保持<image1>的构图,只改成暖色灯光",
|
||
reference_images=["/tmp/local.png"])
|
||
graph = ComfyImageProvider.graph(
|
||
request, {}, 7, references=["nex_animation/scene.png"],
|
||
)
|
||
|
||
assert graph["4"]["inputs"]["prompt"] == (
|
||
"以<image1>为参考,按以下要求生成或编辑:保持<image1>的构图,只改成暖色灯光"
|
||
)
|
||
|
||
|
||
def test_image_seed_changes_with_prompt_but_honors_explicit_seed():
|
||
base = AssetGenRequest(kind="scene_image", prompt="室内场景,光线温暖。",
|
||
reference_images=["/media/scene.png"], idempotency_key="scene-1")
|
||
changed = AssetGenRequest(kind="scene_image", prompt="夜景,冷色月光。",
|
||
reference_images=["/media/scene.png"], idempotency_key="scene-1")
|
||
fixed = AssetGenRequest(kind="scene_image", prompt="夜景,冷色月光。", seed=42,
|
||
reference_images=["/media/scene.png"], idempotency_key="scene-1")
|
||
|
||
assert _image_seed(base) == _image_seed(base)
|
||
assert _image_seed(base) != _image_seed(changed)
|
||
assert _image_seed(fixed) == 42
|
||
|
||
|
||
def test_text_to_video_graph_has_no_image_nodes():
|
||
g, _ = builder().build(prompt="纯文生视频", negative="", seed=1, width=1344, height=768, length=124)
|
||
assert g["10"]["class_type"] == "MiniMaxH3ImageToVideo"
|
||
assert "first_frame" not in g["10"]["inputs"]
|
||
assert not any(n["class_type"] == "LoadImage" for n in g.values())
|
||
|
||
|
||
def test_negative_prompt_is_folded_into_natural_language():
|
||
"""**非**联合模型没有 negative 输入端时才折成正向子句,否则禁忌被静默丢弃。"""
|
||
b = builder()
|
||
plain = {"negative_as_prompt": True, "capabilities": {"joint_av": False}}
|
||
text = b.fold_negative("夜色站台", "文字水印, 肢体畸变,多余手指", plain)
|
||
assert "夜色站台" in text and "画面中不要出现" in text
|
||
assert "文字水印" in text and "肢体畸变" in text and "多余手指" in text
|
||
assert b.fold_negative("只有正向", "", {}) == "只有正向"
|
||
assert b.fold_negative("保留原样", "文字", {"negative_as_prompt": False}) == "保留原样"
|
||
|
||
|
||
def test_joint_av_model_never_gets_a_spoken_negative():
|
||
"""H3 只有一个 prompt 入口,「画面中不要出现:…」会被**原样念出来**。
|
||
|
||
项目 11 v2 成片每段开头的乱语里就有这句(H3 的文本编码器把 prompt 裸拼,
|
||
既是画面脚本也是声音脚本)。负向词继续留在镜头上供换供应商使用,只是不投递。
|
||
"""
|
||
b = builder() # 默认模板 supports_audio=True -> joint
|
||
assert b.fold_negative("夜色站台", "文字水印, 肢体畸变", {"negative_as_prompt": True}) == "夜色站台"
|
||
|
||
|
||
def test_model_names_are_configurable_per_machine():
|
||
config = comfyui_config(models={**DEFAULT_MODELS, "unet": "other_model.safetensors",
|
||
"clip": "other_clip.safetensors", "clip_type": "wan"},
|
||
unet_loader="UNETLoader", clip_loader="CLIPLoader")
|
||
g, _ = WorkflowBuilder(config, WORKFLOW_TEMPLATES["minimax_h3"]).build(
|
||
prompt="p", negative="", seed=1, width=1344, height=768, length=124)
|
||
assert g["1"]["class_type"] == "UNETLoader"
|
||
assert g["1"]["inputs"]["unet_name"] == "other_model.safetensors"
|
||
assert g["2"]["inputs"]["clip_name"] == "other_clip.safetensors"
|
||
assert g["2"]["inputs"]["type"] == "wan"
|
||
|
||
|
||
def test_turbo_lora_is_inserted_on_the_model_chain():
|
||
"""配了 lora 就要挂在 guider/scheduler 的模型上游,且不带 lora 时图里不多节点。"""
|
||
b = builder(lora="minimax_h3_ref2v_turbo_4step_v0.1_comfyui_bf16.safetensors",
|
||
lora_strength=0.8, unet_loader="UNETLoader")
|
||
g, _ = b.build(prompt="p", negative="", seed=1, width=1344, height=768, length=124)
|
||
assert g["90"]["class_type"] == "LoraLoaderModelOnly"
|
||
assert g["90"]["inputs"]["model"] == ["1", 0]
|
||
assert g["90"]["inputs"]["strength_model"] == 0.8
|
||
assert g["21"]["inputs"]["model"] == ["90", 0] # BasicGuider
|
||
assert g["23"]["inputs"]["model"] == ["90", 0] # BasicScheduler
|
||
|
||
plain, _ = builder().build(prompt="p", negative="", seed=1, width=1344, height=768, length=124)
|
||
assert "90" not in plain
|
||
assert plain["21"]["inputs"]["model"] == ["1", 0]
|
||
|
||
|
||
def test_unknown_workflow_is_fatal():
|
||
"""配置写错工作流名要给可执行的提示,而不是 KeyError。"""
|
||
with pytest.raises(FatalProviderError) as exc:
|
||
ComfyVideoProvider()._template({"workflow": "not_a_workflow"})
|
||
assert "minimax_h3" in str(exc.value)
|
||
assert ComfyVideoProvider().capabilities({"workflow": "not_a_workflow"}) is not None
|
||
|
||
|
||
# ---------------------------------------------------------------- 响应语义
|
||
class _Resp:
|
||
def __init__(self, status=200, text="", headers=None):
|
||
self.status_code, self.text = status, text
|
||
self.headers = headers or {}
|
||
|
||
|
||
def test_http_status_maps_to_retry_vs_fatal():
|
||
with pytest.raises(RetryableProviderError):
|
||
_raise_for(_Resp(503, "busy"), context="提交")
|
||
with pytest.raises(RateLimitedError) as exc:
|
||
_raise_for(_Resp(429, "slow down", {"Retry-After": "12"}), context="提交")
|
||
assert exc.value.retry_after == 12.0
|
||
with pytest.raises(FatalProviderError):
|
||
_raise_for(_Resp(400, "bad workflow"), context="提交")
|
||
|
||
|
||
def test_failure_message_reads_execution_errors():
|
||
entry = {"status": {"status_str": "error", "completed": False,
|
||
"messages": [["execution_error", {"node_id": 10,
|
||
"exception_message": "CUDA out of memory"}]]}}
|
||
assert "CUDA out of memory" in _failure_message(entry)
|
||
assert _failure_message({"status": {"status_str": "success", "completed": True, "messages": []}}) == ""
|
||
assert _failure_message({}) == ""
|
||
|
||
|
||
def test_first_file_picks_the_saved_video():
|
||
entry = {"outputs": {"41": {"images": [{"filename": "shot_00001_.mp4", "subfolder": "nex",
|
||
"type": "output"}]}}}
|
||
assert _first_file(entry)["filename"] == "shot_00001_.mp4"
|
||
assert _first_file({"outputs": {}}) is None
|
||
|
||
|
||
def test_file_choices_reads_loaders_from_object_info():
|
||
object_info = {
|
||
"UnetLoaderGGUF": {"input": {"required": {"unet_name": [["a.gguf"], {}]}}},
|
||
"CLIPLoaderGGUF": {"input": {"required": {"clip_name": [["b.gguf"], {}]}}},
|
||
"CLIPLoader": {"input": {"required": {"type": [["minimax", "wan"], {}]}}},
|
||
"VAELoader": {"input": {"required": {"vae_name": [["v.safetensors", "pixel_space"], {}]}}},
|
||
"LoraLoaderModelOnly": {"input": {"required": {"lora_name": [[], {}]}}},
|
||
}
|
||
files = _file_choices(object_info)
|
||
assert files["unet"] == ["a.gguf"] and files["clip"] == ["b.gguf"]
|
||
assert files["vae"] == ["v.safetensors"] # pixel_space 不是模型文件
|
||
assert "minimax" in files["clip_types"]
|
||
|
||
|
||
# ---------------------------------------------------------------- 可用性探测
|
||
def test_unreachable_comfyui_reports_unavailable_without_raising(monkeypatch):
|
||
"""模型注册表页面会高频调用可用性探测,这里必须返回 False 而不是抛异常。"""
|
||
def boom(*_a, **_kw):
|
||
raise RuntimeError("network down")
|
||
|
||
monkeypatch.setattr("httpx.get", boom)
|
||
from app.config import settings
|
||
monkeypatch.setattr(settings, "comfyui_base_url", "", raising=False)
|
||
provider = ComfyVideoProvider()
|
||
assert provider.is_available({"base_url": "http://127.0.0.1:1"}) is False
|
||
info = provider.diagnose({"base_url": "http://127.0.0.1:1"})
|
||
assert info["available"] is False and info["message"]
|
||
assert provider.is_available({}) is False # 没配 base_url 也不许抛
|
||
|
||
|
||
@contextmanager
|
||
def fake_index_tts_comfy(*, include_nodes: bool = True):
|
||
"""最小 ComfyUI IndexTTS2 服务:覆盖节点探测、参考音频上传、提交和下载。"""
|
||
state: dict = {"graph": None, "upload_size": 0}
|
||
audio = _make_wav(0.5, rate=24000)
|
||
|
||
class Handler(BaseHTTPRequestHandler):
|
||
def log_message(self, *_args):
|
||
pass
|
||
|
||
def json(self, payload, status: int = 200):
|
||
body = json.dumps(payload, ensure_ascii=False).encode()
|
||
self.send_response(status)
|
||
self.send_header("Content-Type", "application/json")
|
||
self.send_header("Content-Length", str(len(body)))
|
||
self.end_headers()
|
||
self.wfile.write(body)
|
||
|
||
def do_GET(self):
|
||
parsed = urlparse(self.path)
|
||
if parsed.path == "/system_stats":
|
||
return self.json({"system": {"comfyui_version": "test"}})
|
||
if parsed.path == "/object_info":
|
||
nodes = {"LoadAudio": {}, "SaveAudio": {}}
|
||
if include_nodes:
|
||
nodes.update({"easy downloadIndexTTSAndLoadModel": {},
|
||
"easy indexTTSGenerateSimple": {}})
|
||
return self.json(nodes)
|
||
if parsed.path.startswith("/object_info/"):
|
||
node = unquote(parsed.path.removeprefix("/object_info/"))
|
||
available = {
|
||
"easy downloadIndexTTSAndLoadModel",
|
||
"easy indexTTSGenerateSimple",
|
||
"LoadAudio",
|
||
"SaveAudio",
|
||
}
|
||
if include_nodes and node in available:
|
||
return self.json({node: {"input": {}}})
|
||
return self.json({})
|
||
if parsed.path.startswith("/history/"):
|
||
prompt_id = parsed.path.rsplit("/", 1)[-1]
|
||
return self.json({prompt_id: {
|
||
"status": {"status_str": "success", "completed": True, "messages": []},
|
||
"outputs": {"4": {"audio": [{
|
||
"filename": "index-tts.wav", "subfolder": "audio/nex", "type": "output",
|
||
}]}},
|
||
}})
|
||
if parsed.path == "/view":
|
||
query = parse_qs(parsed.query)
|
||
assert query["filename"] == ["index-tts.wav"]
|
||
self.send_response(200)
|
||
self.send_header("Content-Type", "audio/wav")
|
||
self.send_header("Content-Length", str(len(audio)))
|
||
self.end_headers()
|
||
self.wfile.write(audio)
|
||
return None
|
||
return self.json({"error": "not found"}, 404)
|
||
|
||
def do_POST(self):
|
||
length = int(self.headers.get("Content-Length", "0"))
|
||
body = self.rfile.read(length)
|
||
if self.path == "/upload/image":
|
||
state["upload_size"] = len(body)
|
||
return self.json({"name": "nex-index-ref.wav", "subfolder": "nex_animation"})
|
||
if self.path == "/prompt":
|
||
payload = json.loads(body)
|
||
state["graph"] = payload["prompt"]
|
||
return self.json({"prompt_id": "prompt-1"})
|
||
return self.json({"error": "not found"}, 404)
|
||
|
||
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
||
thread = threading.Thread(target=server.serve_forever, daemon=True)
|
||
thread.start()
|
||
try:
|
||
yield f"http://127.0.0.1:{server.server_port}", state
|
||
finally:
|
||
server.shutdown()
|
||
server.server_close()
|
||
thread.join(timeout=2)
|
||
|
||
|
||
def test_index_tts_complete_reference_audio_graph(tmp_path):
|
||
reference = tmp_path / "reference.wav"
|
||
reference.write_bytes(_make_wav(0.4, rate=24000))
|
||
output = tmp_path / "result.wav"
|
||
with fake_index_tts_comfy() as (base, state):
|
||
provider = ComfyTTSProvider()
|
||
config = {
|
||
"base_url": base, "poll_interval": 0.05, "timeout": 5,
|
||
"input_subfolder": "nex_animation", "filename_prefix": "audio/nex",
|
||
}
|
||
assert provider.is_available(config) is True
|
||
result = provider.synthesize(VoiceGenRequest(
|
||
text="参考音色已经接通。", provider_voice_id=str(reference),
|
||
idempotency_key="index-tts-test", output_path=output,
|
||
), config)
|
||
|
||
assert state["upload_size"] > 0
|
||
graph = state["graph"]
|
||
assert graph["1"]["class_type"] == "easy downloadIndexTTSAndLoadModel"
|
||
assert graph["3"]["class_type"] == "easy indexTTSGenerateSimple"
|
||
assert graph["2"]["inputs"]["audio"] == "nex_animation/nex-index-ref.wav"
|
||
assert graph["3"]["inputs"]["reference_audio"] == ["2", 0]
|
||
assert 0 <= graph["3"]["inputs"]["seed"] <= 2 ** 32 - 1
|
||
assert output.read_bytes()[:4] == b"RIFF"
|
||
assert result.duration_ms >= 450
|
||
assert result.metadata["reference_audio"] == "nex_animation/nex-index-ref.wav"
|
||
|
||
|
||
def test_index_tts_ignores_display_name_and_uses_configured_reference(tmp_path):
|
||
output = tmp_path / "result.wav"
|
||
with fake_index_tts_comfy() as (base, state):
|
||
result = ComfyTTSProvider().synthesize(VoiceGenRequest(
|
||
text="使用模型默认参考音色。", provider_voice_id="低沉男声",
|
||
output_path=output,
|
||
), {"base_url": base, "poll_interval": 0.05, "timeout": 5,
|
||
"reference_audio": "nex_default.wav"})
|
||
|
||
assert state["graph"]["2"]["inputs"]["audio"] == "nex_default.wav"
|
||
assert result.metadata["reference_audio"] == "nex_default.wav"
|
||
|
||
|
||
def test_index_tts_falls_back_when_reference_media_was_deleted(tmp_path):
|
||
output = tmp_path / "result.wav"
|
||
with fake_index_tts_comfy() as (base, state):
|
||
result = ComfyTTSProvider().synthesize(VoiceGenRequest(
|
||
text="失效参考音频回退。", provider_voice_id="platform/voice-1/reference/missing.wav",
|
||
output_path=output,
|
||
), {"base_url": base, "poll_interval": 0.05, "timeout": 5,
|
||
"reference_audio": "nex_default.wav"})
|
||
|
||
assert state["graph"]["2"]["inputs"]["audio"] == "nex_default.wav"
|
||
assert result.metadata["reference_audio"] == "nex_default.wav"
|
||
|
||
|
||
def test_index_tts_reports_missing_nodes_before_submitting(tmp_path):
|
||
reference = tmp_path / "reference.wav"
|
||
reference.write_bytes(_make_wav(0.4, rate=24000))
|
||
with fake_index_tts_comfy(include_nodes=False) as (base, state):
|
||
provider = ComfyTTSProvider()
|
||
config = {"base_url": base, "poll_interval": 0.05, "timeout": 5}
|
||
assert provider.is_available(config) is False
|
||
with pytest.raises(FatalProviderError) as exc:
|
||
provider.synthesize(VoiceGenRequest(
|
||
text="不会提交", provider_voice_id=str(reference),
|
||
output_path=tmp_path / "result.wav",
|
||
), config)
|
||
|
||
assert exc.value.code == "missing_comfy_node"
|
||
assert "easy downloadIndexTTSAndLoadModel" in exc.value.message
|
||
assert state["graph"] is None
|
||
|
||
|
||
def test_index_tts_diagnose_separates_reachability_from_node_readiness():
|
||
with fake_index_tts_comfy(include_nodes=False) as (base, _state):
|
||
info = ComfyTTSProvider().diagnose({"base_url": base})
|
||
|
||
assert info["reachable"] is True
|
||
assert info["available"] is False
|
||
assert info["nodes"]["LoadAudio"] is True
|
||
assert info["nodes"]["easy downloadIndexTTSAndLoadModel"] is False
|
||
assert "easy downloadIndexTTSAndLoadModel" in info["missing_nodes"]
|
||
assert "缺少节点" in info["message"]
|
||
|
||
|
||
def test_comfyui_spec_is_seeded_from_environment(db, monkeypatch):
|
||
"""配了 COMFYUI_BASE_URL 就自动注册模型,并把能力区间写进规格(预检据此拦时长)。"""
|
||
from app.config import settings
|
||
from app.providers import registry
|
||
|
||
monkeypatch.setattr(settings, "comfyui_base_url", "http://comfy.test:9200", raising=False)
|
||
monkeypatch.setattr(settings, "comfyui_model", "minimax-h3", raising=False)
|
||
monkeypatch.setattr(settings, "video_provider", "comfyui", raising=False)
|
||
from app.models import ModelSpec
|
||
|
||
db.query(ModelSpec).delete()
|
||
db.commit()
|
||
registry.seed_specs(db)
|
||
spec = db.query(ModelSpec).filter(ModelSpec.provider == "comfyui",
|
||
ModelSpec.media_type == "video").one()
|
||
assert spec.adapter == "comfyui" and spec.is_default is True
|
||
assert spec.max_concurrency == settings.comfyui_concurrency
|
||
assert spec.config_json["base_url"] == "http://comfy.test:9200"
|
||
assert spec.config_json["timeout"] == settings.comfyui_timeout_seconds
|
||
caps = registry.capabilities_of(spec)
|
||
assert "i2v" in caps.modes
|
||
# 注册表写进 DB 的时长窗口,两端对齐到帧网格后都必须落在模型训练区间内
|
||
b = builder()
|
||
assert 124 <= b.length(caps.min_duration) <= 362, (caps.min_duration, b.length(caps.min_duration))
|
||
assert 124 <= b.length(caps.max_duration) <= 362, (caps.max_duration, b.length(caps.max_duration))
|
||
assert caps.needs_public_media is False # 关键帧由适配器上传,不需要公网地址
|
||
|
||
|
||
# ---------------------------------------------------------------- 原生音轨合成
|
||
def _timeline(keep_audio: bool, **extra):
|
||
from app.models import Timeline, TimelineSegment
|
||
|
||
tl = Timeline(project_id=1, fps=24, width=1920, height=1080,
|
||
settings_json={"keep_shot_audio": keep_audio, **extra})
|
||
for idx in range(3):
|
||
seg = TimelineSegment(timeline_id=1, order_index=idx + 1, duration_ms=5000, source_in_ms=0,
|
||
source_out_ms=5000, start_ms=idx * 5000)
|
||
if idx:
|
||
seg.transition_duration_ms = 400
|
||
tl.segments.append(seg)
|
||
return tl
|
||
|
||
|
||
def _graph(tl, *, clips=None):
|
||
from app.services.compose import build_graph
|
||
|
||
return build_graph(tl, list(tl.segments), [], list(clips or []), {}, total_ms=14200,
|
||
width=1920, height=1080, fps=24, ass_path=None, target_lufs=-16,
|
||
audio_inputs=[(3, None, "silence")])
|
||
|
||
|
||
def test_native_audio_is_concatenated_and_mixed_when_enabled():
|
||
tl = _timeline(True, shot_audio_gain_db=-3.0)
|
||
tl.audio_clips.append(__import__("app.models", fromlist=["AudioClip"]).AudioClip(
|
||
timeline_id=1, kind="voiceover", label="配音", source_path="x.wav", start_ms=160, duration_ms=3000))
|
||
graph, _video = _graph(tl)
|
||
# 直切用 concat=v=1:a=1,转场用 xfade ↔ acrossfade(重叠时长一致,音画天然对齐)
|
||
assert "concat=n=2:v=1:a=1" in graph, graph
|
||
assert "acrossfade=d=0.400" in graph, graph
|
||
assert "xfade=transition=" in graph
|
||
# 原生音轨进入最终混音,并且带上配置的垫底增益
|
||
assert "[nativebus]" in graph and "volume=-3.0dB[nativebus]" in graph, graph
|
||
assert "loudnorm=I=-16.0" in graph
|
||
|
||
|
||
def test_native_audio_stays_out_of_the_mix_by_default():
|
||
"""默认仍按「配音 + BGM」混音,避免旧的 15 秒静音片段带着噪声抢戏。"""
|
||
tl = _timeline(False)
|
||
graph, _video = _graph(tl)
|
||
assert "[nativebus]" not in graph
|
||
assert "concat=n=2:v=1:a=0" in graph
|
||
assert "acrossfade" not in graph
|
||
|
||
|
||
# ---------------------------------------------------------------- 时长窗口
|
||
def test_capabilities_match_trained_frame_range():
|
||
"""能力窗口的两端都必须能换算回模型接受且训练过的帧数。
|
||
|
||
曾经的事故:min_duration=4.0 会把 4s 对齐成 73 帧,虽然落在 17k+5 网格上,
|
||
却远低于模型训练过的 124 帧,出图质量不可控而且已经花掉一次真实渲染。
|
||
"""
|
||
caps = ComfyVideoProvider().capabilities({"base_url": "http://comfy.test:9200"})
|
||
b = builder()
|
||
lo, hi = b.length(caps.min_duration), b.length(caps.max_duration)
|
||
assert 124 <= lo <= hi <= 362, (caps.min_duration, caps.max_duration, lo, hi)
|
||
assert lo % 17 == 5 and hi % 17 == 5, (lo, hi) # 且必须落在 17k+5 网格上
|
||
# 越界时长必须由预检挡下,而不是提交给模型烧一次真实渲染
|
||
assert 3.0 < caps.min_duration and 20.0 > caps.max_duration
|
||
# 反例:曾经的下限 4.0s 会算出 107 帧(3s 是 73 帧)——都在 17k+5 网格上,
|
||
# 却低于模型训练过的 124 帧,属于「语法合法但模型没见过」的静默劣化
|
||
assert b.length(4.0) == 107 and b.length(3.0) == 73
|
||
assert b.length(4.0) < 124 and b.length(3.0) < 124
|
||
|
||
|
||
def test_duration_bounds_are_overridable_per_model():
|
||
"""同一模板挂别的模型时,注册表 config 可以显式改写时长窗口。"""
|
||
caps = ComfyVideoProvider().capabilities({"base_url": "http://x", "capabilities":
|
||
{"min_duration": 6, "max_duration": 10}})
|
||
assert (caps.min_duration, caps.max_duration) == (6.0, 10.0)
|
||
|
||
|
||
# ---------------------------------------------------------------- 生成记录可复现
|
||
def test_generation_record_exposes_provider_metadata(api, client, db):
|
||
"""生成记录 API 必须回吐供应商侧留痕与实际生效的 seed,否则「可复现」是假的。
|
||
|
||
真机跑完后前端只能看到 duration,画布/帧数/seed/产物文件名全被吞在
|
||
metadata_json 里;重跑时既不知道当初的 seed,也不知道实际出图分辨率。
|
||
"""
|
||
from app.models import GenerationRecord
|
||
from app.services import costing
|
||
|
||
pid = api.project(name="复现性留痕")
|
||
row = GenerationRecord(project_id=1, shot_id=1, task_id=1, provider="comfyui",
|
||
model="minimax-h3", kind="generate_shot", prompt="橘猫走过雨夜站台",
|
||
params_json={"mode": "i2v", "seed": 496278262, "duration": 5.0},
|
||
provider_job_id="628b912f-1b97-4112-9cd6-18fe9037c661",
|
||
output_path="1/generated/shot-01.mp4", duration_seconds=5.167,
|
||
metadata_json={"elapsed_seconds": 1152.5,
|
||
"provider_metadata": {"canvas": "1312x736",
|
||
"length_frames": 124,
|
||
"seed": 496278262, "fps": 24.0,
|
||
"comfy_output": "nex/shot_00001_.mp4"}})
|
||
row.project_id = pid
|
||
db.add(row)
|
||
db.commit()
|
||
|
||
rows = costing.generation_records(db, pid)
|
||
g = next(r for r in rows if r["provider"] == "comfyui")
|
||
pm = g["provider_metadata"]
|
||
assert pm["canvas"] == "1312x736" and pm["length_frames"] == 124
|
||
assert pm["comfy_output"] == "nex/shot_00001_.mp4"
|
||
assert g["metadata"]["elapsed_seconds"] == 1152.5
|
||
assert g["params"]["seed"] == 496278262
|
||
|
||
body = client.get(f"/api/projects/{pid}/generations").json()
|
||
api_row = next(r for r in body["generations"] if r["provider"] == "comfyui")
|
||
assert api_row["provider_metadata"]["canvas"] == "1312x736", api_row
|
||
|
||
|
||
def test_generation_record_falls_back_to_actual_provider_seed(api, studio, script_text,
|
||
make_image_png, monkeypatch):
|
||
"""未指定 seed 时,生成记录必须落「供应商实际使用的 seed」,否则重跑不可复现。
|
||
|
||
适配器在不传 seed 时会自行派生一个(ComfyUI 的 RandomNoise 用它),
|
||
而请求对象里的 seed 仍是 None;只记 None 等于没记,这条镜头就没法复现。
|
||
这里用真实 handler + 真实适配器桩验证落库内容,而不是复述 dict.get 的语义。
|
||
"""
|
||
from app.providers import mock as mock_provider
|
||
from app.providers.base import VideoGenResult
|
||
|
||
PROVIDER_SEED = 496278262
|
||
original = mock_provider.MockVideoProvider.generate
|
||
|
||
def fake_generate(self, request, config=None, **kwargs):
|
||
result = original(self, request, config, **kwargs)
|
||
# 模拟 ComfyUI:请求没给 seed,适配器自己派生了一个并回传
|
||
return VideoGenResult(file_path=result.file_path, provider_job_id=result.provider_job_id,
|
||
duration_seconds=result.duration_seconds, cost=result.cost,
|
||
metadata={**result.metadata, "seed": PROVIDER_SEED,
|
||
"canvas": "1312x736", "length_frames": 124})
|
||
|
||
monkeypatch.setattr(mock_provider.MockVideoProvider, "generate", fake_generate)
|
||
|
||
pid = studio["project_id"]
|
||
api.post(f"/api/projects/{pid}/import",
|
||
data={"script_text": script_text, "default_duration": 2},
|
||
files=[("images", ("s01.png", make_image_png(480, 270, "0x226699"), "image/png")),
|
||
("images", ("s02.png", make_image_png(480, 270, "0x993355"), "image/png"))])
|
||
api.post(f"/api/projects/{pid}/tasks", json={"with_voiceover": False, "actor": "seed 回归"})
|
||
assert api.drain(pid)["result"]["drained"]["remaining"] == 0
|
||
|
||
gens = [g for g in client_generations(api, pid) if g["kind"] == "generate_shot"]
|
||
assert gens, "生成记录应存在"
|
||
for g in gens:
|
||
assert g["params"].get("seed") == PROVIDER_SEED, \
|
||
"记录里必须是供应商实际使用的 seed,否则镜头无法复现:%s" % g["params"]
|
||
assert g["provider_metadata"]["canvas"] == "1312x736"
|
||
assert g["provider_metadata"]["length_frames"] == 124
|
||
|
||
|
||
def test_generation_ledger_records_structured_camera_params(api, studio, script_text, make_image_png):
|
||
"""出片台账必须留下「这条片子按哪档景别/机位/运镜生成」的结构化证据。
|
||
|
||
回归背景:记录里只有提示词文本时,镜头改成 push_in 还是 track 无从核对;
|
||
运镜一旦漏下发(真实发生过),旧片与新片在台账里长得一模一样,
|
||
验收只能靠肉眼看片。现在 params 与 provider_metadata 两侧都必须可核对。
|
||
"""
|
||
pid = studio["project_id"]
|
||
api.post(f"/api/projects/{pid}/import",
|
||
data={"script_text": script_text, "default_duration": 2},
|
||
files=[("images", ("s01.png", make_image_png(480, 270, "0x226699"), "image/png")),
|
||
("images", ("s02.png", make_image_png(480, 270, "0x993355"), "image/png"))])
|
||
api.post(f"/api/projects/{pid}/tasks", json={"with_voiceover": False, "actor": "镜头参数留痕"})
|
||
assert api.drain(pid)["result"]["drained"]["remaining"] == 0
|
||
|
||
shots = {s["id"]: s for s in api.get(f"/api/projects/{pid}/shots")["shots"]}
|
||
gens = [g for g in client_generations(api, pid) if g["kind"] == "generate_shot"]
|
||
assert len(gens) == len(shots) == 2, (gens, shots)
|
||
assert {s["camera_movement"] for s in shots.values()} != {"static"}, "两镜应有不同运镜,才能验出留痕"
|
||
for g in gens:
|
||
shot = shots[g["shot_id"]]
|
||
for field in ("shot_size", "camera_angle", "camera_movement"):
|
||
assert g["params"].get(field) == shot[field], (
|
||
f"{field} 未随出片台账留痕:params={g['params']} 镜头={shot[field]}")
|
||
assert (g["provider_metadata"] or {}).get("camera_movement") == shot["camera_movement"], (
|
||
f"模型侧未回传运镜留痕:{g['provider_metadata']}")
|
||
|
||
|
||
def client_generations(api, pid):
|
||
return api.get(f"/api/projects/{pid}/generations")["generations"]
|
||
|
||
|
||
# --------------------------------------------------------------------- GPU 资源锁
|
||
def test_gpu_slot_serialises_adapters_that_only_share_an_endpoint():
|
||
"""视频适配器漏配 resource_group 时,也必须和图像/TTS 抢同一把卡锁。
|
||
|
||
回归背景:video 的锁键是 base_url、image/tts 的锁键是 "comfyui:9200",
|
||
两者不互斥,H3 与 Qwen-Image 在同一 ComfyUI 进程上交替执行,把对方的权重
|
||
挤出统一内存,每段视频要多付 3 分钟重新装载。
|
||
"""
|
||
from app.providers.comfy_common import gpu_slot, lock_keys
|
||
|
||
base = "http://10.100.53.199:9200"
|
||
assert "10.100.53.199:9200" in lock_keys({}, base) # 视频:未配置分组
|
||
assert "10.100.53.199:9200" in lock_keys({"resource_group": "comfyui:9200"}, base)
|
||
|
||
order: list[str] = []
|
||
started = threading.Event()
|
||
|
||
def worker(tag: str, config: dict) -> None:
|
||
with gpu_slot(config, base):
|
||
order.append(f"{tag}:enter")
|
||
if tag == "first":
|
||
started.set()
|
||
time.sleep(0.25)
|
||
order.append(f"{tag}:exit")
|
||
|
||
first = threading.Thread(target=worker, args=("first", {}))
|
||
first.start()
|
||
assert started.wait(2), "第一个任务应已进入临界区"
|
||
second = threading.Thread(target=worker, args=("second", {"resource_group": "comfyui:9200"}))
|
||
second.start()
|
||
second.join(5)
|
||
first.join(5)
|
||
assert order == ["first:enter", "first:exit", "second:enter", "second:exit"], \
|
||
"同一接入点上的视频与图像必须串行执行,实际顺序:%s" % order
|
||
|
||
|
||
def test_gpu_slot_still_honours_custom_resource_groups():
|
||
"""显式分组仍应生效:不同接入点但同组的任务也要串行。"""
|
||
from app.providers.comfy_common import gpu_slot, lock_keys
|
||
|
||
assert lock_keys({"resource_group": "one-gpu"}, "http://10.0.0.1:9200") != \
|
||
lock_keys({"resource_group": "two-gpu"}, "http://10.0.0.1:9200")
|
||
|
||
order: list[str] = []
|
||
|
||
def worker(tag: str, base: str) -> None:
|
||
with gpu_slot({"resource_group": "shared"}, base):
|
||
order.append(f"{tag}:enter")
|
||
time.sleep(0.2)
|
||
order.append(f"{tag}:exit")
|
||
|
||
threads = [threading.Thread(target=worker, args=(t, f"http://127.0.0.{i}:9300"))
|
||
for i, t in enumerate(("a", "b"))]
|
||
for t in threads:
|
||
t.start()
|
||
for t in threads:
|
||
t.join(5)
|
||
assert order[:2] in (["a:enter", "a:exit"], ["b:enter", "b:exit"]), \
|
||
"同组任务必须串行,实际顺序:%s" % order
|