"""手工群 Webhook 出站:配置解析与发送结果。""" from __future__ import annotations import unittest from unittest import mock from agent.channel.wecom import group_webhook as gw class _FakeSettings: def __init__(self, mapping: str = "", default: str = "") -> None: self.wecom_group_webhooks = mapping self.wecom_group_webhook_default = default class _FakeResp: def __init__(self, data: dict) -> None: self._data = data def json(self) -> dict: return self._data class GroupWebhookTests(unittest.TestCase): def test_url_for_prefers_chat_mapping_then_default(self) -> None: mapping = '{"wr1": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=k1"}' with mock.patch.object(gw, "get_settings", return_value=_FakeSettings(mapping, "https://d?key=d")): self.assertIn("key=k1", gw.webhook_url_for("wr1")) self.assertIn("key=d", gw.webhook_url_for("wr2")) with mock.patch.object(gw, "get_settings", return_value=_FakeSettings("bad-json", "")): self.assertEqual(gw.webhook_url_for("wr1"), "") def test_send_text_ok_and_failure(self) -> None: mapping = '{"wr1": "https://x/send?key=k1"}' with mock.patch.object(gw, "get_settings", return_value=_FakeSettings(mapping, "")): with mock.patch.object(gw.httpx, "post", return_value=_FakeResp({"errcode": 0, "errmsg": "ok"})): out = gw.send_webhook_text("wr1", "你好") self.assertTrue(out["ok"]) with mock.patch.object(gw.httpx, "post", return_value=_FakeResp({"errcode": 93001, "errmsg": "deny"})): out = gw.send_webhook_text("wr1", "你好") self.assertFalse(out["ok"]) self.assertEqual(out["errcode"], 93001) def test_send_without_config_returns_no_webhook(self) -> None: with mock.patch.object(gw, "get_settings", return_value=_FakeSettings("", "")): self.assertEqual(gw.send_webhook_text("wr1", "x").get("error"), "no_webhook") def test_send_file_uploads_then_sends(self) -> None: mapping = '{"wr1": "https://x/send?key=k1"}' upload = _FakeResp({"errcode": 0, "errmsg": "ok", "media_id": "m1"}) sent = _FakeResp({"errcode": 0, "errmsg": "ok"}) with mock.patch.object(gw, "get_settings", return_value=_FakeSettings(mapping, "")), \ mock.patch.object(gw.httpx, "post", side_effect=[upload, sent]) as poster: out = gw.send_webhook_file("wr1", "quote.xlsx", b"xlsx-bytes") self.assertTrue(out["ok"]) self.assertEqual(poster.call_count, 2) second = poster.call_args_list[1] self.assertEqual(second.kwargs["json"]["msgtype"], "file") def test_send_file_upload_failure(self) -> None: mapping = '{"wr1": "https://x/send?key=k1"}' upload = _FakeResp({"errcode": 40001, "errmsg": "bad"}) with mock.patch.object(gw, "get_settings", return_value=_FakeSettings(mapping, "")), \ mock.patch.object(gw.httpx, "post", return_value=upload): out = gw.send_webhook_file("wr1", "quote.xlsx", b"x") self.assertFalse(out["ok"]) def test_proactive_reply_uses_webhook_when_no_req_id(self) -> None: from agent.channel.aibot.reply import AibotReplyClient client = AibotReplyClient(response_url="", callback_req_id="") with mock.patch.object(gw, "webhook_url_for", return_value="https://x?key=1"), \ mock.patch.object(gw, "send_webhook_text", return_value={"ok": True}) as sender: out = client._send_proactive("wr1", "回复正文") self.assertTrue(out["ok"]) sender.assert_called_once() def test_proactive_file_uses_webhook_when_no_req_id(self) -> None: from agent.channel.aibot.reply import AibotReplyClient client = AibotReplyClient(response_url="", callback_req_id="") with mock.patch.object(gw, "webhook_url_for", return_value="https://x?key=1"), \ mock.patch.object(gw, "send_webhook_file", return_value={"ok": True}) as sender: out = client.send_group_file(chat_id="wr1", filename="q.xlsx", file_b64="eA==") self.assertTrue(out["ok"]) sender.assert_called_once() def test_proactive_reply_keeps_bridge_when_req_id_present(self) -> None: from agent.channel.aibot.reply import AibotReplyClient client = AibotReplyClient(response_url="", callback_req_id="req-1") with mock.patch.object(gw, "send_webhook_text") as sender, \ mock.patch("agent.channel.aibot.reply.httpx.post", return_value=_FakeResp({"errcode": 0})): client._send_proactive("wr1", "回复正文") sender.assert_not_called() if __name__ == "__main__": unittest.main()