File size: 1,795 Bytes
4448e70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from PIL import Image

from src.postprocess import SIZE_PRESETS, add_badge, add_watermark, fit_canvas
from src.providers.openai_client import orientation_for, resolve_size


def _img(w=1024, h=1536, color="#888888"):
    return Image.new("RGB", (w, h), color)


def test_fit_canvas_cover_exact_size():
    out = fit_canvas(_img(), (900, 1200), "Обрезать по краям")
    assert out.size == (900, 1200)


def test_fit_canvas_contain_pads_with_background():
    out = fit_canvas(_img(1024, 1024), (1920, 1080), "Вписать целиком", "#FFFFFF")
    assert out.size == (1920, 1080)
    assert out.getpixel((5, 5)) == (255, 255, 255)  # поле залито


def test_fit_canvas_passthrough():
    src = _img(512, 512)
    assert fit_canvas(src, None).size == (512, 512)


def test_badge_keeps_size_and_changes_pixels():
    src = _img(900, 1200, "#FFFFFF")
    out = add_badge(src, "Держит тепло 12 часов", position="Слева сверху", fill="#E4287C")
    assert out.size == src.size
    assert out.tobytes() != src.tobytes()


def test_empty_badge_is_noop():
    src = _img(200, 200)
    assert add_badge(src, "   ") is src


def test_watermark_keeps_size():
    src = _img(600, 800)
    assert add_watermark(src, "черновик").size == (600, 800)


def test_orientation_and_size_matrix():
    assert orientation_for(900, 1200) == "portrait"
    assert orientation_for(1920, 1080) == "landscape"
    assert orientation_for(1000, 1000) == "square"
    assert resolve_size("gpt-image-1", "portrait") == "1024x1536"
    assert resolve_size("dall-e-3", "landscape") == "1792x1024"


def test_all_presets_are_valid():
    for name, value in SIZE_PRESETS.items():
        assert value is None or (len(value) == 2 and all(v > 0 for v in value)), name