dzen.nstarikov.ru/dzen-assistant/tests/test_image_generation.py

60 lines
2.5 KiB
Python
Raw Normal View History

import unittest
from app.image_generation import build_prompt, content_blocks, 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", 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_4k_status_is_classified_for_fallback(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")
if __name__ == "__main__":
unittest.main()