fix: fix various issues before merging

This commit is contained in:
Kevin
2026-03-11 11:27:32 +08:00
parent bd5f0905ba
commit 1f98b8bfd6
15 changed files with 297 additions and 31 deletions

View File

@@ -1,12 +1,12 @@
import unittest
from unittest.mock import Mock
from unittest.mock import Mock, patch
from api.services.memoir_images.prompting import MemoirImagePromptService
from api.services.memoir_images.settings import MemoirImageSettings
class MemoirImagePromptingTest(unittest.TestCase):
def test_prompt_service_uses_category_style_and_plain_fallback_without_llm(self):
def test_prompt_service_uses_english_fallback_without_llm(self):
settings = MemoirImageSettings(
enabled=True,
max_per_chapter=2,
@@ -28,7 +28,9 @@ class MemoirImagePromptingTest(unittest.TestCase):
self.assertEqual(result["style"], "watercolor")
self.assertEqual(result["size"], "1024x1024")
self.assertIn("奶奶坐在院子里的藤椅上", result["prompt"])
self.assertIn("childhood memory", result["prompt"])
self.assertIn("watercolor", result["prompt"])
self.assertNotIn("奶奶坐在院子里的藤椅上", result["prompt"])
self.assertIn("childhood", result["prompt_context"])
def test_prompt_service_parses_structured_llm_response(self):
@@ -59,3 +61,32 @@ class MemoirImagePromptingTest(unittest.TestCase):
self.assertEqual(result["prompt"], "A grandmother in a quiet courtyard, summer cicadas, soft watercolor")
self.assertEqual(result["style"], "watercolor")
self.assertEqual(result["size"], "1024x1024")
@patch("api.services.memoir_images.prompting.logger")
def test_prompt_service_logs_warning_and_falls_back_when_llm_response_is_invalid(
self, logger_mock
):
settings = MemoirImageSettings(
enabled=True,
max_per_chapter=2,
provider="liblib",
default_style="watercolor",
default_size="1024x1024",
poll_interval_seconds=3,
max_attempts=20,
liblib_template_uuid="tpl-uuid",
)
llm = Mock()
llm.invoke.return_value.content = "not-json"
service = MemoirImagePromptService(llm=llm, settings=settings)
result = service.build_prompt(
chapter_title="童年的夏天",
chapter_category="childhood",
description="奶奶坐在院子里的藤椅上",
context_excerpt="梧桐树下很安静,夏天总有蝉鸣。",
)
self.assertIn("childhood memory", result["prompt"])
self.assertNotIn("奶奶坐在院子里的藤椅上", result["prompt"])
logger_mock.warning.assert_called_once()