Files
inquiry_robot/inquiry-agent/tests/test_quote_recognition.py
T
linmianxun 9d7bc2b787 员工岗位与数据范围权限上线,企微群机器人接入与报价回执去重
- 新增员工岗位(InquiryStaffRole)实体、服务与后台页面,数据范围默认按岗位,看板由「仅本人」切换为岗位范围,补充业务负责人与角色说明迁移
- 新增企微群 webhook 通道、access_gate/staff_roles 鉴权与智价通/双 BOT 回复配置
- 报价回执增加去重防抖(quote_receipt_debounce),询价原文落库并补单测
- 同义词补陆运类型修正与地名通配 v2,更新种子生成脚本
- 空运/陆运/海运询价流程、报价单按需生成、转人工与多段处理调整
- 后台工单需求字段、BOT 显示名、站点标题与看板配套更新
2026-12-04 20:16:28 +08:00

267 lines
12 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
报价识别分级:完整 / 部分 / 全无(产品 2026-11-23)。
不连企微、主账、TMS,只测识别判断和回执话术。
"""
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.llm.extract_text import extract_product_quote
from agent.policy import inquiry_copy as copy
from agent.policy.quote_confirm import can_confirm_quote, is_confirm_text
from agent.schema.quote_recognition import inspect_quote_recognition
class ConfirmKeywordTests(unittest.TestCase):
"""确认关键词:确认/确定/正确及相近说法;销售本人或产品/航线可确认。"""
def test_confirm_keywords(self) -> None:
for text in ["确认", "确定", "正确", "对的", "没问题", "好的", "OK", "确认无误"]:
self.assertTrue(is_confirm_text(text), text)
# 群里显示名比配置名长时,@称呼必须整体剥掉,不能留下「测试」。
self.assertTrue(is_confirm_text("@智价通测试 正确"))
self.assertTrue(is_confirm_text("@智价 通 确认"))
# 群成员回确认常把工单号一起带上:工单号只指代本单,不影响整句确认。
self.assertTrue(is_confirm_text("@智价通 WO2026120100005 是"))
self.assertTrue(is_confirm_text("WO2026120100005 正确"))
self.assertTrue(is_confirm_text("@智价通测试 工单号WO2026120100005 确认无误"))
for text in ["这个价格再谈谈", "确认报价单", "USD11/kg", "WO2026120100005"]:
self.assertFalse(is_confirm_text(text), text)
def test_ticket_sales_can_confirm_quote(self) -> None:
ticket = {"sales_wecom_id": "u_sales", "business_line": "SEA"}
self.assertTrue(can_confirm_quote(None, ticket, "u_sales", business_line="SEA"))
self.assertFalse(can_confirm_quote(None, ticket, "u_other", business_line="SEA"))
class AirRecognitionTests(unittest.TestCase):
"""空运:标准话术完整;缺线路算部分;闲聊算全无。"""
def _inspect(self, text: str, facts: dict | None = None) -> dict:
quote = extract_product_quote(text, business_line="AIR", transport_mode="空运")
return inspect_quote_recognition(
text, quote, business_line="AIR", transport_mode="空运", facts=facts or {}
)
def test_standard_format_is_full(self) -> None:
text = "工单号,USD11/kg、TH、NKG-KUL、11月2日"
got = self._inspect(
text, {"起运港": "NKG", "目的港": "KUL", "报价日期": "2026-11-02"}
)
self.assertEqual(got["status"], "full")
self.assertEqual(got["missing"], [])
self.assertIn("线路:NKG-KUL", got["recognized"])
def test_receipt_lists_ratio_weight_break_and_services(self) -> None:
"""比例、重量等级、卡转/MUST GO 也是识别内容,回执不能漏。"""
text = "WO202611240005 1:300,+500,USD11/kg,卡转/MUST GO、TH、NKG-KUL、11月24日"
got = self._inspect(
text, {"起运港": "NKG", "目的港": "KUL", "报价日期": "2026-11-24"}
)
self.assertEqual(got["status"], "full")
self.assertIn("比例:1:300", got["recognized"])
self.assertIn("重量等级:+500", got["recognized"])
self.assertIn("增值服务:卡转/MUST GO", got["recognized"])
body = copy.quote_partial_recognized(
got["recognized"],
got["missing"],
business_line="AIR",
work_order_no="WO202611240005",
)
self.assertIn("比例:1:300", body)
self.assertIn("重量等级:+500", body)
self.assertIn("增值服务:卡转/MUST GO", body)
def test_missing_route_is_partial_and_uses_ticket_route(self) -> None:
# 真机 WO202611230013:话术只有价、航司、日期,线路在工单里。
text = "WO202611230013 USD11/kg,\nTH、11月23日"
got = self._inspect(
text, {"起运港": "香港", "目的港": "DEL", "报价日期": "2026-11-23"}
)
self.assertEqual(got["status"], "partial")
self.assertIn("空运费:USD11/kg", got["recognized"])
self.assertIn("航司:TH", got["recognized"])
self.assertIn("线路:香港-DEL", got["recognized"])
body = copy.quote_partial_recognized(
got["recognized"], got["missing"], business_line="AIR"
)
self.assertIn("我识别内容为:", body)
self.assertIn("请确认是否正确。", body)
# 部分识别不再贴格式样例;格式只留给「全无」提示。
self.assertNotIn("工单号,USD11/kg、TH、NKG-KUL、11月2日", body)
with_no = copy.quote_partial_recognized(
got["recognized"], got["missing"], business_line="AIR",
work_order_no="WO202611240001",
)
self.assertTrue(with_no.startswith("WO202611240001 我识别内容为:"))
def test_price_only_is_partial(self) -> None:
got = self._inspect(
"USD11/kg", {"起运港": "香港", "目的港": "DEL", "报价日期": "2026-11-23"}
)
self.assertEqual(got["status"], "partial")
self.assertIn("航司", got["missing"])
def test_chatter_is_none_and_prompt_has_format(self) -> None:
got = self._inspect(
"这个价格我再问下航线,晚点回你",
{"起运港": "香港", "目的港": "DEL", "报价日期": "2026-11-23"},
)
self.assertEqual(got["status"], "none")
body = copy.quote_not_recognized(business_line="AIR")
self.assertIn("我无法识别您的内容", body)
self.assertIn("工单号,USD11/kg、TH、NKG-KUL、11月2日", body)
# 留档说明不再出现在群里(产品 2026-11-23 晚确认),原话仍写后台。
self.assertNotIn("原始报价参考", body)
self.assertNotIn("询价中", body)
class GroupFieldCorrectionTests(unittest.TestCase):
"""陆运/海运字段纠错不能被当成改价(数字不能变运费)。"""
def test_field_corrections_are_not_prices(self) -> None:
from agent.policy.sea_group_ops import _is_group_field_correction
for text in (
"车型改为45HQ",
"税率改为6%",
"目的港改为春疆",
"包装改为木箱",
"贸易条款改为FOB",
):
self.assertTrue(_is_group_field_correction(text, "LAND"), text)
for text in ("运费改为1800", "报关费改为300", "海运费 1000"):
self.assertFalse(_is_group_field_correction(text, "LAND"), text)
class LandRecognitionTests(unittest.TestCase):
"""陆运:线路/车型/价格都在话术里才算完整;缺价格算部分。"""
def _inspect(self, text: str, facts: dict | None = None) -> dict:
quote = extract_product_quote(text, business_line="LAND", transport_mode="陆运")
return inspect_quote_recognition(
text, quote, business_line="LAND", transport_mode="陆运", facts=facts or {}
)
def test_standard_format_is_full(self) -> None:
text = "东莞-春疆,危险品,45HQ,CNY18500;都是含0点税"
got = self._inspect(
text, {"起运港": "东莞", "目的港": "春疆", "车型/数量": "45HQ"}
)
self.assertEqual(got["status"], "full")
self.assertEqual(got["missing"], [])
def test_no_price_is_partial(self) -> None:
text = "东莞-春疆,危险品,45HQ"
got = self._inspect(
text, {"起运港": "东莞", "目的港": "春疆", "车型/数量": "45HQ"}
)
self.assertEqual(got["status"], "partial")
self.assertIn("价格", got["missing"])
self.assertIn("起运地:东莞", got["recognized"])
self.assertIn("目的地:春疆", got["recognized"])
self.assertIn("车型:45HQ", got["recognized"])
def test_chatter_is_none_and_prompt_has_land_format(self) -> None:
got = self._inspect("我晚点问下车队", {})
self.assertEqual(got["status"], "none")
body = copy.quote_not_recognized(business_line="LAND")
self.assertIn("我无法识别您的内容", body)
self.assertIn("东莞-春疆,危险品,45HQ,CNY18500;都是含0点税", body)
def test_glued_quote_keeps_both_fees_vehicle_and_rate_first_conditions(self) -> None:
# 产品 2026-12-03:原话没有分隔符,且点数写在条件前面。
# 旧实现把「运费732」和「海关监管+接驳费310」吞成一条 310,车型只剩 18米,
# 燃油/税/备注全丢。这里锁死:两笔费用、完整车型、原话条件、备注都要在,
# 且「未含10%燃油费」按未含口径把主运费 ×(1+10%)。
text = (
"18米平板运费732USD海关监管+接驳费310USD"
"未含10%燃油费未含8%税,不含报关,不超限"
)
quote = extract_product_quote(text, business_line="LAND", transport_mode="陆运")
rows = {row["name"]: row for row in quote["fee_rows"]}
self.assertEqual(rows["运费"]["amount"], "805.2")
self.assertEqual(rows["海关监管+接驳费"]["amount"], "310")
self.assertAlmostEqual(float(quote["total"]), 1115.2)
self.assertEqual(quote["vehicleType"], "18米平板")
self.assertEqual(quote["tax_rate_text"], "未含8%税")
self.assertEqual(quote["fuel_surcharge_text"], "未含10%燃油费")
self.assertEqual(quote["remark"], "不含报关,不超限")
got = self._inspect(text)
self.assertIn("车型:18米平板", got["recognized"])
self.assertIn("运输费:USD805.2", got["recognized"])
self.assertIn("海关监管+接驳费:USD310", got["recognized"])
self.assertIn("税率:未含8%税", got["recognized"])
self.assertIn("燃油附加费率:未含10%燃油费", got["recognized"])
self.assertIn("备注:不含报关,不超限", got["recognized"])
class LandZeroTaxTest(unittest.TestCase):
"""口语「0税」要算税率 0;「1700车」没斜杠也要认 /车。"""
def test_bare_zero_tax_and_car_unit(self) -> None:
from agent.policy.sea_group_ops import _attach_land_vehicle_rates_segment
text = "运费45HQ1700车0税"
quote = extract_product_quote(text, business_line="LAND", transport_mode="陆运")
self.assertEqual(str(quote.get("tax_rate") or ""), "0")
self.assertEqual(dict(quote.get("vehicleRates") or {}), {"45HQ": "1700"})
vrows = quote.get("vehicle_fee_rows") or []
self.assertTrue(vrows, quote)
self.assertEqual(str((vrows[0] or {}).get("unit") or "").strip(), "车")
got = inspect_quote_recognition(
text,
quote,
business_line="LAND",
transport_mode="陆运",
facts={"起运港": "深圳", "目的港": "香港"},
)
self.assertIn("税率:0", got["recognized"])
out = _attach_land_vehicle_rates_segment(
quote, {"facts": {"起运港": "深圳", "目的港": "香港"}}
)
lines = copy._manual_quote_lines(out, business_line="LAND", layout="manual")
self.assertIn("税率:0", lines)
class ImagePromptTest(unittest.TestCase):
"""图片/非 Excel 文件:回「无法识别,请按文字格式重新报价」。"""
def test_text_format_prompt(self) -> None:
body = copy.quote_not_recognized(
business_line="LAND", at_name="林绵勋", text_format=True
)
self.assertTrue(body.startswith("@林绵勋"))
self.assertIn("我无法识别您的内容,请按文字格式重新报价", body)
self.assertIn("东莞-春疆", body)
class SeaRecognitionTests(unittest.TestCase):
"""海运暂无标准话术:失败提示用「费用名+金额+币种」兜底。"""
def test_sea_failure_uses_generic_hint(self) -> None:
body = copy.quote_not_recognized(business_line="SEA")
self.assertIn("我无法识别您的内容", body)
self.assertIn("费用名+金额+币种", body)
if __name__ == "__main__":
unittest.main()