baodan/tests/poster_render_validation_test.py

78 lines
3.4 KiB
Python
Raw Normal View History

2026-07-31 21:42:32 +08:00
"""最终海报文件校验与画布分流回归测试。"""
import io
import sys
from pathlib import Path
import pytest
from PIL import Image
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "api"))
def _png(width: int, height: int) -> bytes:
buffer = io.BytesIO()
Image.new("RGB", (width, height), "white").save(buffer, format="PNG")
return buffer.getvalue()
def test_single_rendered_png_requires_exact_output_dimensions():
from insurance.poster.render_validation import validate_rendered_png
result = validate_rendered_png(_png(1024, 1536), "single_2_3")
assert result == {"width": 1024, "height": 1536, "format": "PNG"}
def test_wrong_dimensions_and_corrupt_png_are_rejected():
from insurance.poster.render_validation import PosterRenderValidationError, validate_rendered_png
with pytest.raises(PosterRenderValidationError, match="尺寸不匹配"):
validate_rendered_png(_png(1024, 1400), "single_2_3")
with pytest.raises(PosterRenderValidationError, match="无法解码"):
validate_rendered_png(b"\x89PNG\r\n\x1a\nnot-a-real-png", "single_2_3")
def test_long_rendered_png_requires_exact_width_and_safe_height():
from insurance.poster.render_validation import PosterRenderValidationError, validate_rendered_png
assert validate_rendered_png(_png(1242, 3600), "long_1242_auto")["height"] == 3600
with pytest.raises(PosterRenderValidationError, match="宽度不匹配"):
validate_rendered_png(_png(1080, 3600), "long_1242_auto")
def test_custom_long_height_requires_exact_output_height():
from insurance.poster.render_validation import PosterRenderValidationError, validate_rendered_png
result = validate_rendered_png(_png(1242, 4500), "long_1242_auto", expected_height=4500)
assert result["height"] == 4500
with pytest.raises(PosterRenderValidationError, match="自定义高度"):
validate_rendered_png(_png(1242, 4600), "long_1242_auto", expected_height=4500)
2026-07-31 21:42:32 +08:00
def test_single_and_long_use_separate_canvas_components():
stage = (ROOT / "frontend/src/components/poster/workspace/PosterStage.vue").read_text(encoding="utf-8")
single_canvas = ROOT / "frontend/src/components/poster/single/PosterSingleCanvas.vue"
assert single_canvas.exists()
assert "<PosterSingleCanvas" in stage
assert "v-if=\"draft.outputMode === 'single'\"" in stage
assert "<PosterHtmlCanvas" in stage
def test_long_editor_exposes_custom_height_without_silent_crop():
creative = (ROOT / "frontend/src/components/poster/workspace/PosterCreativePanel.vue").read_text(encoding="utf-8")
stage = (ROOT / "frontend/src/components/poster/workspace/PosterStage.vue").read_text(encoding="utf-8")
exporter = (ROOT / "frontend/src/utils/poster-exporter.ts").read_text(encoding="utf-8")
assert "自定义高度" in creative
assert ':custom-output-height="draft.longHeightMode === \'custom\' ? draft.customLongHeight : null"' in stage
assert "当前内容超出自定义高度" in exporter
2026-07-31 21:42:32 +08:00
def test_background_generation_does_not_claim_final_poster_is_done():
celery_source = (ROOT / "api/insurance/generation/celery_tasks.py").read_text(encoding="utf-8")
service_source = (ROOT / "api/insurance/poster/service.py").read_text(encoding="utf-8")
assert 'record.task_status = "background_ready"' in celery_source
assert 'record.task_status = "done"' in service_source