Files
inquiry_robot/inquiry-agent/tests/test_land_options.py
T

181 lines
7.3 KiB
Python

"""
陆运选项单测:三项各自必填、不联动;中港拆进运输类型;样例表跟类型/线路缺项走。
不连企微、主账、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)
from agent.schema.land_options import (
harvest_land_options,
looks_like_confirm,
option_prompt,
option_prompt_needs_sample,
sample_pair_allowed,
validate_land_options,
)
class LandOptionsTest(unittest.TestCase):
def test_any_type_route_combo_ok(self):
got = validate_land_options(
{
"运输类型": "国内运输拼车",
"线路类别": "东南亚",
"运输分类": "急件",
}
)
self.assertTrue(got["ok"])
self.assertEqual(got["normalized"]["运输类型"], "国内运输拼车")
self.assertEqual(got["normalized"]["线路类别"], "东南亚")
def test_bare_hk_does_not_fill_type_or_route(self):
cur = harvest_land_options("中港 急件")
self.assertFalse(cur.get("运输类型"))
self.assertFalse(cur.get("线路类别"))
self.assertEqual(cur.get("运输分类"), "急件")
def test_hk_full_and_lcl_types(self):
self.assertEqual(
harvest_land_options("中港整车")["运输类型"], "中港整车"
)
self.assertEqual(
harvest_land_options("中港零担/集拼 东南亚")["运输类型"],
"中港零担/集拼",
)
self.assertEqual(
harvest_land_options("中港零担")["运输类型"], "中港零担/集拼"
)
def test_new_class_names(self):
cur = harvest_land_options(
"普件(东南亚、中港常用线路/常规车型)"
)
self.assertEqual(
cur["运输分类"], "普件(东南亚、中港常用线路/常规车型)"
)
self.assertFalse(
harvest_land_options("普件,常用线路/常规车型").get("运输分类")
)
def test_no_hk_load_field(self):
got = validate_land_options(
{
"运输类型": "中港整车",
"线路类别": "东南亚",
"运输分类": "急件",
}
)
self.assertTrue(got["ok"])
self.assertNotIn("中港装载方式", got["missing"])
self.assertFalse(got["normalized"].get("中港装载方式"))
def test_followup_route_class_keeps_type(self):
cur = harvest_land_options(
"国内城配/拖车/打包;普件(东南亚、中港常用线路/常规车型)",
current={"运输类型": "国内运输拼车"},
)
self.assertEqual(cur["运输类型"], "国内运输拼车")
self.assertEqual(cur["线路类别"], "国内城配/拖车/打包")
self.assertEqual(
cur["运输分类"], "普件(东南亚、中港常用线路/常规车型)"
)
def test_model_land_does_not_wipe_type(self):
dropped = harvest_land_options(
"国内城配/拖车/打包;急件",
current={"运输类型": "陆运"},
)
self.assertFalse(dropped.get("运输类型"))
cur = harvest_land_options(
"国内城配/拖车/打包;急件",
current={"运输类型": "国内运输拼车"},
)
junk = harvest_land_options("陆运", current=cur)
self.assertEqual(junk["运输类型"], "国内运输拼车")
self.assertEqual(junk["线路类别"], "国内城配/拖车/打包")
cur = harvest_land_options("广州到深圳国内拼车衣服100方")
self.assertEqual(cur["运输类型"], "国内运输拼车")
self.assertFalse(cur.get("线路类别"))
def test_full_prompt_when_empty(self):
text = option_prompt(current={})
self.assertIn("请一次选择:运输类型、线路类别、运输分类", text)
self.assertIn("运输类型(必填):可选择回复,", text)
self.assertIn("中港整车", text)
self.assertIn("中港零担/集拼", text)
self.assertIn("线路类别(必填):可选择回复,", text)
self.assertIn("国内长途/零担", text)
self.assertIn("1.普件(东南亚、中港常用线路/常规车型)", text)
self.assertIn("填写样例见图", text)
self.assertNotIn("国内运输整车 + 国内城配/拖车/打包", text)
self.assertNotIn("┌", text)
self.assertNotIn("| 国内运输整车 |", text)
self.assertNotIn("仅供参考", text)
self.assertNotIn("中港装载方式", text)
self.assertNotIn("跟运输类型联动", text)
def test_copy_alias_lists_new_types(self):
from agent.policy import inquiry_copy as copy
text = copy.ASK_LAND_SUBTYPE
self.assertIn("中港整车", text)
self.assertIn("填写样例", text)
self.assertNotIn("请补充陆运类型:国内运输拼车", text)
def test_sample_table_when_route_missing(self):
text = option_prompt(current={"运输类型": "国内运输拼车"})
self.assertIn("线路类别(必填)", text)
self.assertIn("填写样例见图", text)
self.assertTrue(option_prompt_needs_sample(current={"运输类型": "国内运输拼车"}))
self.assertNotIn("国内运输拼车 + 国内长途/零担", text)
self.assertNotIn("请一次选择:运输类型", text)
self.assertNotIn("运输类型(必填)", text)
def test_sample_pairs_go_to_image_not_text(self):
from agent.policy import inquiry_copy as copy
text = option_prompt(current={})
self.assertIn("填写样例见图", text)
self.assertNotIn("国内运输拼车 + 国内长途/零担", text)
extra = copy.land_sample_image_payload()
self.assertEqual(extra.get("msgtype"), "image")
self.assertTrue(os.path.isfile(str(extra.get("filepath") or "")))
self.assertGreater(os.path.getsize(str(extra["filepath"])), 1000)
def test_no_sample_when_only_class_missing(self):
text = option_prompt(
current={
"运输类型": "国内运输拼车",
"线路类别": "东南亚",
}
)
self.assertIn("运输分类(必填)", text)
self.assertNotIn("填写样例", text)
def test_sample_pair_gate(self):
self.assertTrue(sample_pair_allowed("国内运输拼车", "国内长途/零担"))
self.assertTrue(sample_pair_allowed("国内运输整车", "国内城配/拖车/打包"))
self.assertTrue(sample_pair_allowed("国内运输整车", "国内长途/零担"))
self.assertTrue(sample_pair_allowed("中港整车", "中港/中亚/中欧"))
self.assertFalse(sample_pair_allowed("国内运输拼车", "东南亚"))
self.assertFalse(sample_pair_allowed("中港整车", "东南亚"))
def test_confirm_intent(self):
self.assertTrue(looks_like_confirm("确认"))
self.assertTrue(looks_like_confirm("可以查了"))
self.assertTrue(looks_like_confirm("查价"))
self.assertFalse(looks_like_confirm("广州到深圳衣服"))
if __name__ == "__main__":
unittest.main()