- 新增 inq_synonym 表、62组同义词种子与智能体只读接口 - Python 同义词加载/归一化与 1板/2箱 机读 - 同步当前分支既有协作、报价识别、陆运等改动
486 lines
20 KiB
Python
486 lines
20 KiB
Python
"""
|
||
陆运核对卡与无产品话术。
|
||
"""
|
||
|
||
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)
|
||
|
||
from agent.policy import inquiry_copy as copy
|
||
|
||
|
||
class LandCopyTest(unittest.TestCase):
|
||
def test_domestic_card_no_customs_no_wo(self):
|
||
text = copy.land_confirm_card(
|
||
{
|
||
"运输类型": "国内运输拼车",
|
||
"线路类别": "国内长途/零担",
|
||
"运输分类": "急件",
|
||
"起运港": "广州",
|
||
"目的港": "深圳",
|
||
"品名": "衣服",
|
||
"体积": "100CBM",
|
||
"毛重": "50kg",
|
||
"报价日期": "2026-09-17",
|
||
}
|
||
)
|
||
self.assertIn("始发站(必填):广州", text)
|
||
self.assertNotIn("通关口岸", text)
|
||
self.assertNotIn("WO", text)
|
||
self.assertIn('请回复“确定”进行TMS查价,内容有误时,请重新发内容。', text)
|
||
# 汇总只展示卡片字段:业务类型 / 报价场景,不再写内部运输类型。
|
||
self.assertIn("业务类型(必填):国内拼车", text)
|
||
self.assertIn("报价场景(必填):急件", text)
|
||
self.assertNotIn("运输类型", text)
|
||
|
||
def test_cross_empty_customs(self):
|
||
text = copy.land_confirm_card(
|
||
{
|
||
"运输类型": "跨境集拼",
|
||
"线路类别": "东南亚",
|
||
"运输分类": "急件",
|
||
"起运港": "广州",
|
||
"目的港": "河内",
|
||
"品名": "衣服",
|
||
"体积": "100CBM",
|
||
"毛重": "50kg",
|
||
"报价日期": "2026-09-17",
|
||
}
|
||
)
|
||
self.assertIn("通关口岸(非必填):", text)
|
||
|
||
def test_hk_lcl_shows_volume_weight_not_vehicle(self):
|
||
text = copy.land_confirm_card(
|
||
{
|
||
"运输类型": "中港零担/集拼",
|
||
"线路类别": "中港/中亚/中欧",
|
||
"运输分类": "急件",
|
||
"起运港": "广州",
|
||
"目的港": "香港",
|
||
"品名": "衣服",
|
||
"体积": "20CBM",
|
||
"毛重": "3000KG",
|
||
"车型/数量": "9米6/1",
|
||
"报价日期": "2026-09-17",
|
||
}
|
||
)
|
||
self.assertIn("业务类型(必填):中港/亚/欧拼车", text)
|
||
# 中港集拼按体积+毛重计费(与跨境集拼一致),不展示车型/数量。
|
||
self.assertIn("体积(CBM)(必填):20CBM", text)
|
||
self.assertIn("重量(KG)(必填):3000KG", text)
|
||
self.assertNotIn("车型/数量", text)
|
||
self.assertNotIn("中港装载方式", text)
|
||
self.assertIn("通关口岸(非必填)", text)
|
||
|
||
def test_hk_full_truck_shows_vehicle(self):
|
||
text = copy.land_confirm_card(
|
||
{
|
||
"运输类型": "中港整车",
|
||
"线路类别": "中港/中亚/中欧",
|
||
"运输分类": "急件",
|
||
"起运港": "广州",
|
||
"目的港": "香港",
|
||
"品名": "衣服",
|
||
"车型/数量": "9米6/1",
|
||
"报价日期": "2026-09-17",
|
||
}
|
||
)
|
||
self.assertIn("业务类型(必填):中港/亚/欧整车", text)
|
||
self.assertIn("车型/数量(必填):9米6/1", text)
|
||
self.assertNotIn("中港装载方式", text)
|
||
|
||
def test_domestic_ftl_one_field(self):
|
||
text = copy.land_confirm_card(
|
||
{
|
||
"运输类型": "国内运输整车",
|
||
"线路类别": "国内长途/零担",
|
||
"运输分类": "急件",
|
||
"起运港": "广州",
|
||
"目的港": "深圳",
|
||
"品名": "衣服",
|
||
"车型/数量": "9米6/1",
|
||
"报价日期": "2026-09-17",
|
||
}
|
||
)
|
||
self.assertIn("车型/数量(必填):9米6/1", text)
|
||
self.assertNotIn("\n数量(必填)", text)
|
||
|
||
def test_no_staff_copy(self):
|
||
self.assertIn("陆运产品岗", copy.land_group_no_staff())
|
||
|
||
def test_ask_land_fields_marks_required_and_lists_optional(self):
|
||
text = copy.ask_land_fields(
|
||
facts={
|
||
"运输类型": "跨境集拼",
|
||
"线路类别": "东南亚",
|
||
"运输分类": "急件",
|
||
"起运港": "广州",
|
||
"目的港": "河内",
|
||
"品名": "衣服",
|
||
"毛重": "50kg",
|
||
},
|
||
missing_keys=["体积"],
|
||
land_subtype="跨境集拼",
|
||
)
|
||
self.assertIn("始发站(必填):广州", text)
|
||
self.assertIn(copy.CLARIFY_PASTE_HINT, text)
|
||
self.assertIn("1.体积(CBM)(必填):\n(如:100CBM)", text)
|
||
self.assertIn("2.通关口岸(非必填):\n(如:友谊关)", text)
|
||
self.assertIn("货物品名(必填):衣服", text.split(copy.CLARIFY_PASTE_HINT, 1)[0])
|
||
pending = text.split(copy.CLARIFY_PASTE_HINT, 1)[1]
|
||
self.assertNotIn("货物品名", pending)
|
||
self.assertNotIn("体积(必填):(如:", text)
|
||
self.assertNotIn(copy.LAND_CONFIRM_TAIL, text)
|
||
|
||
def test_ask_land_fields_domestic_has_no_customs(self):
|
||
text = copy.ask_land_fields(
|
||
facts={
|
||
"运输类型": "国内运输拼车",
|
||
"起运港": "广州",
|
||
"目的港": "深圳",
|
||
"品名": "衣服",
|
||
"毛重": "50kg",
|
||
},
|
||
missing_keys=["体积"],
|
||
land_subtype="国内运输拼车",
|
||
)
|
||
self.assertIn("1.体积(CBM)(必填):\n(如:100CBM)", text)
|
||
self.assertNotIn("通关口岸", text)
|
||
|
||
def test_ask_land_fields_filled_customs_not_pending(self):
|
||
text = copy.ask_land_fields(
|
||
facts={
|
||
"运输类型": "中港整车",
|
||
"起运港": "广州",
|
||
"目的港": "香港",
|
||
"品名": "衣服",
|
||
"通关口岸": "皇岗",
|
||
},
|
||
missing_keys=["起运港"],
|
||
land_subtype="中港整车",
|
||
)
|
||
self.assertIn("通关口岸(非必填):皇岗", text)
|
||
pending = text.split(copy.CLARIFY_PASTE_HINT, 1)[1]
|
||
self.assertNotIn("通关口岸", pending)
|
||
|
||
def test_ask_land_fields_cargo_first_in_same_list(self):
|
||
"""品名并进常规必填清单,排第 1;体积/重量顺延 2、3(产品 2026-11 口径)。"""
|
||
text = copy.ask_land_fields(
|
||
facts={
|
||
"运输类型": "国内运输拼车",
|
||
"线路类别": "国内长途/零担",
|
||
"运输分类": "普件(涉及国内+国外段)",
|
||
"起运港": "广州",
|
||
"目的港": "北京",
|
||
},
|
||
# 上游给的顺序故意把品名放最后,展示仍要按产品要求排第 1。
|
||
missing_keys=["体积", "毛重", "品名"],
|
||
land_subtype="国内运输拼车",
|
||
)
|
||
pending = text.split(copy.CLARIFY_PASTE_HINT, 1)[1]
|
||
self.assertIn("1.货物品名(必填):\n(如:衣服)", pending)
|
||
self.assertIn("2.体积(CBM)(必填):\n(如:100CBM)", pending)
|
||
self.assertIn("3.重量(KG)(必填):\n(如:50kg)", pending)
|
||
self.assertLess(pending.index("1.货物品名"), pending.index("2.体积"))
|
||
|
||
def test_ask_land_fields_never_reasks_filled_cargo(self):
|
||
"""品名已填(含别名键)时不再出现在待补充,避免同一条里既回显又追问。"""
|
||
for facts in (
|
||
{"品名": "茶叶"},
|
||
{"货物品名": "茶叶"},
|
||
):
|
||
with self.subTest(facts=facts):
|
||
text = copy.ask_land_fields(
|
||
facts={
|
||
"运输类型": "国内运输拼车",
|
||
"起运港": "广州",
|
||
"目的港": "北京",
|
||
**facts,
|
||
},
|
||
missing_keys=["品名", "体积"],
|
||
land_subtype="国内运输拼车",
|
||
)
|
||
pending = text.split(copy.CLARIFY_PASTE_HINT, 1)[1]
|
||
self.assertNotIn("货物品名", pending)
|
||
self.assertIn("货物品名(必填):茶叶", text.split(copy.CLARIFY_PASTE_HINT, 1)[0])
|
||
self.assertIn("1.体积(CBM)(必填)", pending)
|
||
|
||
def test_land_labels_prefer_card_selection(self):
|
||
"""卡片刚选的两项优先,旧事实里的类型/分类不能把它们盖掉。"""
|
||
self.assertEqual(
|
||
copy.land_biz_scenario_labels(
|
||
{"陆运业务类型": "东南亚拼车", "报价场景": "急件"},
|
||
biz_type_label="国内整车",
|
||
quote_scenario="定制化(涉及标案、境外段清派、打包、超限、特殊要求)",
|
||
),
|
||
("国内整车", "定制化(涉及标案、境外段清派、打包、超限、特殊要求)"),
|
||
)
|
||
|
||
def test_land_labels_derive_from_legacy_three_fields(self):
|
||
"""历史单只有内部三项时,按卡片口径收回业务类型/报价场景。"""
|
||
self.assertEqual(
|
||
copy.land_biz_scenario_labels(
|
||
{
|
||
"运输类型": "中港零担/集拼",
|
||
"线路类别": "中港/中亚/中欧",
|
||
"运输分类": "急件",
|
||
}
|
||
),
|
||
("中港/亚/欧拼车", "急件"),
|
||
)
|
||
# 国内整车/城配共用运输类型,单凭类型分不出业务类型,不能猜。
|
||
self.assertEqual(copy.land_biz_scenario_labels({"运输类型": "国内运输整车"}), ("", ""))
|
||
|
||
def test_inquiry_card_lists_land_fields_and_wo(self):
|
||
facts = {
|
||
"运输类型": "国内运输拼车",
|
||
"线路类别": "国内长途/零担",
|
||
"运输分类": "急件",
|
||
"起运港": "广州",
|
||
"目的港": "深圳",
|
||
"品名": "衣服",
|
||
"体积": "100CBM",
|
||
"毛重": "50kg",
|
||
"报价日期": "2026-09-17",
|
||
}
|
||
text = copy.inquiry_card(
|
||
work_order_no="WO202609170006",
|
||
facts=facts,
|
||
transport_mode="陆运",
|
||
)
|
||
# 产品口径 2026-11-22:确定后只保留工单号 + 正在查询TMS报价,字段段不重复。
|
||
self.assertEqual(
|
||
text,
|
||
"工单号:WO202609170006\n工单已创建,正在查询TMS报价。",
|
||
)
|
||
# 陆运字段展示仍在核对卡 / 拉群摘要这条链路上(inquiry_field_lines)。
|
||
fields = "\n".join(
|
||
copy.inquiry_field_lines(
|
||
work_order_no="WO202609170006", facts=facts, transport_mode="陆运"
|
||
)
|
||
)
|
||
self.assertIn("运输方式:陆运", fields)
|
||
self.assertIn("业务类型:国内拼车", fields)
|
||
self.assertIn("报价场景:急件", fields)
|
||
self.assertNotIn("运输类型", fields)
|
||
self.assertIn("始发站:广州", fields)
|
||
self.assertIn("货物品名:衣服", fields)
|
||
self.assertNotIn("件数", fields)
|
||
|
||
def test_scheme_card_uses_land_fields_not_sea(self):
|
||
facts = {
|
||
"运输类型": "国内运输拼车",
|
||
"线路类别": "国内城配/拖车/打包",
|
||
"运输分类": "普件(东南亚、中港常用线路/常规车型)",
|
||
"起运港": "泉州市",
|
||
"目的港": "南京市",
|
||
"品名": "通信机柜及线缆配件",
|
||
"体积": "10CBM",
|
||
"毛重": "10KG",
|
||
"报价日期": "2026-09-17",
|
||
}
|
||
quote = {"total": "CNY 1200", "eta": "3天", "valid_until": "2026-09-20"}
|
||
text = copy.scheme_card(
|
||
work_order_no="WO202609170011",
|
||
status="已报价",
|
||
facts=facts,
|
||
quote=quote,
|
||
transport_mode="陆运",
|
||
)
|
||
self.assertIn("运输方式:陆运", text)
|
||
self.assertIn("业务类型:国内拼车", text)
|
||
self.assertIn("报价场景:-", text)
|
||
self.assertNotIn("运输类型", text)
|
||
self.assertIn("始发站:泉州市", text)
|
||
self.assertIn("货物品名:通信机柜及线缆配件", text)
|
||
self.assertIn("体积(CBM):10CBM", text)
|
||
self.assertNotIn("整柜或拼柜", text)
|
||
self.assertNotIn("箱型箱量", text)
|
||
self.assertNotIn("件数:", text)
|
||
self.assertNotIn("包装类型:", text)
|
||
rows = {
|
||
x["keyname"]: x["value"]
|
||
for x in copy.scheme_horizontal_rows(
|
||
work_order_no="WO202609170011",
|
||
facts=facts,
|
||
quote=quote,
|
||
transport_mode="陆运",
|
||
)
|
||
}
|
||
self.assertIn("陆运 泉州市→南京市", rows["线路"])
|
||
self.assertIn("国内运输拼车", rows["类型"])
|
||
self.assertIn("国内城配/拖车/打包", rows["类型"])
|
||
self.assertIn("通信机柜及线缆配件", rows["货物"])
|
||
self.assertIn("10CBM", rows["货物"])
|
||
self.assertNotIn("航线", rows)
|
||
self.assertNotIn("箱型", rows)
|
||
self.assertNotIn("包装", rows)
|
||
self.assertNotIn("空运", "".join(rows.values()))
|
||
self.assertNotIn("海运", "".join(rows.values()))
|
||
|
||
def test_scheme_card_ftl_shows_vehicle_combo(self):
|
||
facts = {
|
||
"运输类型": "国内运输整车",
|
||
"线路类别": "国内长途/零担",
|
||
"运输分类": "急件",
|
||
"起运港": "广州",
|
||
"目的港": "深圳",
|
||
"品名": "衣服",
|
||
"车型/数量": "9米6/1",
|
||
}
|
||
rows = {
|
||
x["keyname"]: x["value"]
|
||
for x in copy.scheme_horizontal_rows(
|
||
work_order_no="WO202609170012",
|
||
facts=facts,
|
||
quote={},
|
||
transport_mode="陆运",
|
||
)
|
||
}
|
||
self.assertIn("9米6/1", rows["货物"])
|
||
self.assertNotIn("CBM", rows["货物"])
|
||
|
||
def test_land_brief_only_ltl_collab(self):
|
||
text = copy.sea_group_brief(
|
||
work_order_no="WO1",
|
||
facts={
|
||
"运输类型": "国内运输拼车",
|
||
"线路类别": "国内长途/零担",
|
||
"运输分类": "急件",
|
||
"起运港": "广州",
|
||
"目的港": "深圳",
|
||
"品名": "衣服",
|
||
"体积": "1",
|
||
"毛重": "1",
|
||
"报价日期": "2026-09-18",
|
||
},
|
||
quote=None,
|
||
collab_facts={"包装方式": "纸箱"},
|
||
sales_name="销",
|
||
product_names=["产"],
|
||
has_price=False,
|
||
transport_mode="陆运",
|
||
)
|
||
self.assertIn("包装方式:纸箱", text)
|
||
# 客户名称已改到询价阶段(非必填),协同段不再出现。
|
||
self.assertNotIn("客户名称", text)
|
||
self.assertIn("货值:-", text)
|
||
self.assertIn("是否为危险品:-", text)
|
||
self.assertNotIn("贸易条款", text)
|
||
self.assertNotIn("商品海关编码", text)
|
||
self.assertNotIn("海关编码", text)
|
||
|
||
def test_land_brief_ftl_longhaul_order(self):
|
||
text = copy.sea_group_brief(
|
||
work_order_no="WO2",
|
||
facts={
|
||
"运输类型": "国内运输整车",
|
||
"线路类别": "国内长途/零担",
|
||
"运输分类": "急件",
|
||
"起运港": "广州",
|
||
"目的港": "深圳",
|
||
"品名": "衣服",
|
||
"车型/数量": "9米/1",
|
||
"报价日期": "2026-09-18",
|
||
},
|
||
quote=None,
|
||
collab_facts={},
|
||
sales_name="销",
|
||
product_names=["产"],
|
||
has_price=False,
|
||
transport_mode="陆运",
|
||
)
|
||
collab = text.split("协同补充字段")[1]
|
||
self.assertNotIn("客户名称", collab)
|
||
self.assertLess(collab.find("货值"), collab.find("包装方式"))
|
||
self.assertLess(collab.find("包装方式"), collab.find("是否为危险品"))
|
||
self.assertNotIn("贸易条款", text)
|
||
|
||
def test_hk_lcl_brief_collab_same_as_cross_border_lcl(self):
|
||
"""中港集拼群摘要:协同项与跨境集拼一致,询价字段带体积/重量、不带车型。"""
|
||
text = copy.sea_group_brief(
|
||
work_order_no="WO3",
|
||
facts={
|
||
"运输类型": "中港零担/集拼",
|
||
"线路类别": "中港/中亚/中欧",
|
||
"运输分类": "普件(东南亚、中港常用线路/常规车型)",
|
||
"起运港": "广州",
|
||
"目的港": "香港",
|
||
"品名": "衣服",
|
||
"体积": "20CBM",
|
||
"毛重": "3000KG",
|
||
"报价日期": "2026-09-18",
|
||
"客户名称": "华为",
|
||
},
|
||
quote=None,
|
||
collab_facts={},
|
||
sales_name="销",
|
||
product_names=["产"],
|
||
has_price=False,
|
||
transport_mode="陆运",
|
||
)
|
||
inquiry = text.split("协同补充字段")[0]
|
||
self.assertIn("体积(CBM):20CBM", inquiry)
|
||
self.assertIn("重量(KG):3000KG", inquiry)
|
||
# 客户名称进询价字段,不再进协同段。
|
||
self.assertIn("客户名称:华为", inquiry)
|
||
self.assertNotIn("车型/数量", inquiry)
|
||
collab = text.split("协同补充字段")[1]
|
||
for label in ("海关编码:-", "贸易条款:-", "包装方式:-", "是否为危险品:-"):
|
||
self.assertIn(label, collab)
|
||
self.assertNotIn("客户名称", collab)
|
||
# 与跨境集拼同一套:只这五项,不能多出海运的含油/含电/含磁。
|
||
self.assertNotIn("是否含油", collab)
|
||
self.assertNotIn("是否含电", collab)
|
||
self.assertNotIn("是否含磁", collab)
|
||
from agent.schema.field_validate import land_collab_fields
|
||
|
||
self.assertEqual(
|
||
land_collab_fields("中港零担/集拼", "中港/中亚/中欧"),
|
||
land_collab_fields("跨境集拼", "中港/中亚/中欧"),
|
||
)
|
||
|
||
def test_land_unrecognized_lists_keys(self):
|
||
keys = ("客户名称", "包装方式", "货值", "是否为危险品")
|
||
text = copy.group_fields_not_recognized(keys=keys, business_line="LAND")
|
||
self.assertIn("客户名称", text)
|
||
self.assertNotIn("货好时间", text)
|
||
|
||
def test_image_copy(self):
|
||
self.assertEqual(copy.IMAGE_RECOGNIZING, "正在识别图片")
|
||
self.assertIn("没看清", copy.IMAGE_UNREADABLE)
|
||
self.assertIn("暂时没认出来", copy.IMAGE_TECH_FAIL)
|
||
merged = copy.image_fail_then_clarify(copy.IMAGE_UNREADABLE, copy.ASK_TRANSPORT)
|
||
self.assertTrue(merged.startswith(copy.IMAGE_UNREADABLE))
|
||
self.assertIn(copy.ASK_TRANSPORT, merged)
|
||
|
||
def test_attachment_copy_and_card(self):
|
||
self.assertEqual(copy.ATTACH_RECOGNIZING, "正在识别附件")
|
||
self.assertIn("没看清", copy.ATTACH_UNREADABLE)
|
||
self.assertIn("暂时没认出来", copy.ATTACH_TECH_FAIL)
|
||
payload = copy.attachment_upload_card("https://ai.ytd-scm.com/inquiry-form/upload/tok")
|
||
card = payload["template_card"]
|
||
self.assertEqual(payload["msgtype"], "template_card")
|
||
# 现网上传卡是 text_notice。news_notice 必须带 card_image,企微会 42044 后退成纯文本。
|
||
self.assertEqual(card["card_type"], "text_notice")
|
||
self.assertNotIn("card_image", card)
|
||
self.assertEqual(card["source"]["desc"], "附件询价")
|
||
self.assertEqual(card["main_title"]["title"], "上传询价附件")
|
||
self.assertIn("Excel", card["main_title"]["desc"])
|
||
self.assertIn("图片请直接在企微聊天窗口发送", card["main_title"]["desc"])
|
||
self.assertIn("图片请直接在当前企微会话发送", card["sub_title_text"])
|
||
self.assertEqual(card["jump_list"][0]["title"], "上传询价附件")
|
||
self.assertIn("/upload/tok", card["card_action"]["url"])
|
||
self.assertEqual(card["card_action"]["type"], 1)
|
||
self.assertNotIn("userid", card["card_action"]["url"])
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|