110 lines
4.1 KiB
Python
110 lines
4.1 KiB
Python
"""
|
||
群 PDF 发进群后的成交跟进补推。
|
||
|
||
为什么单独测:PDF 是 LibreOffice 槽异步转的,成交跟进不能在排队时先发
|
||
(那时群里还没有表)。只有任务带 deal_followup 且文件发成功后,槽里才补推,
|
||
内容与 Excel 出表后、报价落定那次完全一致。
|
||
|
||
本机若没装 langgraph,`agent.jobs` 包导不进来(生产装了),整组跳过;
|
||
正式服务器上跑全量时会正常执行。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import sys
|
||
import unittest
|
||
from typing import Any
|
||
|
||
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
||
if ROOT not in sys.path:
|
||
sys.path.insert(0, ROOT)
|
||
|
||
os.environ.setdefault("YTD_ENV", "test")
|
||
os.environ.setdefault("REDIS_BACKEND", "memory")
|
||
os.environ.setdefault("CHECKPOINT_BACKEND", "memory")
|
||
os.environ.setdefault("MESSAGE_STORE_BACKEND", "memory")
|
||
os.environ.setdefault("LEDGER_BACKEND", "memory")
|
||
|
||
from agent.policy import inquiry_copy as copy
|
||
|
||
try: # 生产装了 langgraph;本机骨架可能没有。
|
||
from agent.jobs.pdf_job import _emit_group_deal_after_pdf
|
||
except Exception: # pragma: no cover - 仅本机骨架走这里
|
||
_emit_group_deal_after_pdf = None
|
||
|
||
|
||
class _FakeGroupClient:
|
||
"""只记录发出的群文字与模板卡,不碰网络。"""
|
||
|
||
def __init__(self) -> None:
|
||
self.texts: list[dict[str, Any]] = []
|
||
self.cards: list[dict[str, Any]] = []
|
||
|
||
def send_group(self, *, chat_id: str, content: str, mention_userids=None) -> dict:
|
||
self.texts.append({"chat_id": chat_id, "content": content})
|
||
return {"ok": True}
|
||
|
||
def send_group_card(self, *, chat_id: str, template_card: dict) -> dict:
|
||
self.cards.append({"chat_id": chat_id, "template_card": dict(template_card or {})})
|
||
return {"ok": True}
|
||
|
||
|
||
def _payload(**over: Any) -> dict[str, Any]:
|
||
"""一条群 PDF 任务的出站数据(只留补推用得上的字段)。"""
|
||
data = {
|
||
"inquiry_no": "WO202611170001",
|
||
"chat_id": "wr_pdf_1",
|
||
"business_line": "SEA",
|
||
"deal_followup": True,
|
||
"sales_name": "吴季林",
|
||
"quote": {"total": "1000", "currency": "USD", "source_label": "产品报价"},
|
||
"facts": {"起运港": "深圳", "目的港": "新加坡"},
|
||
}
|
||
data.update(over)
|
||
return data
|
||
|
||
|
||
@unittest.skipIf(_emit_group_deal_after_pdf is None, "本机未装 langgraph,跳过 jobs 包用例")
|
||
class PdfGroupDealFollowupTests(unittest.TestCase):
|
||
def test_sends_text_and_card_after_pdf(self) -> None:
|
||
client = _FakeGroupClient()
|
||
_emit_group_deal_after_pdf(_payload(), client, chat_id="wr_pdf_1") # type: ignore[misc]
|
||
self.assertEqual(len(client.texts), 1)
|
||
text = client.texts[0]["content"]
|
||
self.assertIn("吴季林", text)
|
||
self.assertIn("WO202611170001", text)
|
||
self.assertIn("请确认成交状态", text)
|
||
self.assertEqual(len(client.cards), 1)
|
||
body = client.cards[0]["template_card"]
|
||
self.assertEqual(body["card_type"], "button_interaction")
|
||
self.assertTrue(
|
||
str(body.get("main_title", {}).get("title") or "").endswith("WO202611170001")
|
||
)
|
||
self.assertEqual(
|
||
[b["text"] for b in body.get("button_list") or []],
|
||
[copy.BTN_DEAL, copy.BTN_LOST, copy.BTN_NEGOTIATE],
|
||
)
|
||
|
||
def test_without_flag_sends_nothing(self) -> None:
|
||
"""空白表、或调用方明确不要成交卡时不补推。"""
|
||
client = _FakeGroupClient()
|
||
_emit_group_deal_after_pdf( # type: ignore[misc]
|
||
_payload(deal_followup=False), client, chat_id="wr_pdf_1"
|
||
)
|
||
self.assertEqual(client.texts, [])
|
||
self.assertEqual(client.cards, [])
|
||
|
||
def test_missing_chat_or_wo_is_silent(self) -> None:
|
||
client = _FakeGroupClient()
|
||
_emit_group_deal_after_pdf(_payload(chat_id=""), client, chat_id="") # type: ignore[misc]
|
||
_emit_group_deal_after_pdf( # type: ignore[misc]
|
||
_payload(inquiry_no=""), client, chat_id="wr_pdf_1"
|
||
)
|
||
self.assertEqual(client.texts, [])
|
||
self.assertEqual(client.cards, [])
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|