107 lines
3.3 KiB
Python
107 lines
3.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Routing tests for the local Grok Build customer-service provider."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
import ai_chat
|
|
import ai_config
|
|
from grok_customer_agent import GrokCustomerAgentError
|
|
|
|
|
|
class GrokCustomerProviderRoutingTest(unittest.TestCase):
|
|
def test_stable_wecom_session_is_forwarded_to_local_agent(self) -> None:
|
|
session_id = "00112233445566778899aabbccddeeff"
|
|
with (
|
|
mock.patch.object(
|
|
ai_config,
|
|
"GROK_CUSTOMER_SERVICE_ENABLED",
|
|
True,
|
|
create=True,
|
|
),
|
|
mock.patch(
|
|
"grok_customer_agent.generate_customer_reply",
|
|
return_value="收到,请问有什么可以帮您?",
|
|
) as generate,
|
|
):
|
|
result = ai_chat.call_ai_text(
|
|
"客户最新消息",
|
|
history=[
|
|
{"role": "user", "content": "本地历史由工具读取"},
|
|
],
|
|
session_id=session_id,
|
|
)
|
|
|
|
self.assertEqual("收到,请问有什么可以帮您?", result)
|
|
generate.assert_called_once_with(
|
|
"客户最新消息",
|
|
session_id=session_id,
|
|
)
|
|
|
|
def test_ad_hoc_draft_gets_an_isolated_valid_scope(self) -> None:
|
|
with (
|
|
mock.patch.object(
|
|
ai_config,
|
|
"GROK_CUSTOMER_SERVICE_ENABLED",
|
|
True,
|
|
create=True,
|
|
),
|
|
mock.patch(
|
|
"grok_customer_agent.generate_customer_reply",
|
|
return_value="草稿",
|
|
) as generate,
|
|
):
|
|
ai_chat.call_ai_text("临时草稿")
|
|
|
|
generated_session = generate.call_args.kwargs["session_id"]
|
|
self.assertRegex(generated_session, r"^[0-9a-f]{32}$")
|
|
|
|
def test_disabled_agent_never_falls_back_to_http_model(self) -> None:
|
|
with (
|
|
mock.patch.object(
|
|
ai_config,
|
|
"GROK_CUSTOMER_SERVICE_ENABLED",
|
|
False,
|
|
create=True,
|
|
),
|
|
mock.patch(
|
|
"grok_customer_agent.generate_customer_reply",
|
|
) as generate,
|
|
mock.patch.object(ai_chat.requests, "post") as post,
|
|
self.assertRaisesRegex(RuntimeError, "已关闭"),
|
|
):
|
|
ai_chat.call_ai_text(
|
|
"客户消息",
|
|
session_id="0" * 32,
|
|
)
|
|
|
|
generate.assert_not_called()
|
|
post.assert_not_called()
|
|
|
|
def test_unfinished_agent_reply_is_not_returned_by_unified_entry(self) -> None:
|
|
with (
|
|
mock.patch.object(
|
|
ai_config,
|
|
"GROK_CUSTOMER_SERVICE_ENABLED",
|
|
True,
|
|
create=True,
|
|
),
|
|
mock.patch.object(ai_config, "AI_USE_VISION", False),
|
|
mock.patch(
|
|
"grok_customer_agent.generate_customer_reply",
|
|
side_effect=GrokCustomerAgentError("未完整结束"),
|
|
),
|
|
):
|
|
result = ai_chat.get_ai_reply(
|
|
chat_text="客户消息",
|
|
session_id="0" * 32,
|
|
)
|
|
|
|
self.assertEqual("", result)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|