fix: verify and preview generated test images

This commit is contained in:
Codex
2026-08-02 00:05:13 +08:00
parent 922f8f4f2a
commit 0eb2a86f36
7 changed files with 195 additions and 17 deletions
+60 -1
View File
@@ -1,7 +1,11 @@
import pytest
from app.integrations.openai_compatible import OpenAICompatibleGateway
from app.runtime_settings import RuntimeSettingsTestResult, test_model_pool_item as run_model_pool_test
from app.runtime_settings import (
RuntimeSettingsTestResult,
_verify_generated_image,
test_model_pool_item as run_model_pool_test,
)
def image_values(profile: str, model_id: str) -> dict:
@@ -175,3 +179,58 @@ async def test_generation_endpoint_records_definitive_model_result(monkeypatch)
assert [item.target for item in store.recorded] == ["generation:image", "model:image"]
assert store.recorded[1].ok is True
assert "真实生图端到端验证" in store.recorded[1].message
@pytest.mark.asyncio
async def test_generated_image_url_must_download_as_real_image(monkeypatch) -> None:
png_bytes = b"\x89PNG\r\n\x1a\n" + b"x" * 2048
class FakeResponse:
content = png_bytes
headers = {"content-type": "image/png"}
def raise_for_status(self) -> None:
return None
class FakeClient:
async def __aenter__(self):
return self
async def __aexit__(self, *args) -> None:
return None
async def get(self, url: str) -> FakeResponse:
assert url == "https://cdn.example.com/verified.png"
return FakeResponse()
monkeypatch.setattr("app.runtime_settings.httpx.AsyncClient", lambda **kwargs: FakeClient())
details = await _verify_generated_image(("url", "https://cdn.example.com/verified.png"))
assert details["image_format"] == "png"
assert details["byte_size"] == len(png_bytes)
@pytest.mark.asyncio
async def test_generated_image_url_rejects_html_placeholder(monkeypatch) -> None:
class FakeResponse:
content = b"<html><body>not an image</body></html>" + b"x" * 2048
headers = {"content-type": "text/html"}
def raise_for_status(self) -> None:
return None
class FakeClient:
async def __aenter__(self):
return self
async def __aexit__(self, *args) -> None:
return None
async def get(self, url: str) -> FakeResponse:
return FakeResponse()
monkeypatch.setattr("app.runtime_settings.httpx.AsyncClient", lambda **kwargs: FakeClient())
with pytest.raises(ValueError, match="不是可识别的图片"):
await _verify_generated_image(("url", "https://cdn.example.com/not-image"))
+1 -1
View File
@@ -76,7 +76,7 @@ def test_readiness_requires_configuration_and_successful_tests(tmp_path: Path) -
)
assert store.public_response().readiness.ready is False
for target in ("infrastructure", "storage", "model:planner", "model:designer"):
for target in ("infrastructure", "storage", "model:planner", "model:designer", "generation:designer"):
store.record_test(RuntimeSettingsTestResult(target=target, ok=True, message="ok"))
assert store.public_response().readiness.ready is True