import asyncio import unittest from unittest.mock import AsyncMock, MagicMock, patch from app.image_generation import PolzaClient, build_prompt, content_blocks, cover_image_title, error_from_status, section_context 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) 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) 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) def test_async_unsupported_size_status_is_classified(self): 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") 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) self.assertLessEqual(len(build_prompt("cover", "«" + "А" * 10000 + "»", data)), 5000) 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"] self.assertEqual(payload["input"], {"prompt": "prompt", "aspect_ratio": "16:9", "max_images": 1}) if __name__ == "__main__": unittest.main()