test: 建立单元测试基座

- 新增 unittest tests 基座,覆盖 appconfig 与 db 纯逻辑

- 增加 excel/prompts 未实现模块的契约占位测试

- 补强 config.json 对 *_token 与 *_password 的敏感字段拦截

- 更新任务看板、当前状态、API 合约和进度记录
This commit is contained in:
chengma
2026-06-27 09:17:20 +08:00
parent 0de7c84163
commit aba272dd21
10 changed files with 345 additions and 8 deletions
+118
View File
@@ -0,0 +1,118 @@
import os
import sys
import unittest
sys.path.insert(0, os.path.dirname(__file__))
from _helpers import TempDirMixin
from app import appconfig
class AppConfigTests(TempDirMixin, unittest.TestCase):
def test_config_load_update_and_response_timeout(self):
with self.make_temp_dir() as temp_dir:
config_path = os.path.join(temp_dir, "config.json")
config = appconfig.load_config(config_path)
self.assertTrue(os.path.exists(config_path))
self.assertEqual("images", appconfig.image_dir(config))
self.assertEqual(240, appconfig.response_timeout(config))
updated = appconfig.update_config(
{"ai": {"resolution": "2k"}},
path=config_path,
)
self.assertEqual(360, appconfig.response_timeout(updated))
self.assertEqual((9222, 9260), appconfig.debug_port_range(updated))
self.assert_removed(temp_dir)
def test_config_rejects_sensitive_fields(self):
with self.make_temp_dir() as temp_dir:
config_path = os.path.join(temp_dir, "config.json")
with self.assertRaises(appconfig.ConfigError):
appconfig.save_config({"api_key": "secret"}, path=config_path)
with self.assertRaises(appconfig.ConfigError):
appconfig.save_config(
{"ai": {"provider_token": "secret"}},
path=config_path,
)
self.assert_removed(temp_dir)
def test_ai_models_crud_filter_mask_and_get_model(self):
with self.make_temp_dir() as temp_dir:
models_path = os.path.join(temp_dir, "ai_models.json")
models = appconfig.list_ai_models(path=models_path)
self.assertEqual({"text", "image"}, {model["category"] for model in models})
self.assertTrue(all("api_key_set" in model for model in models))
appconfig.add_ai_model(
{
"name": "Text 2",
"category": "text",
"enabled": True,
"url": "https://example.invalid/v1/chat/completions",
"model": "demo-model",
"api_key": "sk-1234567890",
"api_type": "chat",
"connect_timeout_seconds": 1,
"extra_body": {"temperature": 0},
},
path=models_path,
)
text_models = appconfig.list_ai_models("text", path=models_path)
self.assertEqual(2, len(text_models))
self.assertEqual("sk-1***7890", text_models[-1]["api_key"])
self.assertTrue(text_models[-1]["api_key_set"])
private_model = appconfig.get_model("Text 2", path=models_path)
self.assertEqual("sk-1234567890", private_model["api_key"])
self.assertEqual({"temperature": 0}, private_model["extra_body"])
appconfig.update_ai_model(
"Text 2",
path=models_path,
name="Text 3",
enabled=False,
)
self.assertFalse(appconfig.get_model("Text 3", path=models_path)["enabled"])
self.assert_removed(temp_dir)
def test_ai_model_constraints_and_connection_validation(self):
with self.make_temp_dir() as temp_dir:
models_path = os.path.join(temp_dir, "ai_models.json")
appconfig.list_ai_models(path=models_path)
with self.assertRaises(appconfig.ConfigError):
appconfig.add_ai_model(
{
"name": "GPT-5.5 文本",
"category": "text",
"enabled": True,
"api_type": "chat",
"connect_timeout_seconds": 30,
},
path=models_path,
)
with self.assertRaises(appconfig.ConfigError):
appconfig.delete_ai_model("Nano Banana 2", path=models_path)
result = appconfig.test_ai_model("GPT-5.5 文本", path=models_path)
self.assertFalse(result["ok"])
self.assertIn("url", result["error"])
self.assertIn("model", result["error"])
self.assertIn("api_key", result["error"])
self.assert_removed(temp_dir)
if __name__ == "__main__":
unittest.main()