更新
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
"""手工测试 AI API 连接;自动测试导入本模块时不会发起请求。"""
|
||||
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
import requests
|
||||
|
||||
from ai_chat import _post_with_retry, get_ai_reply
|
||||
|
||||
|
||||
def main() -> None:
|
||||
@@ -37,6 +42,45 @@ class ManualAIIsolationTest(unittest.TestCase):
|
||||
def test_manual_entrypoint_is_import_safe(self) -> None:
|
||||
self.assertTrue(callable(main))
|
||||
|
||||
def test_transient_model_failure_is_retried_once(self) -> None:
|
||||
response = mock.Mock(status_code=200)
|
||||
with (
|
||||
mock.patch(
|
||||
"ai_chat.requests.post",
|
||||
side_effect=[requests.exceptions.Timeout(), response],
|
||||
) as post,
|
||||
mock.patch("ai_chat.time.sleep"),
|
||||
):
|
||||
result = _post_with_retry(
|
||||
"https://example.invalid/v1/chat/completions",
|
||||
purpose="测试模型",
|
||||
timeout=1,
|
||||
)
|
||||
self.assertIs(result, response)
|
||||
self.assertEqual(post.call_count, 2)
|
||||
|
||||
def test_authentication_failure_is_not_retried(self) -> None:
|
||||
response = mock.Mock(status_code=401)
|
||||
with mock.patch("ai_chat.requests.post", return_value=response) as post:
|
||||
result = _post_with_retry(
|
||||
"https://example.invalid/v1/chat/completions",
|
||||
purpose="测试模型",
|
||||
timeout=1,
|
||||
)
|
||||
self.assertIs(result, response)
|
||||
post.assert_called_once()
|
||||
|
||||
def test_final_timeout_keeps_a_specific_chain_error(self) -> None:
|
||||
with (
|
||||
mock.patch(
|
||||
"ai_chat.call_ai_text",
|
||||
side_effect=requests.exceptions.Timeout(),
|
||||
),
|
||||
mock.patch("ai_chat.time.sleep"),
|
||||
):
|
||||
with self.assertRaisesRegex(RuntimeError, "模型链路超时"):
|
||||
get_ai_reply(chat_text="测试")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user