群里按 BOT 通道激活当前工单、航线报价和成交跟进;没有 TMS 报价编号时用工单字段补齐仍调锁舱,避免被本地规则拦住。 Co-authored-by: Cursor <cursoragent@cursor.com>
104 lines
4.1 KiB
Python
104 lines
4.1 KiB
Python
"""
|
|
空运 BOT 出站:没有当场回话地址时,打桥主动往群里说。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
|
if ROOT not in sys.path:
|
|
sys.path.insert(0, ROOT)
|
|
|
|
from agent.channel.aibot import normalize_aibot_payload
|
|
from agent.channel.aibot.reply import AibotReplyClient
|
|
|
|
|
|
class AibotInboundNormalizeTests(unittest.TestCase):
|
|
def test_official_wecom_body(self) -> None:
|
|
parsed = normalize_aibot_payload(
|
|
{
|
|
"msgid": "m1",
|
|
"chatid": "wr_air_1",
|
|
"chattype": "group",
|
|
"from": {"userid": "WuJiLin"},
|
|
"msgtype": "text",
|
|
"text": {"content": "@询价小助手 WO202609160009"},
|
|
"aibot_direct": True,
|
|
}
|
|
)
|
|
self.assertEqual(parsed.sender_id, "WuJiLin")
|
|
self.assertEqual(parsed.message_id, "m1")
|
|
self.assertEqual(parsed.chat_id, "wr_air_1")
|
|
self.assertEqual(parsed.chat_type, "group")
|
|
self.assertIn("WO202609160009", parsed.content)
|
|
self.assertEqual(parsed.raw.get("source"), "aibot")
|
|
|
|
|
|
class AibotReplyTests(unittest.TestCase):
|
|
def test_empty_content_skips(self) -> None:
|
|
client = AibotReplyClient("")
|
|
out = client.send_group(chat_id="wr_1", content=" ")
|
|
self.assertFalse(out["ok"])
|
|
self.assertEqual(out["error"], "empty")
|
|
|
|
def test_proactive_posts_send_group(self) -> None:
|
|
client = AibotReplyClient("")
|
|
fake_http = MagicMock()
|
|
fake_http.post.return_value.status_code = 200
|
|
fake_http.post.return_value.text = '{"status":"sent"}'
|
|
settings = MagicMock()
|
|
settings.wecom_aibot_bridge_url = "http://127.0.0.1:8913"
|
|
settings.wecom_aibot_bridge_token = "tok"
|
|
with patch("agent.channel.aibot.reply.httpx", fake_http), patch(
|
|
"agent.channel.aibot.reply.get_settings", return_value=settings
|
|
):
|
|
out = client.send_group(chat_id="wr_air_1", content="附件已保存")
|
|
self.assertTrue(out["ok"])
|
|
url, = fake_http.post.call_args.args
|
|
self.assertEqual(url, "http://127.0.0.1:8913/send")
|
|
payload = fake_http.post.call_args.kwargs["json"]
|
|
self.assertEqual(payload["chatid"], "wr_air_1")
|
|
self.assertEqual(payload["operation"], "text")
|
|
self.assertEqual(payload["content"], "附件已保存")
|
|
self.assertEqual(
|
|
fake_http.post.call_args.kwargs["headers"]["X-Aibot-Bridge-Token"],
|
|
"tok",
|
|
)
|
|
|
|
def test_send_group_file_posts_send_file(self) -> None:
|
|
client = AibotReplyClient("")
|
|
fake_http = MagicMock()
|
|
fake_http.post.return_value.status_code = 200
|
|
fake_http.post.return_value.text = '{"status":"sent"}'
|
|
settings = MagicMock()
|
|
settings.wecom_aibot_bridge_url = "http://127.0.0.1:8913"
|
|
settings.wecom_aibot_bridge_token = "tok"
|
|
with patch("agent.channel.aibot.reply.httpx", fake_http), patch(
|
|
"agent.channel.aibot.reply.get_settings", return_value=settings
|
|
):
|
|
out = client.send_group_file(
|
|
chat_id="wr_air_1",
|
|
filename="WO1_quote.xlsx",
|
|
file_b64="QQ==",
|
|
)
|
|
self.assertTrue(out["ok"])
|
|
url, = fake_http.post.call_args.args
|
|
self.assertEqual(url, "http://127.0.0.1:8913/send-file")
|
|
payload = fake_http.post.call_args.kwargs["json"]
|
|
self.assertEqual(payload["chatid"], "wr_air_1")
|
|
self.assertEqual(payload["filename"], "WO1_quote.xlsx")
|
|
|
|
def test_stream_url_does_not_hit_bridge(self) -> None:
|
|
client = AibotReplyClient("https://example.com/reply")
|
|
fake_http = MagicMock()
|
|
fake_http.post.return_value.status_code = 200
|
|
with patch("agent.channel.aibot.reply.httpx", fake_http):
|
|
out = client.send_group(chat_id="wr_air_1", content="已激活")
|
|
self.assertTrue(out["ok"])
|
|
url, = fake_http.post.call_args.args
|
|
self.assertEqual(url, "https://example.com/reply")
|