fix: 去除LLM直接生成图片占位符逻辑

This commit is contained in:
yangshilin
2026-03-19 11:18:58 +08:00
parent 67fb5d2cb6
commit f3629efec3
6 changed files with 160 additions and 19 deletions

View File

@@ -3,6 +3,8 @@ import unittest
from app.features.memoir.memoir_images.parser import (
build_initial_image_assets,
parse_image_placeholders,
parse_narrative_json,
parse_narrative_to_sections,
)
@@ -52,3 +54,25 @@ class MemoirImageParserTest(unittest.TestCase):
self.assertEqual(len(items), 1)
self.assertEqual(items[0]["placeholder"], "{{IMAGE:1938年初的上海弄堂口冬日萧瑟}}")
self.assertEqual(items[0]["description"], "1938年初的上海弄堂口冬日萧瑟")
def test_parse_narrative_json_returns_sections_with_content_and_placeholder_info(self):
raw = '{"paragraphs": [{"content": "那年春天。", "image_description": "南方小镇的青石板路"}, {"content": "奶奶坐在藤椅上。", "image_description": "奶奶的藤椅"}]}'
segments = parse_narrative_json(raw)
self.assertEqual(len(segments), 2)
self.assertEqual(segments[0]["content"], "那年春天。")
self.assertEqual(segments[0]["placeholder_info"]["description"], "南方小镇的青石板路")
self.assertEqual(segments[1]["content"], "奶奶坐在藤椅上。")
self.assertEqual(segments[1]["placeholder_info"]["description"], "奶奶的藤椅")
def test_parse_narrative_to_sections_prefers_json_then_fallback_to_placeholder(self):
json_raw = '{"paragraphs": [{"content": "段落一", "image_description": "图一"}]}'
segments = parse_narrative_to_sections(json_raw)
self.assertEqual(len(segments), 1)
self.assertEqual(segments[0]["content"], "段落一")
self.assertEqual(segments[0]["placeholder_info"]["description"], "图一")
placeholder_raw = "正文。\n\n{{{{IMAGE:描述}}}}\n\n结尾。"
segments2 = parse_narrative_to_sections(placeholder_raw)
self.assertEqual(len(segments2), 2)
self.assertIn("正文", segments2[0]["content"])
self.assertEqual(segments2[0]["placeholder_info"]["description"], "描述")