2026-11-10 23:56:50 +08:00
|
|
|
import copy
|
|
|
|
|
import pytest
|
|
|
|
|
from pydantic import ValidationError
|
2026-11-14 14:07:26 +08:00
|
|
|
from app.domain.timeline import WorkflowSpec, compile_timeline
|
2026-11-10 23:56:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def spec():
|
2026-11-14 14:07:26 +08:00
|
|
|
return {
|
|
|
|
|
"shots": [
|
|
|
|
|
{"key": f"shot_{i}", "prompt": f"镜头{i}", "parameters": {"seed": i}}
|
|
|
|
|
for i in range(1, 4)
|
|
|
|
|
],
|
|
|
|
|
"profile": "landscape_1080_v1",
|
|
|
|
|
}
|
2026-11-10 23:56:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_exact_keys_and_unknown_fields():
|
2026-11-14 14:07:26 +08:00
|
|
|
body = spec()
|
|
|
|
|
body["shots"][1]["key"] = "shot_1"
|
|
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
|
WorkflowSpec.model_validate(body)
|
|
|
|
|
body = spec()
|
|
|
|
|
body["render"] = {"filter": "movie=http://bad"}
|
|
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
|
WorkflowSpec.model_validate(body)
|
|
|
|
|
body = spec()
|
|
|
|
|
body["shots"][0]["parameters"]["width"] = 256
|
|
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
|
WorkflowSpec.model_validate(body)
|
2026-11-10 23:56:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_freezes_profiles_and_frame_boundaries():
|
2026-11-14 14:07:26 +08:00
|
|
|
body = spec()
|
|
|
|
|
body["render"] = {"subtitles": [{"start_frame": 119, "end_frame": 241, "text": "跨镜头"}]}
|
2026-11-10 23:56:50 +08:00
|
|
|
plan = compile_timeline(body, {})
|
2026-11-14 14:07:26 +08:00
|
|
|
assert plan.generation_parameters["width"] == 1280
|
|
|
|
|
assert plan.generation_parameters["upscale_height"] == 1056
|
|
|
|
|
assert plan.canvas == (1920, 1080)
|
2026-11-10 23:56:50 +08:00
|
|
|
assert plan.frames == 360 and plan.clip_frames == 120
|
2026-11-14 14:07:26 +08:00
|
|
|
body["profile"] = "portrait_1080_v1"
|
2026-11-10 23:56:50 +08:00
|
|
|
plan = compile_timeline(body, {})
|
2026-11-14 14:07:26 +08:00
|
|
|
assert plan.canvas == (1080, 1920)
|
|
|
|
|
assert plan.generation_parameters["upscale_width"] == 1056
|
2026-11-10 23:56:50 +08:00
|
|
|
|
|
|
|
|
|
2026-11-14 14:07:26 +08:00
|
|
|
@pytest.mark.parametrize("offset,valid", [(120, True), (121, False)])
|
2026-11-10 23:56:50 +08:00
|
|
|
def test_explicit_audio_cut_and_offset(offset, valid):
|
2026-11-14 14:07:26 +08:00
|
|
|
body = spec()
|
|
|
|
|
body["render"] = {
|
|
|
|
|
"audio": {
|
|
|
|
|
"asset_id": "audio",
|
|
|
|
|
"source_start_ms": 0,
|
|
|
|
|
"source_end_ms": 10000,
|
|
|
|
|
"offset_frame": offset,
|
|
|
|
|
"short_policy": "silence",
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-10 23:56:50 +08:00
|
|
|
if valid:
|
2026-11-14 14:07:26 +08:00
|
|
|
compile_timeline(body, {"audio": {"kind": "audio", "duration_seconds": 10}})
|
2026-11-10 23:56:50 +08:00
|
|
|
else:
|
2026-11-14 14:07:26 +08:00
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
|
WorkflowSpec.model_validate(body)
|
2026-11-10 23:56:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_audio_source_bounds_and_subtitle_controls():
|
2026-11-14 14:07:26 +08:00
|
|
|
body = spec()
|
|
|
|
|
body["render"] = {
|
|
|
|
|
"audio": {
|
|
|
|
|
"asset_id": "audio",
|
|
|
|
|
"source_start_ms": 3000,
|
|
|
|
|
"source_end_ms": 18000,
|
|
|
|
|
"offset_frame": 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
compile_timeline(body, {"audio": {"kind": "audio", "duration_seconds": 17}})
|
|
|
|
|
compile_timeline(body, {"audio": {"kind": "audio", "duration_seconds": 18}})
|
|
|
|
|
body = spec()
|
|
|
|
|
body["render"] = {"subtitles": [{"start_frame": 0, "end_frame": 360, "text": r"{\an8}bad"}]}
|
|
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
|
WorkflowSpec.model_validate(body)
|
|
|
|
|
|
2026-11-10 23:56:50 +08:00
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-11-14 14:07:26 +08:00
|
|
|
async def test_media_short_source_rejected_before_encoding(tmp_path, monkeypatch):
|
2026-11-10 23:56:50 +08:00
|
|
|
from types import SimpleNamespace
|
2026-11-14 14:07:26 +08:00
|
|
|
from app.adapters import media
|
|
|
|
|
from app.adapters.media import MediaError
|
|
|
|
|
|
|
|
|
|
plan = compile_timeline(spec(), {})
|
|
|
|
|
paths = []
|
2026-11-10 23:56:50 +08:00
|
|
|
for i in range(3):
|
2026-11-14 14:07:26 +08:00
|
|
|
p = tmp_path / f"{i}.mp4"
|
|
|
|
|
p.write_bytes(b"not decoded")
|
|
|
|
|
paths.append(p)
|
|
|
|
|
|
|
|
|
|
async def inspect(*args):
|
|
|
|
|
return {"frames": 119, "fps": 24, "width": 1920, "height": 1056, "duration": 119 / 24}
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(media, "inspect_video_track", inspect)
|
|
|
|
|
with pytest.raises(MediaError, match="120"):
|
|
|
|
|
await media.compose_workflow(
|
|
|
|
|
plan,
|
|
|
|
|
tmp_path / "work",
|
|
|
|
|
None,
|
|
|
|
|
settings=SimpleNamespace(workspace_root=tmp_path),
|
|
|
|
|
sources=paths,
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-10 23:56:50 +08:00
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-11-14 14:07:26 +08:00
|
|
|
async def test_media_fixed_frame_argv_and_audio_offset(tmp_path, monkeypatch):
|
2026-11-10 23:56:50 +08:00
|
|
|
from types import SimpleNamespace
|
2026-11-14 14:07:26 +08:00
|
|
|
from app.adapters import media
|
|
|
|
|
|
|
|
|
|
body = spec()
|
|
|
|
|
body["render"] = {
|
|
|
|
|
"audio": {
|
|
|
|
|
"asset_id": "audio",
|
|
|
|
|
"source_start_ms": 3000,
|
|
|
|
|
"source_end_ms": 13000,
|
|
|
|
|
"offset_frame": 120,
|
|
|
|
|
},
|
|
|
|
|
"subtitles": [{"start_frame": 119, "end_frame": 241, "text": "镜头"}],
|
|
|
|
|
}
|
|
|
|
|
plan = compile_timeline(body, {"audio": {"kind": "audio", "duration_seconds": 13}})
|
|
|
|
|
paths = []
|
2026-11-10 23:56:50 +08:00
|
|
|
for i in range(3):
|
2026-11-14 14:07:26 +08:00
|
|
|
p = tmp_path / f"{i}.mp4"
|
|
|
|
|
p.write_bytes(b"x")
|
|
|
|
|
paths.append(p)
|
|
|
|
|
audio = tmp_path / "audio.wav"
|
|
|
|
|
audio.write_bytes(b"x")
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
|
|
async def track(path, settings):
|
|
|
|
|
return {
|
|
|
|
|
"frames": 360 if path.name == "workflow.mp4" else 121,
|
|
|
|
|
"fps": 24,
|
|
|
|
|
"width": 1920,
|
|
|
|
|
"height": 1080 if path.name == "workflow.mp4" else 1056,
|
|
|
|
|
"duration": 15,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async def inspect(*args):
|
|
|
|
|
return {"kind": "audio", "duration_seconds": 13}
|
|
|
|
|
|
|
|
|
|
async def process(argv, **kwargs):
|
|
|
|
|
calls.append(argv)
|
|
|
|
|
__import__("pathlib").Path(argv[-1]).write_bytes(b"x")
|
|
|
|
|
return b""
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(media, "inspect_video_track", track)
|
|
|
|
|
monkeypatch.setattr(media, "inspect_media", inspect)
|
|
|
|
|
monkeypatch.setattr(media, "run_process", process)
|
|
|
|
|
result = await media.compose_workflow(
|
|
|
|
|
plan,
|
|
|
|
|
tmp_path / "work",
|
|
|
|
|
None,
|
|
|
|
|
settings=SimpleNamespace(
|
|
|
|
|
workspace_root=tmp_path, ffmpeg_bin="ffmpeg", max_output_bytes=1000000
|
|
|
|
|
),
|
|
|
|
|
sources=paths,
|
|
|
|
|
audio=audio,
|
|
|
|
|
)
|
|
|
|
|
assert len(calls) == 4
|
|
|
|
|
assert all("120" == c[c.index("-frames:v") + 1] for c in calls[:3])
|
|
|
|
|
assert calls[-1][calls[-1].index("-frames:v") + 1] == "360"
|
|
|
|
|
assert any(
|
|
|
|
|
"adelay=5000" in value and "atrim=start=3.000:end=13.000" in value for value in calls[-1]
|
|
|
|
|
)
|
|
|
|
|
assert result.result["frames"] == 360
|
|
|
|
|
|
2026-11-10 23:56:50 +08:00
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-11-14 14:07:26 +08:00
|
|
|
async def test_upstream_capabilities_cache_and_control_envelope(monkeypatch, tmp_path):
|
2026-11-10 23:56:50 +08:00
|
|
|
from types import SimpleNamespace
|
2026-11-14 14:07:26 +08:00
|
|
|
from app.adapters.provider.client import Provider
|
|
|
|
|
|
|
|
|
|
p = Provider(
|
|
|
|
|
SimpleNamespace(
|
|
|
|
|
request_timeout_seconds=5, inference_base_url="http://inference", inference_api_key=""
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
calls = []
|
|
|
|
|
|
|
|
|
|
async def call(method, url, **kwargs):
|
|
|
|
|
calls.append((method, url, kwargs))
|
|
|
|
|
return (
|
|
|
|
|
{"protocol_version": 2, "features": {"scheduling": True, "admission_pause": True}}
|
|
|
|
|
if method == "GET"
|
|
|
|
|
else {"desired_state": "pause"}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(p, "_json", call)
|
2026-11-10 23:56:50 +08:00
|
|
|
try:
|
2026-11-14 14:07:26 +08:00
|
|
|
a = await p.upstream_capabilities()
|
|
|
|
|
b = await p.upstream_capabilities()
|
|
|
|
|
assert a["admission_policy"] is True and a == b and len(calls) == 1
|
|
|
|
|
await p.control(
|
|
|
|
|
"a" * 32, "pause", "cmd-1", expected_version=2, expected_execution_id="e" * 32
|
|
|
|
|
)
|
|
|
|
|
assert calls[-1][0] == "PUT" and calls[-1][2]["payload"] == {
|
|
|
|
|
"desired_state": "pause",
|
|
|
|
|
"command_id": "cmd-1",
|
|
|
|
|
"expected_version": 2,
|
|
|
|
|
"expected_execution_id": "e" * 32,
|
|
|
|
|
}
|
|
|
|
|
finally:
|
|
|
|
|
await p.aclose()
|
|
|
|
|
|
2026-11-10 23:56:50 +08:00
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_phase_callback_is_context_local():
|
|
|
|
|
import asyncio
|
2026-11-14 14:07:26 +08:00
|
|
|
from app.domain.execution import current_phase_callback, enter_phase
|
|
|
|
|
|
|
|
|
|
seen = []
|
|
|
|
|
|
2026-11-10 23:56:50 +08:00
|
|
|
async def one(name):
|
2026-11-14 14:07:26 +08:00
|
|
|
async def hook(key, pool):
|
|
|
|
|
seen.append((name, key, pool))
|
|
|
|
|
|
|
|
|
|
token = current_phase_callback.set(hook)
|
|
|
|
|
try:
|
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
|
await enter_phase("sample_frames", "cpu_media")
|
|
|
|
|
finally:
|
|
|
|
|
current_phase_callback.reset(token)
|
|
|
|
|
|
|
|
|
|
await asyncio.gather(one("first"), one("second"))
|
|
|
|
|
assert sorted(seen) == [
|
|
|
|
|
("first", "sample_frames", "cpu_media"),
|
|
|
|
|
("second", "sample_frames", "cpu_media"),
|
|
|
|
|
]
|