""" 人工报价格式回归:空运短句、陆运米长车型、陆运多币种。 对应产品给的三个样例(2026-11-22): 1. 空运:「1:300,+500,USD11/kg,卡转/MUST GO、TH、NKG-KUL、11月2日」 2. 陆运:「东莞-春疆,危险品,7.6米CNY13000元;9.6米CNY14000;45HQ,CNY18500;都是含0点税」 3. 陆运:「春疆-罗勇,45HQ,运输费USD3200,燃油费USD300,过境费USD70,报关费cny1400」 本文件只测解析与挂段,不连企微/TMS/主账。 禁止把这三种写法退化回「7.6 当运费」或「USD+CNY 相加成 4970」。 """ 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.sea_group_ops import _attach_land_vehicle_rates_segment AIR_SAMPLE = "工单号、1:300,+500,USD11/kg,卡转/MUST GO、TH、NKG-KUL、11月2日" LAND_VEHICLE_SAMPLE = ( "东莞-春疆,危险品,7.6米CNY13000元;9.6米CNY14000;45HQ,CNY18500;都是含0点税" ) LAND_MULTI_CCY_SAMPLE = ( "春疆-罗勇,45HQ,运输费USD3200,燃油费USD300,过境费USD70,报关费cny1400" ) class ManualQuoteAirTest(unittest.TestCase): """空运短句:单价按 KG、+500 只作重量分档,不冒充附加费。""" def test_unit_price_and_weight_break(self) -> None: quote = extract_product_quote(AIR_SAMPLE, business_line="AIR", transport_mode="空运") rows = list(quote.get("fee_rows") or []) self.assertEqual(len(rows), 1) row = rows[0] self.assertEqual(row.get("name"), "空运费") self.assertEqual(row.get("unit"), "KG") self.assertEqual(row.get("unit_price"), "11") self.assertEqual(row.get("currency"), "USD") self.assertEqual(row.get("weight_break"), "+500") # 只有单价没有数量时合计就是单价,摘要显示「单价USD11/KG(+500)」。 self.assertEqual(str(quote.get("total") or ""), "11") def test_side_fields_are_harvested(self) -> None: from agent.policy.air_group_ops import _harvest_air_manual_airline from agent.schema.added_services import harvest_added_services from agent.schema.field_validate import _harvest_air_ratio, _harvest_quote_date self.assertEqual(_harvest_air_manual_airline(AIR_SAMPLE), "TH") self.assertEqual(harvest_added_services(AIR_SAMPLE), ["卡转", "MUST GO"]) self.assertEqual(_harvest_air_ratio(AIR_SAMPLE), "1:300") self.assertTrue(_harvest_quote_date(AIR_SAMPLE).endswith("-11-02")) class ManualQuoteLandVehicleTest(unittest.TestCase): """陆运多车型价:米长与柜型都是车型键,米长数字绝不能当运费。""" def test_mile_and_container_labels_become_rates(self) -> None: quote = extract_product_quote( LAND_VEHICLE_SAMPLE, business_line="LAND", transport_mode="陆运" ) rates = dict(quote.get("vehicleRates") or {}) self.assertEqual(rates.get("7.6米"), "13000") self.assertEqual(rates.get("9.6米"), "14000") self.assertEqual(rates.get("45HQ"), "18500") self.assertEqual(str(quote.get("currency") or ""), "CNY") # 备注由 land_quote_condition 归一成「含0税」;旧写法「含0点税」也算通过, # 两条并行改动谁先落地都不该让用例红。 self.assertIn(str(quote.get("remark") or ""), {"含0点税", "含0税"}) self.assertEqual(str(quote.get("tax_rate") or ""), "0") # 旧缺陷:整句只剩一行「陆运费 7.6」。 for row in quote.get("fee_rows") or []: self.assertNotIn(str(row.get("amount") or ""), {"7.6", "9.6"}) def test_rates_attach_to_segment_and_clear_fee_rows(self) -> None: quote = extract_product_quote( LAND_VEHICLE_SAMPLE, business_line="LAND", transport_mode="陆运" ) out = _attach_land_vehicle_rates_segment( quote, {"facts": {"起运港": "东莞", "目的港": "春疆"}} ) segs = list(out.get("landSegments") or []) self.assertEqual(len(segs), 1) self.assertEqual(segs[0].get("origin"), "东莞") self.assertEqual(segs[0].get("destination"), "春疆") self.assertEqual((segs[0].get("vehicleRates") or {}).get("45HQ"), "18500") # 多车型价只走矩阵列,费目行清空,避免车型名被当成费目写进起始地。 self.assertEqual(list(out.get("fee_rows") or []), []) class ManualQuoteLandMultiCurrencyTest(unittest.TestCase): """陆运跨境:美元运费 + 人民币报关费,各行保币种,合计不跨币种相加。""" def test_rows_keep_their_own_currency(self) -> None: quote = extract_product_quote( LAND_MULTI_CCY_SAMPLE, business_line="LAND", transport_mode="陆运" ) rows = list(quote.get("fee_rows") or []) self.assertEqual([r.get("name") for r in rows], ["运输费", "燃油费", "过境费", "报关费"]) self.assertEqual([r.get("currency") for r in rows], ["USD", "USD", "USD", "CNY"]) def test_total_is_not_cross_currency_sum(self) -> None: quote = extract_product_quote( LAND_MULTI_CCY_SAMPLE, business_line="LAND", transport_mode="陆运" ) # 3200+300+70 = 3570 USD;报关费 1400 CNY 另计,绝不加进 USD 合计。 self.assertEqual(str(quote.get("total") or ""), "3570.0") self.assertEqual(str(quote.get("currency") or ""), "USD") self.assertTrue(quote.get("mixed_currency")) def test_vehicle_type_without_price_keeps_fees(self) -> None: quote = extract_product_quote( LAND_MULTI_CCY_SAMPLE, business_line="LAND", transport_mode="陆运" ) self.assertEqual(str(quote.get("vehicleType") or ""), "45HQ") out = _attach_land_vehicle_rates_segment( quote, {"facts": {"起运港": "春疆", "目的港": "罗勇"}} ) seg = (out.get("landSegments") or [{}])[0] self.assertEqual(seg.get("vehicleType"), "45HQ") # 只有车型没有价矩阵:费用明细必须保留,不能因挂车型被清空。 self.assertEqual(len(out.get("fee_lines") or []), 4) class ManualQuoteSummaryTest(unittest.TestCase): """销售侧摘要:三档车型价和备注必须露出来,不能只回一个合计。""" def test_vehicle_matrix_and_remark_are_shown(self) -> None: from agent.policy import inquiry_copy as copy quote = extract_product_quote( LAND_VEHICLE_SAMPLE, business_line="LAND", transport_mode="陆运" ) out = _attach_land_vehicle_rates_segment( quote, {"facts": {"起运港": "东莞", "目的港": "春疆"}} ) text = "\n".join(copy._manual_quote_lines(out, business_line="LAND", transport_mode="陆运")) self.assertIn("车型价:7.6米 CNY 13000;9.6米 CNY 14000;45HQ CNY 18500", text) self.assertIn("备注:含0", text) self.assertIn("此处取第一档", text) def test_multi_currency_lines_keep_currency(self) -> None: from agent.policy import inquiry_copy as copy quote = extract_product_quote( LAND_MULTI_CCY_SAMPLE, business_line="LAND", transport_mode="陆运" ) out = _attach_land_vehicle_rates_segment( quote, {"facts": {"起运港": "春疆", "目的港": "罗勇"}} ) text = "\n".join(copy._manual_quote_lines(out, business_line="LAND", transport_mode="陆运")) self.assertIn("运输费:USD 3200", text) self.assertIn("报关费:CNY 1400", text) self.assertIn("费用合计:USD 3570.0", text) # 不再出现把美元和人民币加在一起的假数。 self.assertNotIn("4970", text) class ManualQuoteSeaRegressionTest(unittest.TestCase): """海运单币种仍照旧加总,别被多币种改动带偏。""" def test_sea_single_currency_still_sums(self) -> None: quote = extract_product_quote( "码头操作费100CNY,报关费300CNY", business_line="SEA", transport_mode="海运" ) self.assertEqual(str(quote.get("currency") or ""), "CNY") self.assertEqual(str(quote.get("total") or ""), "400.0") self.assertFalse(quote.get("mixed_currency")) class ManualQuoteAirLayoutTest(unittest.TestCase): """空运人工报价对外版式(产品 2026-11-22 定稿):比例/重量等级/舱位信息 + 价格。""" def _text(self) -> str: from agent.policy import inquiry_copy as copy quote = extract_product_quote(AIR_SAMPLE, business_line="AIR", transport_mode="空运") quote["source"] = "PRODUCT" quote["source_label"] = "航线报价" quote["airlineCode"] = "TH" quote["airline"] = "TH" facts = { "起运港": "NKG", "目的港": "KUL", "品名": "普货", "件数": "10", "毛重": "500", "体积": "3", "包装方式": "纸箱", "比例": "1:300", "报价日期": "2026-11-02", "added_services": "卡转、MUST GO", } return "\n".join( copy._manual_quote_lines( quote, business_line="AIR", facts=facts, layout="manual" ) ) def test_full_block_matches_product_layout(self) -> None: self.assertEqual( self._text(), "\n".join( [ "价格方案如下:", "比例:1:300", "重量等级:+500", # 体积 3CBM × 166.7 = 500.1KG > 毛重 500KG → 取体积重; # 产品 2026-11-22 晚确定只写数字,不括注依据。 "计费重:500.1KG", "舱位信息:TH,NKG-KUL,2026-11-2起(包含航司、线路、班期、舱位日期哪天起),具体根据放舱情况而定", "", "价格", "空运费:USD11/kg", "报价有效期:2026-11-2", "原始报价参考:" + AIR_SAMPLE, ] ), ) def test_missing_facts_do_not_print_dashes(self) -> None: from agent.policy import inquiry_copy as copy quote = extract_product_quote(AIR_SAMPLE, business_line="AIR", transport_mode="空运") text = "\n".join( copy._manual_quote_lines(quote, business_line="AIR", facts={}, layout="manual") ) # 没有报价日期就不写有效期,不拿「-」占位、不编日期。 self.assertNotIn("报价有效期", text) self.assertNotIn(":-", text) def test_cabin_scope_note_is_never_dropped(self) -> None: """ 「(包含航司、线路、班期、舱位日期哪天起)」是产品要求保留的说明。 只有航司、没有线路和日期时也必须带上;四样全缺才整行不写。 """ from agent.policy import inquiry_copy as copy quote = extract_product_quote(AIR_SAMPLE, business_line="AIR", transport_mode="空运") quote["airlineCode"] = "TH" text = "\n".join( copy._manual_quote_lines(quote, business_line="AIR", facts={}, layout="manual") ) self.assertIn( "舱位信息:TH(包含航司、线路、班期、舱位日期哪天起),具体根据放舱情况而定", text, ) empty = extract_product_quote(AIR_SAMPLE, business_line="AIR", transport_mode="空运") empty.pop("airlineCode", None) empty.pop("airline", None) bare = "\n".join( copy._manual_quote_lines(empty, business_line="AIR", facts={}, layout="manual") ) self.assertNotIn("舱位信息", bare) class ManualQuoteLandLayoutTest(unittest.TestCase): """陆运多车型人工报价竖排版式:一段一价 + 税率 + 原始报价参考。""" def _text(self) -> str: from agent.policy import inquiry_copy as copy quote = extract_product_quote( LAND_VEHICLE_SAMPLE, business_line="LAND", transport_mode="陆运" ) out = _attach_land_vehicle_rates_segment( quote, {"facts": {"起运港": "东莞", "目的港": "春疆"}} ) out["raw_text"] = LAND_VEHICLE_SAMPLE return "\n".join( copy._manual_quote_lines(out, business_line="LAND", layout="manual") ) def test_vehicle_matrix_is_vertical(self) -> None: self.assertEqual( self._text(), "\n".join( [ "车型/数量:7.6米", "运输费:CNY13000元", "车型/数量:9.6米", "运输费:CNY14000元", "车型/数量:45HQ", "运输费:CNY18500元", "税率:0", "原始报价参考:" + LAND_VEHICLE_SAMPLE, ] ), ) def test_group_brief_drops_tms_miss_block_when_product_quoted(self) -> None: """ 产品 2026-11-22 口径:TMS 没查到价、产品已经在群里报了多车型价时, 摘要不再出现「TMS报价:TMS 暂无匹配报价 + 请另报」这一段,直接进「人工报价:」。 图 WO202611220022 就是这种场景:销售看到红框里的无价空话会以为报价没生效。 """ from agent.policy import inquiry_copy as copy quote = extract_product_quote( LAND_VEHICLE_SAMPLE, business_line="LAND", transport_mode="陆运" ) manual = _attach_land_vehicle_rates_segment( quote, {"facts": {"起运港": "东莞", "目的港": "春疆"}} ) brief = copy.sea_group_brief( work_order_no="WO202611220022", facts={"起运港": "东莞", "目的港": "春疆", "品名": "危险品"}, quote=None, collab_facts={}, sales_name="罗小华", product_names=[], has_price=False, tms_quote={}, product_quote=manual, transport_mode="陆运", ) self.assertNotIn("TMS报价:", brief) self.assertNotIn("TMS 暂无匹配报价", brief) self.assertNotIn("产品人员可通过“纯文字”告知新报价", brief) # TMS 段整段去掉后上面没有小标题,必须补「人工报价:」才知道这是产品报的价。 self.assertIn( "人工报价:\n车型/数量:7.6米\n运输费:CNY13000元", brief ) def test_group_brief_keeps_tms_block_when_tms_priced(self) -> None: """TMS 有价时照旧保留 TMS 段,手工补的价另起「人工报价:」,两段不混。""" from agent.policy import inquiry_copy as copy tms = { "total": "CNY 20000", "currency": "CNY", "source": "TMS", "source_label": "TMS标准报价", "fee_lines": [{"name": "海运费", "amount": "20000.00", "currency": "CNY"}], } brief = copy.sea_group_brief( work_order_no="WO202611220023", facts={"起运港": "上海", "目的港": "海防", "品名": "机器设备"}, quote=tms, collab_facts={}, sales_name="罗小华", product_names=["林绵勋"], has_price=True, tms_quote=tms, product_quote={ "source_label": "产品报价", "fee_lines": [ {"name": "燃油附加费", "amount": "150.00", "currency": "USD"} ], }, transport_mode="海运", ) self.assertIn("TMS报价:", brief) self.assertNotIn("TMS 暂无匹配报价", brief) self.assertIn("人工报价:", brief) def test_zero_tax_word_becomes_tax_rate(self) -> None: quote = extract_product_quote( LAND_VEHICLE_SAMPLE, business_line="LAND", transport_mode="陆运" ) # 「都是含0点税」= 税率 0;备注归一为「含0税」后仍留着给报价单备注列。 self.assertEqual(str(quote.get("tax_rate") or ""), "0") self.assertIn(str(quote.get("remark") or ""), {"含0点税", "含0税"}) def test_plain_tax_word_does_not_invent_rate(self) -> None: # 「含税」看不出点数,禁止猜成 0 或 13。 quote = extract_product_quote( "东莞-春疆,7.6米CNY13000元;9.6米CNY14000;含税", business_line="LAND", transport_mode="陆运", ) self.assertEqual(str(quote.get("tax_rate") or ""), "") def test_multi_currency_fees_keep_ref_line(self) -> None: from agent.policy import inquiry_copy as copy quote = extract_product_quote( LAND_MULTI_CCY_SAMPLE, business_line="LAND", transport_mode="陆运" ) out = _attach_land_vehicle_rates_segment( quote, {"facts": {"起运港": "春疆", "目的港": "罗勇"}} ) text = "\n".join( copy._manual_quote_lines(out, business_line="LAND", layout="manual") ) # 逐项费用写法保持原样,只多一行原话参考。 self.assertIn("运输费:USD 3200", text) self.assertIn("报关费:CNY 1400", text) self.assertIn("原始报价参考:" + LAND_MULTI_CCY_SAMPLE, text) class ManualQuoteLandSingleVehicleTest(unittest.TestCase): """单档车型价:群里「8T CNY990」不能把 8 当运费(WO202611100043)。""" def test_single_vehicle_rate_keeps_model_and_price(self) -> None: quote = extract_product_quote( "产品报价:8T CNY990", business_line="LAND", transport_mode="陆运" ) # 8T 是车型、990 才是费用;旧实现丢掉单档车型价后在金额回退里抽到 8。 self.assertEqual(dict(quote.get("vehicleRates") or {}), {"8T": "990"}) self.assertEqual(str(quote.get("total") or ""), "990") self.assertEqual(str(quote.get("currency") or ""), "CNY") for row in quote.get("fee_rows") or []: self.assertNotEqual(str(row.get("amount") or "").strip(), "8") def test_single_vehicle_rate_summary_shows_model_and_money(self) -> None: from agent.policy import inquiry_copy as copy quote = extract_product_quote( "8T CNY990", business_line="LAND", transport_mode="陆运" ) out = _attach_land_vehicle_rates_segment( quote, {"facts": {"起运港": "深圳", "目的港": "香港"}} ) text = "\n".join( copy._manual_quote_lines( out, business_line="LAND", transport_mode="陆运", layout="manual" ) ) self.assertIn("车型/数量:8T", text) self.assertIn("运输费:CNY990元", text) self.assertNotIn("CNY8元", text) def test_fee_name_glued_vehicle_rate_shows_tonnage_and_unit(self) -> None: """「运费10T:1700HKD/车」:摘要要出车型 10T、1700/车,条款进备注(WO202610300015)。""" from agent.policy import inquiry_copy as copy text = ( "深圳--香港元朗运费10T:1700HKD/车\n" "超时费:免等候2小时,超出按200/小时收取;如遇海关查验,查验等候免3小时," "超过了200/小时,压夜按运费80%,压日按运费100%以上港币报价,不含保,杂费实报实" ) quote = extract_product_quote( text, business_line="LAND", transport_mode="陆运" ) out = _attach_land_vehicle_rates_segment( quote, {"facts": {"起运港": "深圳", "目的港": "香港"}} ) lines = copy._manual_quote_lines( out, business_line="LAND", transport_mode="陆运", layout="manual" ) self.assertIn("车型/数量:10T", lines) self.assertIn("运输费:HKD1700/车", lines) self.assertTrue(any(line.startswith("备注:超时费:免等候2小时") for line in lines)) self.assertFalse(any("HKD10" in line for line in lines)) self.assertFalse(any(line.startswith("压夜按运费:") for line in lines)) def test_non_land_single_rate_keeps_old_two_tier_guard(self) -> None: """非陆运不改旧口径:没写「车型」且只有一档时仍不当车型矩阵。""" quote = extract_product_quote( "8T CNY990", business_line="SEA", transport_mode="海运" ) self.assertFalse(dict(quote.get("vehicleRates") or {})) self.assertEqual(str(quote.get("total") or ""), "990") class ManualQuoteRefMentionTest(unittest.TestCase): """原始报价参考不能把「@机器人」当报价内容(2026-11-22 产品反馈)。""" def _line(self, raw: str) -> str: from agent.policy import inquiry_copy as copy return copy._raw_quote_ref_line({"raw_text": raw}) def test_at_bot_is_stripped(self) -> None: # 群里报价常以「@机器人」开头;参考行只留产品原话。 self.assertEqual( self._line("@智价通AIQ " + AIR_SAMPLE), "原始报价参考:" + AIR_SAMPLE ) def test_bot_name_with_spaces_is_stripped(self) -> None: # 企微显示名可能被 @ 成中间带空格,不能只认写死的一整串。 self.assertEqual( self._line("@智价通 AIQ " + AIR_SAMPLE), "原始报价参考:" + AIR_SAMPLE ) def test_longer_group_display_name_is_stripped(self) -> None: # 测服真机:后台配置名「智价通AIQ」,群里显示名是「智价通AIQ测试」。 # 只按配置名部分替换会留下「测试」两个残字,必须整体剥掉整个称呼。 raw = ( "@智价通AIQ测试 1:300,+500,USD11/kg,卡转/MUST GO、" "TH、NKG-KUL、11月23日" ) self.assertEqual( self._line(raw), "原始报价参考:1:300,+500,USD11/kg,卡转/MUST GO、TH、NKG-KUL、11月23日", ) def test_archive_seat_mention_is_stripped(self) -> None: # 兜底唤醒 @ 的是会话存档席位账号,同样不是报价内容。 self.assertEqual( self._line("@会话存档-机器人 " + AIR_SAMPLE), "原始报价参考:" + AIR_SAMPLE ) def test_extraction_drops_mention_from_raw_text(self) -> None: # 新写入主账的原话也不该带 @机器人,历史单子由上面展示层兜底。 quote = extract_product_quote( "@智价通AIQ " + AIR_SAMPLE, business_line="AIR", transport_mode="空运" ) self.assertEqual(str(quote.get("raw_text") or ""), AIR_SAMPLE) if __name__ == "__main__": unittest.main()