Restructure backend and frontend modules; add story, library, and editor schema migrations. Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews. Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
187 lines
6.7 KiB
Python
187 lines
6.7 KiB
Python
"""真实文件/HTTP边界覆盖故事多图与CPU首帧,测试不加载GPU模型。"""
|
||
|
||
from hashlib import sha256
|
||
|
||
import httpx
|
||
from PIL import Image
|
||
import pytest
|
||
|
||
from app.adapters.provider.client import Provider
|
||
from app.adapters.provider.contracts import capabilities, validate_arguments
|
||
from app.core.config import Settings
|
||
from app.domain.errors import MediaError
|
||
from app.domain.tasks import CPU_TYPES, GPU_TYPES
|
||
|
||
|
||
@pytest.fixture
|
||
def configuration(tmp_path):
|
||
settings = Settings(
|
||
mode="test",
|
||
database_url="sqlite+aiosqlite:///:memory:",
|
||
workspace_root=tmp_path,
|
||
inference_api_key_file=None,
|
||
)
|
||
settings.asset_root.mkdir(parents=True)
|
||
settings.job_work_root.mkdir(parents=True)
|
||
for name in ("attempt", "missing", "contain", "cover"):
|
||
(settings.job_work_root / name).mkdir()
|
||
return settings
|
||
|
||
|
||
def test_story_tasks_registered_but_reference_is_not_advertised_before_certification(configuration):
|
||
assert "image.reference.generate" in GPU_TYPES
|
||
assert "image.prepare_frame" in CPU_TYPES
|
||
info = {item["task_type"]: item for item in capabilities(configuration)}
|
||
assert info["image.reference.generate"]["available"] is False
|
||
assert info["image.reference.generate"]["defaults"]["profile"] == "qwen-image-edit-2511-v1"
|
||
assert info["image.reference.generate"]["constraints"]["max_reference_images"] == 3
|
||
assert info["image.prepare_frame"]["available"] is True
|
||
with pytest.raises(MediaError):
|
||
validate_arguments(
|
||
"image.reference.generate",
|
||
{"prompt": "hello", "reference_asset_ids": ["a"]},
|
||
{},
|
||
configuration,
|
||
)
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_reference_uploads_all_images_in_order_and_has_separate_cfg(configuration):
|
||
configuration.reference_generation_enabled = True
|
||
configuration.inference_remote_files = True
|
||
files = {}
|
||
for name, color in (("first", "red"), ("second", "blue")):
|
||
path = configuration.asset_root / (name + ".png")
|
||
Image.new("RGB", (64, 48), color).save(path)
|
||
files[name] = path
|
||
uploads = []
|
||
|
||
def handle(request):
|
||
if request.url.path == "/capabilities":
|
||
return httpx.Response(
|
||
200,
|
||
json={
|
||
"reference_generation": {
|
||
"available": True,
|
||
"profile": "qwen-image-edit-2511-v1",
|
||
"max_reference_images": 3,
|
||
}
|
||
},
|
||
)
|
||
digest = sha256(request.content).hexdigest()
|
||
uploads.append(digest)
|
||
return httpx.Response(
|
||
201,
|
||
json={
|
||
"path": "/workspace/inputs/llm-services/" + digest + ".png",
|
||
"sha256": digest,
|
||
"size_bytes": len(request.content),
|
||
},
|
||
)
|
||
|
||
provider = Provider(configuration)
|
||
await provider.client.aclose()
|
||
provider.client = httpx.AsyncClient(transport=httpx.MockTransport(handle))
|
||
try:
|
||
payload = await provider.prepare_gpu_payload(
|
||
"image.reference.generate",
|
||
{
|
||
"prompt": "keep first character in second scene",
|
||
"reference_asset_ids": ["second", "first"],
|
||
},
|
||
{},
|
||
files,
|
||
configuration.job_work_root / "attempt",
|
||
)
|
||
assert uploads == [
|
||
sha256(files[name].read_bytes()).hexdigest() for name in ("second", "first")
|
||
]
|
||
assert payload["input"]["image_paths"] == [
|
||
"/workspace/inputs/llm-services/" + digest + ".png" for digest in uploads
|
||
]
|
||
assert payload["parameters"]["guidance_scale"] == 1.0
|
||
assert payload["parameters"]["true_cfg_scale"] == 4.0
|
||
assert (payload["parameters"]["width"], payload["parameters"]["height"]) == (1280, 704)
|
||
uploads.clear()
|
||
with pytest.raises(MediaError):
|
||
await provider.prepare_gpu_payload(
|
||
"image.reference.generate",
|
||
{"prompt": "test", "reference_asset_ids": ["first", "missing"]},
|
||
{},
|
||
files,
|
||
configuration.job_work_root / "missing",
|
||
)
|
||
assert uploads == [] # 缺一张不能部分提交或降级文生图。
|
||
finally:
|
||
await provider.aclose()
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
@pytest.mark.parametrize(
|
||
"remote",
|
||
[
|
||
{},
|
||
{"available": False},
|
||
{"available": True, "profile": "qwen-image-edit-2509-v1", "max_reference_images": 3},
|
||
{"available": True, "profile": "qwen-image-edit-2511-v1", "max_reference_images": 0},
|
||
],
|
||
)
|
||
async def test_upstream_must_certify_the_same_profile_before_upload(configuration, remote):
|
||
configuration.reference_generation_enabled = True
|
||
provider = Provider(configuration)
|
||
await provider.client.aclose()
|
||
provider.client = httpx.AsyncClient(
|
||
transport=httpx.MockTransport(
|
||
lambda request: httpx.Response(200, json={"reference_generation": remote})
|
||
)
|
||
)
|
||
try:
|
||
with pytest.raises(MediaError) as error:
|
||
await provider.prepare_gpu_payload(
|
||
"image.reference.generate",
|
||
{"prompt": "a", "reference_asset_ids": ["missing"]},
|
||
{},
|
||
{},
|
||
configuration.job_work_root / "attempt",
|
||
)
|
||
assert error.value.code == "capability_unavailable"
|
||
finally:
|
||
await provider.aclose()
|
||
|
||
|
||
@pytest.mark.parametrize("ids", [[], ["a"] * 4, "a", [1], ["a", ""]])
|
||
def test_reference_rejects_missing_invalid_and_excess_images(configuration, ids):
|
||
configuration.reference_generation_enabled = True
|
||
with pytest.raises(MediaError):
|
||
validate_arguments(
|
||
"image.reference.generate",
|
||
{"prompt": "x", "reference_asset_ids": ids},
|
||
{},
|
||
configuration,
|
||
)
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
@pytest.mark.parametrize("fit", ["contain", "cover"])
|
||
async def test_prepare_frame_writes_new_exact_rgb_image_without_changing_source(configuration, fit):
|
||
source = configuration.asset_root / "source.png"
|
||
Image.new("RGBA", (20, 40), (255, 0, 0, 128)).save(source)
|
||
original = source.read_bytes()
|
||
provider = Provider(configuration)
|
||
try:
|
||
result = await provider.run_cpu(
|
||
"image.prepare_frame",
|
||
{"image_asset_id": "source"},
|
||
{"width": 1280, "height": 704, "fit": fit},
|
||
{"source": source},
|
||
configuration.job_work_root / fit,
|
||
)
|
||
assert result.outputs[0] != source
|
||
with Image.open(result.outputs[0]) as output:
|
||
assert output.size == (1280, 704)
|
||
assert output.mode == "RGB"
|
||
assert result.metadata["fit"] == fit
|
||
assert source.read_bytes() == original
|
||
finally:
|
||
await provider.aclose()
|