53 lines
2.5 KiB
Python
53 lines
2.5 KiB
Python
"""测试 memory_prompts.inject_image_placeholder_template:占位符花括号统一为四层,避免多余花括号残留"""
|
||
|
||
import unittest
|
||
|
||
from app.agents.memoir.prompts import (
|
||
IMAGE_PLACEHOLDER_TEMPLATE,
|
||
inject_image_placeholder_template,
|
||
)
|
||
|
||
|
||
class InjectImagePlaceholderTemplateTest(unittest.TestCase):
|
||
def test_normalizes_double_brace_to_four(self):
|
||
content = "段落。\n\n{{IMAGE:南方小镇的青石板路}}\n\n结尾。"
|
||
out = inject_image_placeholder_template(content)
|
||
self.assertIn("{{{{IMAGE:", out)
|
||
self.assertIn("}}}}", out)
|
||
# 应为四层占位符,且「结尾。」前不应有多余的 }}
|
||
self.assertIn("\n\n结尾。", out)
|
||
self.assertEqual(out.count("}}}}"), 1)
|
||
|
||
def test_normalizes_quad_brace_unchanged(self):
|
||
content = "段落。\n\n{{{{IMAGE:南方小镇的青石板路}}}}\n\n结尾。"
|
||
out = inject_image_placeholder_template(content)
|
||
self.assertEqual(out.count("{{{{"), out.count("}}}}"))
|
||
self.assertIn("{{{{IMAGE:", out)
|
||
self.assertNotRegex(out, r"\}\}\}\}\}\}") # 不应出现五层及以上闭合括号
|
||
|
||
def test_normalizes_six_braces_to_four_so_no_residue(self):
|
||
# LLM 有时会多打花括号,导致客户端按四层占位符 split 后残留 "{{" "}}" 被显示
|
||
content = "段落。\n\n{{{{{{IMAGE:南方小镇的青石板路}}}}}}\n\n结尾。"
|
||
out = inject_image_placeholder_template(content)
|
||
# 整段应被替换为四层,不应留下多余的 "{{" 或 "}}"
|
||
self.assertIn("{{{{IMAGE:" + IMAGE_PLACEHOLDER_TEMPLATE, out)
|
||
self.assertIn("}}}}", out)
|
||
self.assertNotIn("{{{{{{", out)
|
||
self.assertNotIn("}}}}}}", out)
|
||
# 正文前后不应出现裸花括号
|
||
parts = out.split("}}}}")
|
||
for i, p in enumerate(parts):
|
||
if "南方小镇" in p or i == 0:
|
||
continue
|
||
self.assertNotRegex(p, r"^\s*\{\{", msg=f"残留开括号 in part: {p!r}")
|
||
before, sep, after = out.partition("{{{{IMAGE:")
|
||
self.assertNotRegex(before, r"\{\{\s*$", msg="开头段不应以 {{ 结尾")
|
||
self.assertNotRegex(after, r"^\s*\}\}", msg="占位符后不应以 }} 开头")
|
||
|
||
def test_normalizes_eight_braces_to_four(self):
|
||
content = "前\n\n{{{{{{{{IMAGE:奶奶的藤椅}}}}}}}}\n\n后"
|
||
out = inject_image_placeholder_template(content)
|
||
self.assertIn("{{{{IMAGE:", out)
|
||
self.assertNotIn("{{{{{{{{", out)
|
||
self.assertNotIn("}}}}}}}}", out)
|