feat: cover generation with Pollinations.ai and text fallback
Made-with: Cursor
This commit is contained in:
77
tests/test_cover.py
Normal file
77
tests/test_cover.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import os
|
||||
from datetime import date
|
||||
from unittest.mock import patch, MagicMock
|
||||
from io import BytesIO
|
||||
from PIL import Image as PILImage
|
||||
|
||||
from src.cover import generate_text_cover, generate_ai_cover, generate_cover
|
||||
|
||||
|
||||
def test_text_cover_creates_jpeg(tmp_path):
|
||||
path = generate_text_cover(
|
||||
output_dir=str(tmp_path),
|
||||
week_start=date(2026, 4, 6),
|
||||
week_end=date(2026, 4, 12),
|
||||
headlines=["Article One", "Article Two", "Article Three"],
|
||||
)
|
||||
|
||||
assert os.path.exists(path)
|
||||
img = PILImage.open(path)
|
||||
assert img.format == "JPEG"
|
||||
assert img.width <= 800
|
||||
assert img.height <= 480
|
||||
|
||||
|
||||
def test_ai_cover_creates_jpeg(tmp_path):
|
||||
fake_img = PILImage.new("RGB", (800, 480), color="blue")
|
||||
buf = BytesIO()
|
||||
fake_img.save(buf, format="JPEG")
|
||||
fake_bytes = buf.getvalue()
|
||||
|
||||
mock_response = MagicMock()
|
||||
mock_response.content = fake_bytes
|
||||
mock_response.raise_for_status = MagicMock()
|
||||
|
||||
with patch("src.cover.requests.get", return_value=mock_response):
|
||||
path = generate_ai_cover(
|
||||
output_dir=str(tmp_path),
|
||||
week_start=date(2026, 4, 6),
|
||||
week_end=date(2026, 4, 12),
|
||||
headlines=["Test Headline"],
|
||||
)
|
||||
|
||||
assert os.path.exists(path)
|
||||
img = PILImage.open(path)
|
||||
assert img.format == "JPEG"
|
||||
assert img.width <= 800
|
||||
assert img.height <= 480
|
||||
|
||||
|
||||
def test_ai_cover_falls_back_on_failure(tmp_path):
|
||||
with patch("src.cover.requests.get", side_effect=Exception("API down")):
|
||||
path = generate_ai_cover(
|
||||
output_dir=str(tmp_path),
|
||||
week_start=date(2026, 4, 6),
|
||||
week_end=date(2026, 4, 12),
|
||||
headlines=["Test"],
|
||||
)
|
||||
|
||||
assert os.path.exists(path)
|
||||
img = PILImage.open(path)
|
||||
assert img.format == "JPEG"
|
||||
|
||||
|
||||
def test_generate_cover_dispatches(tmp_path):
|
||||
with patch("src.cover.generate_ai_cover") as mock_ai:
|
||||
mock_ai.return_value = "/fake/ai.jpg"
|
||||
result = generate_cover("ai", str(tmp_path), date(2026, 4, 6),
|
||||
date(2026, 4, 12), ["A"])
|
||||
assert result == "/fake/ai.jpg"
|
||||
mock_ai.assert_called_once()
|
||||
|
||||
with patch("src.cover.generate_text_cover") as mock_text:
|
||||
mock_text.return_value = "/fake/text.jpg"
|
||||
result = generate_cover("text", str(tmp_path), date(2026, 4, 6),
|
||||
date(2026, 4, 12), ["A"])
|
||||
assert result == "/fake/text.jpg"
|
||||
mock_text.assert_called_once()
|
||||
Reference in New Issue
Block a user