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

314 lines
14 KiB
Python

"""
陆运报价附加条件回归:未含税 / 含0税 / 未含燃油 / 含燃油费。
产品 2026-11-22 口径(2026-11-27 修订:燃油不进备注):
1. 未含税N%:税率 N%,运输费不动,备注「未含税N%」;
2. 未含税:税率空,运输费不动,备注「空」;
3. 含0税:税率 0,运输费不动,备注「含0税」;
4. 未含燃油N%:燃油费率 N%,主运费 ×(1+N%),备注不写燃油;
5. 含燃油费N%:燃油费率 N%,主运费不乘,备注不写燃油。
涉及模板:跨境整车、跨境集拼、国内整车、国内拼车、中港集拼。
本文件只测解析/挂段/摘要,不连企微、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.inquiry_copy import _manual_land_vehicle_lines, _manual_quote_lines
from agent.schema.quote_recognition import inspect_quote_recognition
from agent.policy.sea_group_ops import (
_attach_land_vehicle_rates_segment,
_attach_land_weight_band_segment,
)
from agent.schema.land_quote_condition import (
FUEL_EXCLUDED,
FUEL_INCLUDED,
UNTAXED_BLANK,
UNTAXED_RATE,
ZERO_TAX,
parse_land_quote_condition,
strip_land_quote_condition,
)
LAND = dict(business_line="LAND", transport_mode="陆运")
class ParseConditionTest(unittest.TestCase):
"""五句口语条件必须各归各的代码。"""
def test_fuel_excluded(self) -> None:
cond = parse_land_quote_condition("运输费USD3200,未含燃油13%")
self.assertIsNotNone(cond)
self.assertEqual(cond.code, FUEL_EXCLUDED)
self.assertEqual(cond.fuel_rate, "13%")
self.assertEqual(cond.remark, "")
self.assertTrue(cond.multiply_fuel)
def test_fuel_excluded_with_full_width(self) -> None:
cond = parse_land_quote_condition("运输费USD3200,未含燃油13%")
self.assertIsNotNone(cond)
self.assertEqual(cond.fuel_rate, "13%")
def test_fuel_included(self) -> None:
cond = parse_land_quote_condition("运输费12000,含燃油费13%")
self.assertIsNotNone(cond)
self.assertEqual(cond.code, FUEL_INCLUDED)
self.assertEqual(cond.fuel_rate, "13%")
self.assertEqual(cond.remark, "")
self.assertFalse(cond.multiply_fuel)
def test_untaxed_rate(self) -> None:
cond = parse_land_quote_condition("运输费12000,未含税13%")
self.assertIsNotNone(cond)
self.assertEqual(cond.code, UNTAXED_RATE)
self.assertEqual(cond.tax_rate, "13%")
self.assertEqual(cond.remark, "未含税13%")
self.assertFalse(cond.multiply_fuel)
def test_untaxed_blank(self) -> None:
cond = parse_land_quote_condition("运输费12000,未含税")
self.assertIsNotNone(cond)
self.assertEqual(cond.code, UNTAXED_BLANK)
self.assertEqual(cond.tax_rate, "")
self.assertEqual(cond.remark, "空")
def test_zero_tax(self) -> None:
cond = parse_land_quote_condition("运输费12000,含0税")
self.assertIsNotNone(cond)
self.assertEqual(cond.code, ZERO_TAX)
self.assertEqual(cond.tax_rate, "0")
self.assertEqual(cond.remark, "含0税")
def test_strip_removes_token(self) -> None:
stripped = strip_land_quote_condition("运输费12000,含燃油费13%")
self.assertNotIn("含燃油费", stripped)
self.assertNotIn("13", stripped)
self.assertIn("运输费12000", stripped)
def test_tax_and_fuel_can_coexist(self) -> None:
# 产品一句里同时写税和燃油:两项都要出单,不能只取后出现的一项。
cond = parse_land_quote_condition("运输费3200,未含税13%,未含燃油5%")
self.assertIsNotNone(cond)
self.assertEqual(cond.tax_rate, "13%")
self.assertEqual(cond.fuel_rate, "5%")
self.assertEqual(cond.remark, "未含税13%")
self.assertTrue(cond.multiply_fuel)
class ExtractLandConditionTest(unittest.TestCase):
"""条件进报价对象:税率/燃油/乘法正确,燃油不进备注。"""
def test_fuel_excluded_multiplies_transport(self) -> None:
quote = extract_product_quote("运输费USD3200,未含燃油13%", **LAND)
self.assertEqual(quote["fee_rows"][0]["name"], "运输费")
self.assertEqual(quote["fee_rows"][0]["amount"], "3616")
self.assertEqual(quote["total"], "3616")
self.assertEqual(quote["fuel_surcharge_rate"], "13%")
self.assertNotIn("remark", quote)
self.assertNotIn("remark", quote["fee_rows"][0])
def test_fuel_excluded_rate_first_also_multiplies_transport(self) -> None:
# 口语把点数写在前面也要按未含口径乘:未含10%燃油费 = 主运费 ×1.1。
quote = extract_product_quote("运输费USD3200,未含13%燃油费", **LAND)
self.assertEqual(quote["fee_rows"][0]["amount"], "3616")
self.assertEqual(quote["total"], "3616")
self.assertEqual(quote["fuel_surcharge_rate"], "13%")
self.assertEqual(quote["fuel_surcharge_text"], "未含13%燃油费")
def test_fuel_included_keeps_transport(self) -> None:
quote = extract_product_quote("运输费12000,含燃油费13%", **LAND)
self.assertEqual(quote["fee_rows"][0]["amount"], "12000")
self.assertEqual(float(quote["total"]), 12000.0)
self.assertEqual(quote["fuel_surcharge_rate"], "13%")
self.assertNotIn("remark", quote)
def test_untaxed_rate_keeps_transport(self) -> None:
quote = extract_product_quote("运输费12000,未含税13%", **LAND)
self.assertEqual(quote["fee_rows"][0]["amount"], "12000")
self.assertEqual(quote["tax_rate"], "13%")
self.assertEqual(quote["remark"], "未含税13%")
self.assertNotIn("fuel_surcharge_rate", quote)
def test_untaxed_blank_leaves_tax_empty_but_remark_is_kong(self) -> None:
quote = extract_product_quote("运输费12000,未含税", **LAND)
self.assertEqual(quote["fee_rows"][0]["amount"], "12000")
self.assertNotIn("tax_rate", quote)
# 未含税不带点数:税率留空,备注写「空」。
self.assertEqual(quote["remark"], "空")
self.assertEqual(quote["fee_rows"][0]["remark"], "空")
def test_port_shuttle_fee_with_fuel_and_untaxed(self) -> None:
# 正服 WO2026111270056:口岸接驳不带「费」也要按费用行识别;
# 未含燃油只乘主运费;未含税不带点数,税率留空、备注写「空」。
quote = extract_product_quote(
"运费15000CNY,未含燃油10%,未含税 口岸接驳2300CNY", **LAND
)
rows = {row["name"]: row for row in quote["fee_rows"]}
self.assertEqual(rows["运费"]["amount"], "16500")
self.assertEqual(rows["口岸接驳"]["amount"], "2300")
self.assertEqual(quote["total"], "18800")
self.assertEqual(quote["fuel_surcharge_rate"], "10%")
self.assertNotIn("tax_rate", quote)
self.assertEqual(quote["remark"], "空")
self.assertEqual(rows["运费"]["remark"], "空")
self.assertNotIn("remark", rows["口岸接驳"])
def test_recognition_card_shows_port_fee(self) -> None:
text = "运费15000CNY,未含燃油10%,未含税 口岸接驳2300CNY"
quote = extract_product_quote(text, **LAND)
rec = inspect_quote_recognition(text, quote, **LAND)
lines = rec["recognized"]
self.assertIn("运输费:CNY16500", lines)
self.assertIn("口岸接驳:CNY2300", lines)
self.assertIn("备注:空", lines)
def test_zero_tax(self) -> None:
quote = extract_product_quote("运输费12000,含0税", **LAND)
self.assertEqual(quote["fee_rows"][0]["amount"], "12000")
self.assertEqual(quote["tax_rate"], "0")
self.assertEqual(quote["remark"], "含0税")
def test_fuel_multiplies_only_main_transport_rows(self) -> None:
quote = extract_product_quote(
"运输费1000USD,提货费200USD,报关费100USD,未含燃油10%", **LAND
)
amounts = {row["name"]: row["amount"] for row in quote["fee_rows"]}
self.assertEqual(amounts.get("运输费"), "1100")
self.assertEqual(amounts.get("提货费"), "200")
self.assertEqual(amounts.get("报关费"), "100")
self.assertEqual(quote["total"], "1400")
def test_tax_remark_goes_to_main_transport_row(self) -> None:
# 跨境整车无 landSegments 时,主账用运输费行拼路线行;税备注必须在运输费行。
quote = extract_product_quote(
"提货费200USD,运输费1000USD,未含税13%", **LAND
)
rows = {row["name"]: row for row in quote["fee_rows"]}
self.assertEqual(rows["运输费"]["amount"], "1000")
self.assertEqual(rows["运输费"]["remark"], "未含税13%")
self.assertNotIn("remark", rows["提货费"])
def test_tax_and_fuel_combined_applies_both(self) -> None:
quote = extract_product_quote(
"运输费1000USD,未含税13%,未含燃油5%", **LAND
)
self.assertEqual(quote["fee_rows"][0]["amount"], "1050")
self.assertEqual(quote["total"], "1050")
self.assertEqual(quote["tax_rate"], "13%")
self.assertEqual(quote["fuel_surcharge_rate"], "5%")
self.assertEqual(quote["remark"], "未含税13%")
def test_mixed_currency_total_not_corrupted(self) -> None:
# 合计只统计主币种小计;异币种主运费不能把增量加进 USD 合计。
quote = extract_product_quote(
"运输费1000USD,报关费100CNY,未含燃油10%", **LAND
)
amounts = {row["name"]: row["amount"] for row in quote["fee_rows"]}
self.assertEqual(amounts.get("运输费"), "1100")
self.assertEqual(amounts.get("报关费"), "100")
self.assertEqual(quote["total"], "1100")
def test_fuel_multiplies_vehicle_rates(self) -> None:
quote = extract_product_quote(
"东莞-春疆,7.6米CNY13000元;9.6米CNY14000;未含燃油10%", **LAND
)
self.assertEqual(quote["vehicleRates"]["7.6米"], "14300")
self.assertEqual(quote["vehicleRates"]["9.6米"], "15400")
self.assertEqual(quote["total"], "14300")
self.assertEqual(quote["fuel_surcharge_rate"], "10%")
self.assertNotIn("remark", quote)
def test_fuel_multiplies_weight_prices(self) -> None:
quote = extract_product_quote(
"2-500KG 单价CNY990,未含燃油10%", **LAND
)
self.assertEqual(quote["weightPrices"][0]["price"], "1089")
self.assertEqual(quote["total"], "1089")
self.assertEqual(quote["price_per_kg"], "1089")
def test_non_land_ignores_condition(self) -> None:
quote = extract_product_quote(
"海运费1000USD,未含燃油13%", business_line="SEA", transport_mode="海运"
)
self.assertEqual(quote["fee_rows"][0]["amount"], "1000")
self.assertNotIn("fuel_surcharge_rate", quote)
def test_injected_quote_also_applies_condition(self) -> None:
# 上层预抽取报价不能绕过乘法,否则出单有燃油费率却没有乘运费。
quote = extract_product_quote(
"运输费3200,未含燃油13%",
injected={
"total": "3200",
"currency": "USD",
"fee_rows": [{"name": "运输费", "amount": "3200", "currency": "USD"}],
},
**LAND,
)
self.assertEqual(quote["fee_rows"][0]["amount"], "3616")
self.assertEqual(quote["total"], "3616")
self.assertNotIn("remark", quote)
class AttachSegmentTest(unittest.TestCase):
"""条件要随 landSegments 进主账,模板备注列/税率列才有值。"""
def _view(self) -> dict:
return {"facts": {"起运地": "东莞", "目的地": "春疆"}}
def test_vehicle_segment_carries_condition(self) -> None:
quote = extract_product_quote(
"东莞-春疆,7.6米CNY13000元;9.6米CNY14000;未含燃油10%", **LAND
)
quote = _attach_land_vehicle_rates_segment(quote, self._view())
seg = quote["landSegments"][0]
self.assertEqual(seg["fuel_surcharge_rate"], "10%")
self.assertNotIn("remark", seg)
self.assertEqual(seg["vehicleRates"]["7.6米"], "14300")
def test_weight_segment_carries_condition(self) -> None:
quote = extract_product_quote("2-500KG 单价CNY990,未含税13%", **LAND)
quote = _attach_land_weight_band_segment(quote, self._view())
seg = quote["landSegments"][0]
self.assertEqual(seg["tax_rate"], "13%")
self.assertEqual(seg["remark"], "未含税13%")
self.assertEqual(seg["weightPrices"][0]["price"], "990")
class SummaryCopyTest(unittest.TestCase):
"""群摘要要露出燃油费率,销售才知道运费是否已乘燃油;备注不再重复燃油。"""
def test_vehicle_lines_show_fuel_only(self) -> None:
quote = extract_product_quote(
"东莞-春疆,7.6米CNY13000元;9.6米CNY14000;未含燃油10%", **LAND
)
text = "\n".join(_manual_land_vehicle_lines(quote))
self.assertIn("燃油费率:10%", text)
self.assertNotIn("备注:", text)
def test_single_fee_lines_show_fuel_only(self) -> None:
quote = extract_product_quote("运输费USD3200,未含燃油13%", **LAND)
text = "\n".join(_manual_quote_lines(quote, **LAND, layout="manual"))
self.assertIn("燃油费率:13%", text)
self.assertNotIn("备注:", text)
if __name__ == "__main__":
unittest.main()