baodan/tests/poster_format_registry_test.py
wsb1224 e1770be4cc 现在选择“长图海报”后,在“海报尺寸”下方可以切换:
自动高度
自定义高度:1920–30000px,默认 4500px
自定义高度会同步影响画布预览、任务快照、renderDocument、历史恢复和最终 PNG。若内容超过指定高度,系统会阻止导出并提示最低所需高度,不会静默裁掉底部内容。
关键修改:
[PosterCreativePanel.vue (line 51)](D:/work/code/python/coding/baodanagent/frontend/src/components/poster/workspace/PosterCreativePanel.vue:51)
[PosterHtmlCanvas.vue (line 1)](D:/work/code/python/coding/baodanagent/frontend/src/components/poster/long/PosterHtmlCanvas.vue:1)
[poster-exporter.ts (line 95)](D:/work/code/python/coding/baodanagent/frontend/src/utils/poster-exporter.ts:95)
[format_registry.py (line 17)](D:/work/code/python/coding/baodanagent/api/insurance/poster/format_registry.py:17)
[render_validation.py (line 13)](D:/work/code/python/coding/baodanagent/api/insurance/poster/render_validation.py:13)
验证结果:33 项海报测试通过、Vue 类型检查通过、生产构建通过、UI 静态检查通过
2026-07-31 22:05:57 +08:00

97 lines
3.4 KiB
Python

"""海报输出格式契约测试。"""
import sys
from pathlib import Path
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "api"))
def test_poster_formats_have_distinct_layout_and_output_dimensions():
from insurance.poster.format_registry import list_poster_formats
formats = list_poster_formats()
assert [item["id"] for item in formats] == [
"single_2_3",
"single_9_16",
"long_1242_auto",
]
assert formats[0]["layout"] == {"width": 512, "height": 768}
assert formats[0]["output"] == {"width": 1024, "height": 1536}
assert formats[2]["layout"] == {"width": 414, "height": None}
assert formats[2]["output"] == {"width": 1242, "height": None}
def test_resolve_poster_format_rejects_mode_mismatch_and_unknown_values():
from insurance.poster.format_registry import PosterFormatError, resolve_poster_format
with pytest.raises(PosterFormatError, match="不支持长图"):
resolve_poster_format(format_id="single_2_3", output_mode="long")
with pytest.raises(PosterFormatError, match="不支持的海报格式"):
resolve_poster_format(format_id="custom_999", output_mode="single")
with pytest.raises(PosterFormatError, match="不支持的海报尺寸"):
resolve_poster_format(legacy_size="2480x3508", output_mode="single")
with pytest.raises(PosterFormatError, match="不支持的输出模式"):
resolve_poster_format(output_mode="square")
def test_legacy_sizes_map_to_safe_formats_without_silent_shape_changes():
from insurance.poster.format_registry import resolve_poster_format
assert resolve_poster_format(
legacy_size="1024x1792",
output_mode="single",
)["id"] == "single_2_3"
assert resolve_poster_format(
legacy_size="1080x1920",
output_mode="single",
)["id"] == "single_9_16"
assert resolve_poster_format(
legacy_size="1080x4320",
output_mode="long",
)["id"] == "long_1242_auto"
def test_background_asset_size_is_provider_size_not_final_poster_size():
from insurance.poster.format_registry import resolve_poster_format
long_format = resolve_poster_format(
format_id="long_1242_auto",
output_mode="long",
)
assert long_format["exportSize"] == "1242xauto"
assert long_format["backgroundAssetSize"] == "1024x1536"
def test_custom_long_height_is_bounded_and_explicit():
from insurance.poster.format_registry import PosterFormatError, resolve_custom_long_height
assert resolve_custom_long_height(4500, "long") == 4500
assert resolve_custom_long_height(None, "long") is None
with pytest.raises(PosterFormatError, match="1920"):
resolve_custom_long_height(1200, "long")
with pytest.raises(PosterFormatError, match="仅长图"):
resolve_custom_long_height(4500, "single")
def test_fallback_background_never_draws_marketing_copy(monkeypatch):
from PIL import ImageDraw
from insurance.poster.image_generator import generate_fallback
def fail_if_text_is_drawn(*_args, **_kwargs):
raise AssertionError("fallback 背景不应绘制任何文案")
monkeypatch.setattr(ImageDraw.ImageDraw, "text", fail_if_text_is_drawn)
image_bytes = generate_fallback(
{
"headline": "不应进入背景",
"body": "不应进入背景",
"call_to_action": "不应进入背景",
},
size="1024x1536",
)
assert image_bytes.startswith(b"\x89PNG\r\n\x1a\n")