Files
inquiry_robot/inquiry-agent/tests/test_air_chargeable.py
T
linmianxunandCursor d9b2029c47 协同群改为智价通BOT接管对话,报价单按需生成;补客户名称、计费重与陆运报价条件
为什么改:应用建的协同群只能代答,销售在群里发文件、@人、问报价都对不上人;
本次把群对话交给智价通BOT,应用只负责拉群和解散群,并把几处销售已反馈的口径补齐。

群与身份:
- 新增 group_bot 短协调(Redis 带环境前缀):记录 BOT 已接管群、当前关联工单、
  @BOT 指令窗口与窗口外先到的附件,只做协调不当六态,六态仍在主账。
- 群里没有 @BOT 的文字不回;非 @ 文字断开当前单关联,之后必须重新 @BOT 带工单号。
- 先发文件再 @BOT 也认:窗口外附件先缓冲,等 @BOT 认领后补办。
- 会话存档只补附件、不重复办理 BOT 已办的同一句文字,避免同一条消息回两遍。
- 入站 from.userid 是加密 open_userid 时按官方接口换明文(带 Redis 缓存),
  否则与主账 sales_wecom_id 永远比不上,航线本人会被判成「不是本人」。
- 桥侧只认本群 req_id,不再借用别的群 req_id,避免测服把话发进别的群。

报价单:
- 建群后只发字段摘要和成交卡,Excel/PDF 等群里 @BOT 说「生成报价单」再出;
  多段逐段出,没价出空白表;出表后补推成交跟进不涨 deal_version、不重置等待计时。
- 陆运转人工先选业务类型和报价场景再建群,按选定模板名直取,不再靠关键词猜。
- 空运舱位只回一条时直接出该条报价,不再发选择卡。

字段与金额:
- 客户名称改为海/空/陆询价非必填,有值写回工单列,后台列表、报价单与 factsJson 一致。
- 空运新增计费重投影:体积重=CBM×166.7,计费重=max(毛重,体积重);
  TMS 无价、航线手工按 KG 报价时按计费重×单价算整票金额,群摘要与报价单同公式。
- 陆运报价附加条件统一到 land_quote_condition:未含税/含0税/未含燃油/含燃油费,
  燃油只乘主运费,不乘提货送货报关等杂费;跨境整车无备注列时追加到底部备注事项。
- 海运拼柜不适用箱型箱量:不进必填、不进补问、不出站,改回整柜可再用。
- 海运提货地址/派送地址改为非必填,写了才出站(placeOfReceipt/deliveryAddress),
  禁止从港口或贸易条款反推地址;TMS 查不到价时把 TMS 原话透给销售。
- 中港集拼与跨境集拼同口径:按体积+毛重,不采车型数量;修正被国内拼车别名抢单。
- 报关费区只收报关类费目;口岸海关监管费+接驳费按车型列回填固定行。

主账与后台:
- 看板处理量按协同群处理人统计;下线「平均报价销售额」;处理人搜索先把姓名换回企微 id。
- 「转人工」筛选同时兼容旧六态与新 collab_mode=handoff,新版单不再漏。
- 报价单客户名称、提货/派送地址、空运计费重、拼柜空箱型按新口径填表。
- 新增协同成员查询支持与 AIR_NEED_WO v2 迁移脚本。

文档:04 补海运/空运/陆运必填与地址口径,10 补地址出站规则,12 补测服独立应用 1000015 的部署与切换记录。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-23 14:18:22 +08:00

