cmvr-visual/backend/tests/test_workflow_output_contract.py

226 lines
8.2 KiB
Python
Raw Normal View History

from types import SimpleNamespace
from unittest.mock import AsyncMock
from sqlalchemy import select
from app.models import Job
import pytest
@pytest.mark.parametrize("candidate", [False, True])
async def test_workflow_imports_only_final_enhanced_output(system, monkeypatch, candidate):
response = await system.client.post(
"/api/jobs",
json={
"task_type": "video.generate",
"input": {"prompt": "three-shot source"},
"parameters": {},
},
headers={"Idempotency-Key": "dual-stage-output"},
)
assert response.status_code == 202
job_id = response.json()["jobs"][0]["id"]
async with system.app.state.db.session() as session:
job = await session.get(Job, job_id)
if candidate:
job.request_key = "candidate:test"
else:
job.run_id = "run"
job.step_key = "shot_1"
job.upstream_id = "gpu-result"
job.upstream_result = {
"outputs": [{"path": "native.mp4"}, {"path": "enhanced.mp4"}],
"metadata": {"final_output_indices": [1]},
}
await session.commit()
imported = []
async def import_one(db, settings, job, path, index, **kwargs):
imported.append(index)
return SimpleNamespace(kind="video")
monkeypatch.setattr("app.services.orchestration.coordinator.import_output", import_one)
coordinator = system.app.state.coordinator
monkeypatch.setattr(coordinator, "_complete", AsyncMock())
await coordinator._collect_gpu(job)
assert imported == [1]
assert system.provider.download_count == 1
@pytest.mark.parametrize(
"run_kind,upscale", [("workflow_auto", False), ("story_auto", True), ("legacy", False)]
)
async def test_real_short_auto_video_import_respects_frozen_native_or_upscaled_profile(
system, monkeypatch, run_kind, upscale
):
import shutil
from app.adapters.media import run_process, inspect_video_track
from app.models import WorkflowDraft, WorkflowRun, User
from app.services.assets import import_output
from pathlib import Path
ffmpeg = shutil.which(system.settings.ffmpeg_bin)
ffprobe = shutil.which(system.settings.ffprobe_bin)
local = Path(__file__).resolve().parents[2] / ".local" / "tools" / "ffmpeg"
ffmpeg = ffmpeg or next((str(p) for p in local.glob("*/bin/ffmpeg.exe")), None)
ffprobe = ffprobe or next((str(p) for p in local.glob("*/bin/ffprobe.exe")), None)
if not ffmpeg or not ffprobe:
pytest.skip("FFmpeg required")
system.settings.ffmpeg_bin = ffmpeg
system.settings.ffprobe_bin = ffprobe
async with system.app.state.db.session() as s:
owner = await s.scalar(select(User))
draft = WorkflowDraft(owner_id=owner.id, name="import boundary", spec={})
s.add(draft)
await s.flush()
run = WorkflowRun(
owner_id=owner.id,
draft_id=draft.id,
draft_revision=1,
spec_snapshot={},
spec_hash="a" * 64,
run_kind=run_kind,
)
s.add(run)
await s.flush()
job = Job(
owner_id=owner.id,
task_type="video.generate",
request_hash="a" * 64,
upstream_key="test:" + run.id,
run_id=run.id,
step_key="video_0001",
state="collecting",
phase="collecting",
input_json={"prompt": "mock"},
parameters={
"num_frames": 25,
"fps": 24,
"upscale": upscale,
"width": 640 if upscale else 1280,
"height": 352 if upscale else 704,
"upscale_width": 1280 if upscale else 1920,
"upscale_height": 704 if upscale else 1056,
},
)
s.add(job)
await s.commit()
source = system.settings.job_work_root / "short.mp4"
source.parent.mkdir(parents=True, exist_ok=True)
await run_process(
[
ffmpeg,
"-nostdin",
"-v",
"error",
"-f",
"lavfi",
"-i",
"color=c=blue:s=1280x704:r=24",
"-frames:v",
"25",
"-c:v",
"libx264",
"-threads",
"1",
"-pix_fmt",
"yuv420p",
str(source),
],
timeout=20,
)
track = await inspect_video_track(source, system.settings)
assert track["frames"] == 25 and track["fps"] == 24
if run_kind == "legacy":
with pytest.raises(ValueError, match="Workflow shot"):
await import_output(system.app.state.db, system.settings, job, source, 0)
else:
asset = await import_output(system.app.state.db, system.settings, job, source, 0)
assert asset.kind == "video" and (asset.width, asset.height) == (1280, 704)
assert asset.duration_seconds < 1.1
replay = await import_output(system.app.state.db, system.settings, job, source, 0)
assert replay.id == asset.id
async with system.app.state.db.session() as session:
saved = await session.get(Job, job.id)
saved.upstream_id = "existing-native-video"
saved.upstream_result = {
"outputs": [{"path": "native.mp4"}],
"metadata": {"final_output_indices": [0]},
}
await session.commit()
# 断线后已经入库的短源也必须使用冻结的auto运行类型,不能重新套旧120帧下限。
coordinator = system.app.state.coordinator
complete = AsyncMock()
monkeypatch.setattr(coordinator, "_complete", complete)
await coordinator._collect_gpu(saved)
complete.assert_awaited_once()
async def test_real_h3_standalone_imports_native_and_enhanced_outputs_and_replays(system):
import shutil
from pathlib import Path
from app.adapters.media import run_process
from app.services.assets import import_output
local = Path(__file__).resolve().parents[2] / ".local" / "tools" / "ffmpeg"
ffmpeg = shutil.which(system.settings.ffmpeg_bin) or next(
(str(p) for p in local.glob("*/bin/ffmpeg.exe")), None
)
ffprobe = shutil.which(system.settings.ffprobe_bin) or next(
(str(p) for p in local.glob("*/bin/ffprobe.exe")), None
)
if not ffmpeg or not ffprobe:
pytest.skip("FFmpeg required for real two-output H3 import")
system.settings.ffmpeg_bin, system.settings.ffprobe_bin = ffmpeg, ffprobe
response = await system.client.post(
"/api/jobs",
json={
"task_type": "video.generate",
"input": {"prompt": "two stage H3"},
"parameters": {
"profile": "minimax-h3-fl2va-bf16-v1",
"num_frames": 25,
"upscale": True,
},
},
)
assert response.status_code == 202, response.text
async with system.app.state.db.session() as session:
job = await session.get(Job, response.json()["jobs"][0]["id"])
sources = []
for index, size in enumerate(("1344x768", "1920x1080")):
source = system.settings.job_work_root / f"h3-stage-{index}.mp4"
source.parent.mkdir(parents=True, exist_ok=True)
await run_process(
[
ffmpeg,
"-nostdin",
"-v",
"error",
"-f",
"lavfi",
"-i",
f"color=c=blue:s={size}:r=24",
"-frames:v",
"25",
"-c:v",
"libx264",
"-threads",
"1",
"-pix_fmt",
"yuv420p",
str(source),
],
timeout=20,
)
sources.append(source)
imported = [
await import_output(system.app.state.db, system.settings, job, source, index)
for index, source in enumerate(sources)
]
assert [(a.width, a.height) for a in imported] == [(1344, 768), (1920, 1080)]
assert [a.source_output_index for a in imported] == [0, 1]
replay = [
await import_output(system.app.state.db, system.settings, job, source, index)
for index, source in enumerate(sources)
]
assert [a.id for a in replay] == [a.id for a in imported]