feat(product-suite): add prompt template settings

This commit is contained in:
chengma
2026-07-16 16:18:11 +08:00
parent 613950c9f3
commit eea3fdd0a9
14 changed files with 1017 additions and 35 deletions
+80
View File
@@ -1,6 +1,7 @@
import os
import sys
import unittest
from unittest import mock
sys.path.insert(0, os.path.dirname(__file__))
@@ -183,6 +184,85 @@ class PromptTests(TempDirMixin, unittest.TestCase):
rendered,
)
def test_product_suite_prompt_seed_save_and_restore_are_isolated(self):
with self.make_temp_dir() as temp_dir:
path = os.path.join(temp_dir, "prompts", "product_suite", "base.txt")
default_text = prompts.load_default_product_suite_prompt()
self.assertEqual(default_text, prompts.ensure_default_product_suite_prompt(path))
self.assertEqual(default_text, prompts.load_product_suite_prompt(path))
custom_text = "自定义说明\n" + default_text
prompts.save_product_suite_prompt(custom_text, path)
self.assertEqual(custom_text, prompts.ensure_default_product_suite_prompt(path))
self.assertEqual(custom_text, prompts.load_product_suite_prompt(path))
self.assertEqual(default_text, prompts.restore_default_product_suite_prompt(path))
self.assertEqual(default_text, prompts.load_product_suite_prompt(path))
self.assert_removed(temp_dir)
def test_product_suite_prompt_invalid_user_file_is_preserved(self):
with self.make_temp_dir() as temp_dir:
path = os.path.join(temp_dir, "prompts", "product_suite", "base.txt")
os.makedirs(os.path.dirname(path), exist_ok=True)
invalid = "坏模板{未知变量}"
with open(path, "w", encoding="utf-8") as handle:
handle.write(invalid)
with self.assertRaisesRegex(prompts.PromptError, "套图提示词模板无效"):
prompts.ensure_default_product_suite_prompt(path)
with open(path, "r", encoding="utf-8") as handle:
self.assertEqual(invalid, handle.read())
self.assert_removed(temp_dir)
def test_product_suite_prompt_unreadable_utf8_is_not_overwritten(self):
with self.make_temp_dir() as temp_dir:
path = os.path.join(temp_dir, "prompts", "product_suite", "base.txt")
os.makedirs(os.path.dirname(path), exist_ok=True)
raw = b"\xff\xfe\x00\x80"
with open(path, "wb") as handle:
handle.write(raw)
with self.assertRaisesRegex(prompts.PromptError, "读取失败"):
prompts.ensure_default_product_suite_prompt(path)
with open(path, "rb") as handle:
self.assertEqual(raw, handle.read())
self.assert_removed(temp_dir)
def test_product_suite_prompt_invalid_packaged_default_does_not_create_file(self):
with self.make_temp_dir() as temp_dir:
path = os.path.join(temp_dir, "prompts", "product_suite", "base.txt")
with mock.patch.object(
prompts,
"load_default_product_suite_prompt",
side_effect=prompts.PromptError(prompts.PRODUCT_SUITE_DEFAULT_ERROR),
):
with self.assertRaisesRegex(prompts.PromptError, "内置套图提示词模板无效"):
prompts.ensure_default_product_suite_prompt(path)
self.assertFalse(os.path.exists(path))
self.assert_removed(temp_dir)
def test_product_suite_prompt_atomic_save_failure_keeps_original(self):
with self.make_temp_dir() as temp_dir:
path = os.path.join(temp_dir, "prompts", "product_suite", "base.txt")
default_text = prompts.ensure_default_product_suite_prompt(path)
custom_text = "自定义说明\n" + default_text
with mock.patch("app.prompts.os.replace", side_effect=OSError("文件占用")):
with self.assertRaisesRegex(prompts.PromptError, "保存失败"):
prompts.save_product_suite_prompt(custom_text, path)
self.assertEqual(default_text, prompts.load_product_suite_prompt(path))
self.assertFalse(
any(name.startswith(".prompt-") for name in os.listdir(os.path.dirname(path)))
)
self.assert_removed(temp_dir)
if __name__ == "__main__":
unittest.main()