257 lines
11 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.
"""
空运计费重单测:体积重 = 体积(CBM) × 166.7,计费重 = max(毛重, 体积重)。
覆盖场景(产品口径 2026-11-22):
- TMS 没有报价、航线按 KG 手工报价时,由工单体积/毛重算出计费重;
- 计费重进报价对象、KG 费用行按「单价×计费重」落金额、合计跟着重算;
- 群里和报价单都看得到同一个数;
- 读不到体积也读不到毛重时绝不编数字;
- 海运/陆运不套空运体积重。
线程:纯函数单测,不碰网络与 DB。
"""
from __future__ import annotations
import unittest
from agent.schema.air_chargeable import (
apply_to_quote,
compute_chargeable,
format_kg,
harvest_declared_chargeable,
parse_quantity,
volume_weight_kg,
)
class ParseQuantityTest(unittest.TestCase):
"""数字解析只去单位,不做语义猜测。"""
def test_strips_weight_and_volume_units(self) -> None:
self.assertEqual(parse_quantity("1,200KG"), 1200.0)
self.assertEqual(parse_quantity("500KGS"), 500.0)
self.assertEqual(parse_quantity("3CBM"), 3.0)
self.assertEqual(parse_quantity("2.5 立方米"), 2.5)
def test_ton_is_projected_to_kg(self) -> None:
# 1.5 吨 = 1500KG,确定性单位投影,不是猜货型。
self.assertEqual(parse_quantity("1.5吨"), 1500.0)
def test_currency_prefix_is_not_a_quantity(self) -> None:
self.assertEqual(parse_quantity("USD 11.00"), 11.0)
def test_blank_and_garbage_return_none(self) -> None:
for raw in (None, "", "-", "—", "左右", "电放"):
self.assertIsNone(parse_quantity(raw), raw)
class ChargeableFormulaTest(unittest.TestCase):
"""体积重与取大规则。"""
def test_volume_weight_uses_166_7(self) -> None:
self.assertAlmostEqual(volume_weight_kg(1.0) or 0, 166.7, places=4)
self.assertAlmostEqual(volume_weight_kg(3.0) or 0, 500.1, places=4)
def test_volume_heavier_than_gross(self) -> None:
out = compute_chargeable({"体积": "3", "毛重": "500"})
self.assertIsNotNone(out)
assert out is not None
self.assertAlmostEqual(out["chargeable_kg"], 500.1, places=4)
self.assertEqual(out["basis"], "volume")
self.assertEqual(format_kg(out["chargeable_kg"]), "500.1")
def test_gross_heavier_than_volume(self) -> None:
out = compute_chargeable({"体积": "1", "毛重": "500"})
assert out is not None
self.assertAlmostEqual(out["chargeable_kg"], 500.0, places=4)
self.assertEqual(out["basis"], "gross")
def test_only_one_side_still_works(self) -> None:
only_volume = compute_chargeable({"体积": "2"})
assert only_volume is not None
self.assertAlmostEqual(only_volume["chargeable_kg"], 333.4, places=4)
only_weight = compute_chargeable({"毛重": "800"})
assert only_weight is not None
self.assertAlmostEqual(only_weight["chargeable_kg"], 800.0, places=4)
def test_explicit_fact_wins(self) -> None:
out = compute_chargeable({"计费重": "800", "体积": "10", "毛重": "100"})
assert out is not None
self.assertAlmostEqual(out["chargeable_kg"], 800.0, places=4)
self.assertEqual(out["basis"], "explicit")
def test_no_numbers_returns_none(self) -> None:
self.assertIsNone(compute_chargeable({}))
self.assertIsNone(compute_chargeable({"体积": "-", "毛重": "待定"}))
class DeclaredChargeableTextTest(unittest.TestCase):
"""航线在群里明说计费重:以他说的为准,且不能被抽成费用行。"""
def test_declared_number_is_harvested(self) -> None:
self.assertEqual(harvest_declared_chargeable("计费重800KG,USD11/kg"), 800.0)
self.assertEqual(harvest_declared_chargeable("计费重量:1,200.5 公斤"), 1200.5)
self.assertIsNone(harvest_declared_chargeable("计费重按实重"))
def test_declared_chargeable_is_not_a_fee_row(self) -> None:
from agent.llm.extract_text import extract_product_quote
quote = extract_product_quote(
"计费重800KG,空运费USD11/kg", business_line="AIR", transport_mode="空运"
)
names = [str(row.get("name") or "") for row in quote.get("fee_rows") or []]
self.assertIn("空运费", names)
self.assertNotIn("计费", names)
class ApplyToQuoteTest(unittest.TestCase):
"""报价对象补齐:计费重 + 行金额 + 合计。"""
def _manual_quote(self) -> dict:
# 与 extract_product_quote("USD11/kg") 的产出同形:只有单价,没有数量。
return {
"total": "11",
"currency": "USD",
"fee_rows": [
{"name": "空运费", "amount": "11", "currency": "USD", "unit": "KG", "unit_price": "11"}
],
"fee_lines": [
{"name": "空运费", "amount": "11", "currency": "USD", "unit": "KG", "unit_price": "11"}
],
}
def test_kg_row_becomes_unit_price_times_chargeable(self) -> None:
out = apply_to_quote(self._manual_quote(), {"体积": "3", "毛重": "500"})
self.assertEqual(str(out.get("chargeableWeightKg")), "500.1")
self.assertEqual(str(out.get("volumeWeightKg")), "500.1")
row = dict((out.get("fee_rows") or [])[0])
self.assertEqual(row.get("quantity"), "500.1")
self.assertAlmostEqual(float(row.get("amount")), 5501.1, places=4)
self.assertEqual(str(out.get("total")), "5501.1")
# 币种不能被计费重改动。
self.assertEqual(str(out.get("currency")), "USD")
def test_total_only_sums_same_currency(self) -> None:
quote = self._manual_quote()
quote["fee_rows"].append({"name": "报关费", "amount": "400", "currency": "CNY"})
quote["fee_lines"] = list(quote["fee_rows"])
out = apply_to_quote(quote, {"毛重": "500"})
# 合计只加第一行币种(USD),CNY 报关费留在明细里不折算。
self.assertEqual(str(out.get("total")), "5500")
def test_existing_chargeable_is_not_overwritten(self) -> None:
quote = self._manual_quote()
quote["chargeableWeightKg"] = "800"
out = apply_to_quote(quote, {"体积": "10", "毛重": "100"})
self.assertEqual(str(out.get("chargeableWeightKg")), "800")
self.assertIn((out.get("fee_rows") or [])[0].get("quantity"), (None, ""))
def test_airline_total_without_qty_is_kept(self) -> None:
# 「空运费 USD8800,单价 11/kg」:航线给了整票金额,不能按体积重覆盖。
quote = {
"total": "8800",
"currency": "USD",
"fee_rows": [
{"name": "空运费", "amount": "8800", "currency": "USD", "unit": "KG", "unit_price": "11"}
],
"fee_lines": [],
}
quote["fee_lines"] = list(quote["fee_rows"])
out = apply_to_quote(quote, {"体积": "3", "毛重": "500"})
self.assertEqual(str((out.get("fee_rows") or [])[0].get("amount")), "8800")
self.assertEqual(str(out.get("total")), "8800")
def test_non_air_lines_are_untouched(self) -> None:
for line in ("SEA", "LAND"):
out = apply_to_quote(self._manual_quote(), {"体积": "3", "毛重": "500"}, business_line=line)
self.assertNotIn("chargeableWeightKg", out)
def test_no_facts_keeps_quote_as_is(self) -> None:
out = apply_to_quote(self._manual_quote(), {})
self.assertEqual(str(out.get("total")), "11")
self.assertNotIn("chargeableWeightKg", out)
class AirManualLayoutChargeableTest(unittest.TestCase):
"""会话界面:人工报价版式里要有计费重。"""
def test_layout_prints_chargeable_without_basis(self) -> None:
"""
计费重只写数字。
产品 2026-11-22 晚在空运协同群核对:人工报价里「计费重:500KG(按实重)」
「计费重:333.4KG(按体积重)」的括注要全部去掉,销售只看 KG 数。
"""
from agent.policy import inquiry_copy as copy
quote = {
"currency": "USD",
"fee_rows": [
{"name": "空运费", "amount": "11", "currency": "USD", "unit": "KG", "unit_price": "11"}
],
"fee_lines": [],
}
quote["fee_lines"] = list(quote["fee_rows"])
facts = {"毛重": "500", "体积": "3", "起运港": "NKG", "目的港": "KUL"}
text = "\n".join(
copy._manual_quote_lines(quote, business_line="AIR", facts=facts, layout="manual")
)
self.assertIn("计费重:500.1KG", text)
self.assertNotIn("按体积重", text)
self.assertNotIn("按实重", text)
def test_stored_basis_never_leaks_hint(self) -> None:
"""
报价对象上已落库 chargeable_weight_basis(volume/gross)时也不能把括注写出来。
这是本次 BUG 的原始路径:落库时写了依据,渲染时又拿依据拼括注。
依据只留在数据里供排查,不参与对外文字。
"""
from agent.policy import inquiry_copy as copy
quote = {
"currency": "USD",
"chargeableWeightKg": "333.4",
"chargeable_weight_basis": "volume",
"fee_rows": [
{"name": "空运费", "amount": "11", "currency": "USD", "unit": "KG", "unit_price": "11"}
],
}
text = "\n".join(
copy._manual_quote_lines(quote, business_line="AIR", facts={}, layout="manual")
)
self.assertIn("计费重:333.4KG", text)
self.assertNotIn("按体积重", text)
self.assertNotIn("按实重", text)
def test_layout_prints_explicit_chargeable_without_hint(self) -> None:
from agent.policy import inquiry_copy as copy
quote = {
"currency": "USD",
"chargeableWeightKg": "800",
"fee_rows": [{"name": "空运费", "amount": "11", "currency": "USD", "unit": "KG"}],
}
text = "\n".join(
copy._manual_quote_lines(quote, business_line="AIR", facts={}, layout="manual")
)
self.assertIn("计费重:800KG", text)
self.assertNotIn("按体积重", text)
def test_no_facts_no_chargeable_line(self) -> None:
from agent.policy import inquiry_copy as copy
quote = {
"currency": "USD",
"fee_rows": [{"name": "空运费", "amount": "11", "currency": "USD", "unit": "KG"}],
}
text = "\n".join(
copy._manual_quote_lines(quote, business_line="AIR", facts={}, layout="manual")
)
self.assertNotIn("计费重", text)
if __name__ == "__main__":
unittest.main()