"""海报输出格式契约测试。""" 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")