feat: add diagnostic logs for generation flows

This commit is contained in:
chengma
2026-06-29 17:49:45 +08:00
parent 588b352eac
commit cd1256bc5e
17 changed files with 1455 additions and 104 deletions
+105
View File
@@ -1,6 +1,7 @@
import os
import sys
import unittest
from unittest import mock
sys.path.insert(0, os.path.dirname(__file__))
@@ -113,6 +114,110 @@ class AppConfigTests(TempDirMixin, unittest.TestCase):
self.assert_removed(temp_dir)
def test_model_request_url_accepts_base_and_full_endpoint(self):
self.assertEqual(
"https://api.example.com/v1/chat/completions",
appconfig.model_request_url(
{"url": "https://api.example.com/v1", "api_type": "chat"}
),
)
self.assertEqual(
"https://openrouter.ai/api/v1/chat/completions",
appconfig.model_request_url(
{"url": "https://openrouter.ai/api/v1/", "api_type": "auto"}
),
)
self.assertEqual(
"https://api.example.com/v1/chat/completions?region=tw",
appconfig.model_request_url(
{"url": "https://api.example.com/v1?region=tw", "api_type": "chat"}
),
)
self.assertEqual(
"https://api.example.com/v1/chat/completions",
appconfig.model_request_url(
{
"url": "https://api.example.com/v1/chat/completions",
"api_type": "chat",
}
),
)
self.assertEqual(
"https://api.example.com/v1/images/edits",
appconfig.model_request_url(
{"url": "https://api.example.com/v1", "api_type": "images_edits"}
),
)
self.assertEqual(
"https://api.example.com/custom/generate",
appconfig.model_request_url(
{"url": "https://api.example.com/custom/generate", "api_type": "chat"}
),
)
def test_ai_model_test_uses_resolved_base_url(self):
with self.make_temp_dir() as temp_dir:
models_path = os.path.join(temp_dir, "ai_models.json")
appconfig.save_ai_models_config(
{
"models": [
{
"name": "Text",
"category": "text",
"enabled": True,
"url": "https://api.example.com/v1",
"model": "text-model",
"api_key": "sk-text-secret",
"api_type": "chat",
"connect_timeout_seconds": 1,
"extra_body": {},
},
{
"name": "Image",
"category": "image",
"enabled": True,
"url": "https://api.example.com/v1/chat/completions",
"model": "image-model",
"api_key": "sk-image-secret",
"api_type": "auto",
"connect_timeout_seconds": 1,
"extra_body": {},
},
]
},
path=models_path,
)
calls = []
class Response:
status = 200
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
return False
def read(self, size=-1):
return b"{}"
def fake_urlopen(request, timeout=None):
calls.append((request, timeout))
return Response()
with mock.patch("app.appconfig.urllib.request.urlopen", side_effect=fake_urlopen):
result = appconfig.test_ai_model("Text", path=models_path)
self.assertTrue(result["ok"])
self.assertEqual(200, result["status"])
self.assertEqual(
"https://api.example.com/v1/chat/completions",
calls[0][0].full_url,
)
self.assertEqual(1, calls[0][1])
self.assert_removed(temp_dir)
def test_sanitize_for_log_masks_secret_fields(self):
payload = {
"name": "demo",