nex_animation/backend/tests/conftest.py

338 lines
13 KiB
Python
Raw Permalink 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.

"""pytest 环境:必须在 import app.* 之前设定环境变量,
让整套自检在无 Redis、无外部供应商的机器上跑通图2 全流程。"""
from __future__ import annotations
import json
import os
import shutil
import tempfile
from pathlib import Path
TEST_ROOT = Path(tempfile.gettempdir()) / "nex_animation_tests"
# 素材仓库同样支持独立目录:另一个会话在跑自检时会清空共享 MEDIA_ROOT,
# 两边并发时就会出现「文件明明刚写进去,断言时不见了」的假失败。
MEDIA_ROOT = Path(os.environ.get("NEX_TEST_MEDIA") or (TEST_ROOT / "media"))
# 允许多个自检进程并行(例如另一个会话正在跑全量):指定 NEX_TEST_DB
# 就换一份独立的库,避免两边同时 drop_all/create_all 互相踩表。
DB_PATH = Path(os.environ.get("NEX_TEST_DB") or (TEST_ROOT / "test.db"))
TEST_ROOT.mkdir(parents=True, exist_ok=True)
os.environ.update({
"DATABASE_URL": f"sqlite:///{DB_PATH}",
"TASK_EXECUTOR": "inline",
"VIDEO_PROVIDER": "mock",
"VIDEO_MODEL": "mock-video-1",
"TTS_PROVIDER": "mock",
# ComfyUI 逃生门必须显式钉住:registry.seed_specs 只有在 COMFYUI_BASE_URL 非空时才登记
# comfyui 的视频/图像/配音三条模型,而模型配置搬进数据库之后开发机 .env 里已经没有它了。
# 不钉住的话,「IndexTTS2 是否在册」这类断言会跟着**本机 .env** 漂
# (换台机器 / 换份 .env 就假失败),CI 上更是必挂。适配器只判空不联网,钉成假地址即可。
"COMFYUI_BASE_URL": "http://comfyui.test:9200",
"MEDIA_ROOT": str(MEDIA_ROOT),
"DISPATCH_INTERVAL_SECONDS": "1",
"DISPATCH_BATCH_SIZE": "32",
"MAX_SHOT_RETRIES": "2",
"RETRY_BACKOFF_BASE_SECONDS": "1",
"RETRY_BACKOFF_MAX_SECONDS": "2",
"STALE_RUNNING_SECONDS": "2",
"INLINE_WORKERS": "4",
"CORS_ORIGINS": "*",
"LOG_LEVEL": "WARNING",
# 既有的全流程用例关心的是出片链路,不是登录;把鉴权关掉,
# 用户体系自己有一组用例(tests/test_auth.py)在 AUTH_ENABLED=true 下跑。
"AUTH_ENABLED": "false",
"PASSWORD_ITERATIONS": "2000",
})
import pytest # noqa: E402
from fastapi.testclient import TestClient # noqa: E402
from app.db import Base, SessionLocal, engine, init_db # noqa: E402
from app.main import app # noqa: E402
from app.services import daemon, execution, storage # noqa: E402
@pytest.fixture(scope="session", autouse=True)
def _database():
init_db()
yield
execution.shutdown()
@pytest.fixture(autouse=True)
def _clean_state():
"""每个用例都从空库 + 空素材仓库开始。"""
Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)
if MEDIA_ROOT.exists():
for child in MEDIA_ROOT.iterdir():
shutil.rmtree(child, ignore_errors=True) if child.is_dir() else child.unlink(missing_ok=True)
yield
execution.wait_idle(timeout=30)
@pytest.fixture
def db():
session = SessionLocal()
try:
yield session
finally:
session.close()
@pytest.fixture
def client():
"""不启动后台守护线程:调度由用例显式 dispatch/drain 驱动,结果完全确定。"""
original_start = daemon.start
daemon.start = lambda **kwargs: False # type: ignore[assignment]
with TestClient(app) as test_client:
yield test_client
daemon.start = original_start # type: ignore[assignment]
# ---------------------------------------------------------------- 常用构造器
PNG_1x1 = bytes.fromhex(
"89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4"
"890000000a49444154789c6360000002000100ffff03000006000557bfabd400"
"00000049454e44ae426082")
def _make_image_png(width: int = 480, height: int = 270, color: str = "0x3a6ea5") -> bytes:
"""用 ffmpeg 造一张真实可解码的 PNG(比 1x1 更接近真实关键帧)。"""
import subprocess
from app.services import ffmpeg as ff
if not ff.available():
return PNG_1x1
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as handle:
target = handle.name
try:
subprocess.run([ff.FFMPEG, "-hide_banner", "-loglevel", "error", "-y",
"-f", "lavfi", "-i", f"color=c={color}:s={width}x{height}:d=0.1",
"-frames:v", "1", target], check=True, capture_output=True)
return Path(target).read_bytes()
except Exception:
return PNG_1x1
finally:
Path(target).unlink(missing_ok=True)
@pytest.fixture
def api(client):
"""薄封装:自动拆开 {ok,result,summary} 信封,出错时给出可读断言信息。"""
class Api:
def __init__(self, client_):
self.client = client_
def call(self, method, url, *, expect=200, **kwargs):
response = getattr(self.client, method.lower())(url, **kwargs)
if expect is not None:
assert response.status_code == expect, f"{method} {url} -> {response.status_code}: {response.text[:800]}"
if response.headers.get("content-type", "").startswith("application/json"):
return response.json()
return response
def get(self, url, **kw):
return self.call("GET", url, **kw)
def post(self, url, **kw):
return self.call("POST", url, **kw)
def patch(self, url, **kw):
return self.call("PATCH", url, **kw)
def delete(self, url, **kw):
return self.call("DELETE", url, **kw)
def project(self, **overrides):
payload = {"name": "星海纪元", "description": "测试长片", "resolution": "480p",
"aspect_ratio": "16:9", "fps": 24, "target_duration": 12,
"default_provider": "mock", "style_guide": "电影感,冷色调"}
payload.update(overrides)
body = self.post("/api/projects", json=payload)
return body["result"]["id"]
def generate(self, url, json, *, timeout=120, expect=202, task_only=False):
"""发起一次平台资产生成,并**等它真的跑完**,返回生成出来的素材字典。
接口只登记任务并立刻返 202(同步等出图那一版会被反代 60s 读超时打断),
所以自检不能再直接读 POST 响应体:必须等 inline 线程池跑空、
再回读任务行,否则断言读到的是「排队中」的空结果。
``expect`` 非 202 时(表单校验、去重拦截)原样返回响应体,不做等待。
"""
body = self.call("POST", url, expect=expect, json=json)
if expect != 202:
return body
task_id = body["result"]["id"]
assert execution.wait_idle(timeout), f"平台生成任务 #{task_id} 在 {timeout}s 内没有跑完"
task = self.get(f"/api/tasks/{task_id}")["task"]
assert task["status"] == "succeeded", (
f"平台生成任务 #{task_id} 未成功:{task['status']} / "
f"{task['error_code']} {task['error_message']}")
return task if task_only else task["result"]["media"]
def drain(self, project_id, timeout=600):
return self.post(f"/api/projects/{project_id}/dispatch", json={"drain": True,
"timeout": timeout})
return Api(client)
@pytest.fixture
def studio(api, db):
"""预置一个「有角色 + 有声线 + 有定妆图」的项目,返回 ids 与常用脚本。"""
project_id = api.project()
character = api.post(f"/api/projects/{project_id}/characters",
json={"name": "林澈", "role": "主角", "appearance": "银色短发,深蓝机甲",
"persona_prompt": "冷静果决的星舰领航员", "voice_profile": "低沉男声"})["result"]["id"]
voice = api.post(f"/api/projects/{project_id}/voices",
json={"name": "低沉男声", "provider": "mock", "provider_voice_id": "v-low",
"gender": "male", "is_default": True})["result"]["id"]
api.patch(f"/api/projects/{project_id}/characters/{character}", json={"default_voice_id": voice})
api.post(f"/api/projects/{project_id}/characters/{character}/assets",
files={"file": ("portrait.png", _make_image_png(480, 270, "0x224466"), "image/png")},
data={"kind": "portrait", "name": "林澈定妆图"})
api.post(f"/api/projects/{project_id}/scenes",
json={"name": "星舰舰桥", "description": "环形舰桥,全息星图", "lighting": "冷蓝", "prompt": "星舰舰桥内部"})
api.call("PATCH", f"/api/projects/{project_id}/voices/{voice}", json={})
return {"project_id": project_id, "character_id": character, "voice_id": voice,
"scene_name": "星舰舰桥"}
SCRIPT = """# 镜头 1:启航
画面: 星舰舰桥,林澈凝视全息星图
景别: 中景
运镜: 推镜
时长: 2
台词: 我们出发吧
说话人: 林澈
出镜角色: 林澈
场景: 星舰舰桥
关键帧: s01.png
# 镜头 2:跃迁
画面: 星舰跃入蓝色虫洞
景别: 大远景
运镜: 拉镜
时长: 2
台词: 前方就是目标
说话人: 林澈
出镜角色: 林澈
关键帧: s02.png
"""
@pytest.fixture
def script_text() -> str:
return SCRIPT
@pytest.fixture
def make_image_png():
"""给用例提供「造真实可解码 PNG」的能力。"""
return _make_image_png
@pytest.fixture
def media_root() -> Path:
return MEDIA_ROOT
def absolute(relative_path: str):
return storage.to_absolute(relative_path)
# ------------------------------------------------- LLM/图像模型替身(供调度类用例复用)
from app.models import ModelSpec # noqa: E402
from app.providers import registry as model_registry # noqa: E402
from app.providers.base import (AssetGenRequest, AssetGenResult, FatalProviderError, # noqa: E402
TextProviderAdapter)
# ---------------------------------------------------------------- LLM 调度替身
# 多个用例文件都要「装一个会按脚本回 JSON 的 LLM / 一个能真出图的图像模型」,
# fixture 放这里,避免每个文件复制一份。
class FakeOrchestratorLLM(TextProviderAdapter):
@property
def name(self) -> str:
return "fake_orchestrator_llm"
def is_available(self, config: dict | None = None) -> bool:
return bool((config or {}).get("response"))
def generate(self, request: AssetGenRequest, config: dict | None = None) -> AssetGenResult:
response = str((config or {}).get("response") or "")
if not response:
raise FatalProviderError("测试 LLM 未配置响应", code="missing_test_response")
return AssetGenResult(text=response, metadata={"usage": {"total_tokens": 10}})
@pytest.fixture
def install_image(db, make_image_png):
"""注册一个「非 mock 命名」的真实图像模型:调度器禁止回退 mock,测试要有可用替身。"""
from app.providers.base import ImageProviderAdapter
class FakeStudioImage(ImageProviderAdapter):
@property
def name(self):
return "fake_studio_image"
def is_available(self, config: dict | None = None) -> bool:
return True
def generate(self, request, config: dict | None = None):
from pathlib import Path as _Path
from app.providers.base import AssetGenResult
target = _Path(request.output_path or "fake-image.png")
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(make_image_png(480, 270, "0x2f6d4f"))
return AssetGenResult(file_path=str(target), metadata={"model": "fake-studio-image"})
model_registry.register_image_adapter("fake_studio_image", FakeStudioImage())
def install() -> None:
db.query(ModelSpec).filter(
ModelSpec.media_type == "image",
ModelSpec.provider == "fake",
ModelSpec.model == "studio-image",
).delete()
db.query(ModelSpec).filter(ModelSpec.media_type == "image",
ModelSpec.is_default.is_(True)).update({"is_default": False})
db.add(ModelSpec(provider="fake", model="studio-image", display_name="测试图像模型",
media_type="image", adapter="fake_studio_image", enabled=True,
is_default=True, modes=["t2i", "edit"], resolutions=["1024p"],
aspect_ratios=["16:9", "9:16"], max_reference_images=2))
db.commit()
return install
@pytest.fixture
def install_llm(db):
model_registry.register_llm_adapter("fake_orchestrator_llm", FakeOrchestratorLLM())
db.query(ModelSpec).filter(ModelSpec.media_type == "llm").update({"is_default": False})
db.commit()
def install(response: dict | str) -> None:
text = response if isinstance(response, str) else json.dumps(response, ensure_ascii=False)
db.query(ModelSpec).filter(
ModelSpec.media_type == "llm",
ModelSpec.provider == "fake",
ModelSpec.model == "orchestrator",
).delete()
db.add(ModelSpec(
provider="fake",
model="orchestrator",
display_name="测试调度 LLM",
media_type="llm",
adapter="fake_orchestrator_llm",
enabled=True,
is_default=True,
modes=["chat", "prompt"],
config_json={"response": text},
))
db.commit()
return install