2026-08-11 19:40:34 +03:00
|
|
|
|
import asyncio
|
2026-08-11 17:53:54 +03:00
|
|
|
|
import unittest
|
2026-08-11 19:40:34 +03:00
|
|
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
2026-08-11 17:53:54 +03:00
|
|
|
|
|
2026-08-11 19:49:15 +03:00
|
|
|
|
from app.image_generation import PolzaClient, build_prompt, content_blocks, cover_image_title, error_from_status, section_context
|
2026-08-11 17:53:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EDITOR_DATA = {
|
|
|
|
|
|
"blocks": [
|
|
|
|
|
|
{"id": "intro", "type": "paragraph", "data": {"text": "Вводный абзац."}},
|
|
|
|
|
|
{"id": "middle-heading", "type": "header", "data": {"text": "Вторая часть"}},
|
|
|
|
|
|
{"id": "middle", "type": "paragraph", "data": {"text": "Факты второй части."}},
|
|
|
|
|
|
{"id": "image", "type": "image", "data": {"file": {"url": "https://example.test/old.jpg"}}},
|
|
|
|
|
|
{"id": "final-heading", "type": "header", "data": {"text": "Вывод"}},
|
|
|
|
|
|
{"id": "final", "type": "paragraph", "data": {"text": "Заключительный вывод статьи."}},
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ImageGenerationPromptTest(unittest.TestCase):
|
|
|
|
|
|
def test_cover_keeps_exact_title_and_forbids_extra_text(self):
|
|
|
|
|
|
title = "КТО НАВЁЛ УДАР?"
|
|
|
|
|
|
prompt = build_prompt("cover", title, EDITOR_DATA)
|
|
|
|
|
|
|
|
|
|
|
|
self.assertIn(title, prompt)
|
|
|
|
|
|
self.assertIn("ЕДИНСТВЕННЫЙ ТЕКСТ", prompt)
|
2026-08-11 19:49:15 +03:00
|
|
|
|
self.assertIn("Bebas Neue Cyrillic", prompt)
|
|
|
|
|
|
|
|
|
|
|
|
def test_cover_title_is_uppercase_and_has_no_quotes(self):
|
|
|
|
|
|
self.assertEqual(cover_image_title('«Кто “навёл” удар?»'), "КТО НАВЁЛ УДАР?")
|
|
|
|
|
|
prompt = build_prompt("cover", '«Кто “навёл” удар?»', EDITOR_DATA)
|
|
|
|
|
|
self.assertIn("КТО НАВЁЛ УДАР?", prompt)
|
|
|
|
|
|
self.assertIn("Все буквы заголовка должны быть ЗАГЛАВНЫМИ", prompt)
|
2026-08-11 17:53:54 +03:00
|
|
|
|
|
2026-08-13 10:26:08 +03:00
|
|
|
|
def test_cover_selects_and_highlights_key_phrase_in_gold(self):
|
|
|
|
|
|
prompt = build_prompt("cover", "КТО НАВЁЛ УДАР ПО ГОРОДУ?", EDITOR_DATA)
|
|
|
|
|
|
|
|
|
|
|
|
self.assertIn("САМОСТОЯТЕЛЬНО ВЫБЕРИ ОДНУ КЛЮЧЕВУЮ СМЫСЛОВУЮ ФРАЗУ", prompt)
|
|
|
|
|
|
self.assertIn("2–5 слов", prompt)
|
|
|
|
|
|
self.assertIn("только её золотым металлическим градиентом", prompt)
|
|
|
|
|
|
self.assertIn("мягким свечением и тонким блеском", prompt)
|
|
|
|
|
|
self.assertIn("Остальной заголовок оставь холодно-серебристым", prompt)
|
|
|
|
|
|
self.assertIn("Если не уверена в выборе, выдели первые 2–4 смысловых слова", prompt)
|
|
|
|
|
|
|
|
|
|
|
|
def test_cover_key_phrase_cannot_change_title_or_add_text(self):
|
|
|
|
|
|
prompt = build_prompt("cover", "ЗАГОЛОВОК СТАТЬИ", EDITOR_DATA)
|
|
|
|
|
|
|
|
|
|
|
|
self.assertIn("только из существующего заголовка", prompt)
|
|
|
|
|
|
self.assertIn("Не добавляй, не удаляй и не изменяй слова или знаки препинания", prompt)
|
|
|
|
|
|
self.assertIn("не создавай новых надписей", prompt)
|
|
|
|
|
|
|
2026-08-11 17:53:54 +03:00
|
|
|
|
def test_body_prompts_are_distinct_and_forbid_text(self):
|
|
|
|
|
|
middle = build_prompt("middle", "Заголовок", EDITOR_DATA)
|
|
|
|
|
|
final = build_prompt("final", "Заголовок", EDITOR_DATA)
|
|
|
|
|
|
|
|
|
|
|
|
self.assertNotEqual(middle, final)
|
|
|
|
|
|
self.assertIn("без текста", middle.lower())
|
|
|
|
|
|
self.assertIn("без текста", final.lower())
|
|
|
|
|
|
|
|
|
|
|
|
def test_image_blocks_are_excluded_from_context(self):
|
|
|
|
|
|
blocks = content_blocks(EDITOR_DATA)
|
|
|
|
|
|
|
|
|
|
|
|
self.assertNotIn("image", {block["id"] for block in blocks})
|
|
|
|
|
|
self.assertIn("Факты второй части.", section_context(EDITOR_DATA, "middle"))
|
|
|
|
|
|
self.assertIn("Заключительный вывод статьи.", section_context(EDITOR_DATA, "final"))
|
|
|
|
|
|
|
|
|
|
|
|
def test_inline_regeneration_uses_nearby_text(self):
|
|
|
|
|
|
context = section_context(EDITOR_DATA, "middle", "image")
|
|
|
|
|
|
|
|
|
|
|
|
self.assertIn("Факты второй части.", context)
|
|
|
|
|
|
self.assertIn("Заключительный вывод статьи.", context)
|
|
|
|
|
|
|
2026-08-11 19:40:34 +03:00
|
|
|
|
def test_async_unsupported_size_status_is_classified(self):
|
2026-08-11 19:38:38 +03:00
|
|
|
|
error = error_from_status({
|
|
|
|
|
|
"status": "failed",
|
|
|
|
|
|
"error": {"code": "BAD_REQUEST", "message": "image_size '4K' is not supported"},
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
self.assertTrue(error.size_error)
|
|
|
|
|
|
self.assertEqual(error.code, "provider_error")
|
|
|
|
|
|
|
2026-08-11 19:40:34 +03:00
|
|
|
|
def test_prompts_never_exceed_provider_limit(self):
|
|
|
|
|
|
data = {"blocks": [{"id": "long", "type": "paragraph", "data": {"text": "А" * 10000}}]}
|
|
|
|
|
|
|
|
|
|
|
|
self.assertLessEqual(len(build_prompt("cover", "Заголовок", data)), 5000)
|
|
|
|
|
|
self.assertLessEqual(len(build_prompt("middle", "Заголовок", data)), 5000)
|
|
|
|
|
|
self.assertLessEqual(len(build_prompt("final", "Заголовок", data)), 5000)
|
2026-08-11 19:51:16 +03:00
|
|
|
|
self.assertLessEqual(len(build_prompt("cover", "«" + "А" * 10000 + "»", data)), 5000)
|
2026-08-11 19:40:34 +03:00
|
|
|
|
|
|
|
|
|
|
def test_stable_model_request_uses_supported_landscape_size_only(self):
|
|
|
|
|
|
response = MagicMock(is_error=False)
|
|
|
|
|
|
response.json.return_value = {"id": "gen-test"}
|
|
|
|
|
|
http_client = MagicMock()
|
|
|
|
|
|
http_client.post = AsyncMock(return_value=response)
|
|
|
|
|
|
context = MagicMock()
|
|
|
|
|
|
context.__aenter__ = AsyncMock(return_value=http_client)
|
|
|
|
|
|
context.__aexit__ = AsyncMock(return_value=False)
|
|
|
|
|
|
|
|
|
|
|
|
with patch("app.image_generation.httpx.AsyncClient", return_value=context):
|
|
|
|
|
|
generation_id = asyncio.run(PolzaClient("https://polza.ai/api", "secret").create("model", "prompt"))
|
|
|
|
|
|
|
|
|
|
|
|
self.assertEqual(generation_id, "gen-test")
|
|
|
|
|
|
payload = http_client.post.await_args.kwargs["json"]
|
2026-08-11 19:42:36 +03:00
|
|
|
|
self.assertEqual(payload["input"], {"prompt": "prompt", "aspect_ratio": "16:9", "max_images": 1})
|
2026-08-11 19:40:34 +03:00
|
|
|
|
|
2026-08-11 17:53:54 +03:00
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
unittest.main()
|