379 lines
13 KiB
Python
379 lines
13 KiB
Python
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]
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
"case",
|
||
[
|
||
"valid",
|
||
"multi",
|
||
"profile",
|
||
"no_upscale",
|
||
"upscale",
|
||
"wan",
|
||
"wrong_frames",
|
||
"wrong_size",
|
||
"wrong_sha",
|
||
"fence",
|
||
"invalid_indices",
|
||
],
|
||
)
|
||
async def test_tp2_workflow_recovers_completed_single_output(system, monkeypatch, tmp_path, case):
|
||
import shutil
|
||
from app.adapters.media import run_process
|
||
from app.domain.video_generation import H3_TP2_PROFILE
|
||
from app.models import WorkflowDraft, WorkflowRun, User, Asset
|
||
from app.services.artifacts import digest_file
|
||
|
||
params = dict(profile=H3_TP2_PROFILE, upscale=False, width=1344, height=768, num_frames=145)
|
||
if case == "wan":
|
||
params.pop("profile")
|
||
if case == "no_upscale":
|
||
params.pop("upscale")
|
||
db = system.app.state.db
|
||
coord = system.app.state.coordinator
|
||
upstream = "completed-tp2"
|
||
async with db.session() as s:
|
||
owner = await s.scalar(select(User))
|
||
draft = WorkflowDraft(owner_id=owner.id, name="TP2 import", spec={})
|
||
s.add(draft)
|
||
await s.flush()
|
||
run = WorkflowRun(
|
||
owner_id=owner.id,
|
||
draft_id=draft.id,
|
||
draft_revision=1,
|
||
spec_snapshot={},
|
||
spec_hash="b" * 64,
|
||
run_kind="story_auto",
|
||
)
|
||
s.add(run)
|
||
await s.flush()
|
||
job = Job(
|
||
owner_id=owner.id,
|
||
task_type="video.generate",
|
||
input_json={"prompt": "rain"},
|
||
parameters=params,
|
||
run_id=run.id,
|
||
step_key="video_0003",
|
||
state="running",
|
||
phase="running",
|
||
upstream_id=upstream,
|
||
upstream_key="tp2-test",
|
||
request_hash="b" * 64,
|
||
)
|
||
s.add(job)
|
||
await s.commit()
|
||
jid = job.id
|
||
mp4 = tmp_path / "source.mp4"
|
||
await run_process(
|
||
[
|
||
system.settings.ffmpeg_bin,
|
||
"-v",
|
||
"error",
|
||
"-f",
|
||
"lavfi",
|
||
"-i",
|
||
f"color=s={'1280x704' if case == 'wrong_size' else '1344x768'}:r=24",
|
||
"-frames:v",
|
||
"144" if case == "wrong_frames" else "145",
|
||
"-threads",
|
||
"1",
|
||
"-pix_fmt",
|
||
"yuv420p",
|
||
str(mp4),
|
||
],
|
||
timeout=30,
|
||
)
|
||
remote = dict(
|
||
id=upstream,
|
||
execution_id=upstream + ":1",
|
||
state="succeeded",
|
||
attempt_count=1,
|
||
result={
|
||
"outputs": [{"path": "tp2.mp4"}],
|
||
"metadata": {
|
||
"profile": H3_TP2_PROFILE,
|
||
"parameters": dict(params),
|
||
"requested_frames": 145,
|
||
"native_frames": 158,
|
||
},
|
||
},
|
||
)
|
||
result = remote["result"]
|
||
if case == "multi":
|
||
result["outputs"].append({"path": "second.mp4"})
|
||
if case == "profile":
|
||
result["metadata"]["profile"] = "minimax-h3-fl2va-bf16-v1"
|
||
if case == "upscale":
|
||
result["metadata"]["parameters"]["upscale"] = True
|
||
if case == "wrong_sha":
|
||
result["outputs"][0]["sha256"] = "f" * 64
|
||
if case == "invalid_indices":
|
||
result["metadata"]["final_output_indices"] = []
|
||
await coord._update(
|
||
jid,
|
||
remote_observation=remote,
|
||
state="collecting",
|
||
phase="importing_outputs",
|
||
attempt_count=1,
|
||
upstream_result=remote["result"],
|
||
)
|
||
|
||
async def download(_, index, dst):
|
||
assert index == 0
|
||
shutil.copyfile(mp4, dst)
|
||
|
||
monkeypatch.setattr(system.provider, "download", download)
|
||
async with db.session() as s:
|
||
job = await s.get(Job, jid)
|
||
assert job.upstream_result["_visual_execution"]
|
||
if case == "fence":
|
||
job.upstream_result = {
|
||
**job.upstream_result,
|
||
"_visual_execution": {
|
||
**job.upstream_result["_visual_execution"],
|
||
"result_hash": "0" * 64,
|
||
},
|
||
}
|
||
await s.commit()
|
||
if case != "valid":
|
||
reason = {
|
||
"wrong_sha": "upstream digest",
|
||
"fence": "Stale GPU",
|
||
"wrong_frames": "frozen actual frame",
|
||
"wrong_size": "frozen actual frame",
|
||
}.get(case, "requires explicit final")
|
||
with pytest.raises(ValueError, match=reason):
|
||
await coord._collect_gpu(job)
|
||
async with db.session() as s:
|
||
assert not await s.scalar(select(Asset.id).where(Asset.source_job_id == jid))
|
||
return
|
||
await coord._collect_gpu(job)
|
||
async with db.session() as s:
|
||
assert (await s.get(Job, jid)).state == "succeeded"
|
||
asset = (await s.scalars(select(Asset).where(Asset.source_job_id == jid))).one()
|
||
assert asset.ready and asset.source_output_index == 0 and asset.sha256 == digest_file(mp4)
|