- 新增员工岗位(InquiryStaffRole)实体、服务与后台页面,数据范围默认按岗位,看板由「仅本人」切换为岗位范围,补充业务负责人与角色说明迁移 - 新增企微群 webhook 通道、access_gate/staff_roles 鉴权与智价通/双 BOT 回复配置 - 报价回执增加去重防抖(quote_receipt_debounce),询价原文落库并补单测 - 同义词补陆运类型修正与地名通配 v2,更新种子生成脚本 - 空运/陆运/海运询价流程、报价单按需生成、转人工与多段处理调整 - 后台工单需求字段、BOT 显示名、站点标题与看板配套更新
1756 lines
70 KiB
Python
1756 lines
70 KiB
Python
"""
|
||
询价/空运协同群「工单号识别」话术:
|
||
|
||
- 句中已有工单号:直接按工单办,不再反问工单号;
|
||
- 句中没有工单号、本群也没有当前单:教销售按「已成交WO****@机器人」或
|
||
「WO****已成交@机器人」下达任务。
|
||
|
||
不连企微、真实 TMS、Redis、主账。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import sys
|
||
import unittest
|
||
|
||
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("LLM_DATA_USAGE_CONFIRMED", "false")
|
||
os.environ.setdefault("LLM_ALLOW_NETWORK", "false")
|
||
os.environ.setdefault("LEDGER_BACKEND", "memory")
|
||
|
||
from agent.channel.wecom.models import InboundMessage
|
||
from agent.handlers.air_group_collab import handle_air_group
|
||
from agent.ledger.memory_ledger import MemoryLedger
|
||
from agent.policy import bot_reply
|
||
from agent.policy import handoff_human as hh
|
||
from agent.policy.air_text_flow import AirTextInquiryFlow, FlowSession
|
||
from agent.policy.sea_group_ops import is_handoff_ticket
|
||
from agent.policy import air_group_ops as air_ops
|
||
from agent.routing.air_group_intent import (
|
||
INTENT_ACTIVATE,
|
||
INTENT_DEAL,
|
||
INTENT_OTHER,
|
||
INTENT_QUOTE,
|
||
)
|
||
|
||
from unittest import mock
|
||
|
||
|
||
class FakeGroupClient:
|
||
"""单测假 BOT 出站:只记正文,不打桥。"""
|
||
|
||
def __init__(self) -> None:
|
||
self.sent: list[dict] = []
|
||
|
||
def send_group(
|
||
self,
|
||
*,
|
||
chat_id: str,
|
||
content: str,
|
||
mention_userids: list | None = None,
|
||
) -> dict:
|
||
self.sent.append({"chat_id": chat_id, "content": content})
|
||
return {"ok": True}
|
||
|
||
def send_group_file(self, **kwargs) -> dict: # pragma: no cover - 本文件不出文件
|
||
self.sent.append({"kind": "file", **kwargs})
|
||
return {"ok": True}
|
||
|
||
|
||
class AirGroupWorkOrderHintTests(unittest.TestCase):
|
||
def setUp(self) -> None:
|
||
# 让显示名/回复语走默认值,避免受其它单测的后台注入影响。
|
||
bot_reply.override_catalog(None)
|
||
self.client = FakeGroupClient()
|
||
self.flow = AirTextInquiryFlow(MemoryLedger(), group_client=self.client)
|
||
|
||
def _message(self, content: str) -> InboundMessage:
|
||
return InboundMessage(
|
||
sender_id="u_sales",
|
||
message_id=f"m-{abs(hash(content))}",
|
||
content=content,
|
||
msg_type="text",
|
||
chat_id="wr_air_collab",
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
)
|
||
|
||
def _sent_text(self) -> str:
|
||
return "\n".join(
|
||
str(item.get("content") or "")
|
||
for item in self.client.sent
|
||
if item.get("kind") != "file"
|
||
)
|
||
|
||
def test_no_work_order_teaches_task_format(self) -> None:
|
||
"""没识别到工单号:只教一种写法「已成交WO****@机器人」。"""
|
||
phase = handle_air_group(
|
||
self._message("@智价通 帮我处理一下"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_OTHER,
|
||
)
|
||
self.assertEqual(phase, "need_work_order")
|
||
body = self._sent_text()
|
||
self.assertIn("已成交WO****@", body)
|
||
self.assertNotIn("WO****已成交@", body)
|
||
self.assertIn("下达任务", body)
|
||
|
||
def test_work_order_present_does_not_ask_work_order(self) -> None:
|
||
"""已识别到工单号:按工单办(这里是找不到),不能再反问怎么下达任务。"""
|
||
phase = handle_air_group(
|
||
self._message("已成交WO202609160001@智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_DEAL,
|
||
)
|
||
self.assertEqual(phase, "ticket_missing")
|
||
body = self._sent_text()
|
||
self.assertNotIn("下达任务", body)
|
||
self.assertNotIn("WO****", body)
|
||
self.assertIn("WO202609160001", body)
|
||
|
||
def test_bound_ticket_without_work_order_still_follows_current(self) -> None:
|
||
"""本群已激活当前单:没写工单号也直接办,不弹下达任务提示。"""
|
||
created = self.flow.ledger.create_ticket(
|
||
sender_id="u_sales",
|
||
business_line="AIR",
|
||
facts={"起运港": "南京", "目的港": "吉隆坡", "品名": "普货"},
|
||
)
|
||
no = created["work_order_no"]
|
||
self.flow.ledger.activate_air_group(
|
||
chat_id="wr_air_collab", work_order_no=no
|
||
)
|
||
phase = handle_air_group(
|
||
self._message("已成交@智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_DEAL,
|
||
)
|
||
# 还没有报价,走「没有报价不能成交」的业务句;关键是不能再要工单号。
|
||
self.assertEqual(phase, "deal_need_quote")
|
||
body = self._sent_text()
|
||
self.assertNotIn("下达任务", body)
|
||
self.assertNotIn("WO****", body)
|
||
self.assertIn(no, body)
|
||
|
||
def test_brief_mentions_route_staff_when_origin_not_configured(self) -> None:
|
||
"""
|
||
上海-BLR:后台只配了 CGO-BLR 的航线,摘要也要点到负责目的站的人。
|
||
(生产 WO202611260074 只点到销售,正是这条漏了。)
|
||
"""
|
||
self.flow.ledger.upsert_staff(
|
||
{
|
||
"wecomId": "A0223",
|
||
"name": "肖杰",
|
||
"roleCode": "air",
|
||
"roleCodes": "air",
|
||
"routes": "CGO-BLR,CGO-MAA",
|
||
"status": "active",
|
||
}
|
||
)
|
||
created = self.flow.ledger.create_ticket(
|
||
sender_id="u_sales",
|
||
business_line="AIR",
|
||
facts={"起运港": "上海", "目的港": "BLR", "品名": "普货"},
|
||
)
|
||
no = created["work_order_no"]
|
||
phase = handle_air_group(
|
||
self._message(f"{no}@智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_ACTIVATE,
|
||
)
|
||
self.assertEqual(phase, "activated")
|
||
body = self._sent_text()
|
||
self.assertIn("@肖杰", body)
|
||
self.assertIn("工单号:" + no, body)
|
||
|
||
|
||
class AirHandoffCollabTests(unittest.TestCase):
|
||
"""
|
||
转人工协同:六态不写「转人工」,产品群报价能落账、不生成交跟进。
|
||
|
||
背景:旧实现把「转人工」当六态终态,报价被 handoff_frozen 挡掉,
|
||
群消息也被当冻结单静默丢弃。新流程用 collab_mode=handoff 表达「人工协同」。
|
||
"""
|
||
|
||
def setUp(self) -> None:
|
||
bot_reply.override_catalog(None)
|
||
self.client = FakeGroupClient()
|
||
self.ledger = MemoryLedger()
|
||
self.flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
|
||
def _handoff_ticket(self, facts: dict | None = None) -> str:
|
||
created = self.ledger.create_ticket(
|
||
sender_id="u_sales",
|
||
business_line="AIR",
|
||
facts=facts or {"起运港": "南京", "目的港": "吉隆坡", "品名": "普货"},
|
||
collab_mode="handoff",
|
||
)
|
||
no = str(created["work_order_no"])
|
||
# 各用例的工单号会重号,清掉上一例遗留的待确认,避免被当成同一轮改价。
|
||
from agent.redis_coord.pending_quote_confirm import clear_pending_confirm
|
||
|
||
clear_pending_confirm(no)
|
||
return no
|
||
|
||
def test_commit_does_not_freeze_status(self) -> None:
|
||
no = self._handoff_ticket()
|
||
sess = FlowSession(
|
||
sender_id="u_sales",
|
||
thread_id="u_sales",
|
||
phase="handoff_offer",
|
||
business_line="AIR",
|
||
handoff_offer="intent",
|
||
handoff_work_order_no=no,
|
||
facts={"起运港": "南京", "目的港": "吉隆坡", "品名": "普货"},
|
||
)
|
||
self.flow._save(sess)
|
||
out = hh.try_commit_air_group_handoff(
|
||
self.flow, chat_id="wr_air_collab", work_order_no=no
|
||
)
|
||
self.assertEqual(out, "handoff_done")
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
# 关键:不能写六态「转人工」,否则后续产品报价会被主账拒绝。
|
||
self.assertEqual(ticket.status, "询价中")
|
||
self.assertEqual(ticket.collab_mode, "handoff")
|
||
self.assertEqual(ticket.collab_chat_id, "wr_air_collab")
|
||
self.assertEqual(sess.collab_mode, "handoff")
|
||
self.assertTrue(sess.handoff_committed)
|
||
self.assertTrue(self._sent_text().find("转人工") >= 0)
|
||
|
||
def test_handoff_ticket_is_not_frozen_for_group(self) -> None:
|
||
no = self._handoff_ticket()
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
# 旧书签即使已提交转人工,只要主账是 handoff 协同,群消息仍要收。
|
||
sess = FlowSession(
|
||
sender_id="u_sales",
|
||
thread_id="u_sales",
|
||
business_line="AIR",
|
||
handoff_committed=True,
|
||
collab_mode="handoff",
|
||
)
|
||
self.assertFalse(is_handoff_ticket(ticket))
|
||
self.assertFalse(is_handoff_ticket(ticket, sess))
|
||
|
||
def test_commit_quote_skips_deal_followup(self) -> None:
|
||
no = self._handoff_ticket()
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id="wr_air_collab", member_ids=[], collab_mode="handoff"
|
||
)
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
with mock.patch.object(air_ops, "send_air_brief_and_sheet"), mock.patch.object(
|
||
air_ops, "emit_group_deal"
|
||
) as deal:
|
||
phase = air_ops._commit_quote(
|
||
flow,
|
||
ticket,
|
||
{"total": "12.5", "currency": "CNY"},
|
||
"wr_air_collab",
|
||
"u_air",
|
||
raw_text="南京到吉隆坡 12.5/KG",
|
||
raw_source="text",
|
||
)
|
||
self.assertEqual(phase, "quoted_handoff")
|
||
deal.assert_not_called()
|
||
self.assertEqual(ticket.status, "已报价")
|
||
self.assertEqual(ticket.quote.get("raw_text"), "南京到吉隆坡 12.5/KG")
|
||
|
||
def test_nonstandard_quote_keeps_inquiring_and_keeps_raw(self) -> None:
|
||
no = self._handoff_ticket()
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id="wr_air_collab", member_ids=[], collab_mode="handoff"
|
||
)
|
||
self.ledger._staff_by_wecom["u_air"] = {
|
||
"name": "航线小李",
|
||
"wecomId": "u_air",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
phase = air_ops.apply_air_quote(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
text="这个价格我再问下航线,晚点回你",
|
||
sender_id="u_air",
|
||
chat_id="wr_air_collab",
|
||
)
|
||
self.assertEqual(phase, "quote_raw_saved")
|
||
self.assertEqual(ticket.status, "询价中")
|
||
self.assertIn("晚点回你", ticket.quote.get("raw_text") or "")
|
||
self.assertIn("我无法识别您的内容", self._sent_text())
|
||
|
||
def test_partial_quote_writes_price_and_echoes_recognition(self) -> None:
|
||
"""真机 WO202611230013:缺线路算部分识别,价照常入账,再回「我识别内容为…」。"""
|
||
no = self._handoff_ticket(
|
||
facts={
|
||
"起运港": "香港",
|
||
"目的港": "DEL",
|
||
"品名": "普货",
|
||
"报价日期": "2026-11-23",
|
||
}
|
||
)
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id="wr_air_collab", member_ids=[], collab_mode="handoff"
|
||
)
|
||
self.ledger._staff_by_wecom["u_air"] = {
|
||
"name": "航线小李",
|
||
"wecomId": "u_air",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
phase = air_ops.apply_air_quote(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
text="USD11/kg,\nTH、11月23日",
|
||
sender_id="u_air",
|
||
chat_id="wr_air_collab",
|
||
)
|
||
self.assertIn(phase, {"quoted_handoff", "quoted"})
|
||
self.assertEqual(ticket.status, "已报价")
|
||
texts = self._sent_text()
|
||
self.assertIn("我识别内容为", texts)
|
||
self.assertIn("空运费:USD11/kg", texts)
|
||
self.assertIn("航司:TH", texts)
|
||
self.assertIn("线路:香港-DEL", texts)
|
||
# 回执要 @ 当前报价人员(这里是航线小李)。
|
||
self.assertIn("@航线小李", texts)
|
||
self.assertIn("请确认是否正确", texts)
|
||
|
||
def test_quote_waits_for_product_confirmation(self) -> None:
|
||
"""产品 2026-11-24:先回识别回执,航线回「确认」后才出价格方案。"""
|
||
import os
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
no = self._handoff_ticket(
|
||
facts={
|
||
"起运港": "南京",
|
||
"目的港": "吉隆坡",
|
||
"品名": "普货",
|
||
"报价日期": "2026-11-24",
|
||
}
|
||
)
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id="wr_air_collab", member_ids=[], collab_mode="handoff"
|
||
)
|
||
self.ledger._staff_by_wecom["u_air"] = {
|
||
"name": "航线小李",
|
||
"wecomId": "u_air",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
phase = air_ops.apply_air_quote(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
text="USD11/kg、TH、NKG-KUL、11月23日",
|
||
sender_id="u_air",
|
||
chat_id="wr_air_collab",
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
# 后台「处理人」确认前就要能看到报价人,不能等落价(2026-11-25 反馈)。
|
||
self.assertIn(
|
||
"u_air", self.ledger.get_ticket(work_order_no=no).collab_actor_ids
|
||
)
|
||
self.assertIn(f"{no} 我识别内容为:", self._sent_text())
|
||
self.assertIn("航线日期:2026-11-23", self._sent_text())
|
||
self.assertEqual(self.ledger.get_ticket(work_order_no=no).status, "询价中")
|
||
confirm = InboundMessage(
|
||
sender_id="u_air",
|
||
message_id="m-air-confirm",
|
||
content="正确",
|
||
msg_type="text",
|
||
chat_id="wr_air_collab",
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
)
|
||
phase2 = air_ops.confirm_pending_air_quote(
|
||
flow, self.ledger.get_ticket(work_order_no=no), confirm
|
||
)
|
||
self.assertIn(phase2, {"quoted", "quoted_handoff"})
|
||
self.assertEqual(self.ledger.get_ticket(work_order_no=no).status, "已报价")
|
||
self.assertIn("空运费:USD11/kg", self._sent_text())
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_work_order_plus_confirm_commits_quote_without_reasking(self) -> None:
|
||
"""
|
||
真机 2026-12-01(WO202612010067):回执后航线回「工单号+是@机器人」,
|
||
必须落账并汇总人工报价;不能被当成切单再发「TMS 暂无匹配报价/请另报」。
|
||
"""
|
||
import os
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
no = self._handoff_ticket(
|
||
facts={
|
||
"起运港": "CGO",
|
||
"目的港": "BLR",
|
||
"品名": "黑色硒鼓",
|
||
"报价日期": "2026-12-01",
|
||
}
|
||
)
|
||
# 单独一个群号:入站去重按「群+人+去@原文」认同一句,MemoryLedger 每例
|
||
# 都从 WO…0001 起号,和本文件其它「工单号+是」用例共用群号会被当成重复。
|
||
room = "wr_air_confirm_wo"
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id=room, member_ids=[], collab_mode="handoff"
|
||
)
|
||
self.ledger._staff_by_wecom["u_air"] = {
|
||
"name": "航线石畅畅",
|
||
"wecomId": "u_air",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
phase = air_ops.apply_air_quote(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
text="CNY15.3/kg、SQ、CAN-SIN",
|
||
sender_id="u_air",
|
||
chat_id=room,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
self.client.sent.clear()
|
||
confirm = InboundMessage(
|
||
sender_id="u_air",
|
||
message_id="m-air-confirm-wo",
|
||
content=f"{no} 是",
|
||
msg_type="text",
|
||
chat_id=room,
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
)
|
||
# 模型把「工单号+是」判成激活也不能抢走确认:这是本次 BUG 的入口。
|
||
phase2 = handle_air_group(
|
||
confirm, flow=flow, injected_intent=INTENT_ACTIVATE
|
||
)
|
||
self.assertIn(phase2, {"quoted", "quoted_handoff"})
|
||
self.assertEqual(self.ledger.get_ticket(work_order_no=no).status, "已报价")
|
||
body = self._sent_text()
|
||
self.assertIn("人工报价", body)
|
||
self.assertIn("空运费:CNY15.3/kg", body)
|
||
# 不能再重发「暂无匹配报价 + 请另报」,否则群里看着像重新让航线报价。
|
||
self.assertNotIn("TMS 暂无匹配报价", body)
|
||
self.assertNotIn("纯文字", body)
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
from agent.redis_coord.pending_quote_confirm import clear_pending_confirm
|
||
|
||
clear_pending_confirm(no)
|
||
|
||
def test_reactivate_while_quote_pending_keeps_pending_quote(self) -> None:
|
||
"""报价待确认时再发工单号:摘要不能退回报价前那版,要带出待确认报价。"""
|
||
import os
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
raw = "空运目的地BLR茶叶 4吨/2件/2CBM托盘客户随时可送货到仓"
|
||
created = self.ledger.create_ticket(
|
||
sender_id="u_sales",
|
||
business_line="AIR",
|
||
facts={
|
||
"运输方式": "空运",
|
||
"目的地": "班加罗尔",
|
||
"品名": "茶叶",
|
||
"件数": "2件",
|
||
},
|
||
collab_mode="handoff",
|
||
raw_text=raw,
|
||
)
|
||
no = str(created["work_order_no"])
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id="wr_air_pending", member_ids=[], collab_mode="handoff"
|
||
)
|
||
self.ledger._staff_by_wecom["u_air"] = {
|
||
"name": "航线小李",
|
||
"wecomId": "u_air",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
phase = air_ops.apply_air_quote(
|
||
flow=flow,
|
||
ticket=self.ledger.get_ticket(work_order_no=no),
|
||
text="CNY11/KG",
|
||
sender_id="u_air",
|
||
chat_id="wr_air_pending",
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
self.client.sent.clear()
|
||
# 报价还没确认,群里又发了一次工单号(再激活)。
|
||
again = handle_air_group(
|
||
InboundMessage(
|
||
sender_id="u_air",
|
||
message_id="m-air-pending-wo",
|
||
content=f"{no}@智价通",
|
||
msg_type="text",
|
||
chat_id="wr_air_pending",
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
),
|
||
flow=flow,
|
||
)
|
||
self.assertIn(again, {"activated_handoff", "activated"})
|
||
body = self._sent_text()
|
||
# 摘要仍列询价字段,但必须带上待确认报价,不能再教一遍怎么报价。
|
||
self.assertIn("询价字段:", body)
|
||
self.assertIn("待确认人工报价(回复「确认」后生效):", body)
|
||
self.assertIn("空运费:CNY11/kg", body)
|
||
self.assertNotIn("航线人员可通过", body)
|
||
# 待确认状态不能被再激活清掉。
|
||
self.assertEqual(self.ledger.get_ticket(work_order_no=no).status, "询价中")
|
||
# 航线已响应、等的是确认:再激活不能重新开始「航线响应超时」计时,
|
||
# 但待确认期间要保留 deal(air_sales 3 小时),否则巡检整单跳过。
|
||
self.assertEqual(
|
||
self.ledger.get_ticket(work_order_no=no).event_wait_kind, "deal"
|
||
)
|
||
|
||
self.client.sent.clear()
|
||
confirmed = handle_air_group(
|
||
InboundMessage(
|
||
sender_id="u_air",
|
||
message_id="m-air-pending-ok",
|
||
content="确认@智价通",
|
||
msg_type="text",
|
||
chat_id="wr_air_pending",
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
),
|
||
flow=flow,
|
||
)
|
||
self.assertIn(confirmed, {"quoted", "quoted_handoff"})
|
||
body = self._sent_text()
|
||
self.assertIn("人工报价:", body)
|
||
self.assertNotIn("待确认人工报价", body)
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_quote_pending_clears_air_quote_wait(self) -> None:
|
||
"""
|
||
航线报出可识别的价后,要停「航线响应超时」计时,改盯销售响应。
|
||
|
||
2026-12-02 真机 WO202612020007:航线 10:08 已报价并回执,10:24 还被催
|
||
「航线响应超时」。待确认分支不出价也不落账,必须自己清掉等待;
|
||
但清掉后不能再让 event_wait_kind 为空——否则 2026-12-03 WO202612030001
|
||
的 air_sales 3 小时销售超时永远不会触发。
|
||
"""
|
||
import os
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
no = self._handoff_ticket(
|
||
facts={
|
||
"起运港": "CAN",
|
||
"目的港": "SIN",
|
||
"品名": "普货",
|
||
"报价日期": "2026-12-02",
|
||
}
|
||
)
|
||
room = "wr_air_wait_clear"
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id=room, member_ids=[], collab_mode="handoff"
|
||
)
|
||
self.ledger._staff_by_wecom["u_air"] = {
|
||
"name": "航线小李",
|
||
"wecomId": "u_air",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
ticket.event_wait_kind = "air_quote"
|
||
ticket.event_wait_started_at = "2026-12-02 10:09:00"
|
||
flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
phase = air_ops.apply_air_quote(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
text="CNY12/kg、TH、CAN-SIN、12月10日",
|
||
sender_id="u_air",
|
||
chat_id=room,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
pending_ticket = self.ledger.get_ticket(work_order_no=no)
|
||
self.assertEqual(pending_ticket.event_wait_kind, "deal")
|
||
self.assertTrue(pending_ticket.event_wait_started_at)
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
from agent.redis_coord.pending_quote_confirm import clear_pending_confirm
|
||
|
||
clear_pending_confirm(no)
|
||
|
||
def test_silent_activate_keeps_deal_wait(self) -> None:
|
||
"""句中带工单号的静默切单不能把成交等待改回航线报价等待。"""
|
||
from agent.redis_coord.pending_quote_confirm import clear_pending_confirm
|
||
|
||
created = self.ledger.create_ticket(
|
||
sender_id="u_sales",
|
||
business_line="AIR",
|
||
facts={"起运港": "CAN", "目的港": "SIN", "品名": "普货"},
|
||
)
|
||
no = str(created["work_order_no"])
|
||
clear_pending_confirm(no)
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
ticket.status = "已报价"
|
||
ticket.event_wait_kind = "deal"
|
||
ticket.event_wait_started_at = "2026-12-02 10:04:02"
|
||
ticket.event_wait_alerted = 1
|
||
flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
got, phase = air_ops.activate_ticket(
|
||
flow=flow, chat_id="wr_air_silent", work_order_no=no, speak=False
|
||
)
|
||
self.assertIsNotNone(got)
|
||
self.assertEqual(phase, "activated")
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
self.assertEqual(ticket.event_wait_kind, "deal")
|
||
self.assertEqual(ticket.event_wait_alerted, 1)
|
||
|
||
def test_quote_receipt_lists_extra_fields(self) -> None:
|
||
"""件数/毛重/体积/包装/品名/计费重也要识别并写进回执(尽可能识别)。"""
|
||
import os
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
no = self._handoff_ticket(
|
||
facts={
|
||
"起运港": "NKG",
|
||
"目的港": "KUL",
|
||
"品名": "普货",
|
||
"报价日期": "2026-11-24",
|
||
}
|
||
)
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id="wr_air_collab", member_ids=[], collab_mode="handoff"
|
||
)
|
||
self.ledger._staff_by_wecom["u_air"] = {
|
||
"name": "航线小李",
|
||
"wecomId": "u_air",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
text = (
|
||
"1:300,+500,USD11/kg,卡转/MUST GO、TH、NKG-KUL、11月24日,"
|
||
"件数2件,毛重500KG,体积3CBM,包装纸箱,品名普货,计费重800KG"
|
||
)
|
||
phase = air_ops.apply_air_quote(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
text=text,
|
||
sender_id="u_air",
|
||
chat_id="wr_air_collab",
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
sent = self._sent_text()
|
||
for expected in (
|
||
"件数:2件",
|
||
"毛重:500KG",
|
||
"体积:3CBM",
|
||
# 同义词表:纸箱/袋/CTN 统一归「散货」标准值(装箱单/TMS 口径)。
|
||
"包装类型:散货",
|
||
"品名:普货",
|
||
"计费重:800KG",
|
||
):
|
||
self.assertIn(expected, sent)
|
||
updated = self.ledger.get_ticket(work_order_no=no)
|
||
self.assertEqual(updated.facts.get("件数"), "2件")
|
||
self.assertEqual(updated.facts.get("毛重"), "500KG")
|
||
self.assertEqual(updated.facts.get("体积"), "3CBM")
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_pending_quote_airline_correction_not_lock(self) -> None:
|
||
"""待确认报价时产品说「航司改为KUL」改的是航司,不是线路,也不能回「先确认已成交」。"""
|
||
import os
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
no = self._handoff_ticket(
|
||
facts={
|
||
"起运港": "香港",
|
||
"目的港": "DEL",
|
||
"品名": "普货",
|
||
"报价日期": "2026-11-24",
|
||
}
|
||
)
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id="wr_air_collab", member_ids=[], collab_mode="handoff"
|
||
)
|
||
self.ledger._staff_by_wecom["u_air"] = {
|
||
"name": "航线小李",
|
||
"wecomId": "u_air",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
phase = air_ops.apply_air_quote(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
text="USD11/kg、TH、DEL、11月23日",
|
||
sender_id="u_air",
|
||
chat_id="wr_air_collab",
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
self.client.sent.clear()
|
||
correction = InboundMessage(
|
||
sender_id="u_air",
|
||
message_id="m-air-correct",
|
||
content="这个航班没有了,航司给我改为KUL",
|
||
msg_type="text",
|
||
chat_id="wr_air_collab",
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
)
|
||
phase2 = air_ops.try_pending_air_correction(
|
||
flow, self.ledger.get_ticket(work_order_no=no), correction
|
||
)
|
||
self.assertEqual(phase2, "quote_confirm_pending")
|
||
text = self._sent_text()
|
||
self.assertIn("航司:KUL", text)
|
||
self.assertIn("线路:香港-DEL", text)
|
||
self.assertNotIn("已成交", text)
|
||
from agent.redis_coord.pending_quote_confirm import load_pending_confirm
|
||
|
||
pending = load_pending_confirm(no)
|
||
self.assertEqual(pending.get("airline_override"), "KUL")
|
||
self.assertEqual((pending.get("quote") or {}).get("airlineCode"), "KUL")
|
||
self.assertFalse(pending.get("route_override"))
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_pending_quote_weight_break_correction(self) -> None:
|
||
"""「重量等级改为+300」改的是重量等级,300 不能当空运费。"""
|
||
import os
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
no = self._handoff_ticket(
|
||
facts={
|
||
"起运港": "NKG",
|
||
"目的港": "KUL",
|
||
"品名": "普货",
|
||
"报价日期": "2026-11-24",
|
||
}
|
||
)
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id="wr_air_collab", member_ids=[], collab_mode="handoff"
|
||
)
|
||
self.ledger._staff_by_wecom["u_air"] = {
|
||
"name": "航线小李",
|
||
"wecomId": "u_air",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
phase = air_ops.apply_air_quote(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
text="1:300,+500,USD11/kg,卡转/MUST GO、TH、NKG-KUL、11月24日",
|
||
sender_id="u_air",
|
||
chat_id="wr_air_collab",
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
self.client.sent.clear()
|
||
correction = InboundMessage(
|
||
sender_id="u_air",
|
||
message_id="m-air-weight",
|
||
content="重量等级改为+300",
|
||
msg_type="text",
|
||
chat_id="wr_air_collab",
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
)
|
||
phase2 = air_ops.try_pending_air_correction(
|
||
flow, self.ledger.get_ticket(work_order_no=no), correction
|
||
)
|
||
self.assertEqual(phase2, "quote_confirm_pending")
|
||
text = self._sent_text()
|
||
self.assertIn("重量等级:+300", text)
|
||
self.assertIn("空运费:USD11/kg", text)
|
||
self.assertNotIn("空运费:300", text)
|
||
from agent.redis_coord.pending_quote_confirm import load_pending_confirm
|
||
|
||
pending = load_pending_confirm(no)
|
||
self.assertEqual(pending.get("weight_break_override"), "+300")
|
||
self.assertEqual((pending.get("quote") or {}).get("weight_break"), "+300")
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_pending_quote_ratio_and_date_correction(self) -> None:
|
||
"""「比例改为1:200」「日期改为11月25日」不能把数字当空运费。"""
|
||
import os
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
no = self._handoff_ticket(
|
||
facts={
|
||
"起运港": "NKG",
|
||
"目的港": "KUL",
|
||
"品名": "普货",
|
||
"报价日期": "2026-11-24",
|
||
}
|
||
)
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id="wr_air_collab", member_ids=[], collab_mode="handoff"
|
||
)
|
||
self.ledger._staff_by_wecom["u_air"] = {
|
||
"name": "航线小李",
|
||
"wecomId": "u_air",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
phase = air_ops.apply_air_quote(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
text="1:300,+500,USD11/kg、TH、NKG-KUL、11月24日",
|
||
sender_id="u_air",
|
||
chat_id="wr_air_collab",
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
ratio_msg = InboundMessage(
|
||
sender_id="u_air",
|
||
message_id="m-air-ratio",
|
||
content="比例改为1:200",
|
||
msg_type="text",
|
||
chat_id="wr_air_collab",
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
)
|
||
phase2 = air_ops.try_pending_air_correction(
|
||
flow, self.ledger.get_ticket(work_order_no=no), ratio_msg
|
||
)
|
||
self.assertEqual(phase2, "quote_confirm_pending")
|
||
ratio_text = self._sent_text()
|
||
self.assertIn("比例:1:200", ratio_text)
|
||
self.assertIn("空运费:USD11/kg", ratio_text)
|
||
self.assertNotIn("空运费:1", ratio_text)
|
||
date_msg = InboundMessage(
|
||
sender_id="u_air",
|
||
message_id="m-air-date",
|
||
content="日期改为11月25日",
|
||
msg_type="text",
|
||
chat_id="wr_air_collab",
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
)
|
||
phase3 = air_ops.try_pending_air_correction(
|
||
flow, self.ledger.get_ticket(work_order_no=no), date_msg
|
||
)
|
||
self.assertEqual(phase3, "quote_confirm_pending")
|
||
date_text = self._sent_text()
|
||
self.assertIn("航线日期:2026-11-25", date_text)
|
||
self.assertNotIn("空运费:11", date_text)
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_pending_quote_multiple_corrections_one_message(self) -> None:
|
||
"""一句里「重量等级改为+300,航司改为KUL」要两项都更新。"""
|
||
import os
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
no = self._handoff_ticket(
|
||
facts={
|
||
"起运港": "NKG",
|
||
"目的港": "KUL",
|
||
"品名": "普货",
|
||
"报价日期": "2026-11-24",
|
||
}
|
||
)
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id="wr_air_collab", member_ids=[], collab_mode="handoff"
|
||
)
|
||
self.ledger._staff_by_wecom["u_air"] = {
|
||
"name": "航线小李",
|
||
"wecomId": "u_air",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
phase = air_ops.apply_air_quote(
|
||
flow=flow,
|
||
ticket=ticket,
|
||
text="1:300,+500,USD11/kg、TH、NKG-KUL、11月24日",
|
||
sender_id="u_air",
|
||
chat_id="wr_air_collab",
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
self.client.sent.clear()
|
||
correction = InboundMessage(
|
||
sender_id="u_air",
|
||
message_id="m-air-multi",
|
||
content="重量等级改为+300,航司改为KUL",
|
||
msg_type="text",
|
||
chat_id="wr_air_collab",
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
)
|
||
phase2 = air_ops.try_pending_air_correction(
|
||
flow, self.ledger.get_ticket(work_order_no=no), correction
|
||
)
|
||
self.assertEqual(phase2, "quote_confirm_pending")
|
||
text = self._sent_text()
|
||
self.assertIn("重量等级:+300", text)
|
||
self.assertIn("航司:KUL", text)
|
||
self.assertNotIn("航司:TH", text)
|
||
from agent.redis_coord.pending_quote_confirm import load_pending_confirm
|
||
|
||
pending = load_pending_confirm(no)
|
||
self.assertEqual(pending.get("weight_break_override"), "+300")
|
||
self.assertEqual(pending.get("airline_override"), "KUL")
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def _sent_text(self) -> str:
|
||
return "\n".join(
|
||
str(item.get("content") or "")
|
||
for item in self.client.sent
|
||
if item.get("kind") != "file"
|
||
)
|
||
|
||
|
||
class AirConfirmTmsCopyTests(unittest.TestCase):
|
||
"""
|
||
空运协同群「确认报价」(沿用 TMS):只落模板,群里没上传附件。
|
||
|
||
正式环境 2026-11-29 BUG:航线回确认报价后,BOT 却提示
|
||
「附件已保存后台,智能体仅将附件保存在后台」,实际根本没有附件。
|
||
"""
|
||
|
||
def setUp(self) -> None:
|
||
bot_reply.override_catalog(None)
|
||
self.client = FakeGroupClient()
|
||
self.ledger = MemoryLedger()
|
||
self.flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
|
||
def _tms_ticket(self) -> str:
|
||
created = self.ledger.create_ticket(
|
||
sender_id="u_sales",
|
||
business_line="AIR",
|
||
facts={"起运港": "南京", "目的港": "吉隆坡", "品名": "普货"},
|
||
collab_mode="collab",
|
||
)
|
||
no = str(created["work_order_no"])
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id="wr_air_collab", member_ids=[]
|
||
)
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
ticket.quote = {
|
||
"total": "9585.25",
|
||
"source": "TMS",
|
||
"source_label": "TMS",
|
||
"airline": "CZ",
|
||
}
|
||
return no
|
||
|
||
def test_confirm_tms_does_not_claim_attachment_saved(self) -> None:
|
||
no = self._tms_ticket()
|
||
with mock.patch.object(air_ops, "is_airline", return_value=True):
|
||
phase = air_ops.apply_air_confirm_tms(
|
||
flow=self.flow,
|
||
ticket=self.ledger.get_ticket(work_order_no=no),
|
||
sender_id="u_airline",
|
||
chat_id="wr_air_collab",
|
||
)
|
||
self.assertEqual(phase, "quote_confirmed")
|
||
texts = "\n".join(
|
||
str(item.get("content") or "")
|
||
for item in self.client.sent
|
||
if item.get("kind") != "file"
|
||
)
|
||
# 没附件就不能说「附件已保存后台」;成交跟进照发。
|
||
self.assertNotIn("附件已保存", texts)
|
||
self.assertIn("成交", texts)
|
||
|
||
|
||
class AirPendingQuoteConfirmWithWorkOrderTests(unittest.TestCase):
|
||
"""
|
||
真机 2026-12-01 BUG:同一工单第二次手工改价,航线带工单号回「是」。
|
||
|
||
带工单号回确认曾被当成切单:激活分支看到旧报价,直接发
|
||
「报价单已发送,请确认成交状态」,本次手工报价既没落账也没汇总。
|
||
正确顺序:先汇总本次报价,再发成交跟进。
|
||
"""
|
||
|
||
def setUp(self) -> None:
|
||
bot_reply.override_catalog(None)
|
||
# 新 MemoryLedger 的工单号从同一串开始,重复跑同一句会被入站去重挡掉。
|
||
from agent.redis_coord.air_inbound import reset_air_inbound_for_tests
|
||
|
||
reset_air_inbound_for_tests()
|
||
# 同一进程内各用例的工单号会重号;群也按用例分开,避免入站去重把后来的句子挡掉。
|
||
self.chat = f"wr_air_pending_{self._testMethodName}"
|
||
self.client = FakeGroupClient()
|
||
self.ledger = MemoryLedger()
|
||
self.flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
self.ledger._staff_by_wecom["u_air"] = {
|
||
"name": "航线小李",
|
||
"wecomId": "u_air",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
|
||
def _ticket(self) -> str:
|
||
created = self.ledger.create_ticket(
|
||
sender_id="u_sales",
|
||
business_line="AIR",
|
||
facts={"起运港": "CAN", "目的港": "SIN", "品名": "普货"},
|
||
collab_mode="collab",
|
||
)
|
||
no = str(created["work_order_no"])
|
||
# 各用例的工单号会重号,清掉上一例遗留的待确认,避免被当成同一轮改价。
|
||
from agent.redis_coord.pending_quote_confirm import clear_pending_confirm
|
||
|
||
clear_pending_confirm(no)
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id=self.chat, member_ids=[]
|
||
)
|
||
return no
|
||
|
||
def _message(self, content: str) -> InboundMessage:
|
||
return InboundMessage(
|
||
sender_id="u_air",
|
||
message_id=f"m-air-{abs(hash(content))}",
|
||
content=content,
|
||
msg_type="text",
|
||
chat_id=self.chat,
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
)
|
||
|
||
def _texts(self) -> list[str]:
|
||
return [
|
||
str(item.get("content") or "")
|
||
for item in self.client.sent
|
||
if item.get("kind") != "file"
|
||
]
|
||
|
||
def test_pending_confirm_keeps_deal_wait_for_sales_timeout(self) -> None:
|
||
"""
|
||
2026-12-03 WO202612030001:报价回执后、确认前必须保留 deal 等待。
|
||
|
||
待确认分支原本只 clear_wait(air_quote),工单 event_wait_kind 为空,
|
||
Java listEventWaits 用 isNotNull 过滤,巡检整单跳过,air_sales 3 小时
|
||
销售超时永远不会触发。这里锁住「回执后等待仍在巡检名单里」。
|
||
"""
|
||
import os
|
||
|
||
from agent.redis_coord.pending_quote_confirm import clear_pending_confirm
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
no = ""
|
||
try:
|
||
no = self._ticket()
|
||
phase = air_ops.apply_air_quote(
|
||
flow=self.flow,
|
||
ticket=self.ledger.get_ticket(work_order_no=no),
|
||
text="CNY11/kg",
|
||
sender_id="u_air",
|
||
chat_id=self.chat,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
self.assertEqual(ticket.event_wait_kind, "deal")
|
||
self.assertTrue(ticket.event_wait_started_at)
|
||
waits = self.ledger.list_event_waits()
|
||
self.assertIn(no, [w.get("work_order_no") for w in waits])
|
||
|
||
# 待确认期间的纠错不重置起点:第一次 3 小时提醒不能被改价拖后。
|
||
ticket.event_wait_started_at = "2026-12-03 10:29:00"
|
||
corrected = air_ops.try_pending_air_correction(
|
||
self.flow,
|
||
self.ledger.get_ticket(work_order_no=no),
|
||
InboundMessage(
|
||
sender_id="u_air",
|
||
message_id="m-air-pending-correct",
|
||
content="比例改为1:300",
|
||
msg_type="text",
|
||
chat_id=self.chat,
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
),
|
||
)
|
||
self.assertEqual(corrected, "quote_confirm_pending")
|
||
after = self.ledger.get_ticket(work_order_no=no)
|
||
self.assertEqual(after.event_wait_kind, "deal")
|
||
self.assertEqual(after.event_wait_started_at, "2026-12-03 10:29:00")
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
if no:
|
||
clear_pending_confirm(no)
|
||
|
||
def test_price_with_work_order_is_quote_not_stale_deal(self) -> None:
|
||
"""工单号+新价格同一句:先出识别回执,不能先发旧报价的成交跟进。"""
|
||
import os
|
||
|
||
from agent.redis_coord.pending_quote_confirm import clear_pending_confirm
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
no = self._ticket()
|
||
# 先落一版旧报价(CNY11),模拟「同一工单第二次改价」。
|
||
phase = handle_air_group(
|
||
self._message(f"CNY11/kg; CAN-SIN-CGK {no} @智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
phase = handle_air_group(
|
||
self._message(f"@智价通 {no} 正确"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_ACTIVATE,
|
||
)
|
||
self.assertIn(phase, {"quoted", "quoted_handoff"})
|
||
self.client.sent.clear()
|
||
|
||
# 模型把「工单号+新价」判成切单/其它时,也只能回识别回执,
|
||
# 不能按旧报价先发「报价单已发送,请确认成交状态」。
|
||
for misread, price in ((INTENT_ACTIVATE, "16.3"), (INTENT_OTHER, "16.4")):
|
||
with self.subTest(intent=misread):
|
||
phase = handle_air_group(
|
||
self._message(f"CNY{price}/kg; CAN-SIN-CGK {no} @智价通"),
|
||
flow=self.flow,
|
||
injected_intent=misread,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
texts = self._texts()
|
||
self.assertTrue(any("我识别内容为" in t for t in texts), texts)
|
||
self.assertTrue(any(f"CNY{price}/kg" in t for t in texts), texts)
|
||
self.assertFalse(
|
||
any("请确认成交状态" in t for t in texts), texts
|
||
)
|
||
self.assertFalse(any("报价单已发送" in t for t in texts), texts)
|
||
self.client.sent.clear()
|
||
clear_pending_confirm(no)
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_second_manual_quote_summarizes_before_deal(self) -> None:
|
||
import os
|
||
|
||
from agent.redis_coord.pending_quote_confirm import load_pending_confirm
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
no = self._ticket()
|
||
# 第一次手工报价:回执 + 确认后落成「已报价」。
|
||
phase = handle_air_group(
|
||
self._message(f"CNY16.0/kg; CAN-SIN-CGK {no} @智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
phase = handle_air_group(
|
||
self._message(f"@智价通 {no} 正确"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_ACTIVATE,
|
||
)
|
||
self.assertIn(phase, {"quoted", "quoted_handoff"})
|
||
self.assertEqual(self.ledger.get_ticket(work_order_no=no).status, "已报价")
|
||
self.client.sent.clear()
|
||
|
||
# 同一工单第二次手工改价;确认时航线把工单号一起带上。
|
||
phase = handle_air_group(
|
||
self._message(f"CNY16.3/kg; CAN-SIN-CGK {no} @智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
# 模型可能把「WO… 是」判成切单;确认不能被激活分支吞掉。
|
||
phase = handle_air_group(
|
||
self._message(f"@智价通 {no} 是"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_ACTIVATE,
|
||
)
|
||
self.assertEqual(phase, "quoted")
|
||
self.assertEqual(load_pending_confirm(no), {})
|
||
self.assertEqual(self.ledger.get_ticket(work_order_no=no).status, "已报价")
|
||
|
||
texts = self._texts()
|
||
summary_at = next(
|
||
(i for i, t in enumerate(texts) if "16.3" in t and "空运费" in t),
|
||
None,
|
||
)
|
||
deal_at = next(
|
||
(i for i, t in enumerate(texts) if "请确认成交状态" in t),
|
||
None,
|
||
)
|
||
self.assertIsNotNone(summary_at, texts)
|
||
self.assertIsNotNone(deal_at, texts)
|
||
self.assertLess(summary_at, deal_at)
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_price_with_correction_keeps_new_price(self) -> None:
|
||
"""「航司改为TH,空运费18.3/kg」不能被纠错分支吞掉新价、按旧价重发回执。"""
|
||
import os
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
no = self._ticket()
|
||
phase = handle_air_group(
|
||
self._message(f"CNY16.3/kg; CAN-SIN-CGK {no} @智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
self.client.sent.clear()
|
||
|
||
phase = handle_air_group(
|
||
self._message("航司改为TH,空运费18.3/kg"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_OTHER,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
texts = self._texts()
|
||
self.assertEqual(len(texts), 1, texts)
|
||
self.assertIn("18.3", texts[0])
|
||
self.assertNotIn("16.3", texts[0])
|
||
self.assertIn("航司:TH", texts[0])
|
||
# 改价也同步最新内容并再次请确认;连续消息由防抖合并。
|
||
self.assertIn("已更新识别内容", texts[0])
|
||
self.assertIn("请确认是否正确", texts[0])
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_other_sender_same_quote_still_gets_receipt(self) -> None:
|
||
"""不同人重复同一报价:不能因内容相同静默,仍要回执并请确认。"""
|
||
import os
|
||
from dataclasses import replace
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
self.ledger._staff_by_wecom["u_air2"] = {
|
||
"name": "航线小王",
|
||
"wecomId": "u_air2",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
self._ticket()
|
||
handle_air_group(
|
||
self._message("HT,CNY16/kg"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
first = self._texts()[-1]
|
||
self.assertIn("16", first)
|
||
self.assertIn("请确认是否正确", first)
|
||
self.client.sent.clear()
|
||
|
||
second = replace(
|
||
self._message("HT,CNY16/kg"),
|
||
sender_id="u_air2",
|
||
message_id="m-air-dup-other-sender",
|
||
)
|
||
phase = handle_air_group(
|
||
second,
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
texts = self._texts()
|
||
self.assertEqual(len(texts), 1, texts)
|
||
self.assertIn("16", texts[0])
|
||
self.assertIn("请确认是否正确", texts[0])
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_bare_confirm_binds_single_pending_quote_in_group(self) -> None:
|
||
"""引用回执只回「是」:当前单没待确认、本群只有另一张单待确认时,归给那张单。"""
|
||
import os
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
pending_no = self._ticket()
|
||
pending_ticket = self.ledger.get_ticket(work_order_no=pending_no)
|
||
phase = air_ops.apply_air_quote(
|
||
flow=self.flow,
|
||
ticket=pending_ticket,
|
||
text="HT,CNY18/kg",
|
||
sender_id="u_air",
|
||
chat_id=self.chat,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
self.client.sent.clear()
|
||
|
||
active_no = self._ticket()
|
||
self.assertNotEqual(active_no, pending_no)
|
||
phase = handle_air_group(
|
||
self._message("@智价通 是"),
|
||
flow=self.flow,
|
||
injected_intent="",
|
||
)
|
||
self.assertIn(phase, {"quoted", "quoted_handoff"})
|
||
from agent.redis_coord.pending_quote_confirm import load_pending_confirm
|
||
|
||
self.assertEqual(load_pending_confirm(pending_no), {})
|
||
self.assertEqual(
|
||
self.ledger.get_ticket(work_order_no=pending_no).status, "已报价"
|
||
)
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_multi_pending_bare_confirm_uses_latest(self) -> None:
|
||
"""同群多张待确认:裸确认归给最近更新的一张,旧单不误确认。"""
|
||
import os
|
||
import time
|
||
|
||
import agent.redis_coord.pending_quote_confirm as pqc
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
old_no = self._ticket()
|
||
air_ops.apply_air_quote(
|
||
flow=self.flow,
|
||
ticket=self.ledger.get_ticket(work_order_no=old_no),
|
||
text="HT,CNY17/kg",
|
||
sender_id="u_air",
|
||
chat_id=self.chat,
|
||
)
|
||
new_no = self._ticket()
|
||
air_ops.apply_air_quote(
|
||
flow=self.flow,
|
||
ticket=self.ledger.get_ticket(work_order_no=new_no),
|
||
text="HT,CNY18/kg",
|
||
sender_id="u_air",
|
||
chat_id=self.chat,
|
||
)
|
||
# 老单的待确认时间往前拨 1 小时,模拟同群遗留的旧待确认。
|
||
if old_no in pqc._local:
|
||
pqc._local[old_no]["_ts"] = time.time() - 3600
|
||
pqc._local[old_no]["receipt_queued_at"] = time.time() - 3600
|
||
|
||
active_no = self._ticket()
|
||
self.assertNotEqual(active_no, new_no)
|
||
phase = handle_air_group(
|
||
self._message("@智价通 是"),
|
||
flow=self.flow,
|
||
injected_intent="",
|
||
)
|
||
self.assertIn(phase, {"quoted", "quoted_handoff"})
|
||
self.assertEqual(pqc.load_pending_confirm(new_no), {})
|
||
self.assertEqual(
|
||
self.ledger.get_ticket(work_order_no=new_no).status, "已报价"
|
||
)
|
||
self.assertTrue(pqc.load_pending_confirm(old_no))
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_noop_correction_does_not_resend_identical_receipt(self) -> None:
|
||
"""航司本来就是 TH 时再说「航司改为TH」,不能再发一条一模一样的确认。"""
|
||
import os
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
no = self._ticket()
|
||
phase = handle_air_group(
|
||
self._message(f"CNY16.3/kg、TH、CAN-SIN {no} @智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
self.assertTrue(any("航司:TH" in t for t in self._texts()), self._texts())
|
||
self.client.sent.clear()
|
||
|
||
phase = handle_air_group(
|
||
self._message("航司改为TH"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_OTHER,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
self.assertEqual(self._texts(), [])
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_adjust_after_first_receipt_asks_confirm_again(self) -> None:
|
||
"""首条确认发出后再改价(测服真机间隔 31 秒):发「已更新」,并再次请确认。"""
|
||
import os
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
try:
|
||
no = self._ticket()
|
||
handle_air_group(
|
||
self._message(f"CNY15/kg; CAN-SIN-CGK {no} @智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
first = self._texts()[-1]
|
||
self.assertIn("请确认是否正确", first)
|
||
self.assertIn("15", first)
|
||
|
||
# 模拟 31 秒后改价。时间窗口已过,改价回执仍要请确认。
|
||
handle_air_group(
|
||
self._message(f"TH, CNY18.3/kg; CAN-SIN-CGK {no} @智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
texts = self._texts()
|
||
self.assertEqual(len(texts), 2, texts)
|
||
self.assertIn("已更新识别内容", texts[-1])
|
||
self.assertIn("18.3", texts[-1])
|
||
self.assertIn("请确认是否正确", texts[-1])
|
||
|
||
# 再改一次仍发更新并请确认。
|
||
handle_air_group(
|
||
self._message(f"CNY18.5/kg; CAN-SIN-CGK {no} @智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
self.assertIn("18.5", self._texts()[-1])
|
||
self.assertIn("请确认是否正确", self._texts()[-1])
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
|
||
class AirQuoteReceiptDebounceTests(unittest.TestCase):
|
||
"""连续改价只回最后一条:静默窗口内旧回执不发送。"""
|
||
|
||
def setUp(self) -> None:
|
||
bot_reply.override_catalog(None)
|
||
from agent.redis_coord.air_inbound import reset_air_inbound_for_tests
|
||
from agent.redis_coord.quote_receipt_debounce import (
|
||
reset_quote_receipts_for_tests,
|
||
)
|
||
|
||
reset_air_inbound_for_tests()
|
||
reset_quote_receipts_for_tests()
|
||
self.chat = f"wr_air_debounce_{self._testMethodName}"
|
||
self.client = FakeGroupClient()
|
||
self.ledger = MemoryLedger()
|
||
self.flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
self.ledger._staff_by_wecom["u_air"] = {
|
||
"name": "航线小李",
|
||
"wecomId": "u_air",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
|
||
def tearDown(self) -> None:
|
||
import os
|
||
|
||
from agent.redis_coord.quote_receipt_debounce import (
|
||
reset_quote_receipts_for_tests,
|
||
)
|
||
|
||
os.environ["QUOTE_RECEIPT_DEBOUNCE_SEC"] = "0"
|
||
reset_quote_receipts_for_tests()
|
||
|
||
def _ticket(self) -> str:
|
||
created = self.ledger.create_ticket(
|
||
sender_id="u_sales",
|
||
business_line="AIR",
|
||
facts={"起运港": "CAN", "目的港": "SIN", "品名": "普货"},
|
||
collab_mode="collab",
|
||
)
|
||
no = str(created["work_order_no"])
|
||
# 各用例的工单号会重号,清掉上一例遗留的待确认,避免被当成同一轮改价。
|
||
from agent.redis_coord.pending_quote_confirm import clear_pending_confirm
|
||
|
||
clear_pending_confirm(no)
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id=self.chat, member_ids=[]
|
||
)
|
||
return no
|
||
|
||
def _message(self, content: str) -> InboundMessage:
|
||
return InboundMessage(
|
||
sender_id="u_air",
|
||
message_id=f"m-air-{abs(hash(content))}",
|
||
content=content,
|
||
msg_type="text",
|
||
chat_id=self.chat,
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
)
|
||
|
||
def _texts(self) -> list[str]:
|
||
return [
|
||
str(item.get("content") or "")
|
||
for item in self.client.sent
|
||
if item.get("kind") != "file"
|
||
]
|
||
|
||
def test_only_last_receipt_is_sent(self) -> None:
|
||
import os
|
||
|
||
from agent.redis_coord.pending_quote_confirm import load_pending_confirm
|
||
from agent.redis_coord.quote_receipt_debounce import (
|
||
flush_quote_receipt,
|
||
send_pending_receipt,
|
||
)
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
os.environ["QUOTE_RECEIPT_DEBOUNCE_SEC"] = "30"
|
||
try:
|
||
no = self._ticket()
|
||
phase = handle_air_group(
|
||
self._message(f"CNY16.3/kg; CAN-SIN-CGK {no} @智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
first_seq = str(load_pending_confirm(no).get("receipt_seq") or "")
|
||
self.assertTrue(first_seq)
|
||
for price in ("16.4", "16.5"):
|
||
phase = handle_air_group(
|
||
self._message(f"CNY{price}/kg; CAN-SIN-CGK {no} @智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
self.assertEqual(phase, "quote_confirm_pending")
|
||
# 静默窗口没到:三条都只排队,一条也不发。
|
||
self.assertEqual(self._texts(), [])
|
||
|
||
# 旧序号已作废,旧定时器到点也不能发。
|
||
self.assertFalse(
|
||
send_pending_receipt(
|
||
work_order_no=no,
|
||
seq=first_seq,
|
||
chat_id=self.chat,
|
||
client=self.client,
|
||
)
|
||
)
|
||
|
||
self.assertTrue(
|
||
flush_quote_receipt(
|
||
work_order_no=no, client=self.client, chat_id=self.chat
|
||
)
|
||
)
|
||
texts = self._texts()
|
||
self.assertEqual(len(texts), 1, texts)
|
||
self.assertIn("16.5", texts[0])
|
||
self.assertNotIn("16.3", texts[0])
|
||
self.assertNotIn("16.4", texts[0])
|
||
|
||
# 同一条回执只发一次。
|
||
self.assertFalse(
|
||
flush_quote_receipt(
|
||
work_order_no=no, client=self.client, chat_id=self.chat
|
||
)
|
||
)
|
||
self.assertEqual(len(self._texts()), 1)
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_restart_recovery_sends_unsent_receipt(self) -> None:
|
||
"""静默窗口内进程重启,定时器丢了,启动补偿要把最后一条补发出去。"""
|
||
import os
|
||
|
||
from agent.redis_coord.quote_receipt_debounce import (
|
||
recover_unsent_receipts,
|
||
reset_quote_receipts_for_tests,
|
||
)
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
os.environ["QUOTE_RECEIPT_DEBOUNCE_SEC"] = "30"
|
||
try:
|
||
no = self._ticket()
|
||
handle_air_group(
|
||
self._message(f"CNY17.2/kg; CAN-SIN {no} @智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
self.assertEqual(self._texts(), [])
|
||
|
||
# 模拟重启:定时器全部消失,待确认仍留在 Redis。
|
||
reset_quote_receipts_for_tests()
|
||
sent, remaining = recover_unsent_receipts(client=self.client)
|
||
self.assertEqual((sent, remaining), (1, 0))
|
||
texts = self._texts()
|
||
self.assertEqual(len(texts), 1, texts)
|
||
self.assertIn("17.2", texts[0])
|
||
|
||
# 已补发过,再扫不重复。
|
||
sent2, _ = recover_unsent_receipts(client=self.client)
|
||
self.assertEqual(sent2, 0)
|
||
self.assertEqual(len(self._texts()), 1)
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
def test_recovery_keeps_unsent_when_client_cannot_send(self) -> None:
|
||
"""出站客户端不可用时不能标记已发,否则这条回执永远补不回来。"""
|
||
import os
|
||
|
||
from agent.redis_coord.quote_receipt_debounce import (
|
||
recover_unsent_receipts,
|
||
reset_quote_receipts_for_tests,
|
||
)
|
||
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "true"
|
||
os.environ["QUOTE_RECEIPT_DEBOUNCE_SEC"] = "30"
|
||
try:
|
||
no = self._ticket()
|
||
handle_air_group(
|
||
self._message(f"CNY17.5/kg; CAN-SIN {no} @智价通"),
|
||
flow=self.flow,
|
||
injected_intent=INTENT_QUOTE,
|
||
)
|
||
reset_quote_receipts_for_tests()
|
||
|
||
sent, remaining = recover_unsent_receipts(client=object())
|
||
self.assertEqual((sent, remaining), (0, 1))
|
||
|
||
sent2, remaining2 = recover_unsent_receipts(client=self.client)
|
||
self.assertEqual((sent2, remaining2), (1, 0))
|
||
self.assertEqual(len(self._texts()), 1)
|
||
finally:
|
||
os.environ["QUOTE_CONFIRM_ENABLED"] = "false"
|
||
|
||
|
||
class AirPriceWithWorkOrderIntentTests(unittest.TestCase):
|
||
"""工单号+具体费用金额同一句,模型判成切单也要按报价办。"""
|
||
|
||
def test_price_with_work_order_skips_model(self) -> None:
|
||
from types import SimpleNamespace
|
||
|
||
from agent.routing import air_group_intent as agi
|
||
|
||
text = "CNY16.3/kg; CAN-SIN-CGK WO202612010005 @智价通"
|
||
settings = SimpleNamespace(
|
||
llm_allow_network=True, llm_data_usage_confirmed=True
|
||
)
|
||
with mock.patch("agent.config.get_settings", return_value=settings), mock.patch.object(
|
||
agi,
|
||
"_classify_with_model",
|
||
side_effect=AssertionError("工单号+报价不该再问模型"),
|
||
):
|
||
self.assertEqual(agi.classify_air_group_text(text), INTENT_QUOTE)
|
||
|
||
def test_plain_work_order_still_activates(self) -> None:
|
||
from agent.routing.air_group_intent import classify_air_group_text
|
||
|
||
self.assertEqual(
|
||
classify_air_group_text("WO202612010005 @智价通"), INTENT_ACTIVATE
|
||
)
|
||
|
||
def test_non_price_texts_are_not_quotes(self) -> None:
|
||
from agent.routing.air_group_intent import air_text_has_manual_price
|
||
|
||
for text in (
|
||
"WO202612010005 2",
|
||
"件数2件,毛重500KG",
|
||
"计费重改为800KG",
|
||
"计费重:800KG",
|
||
"重量等级改为+300",
|
||
"比例改为1:200",
|
||
"比例上调10%",
|
||
"总价改为1000",
|
||
"总价改为1000元",
|
||
"报价日期改为11月25日",
|
||
"报价有效期改为11月25日",
|
||
"时效改为3-5天",
|
||
"含电改为是",
|
||
"航司改为KUL",
|
||
"@智价通 WO202612010005 是",
|
||
):
|
||
self.assertFalse(air_text_has_manual_price(text), text)
|
||
for text in (
|
||
"CNY16.3/kg; CAN-SIN-CGK",
|
||
"USD11/kg、TH、NKG-KUL、11月23日",
|
||
"空运费改为900",
|
||
"码头操作费100CNY",
|
||
"1600元",
|
||
):
|
||
self.assertTrue(air_text_has_manual_price(text), text)
|
||
|
||
|
||
class AirLostDealBeatsCabinActionTests(unittest.TestCase):
|
||
"""
|
||
成交跟进回「未成交,取消舱位」:先存未成交,不能当释放去调主账。
|
||
|
||
真机 WO202612020070:整句被锁舱/释放规则抢走,工单没有有效锁舱,
|
||
群里回「请先完成工单…锁舱,再操作取消舱位」。产品口径:未成交只需存后台,
|
||
锁舱提示只给已成交的单。
|
||
"""
|
||
|
||
def setUp(self) -> None:
|
||
bot_reply.override_catalog(None)
|
||
from agent.redis_coord.air_inbound import reset_air_inbound_for_tests
|
||
|
||
reset_air_inbound_for_tests()
|
||
self.chat = f"wr_air_lost_{self._testMethodName}"
|
||
self.client = FakeGroupClient()
|
||
self.ledger = MemoryLedger()
|
||
self.flow = AirTextInquiryFlow(self.ledger, group_client=self.client)
|
||
# 同一个人既是本单销售、又是航线岗:先复现旧路径(is_airline 放行去释放)。
|
||
self.ledger._staff_by_wecom["u_sales"] = {
|
||
"name": "销售兼航线",
|
||
"wecomId": "u_sales",
|
||
"roleCode": "air",
|
||
"status": "active",
|
||
}
|
||
|
||
def _quoted_ticket(self) -> str:
|
||
created = self.ledger.create_ticket(
|
||
sender_id="u_sales",
|
||
business_line="AIR",
|
||
facts={"起运港": "CAN", "目的港": "SIN", "品名": "普货"},
|
||
collab_mode="collab",
|
||
)
|
||
no = str(created["work_order_no"])
|
||
self.ledger.bind_collab_group(
|
||
work_order_no=no, chat_id=self.chat, member_ids=[]
|
||
)
|
||
ticket = self.ledger.get_ticket(work_order_no=no)
|
||
ticket.quote = {"total": "9585.25", "source": "TMS", "source_label": "TMS"}
|
||
return no
|
||
|
||
def _message(self, content: str) -> InboundMessage:
|
||
return InboundMessage(
|
||
sender_id="u_sales",
|
||
message_id=f"m-lost-{abs(hash(content))}",
|
||
content=content,
|
||
msg_type="text",
|
||
chat_id=self.chat,
|
||
chat_type="group",
|
||
raw={"source": "aibot"},
|
||
)
|
||
|
||
def test_classifier_keeps_lost_before_release(self) -> None:
|
||
from agent.routing.air_group_intent import classify_air_group_text
|
||
from agent.routing.group_intent import INTENT_LOST
|
||
|
||
self.assertEqual(classify_air_group_text("未成交,取消舱位"), INTENT_LOST)
|
||
|
||
def test_injected_release_still_records_lost(self) -> None:
|
||
from agent.routing.group_intent import INTENT_LOST
|
||
|
||
no = self._quoted_ticket()
|
||
phase = handle_air_group(
|
||
self._message("未成交,取消舱位"),
|
||
flow=self.flow,
|
||
injected_intent="air_release",
|
||
)
|
||
self.assertIn(phase, {"wait_lost_reason", "done"})
|
||
self.assertEqual(self.ledger.get_ticket(work_order_no=no).status, "未成交")
|
||
texts = [
|
||
str(item.get("content") or "")
|
||
for item in self.client.sent
|
||
if item.get("kind") != "file"
|
||
]
|
||
body = "\n".join(texts)
|
||
self.assertNotIn("锁舱", body)
|
||
self.assertNotIn("不能取消舱位", body)
|
||
self.assertIn("未成交", body)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|