主要变更: 1. 移除废弃的发货通知字段抽取独立Workflow Agent相关代码 2. 重构Dify服务调用,新增inputs参数支持注入环境变量配置的工具API地址与密钥 3. 新增auto_progress工具API与checkpoint catalog接口,支持流程自动推进 4. 统一字段命名,将column_mapping_json改为map_json并兼容旧有调用 5. 重构库存匹配、解析结果的展示格式,改为标准化多语言Markdown表格 6. 新增ODM ID库存字段支持,优化库存解析与映射逻辑 7. 新增LibreOffice转换隔离Profile配置,避免多实例运行冲突 8. 新增完整环境变量配置、数据库迁移脚本与测试用例 9. 适配前端代码,更新字段命名与聊天跳转逻辑 10. 更新OpenAPI规范适配Dify工具调用,删除已归档的旧部署文档
886 lines
28 KiB
Python
886 lines
28 KiB
Python
from app import db
|
||
from app.models import ShippingInventoryMapping, ShippingTask
|
||
|
||
|
||
def _tool_headers(app):
|
||
return {
|
||
"Content-Type": "application/json",
|
||
}
|
||
|
||
|
||
def test_shipping_notice_tool_flow(app, client):
|
||
headers = _tool_headers(app)
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual", "raw_content": "客户:ACME 料号A001 x 10"},
|
||
)
|
||
assert create_res.status_code == 200
|
||
create_data = create_res.get_json()
|
||
assert create_data["status"] == "ok"
|
||
task_id = create_data["task_id"]
|
||
|
||
parse_res = client.post(
|
||
"/api/shipping-notice/tools/parse_notice",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"structured": {
|
||
"customer_name": "ACME",
|
||
"lines": [{"sku": "A001", "reservation_id": "R-1", "qty": 10, "uom": "EA"}],
|
||
"confidence": 0.95,
|
||
},
|
||
},
|
||
)
|
||
assert parse_res.status_code == 200
|
||
parse_body = parse_res.get_json()
|
||
assert parse_body["status"] in {"ok", "needs_human"}
|
||
assert "【发货通知解析确认】" in parse_body["message"]
|
||
assert "| 料号 | A001 |" in parse_body["message"]
|
||
|
||
inv_res = client.post(
|
||
"/api/shipping-notice/tools/ensure_inventory",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"inventory_lines": [{"sku": "A001", "reservation_id": "R-1", "available_qty": 10}],
|
||
},
|
||
)
|
||
assert inv_res.status_code == 200
|
||
assert inv_res.get_json()["status"] == "ok"
|
||
|
||
match_res = client.post(
|
||
"/api/shipping-notice/tools/match_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
assert match_res.status_code == 200
|
||
match_data = match_res.get_json()
|
||
assert match_data["status"] == "ok"
|
||
assert "订单" in match_data["message"]
|
||
assert "库存" in match_data["message"]
|
||
assert "A001" in match_data["message"]
|
||
|
||
excel_res = client.post(
|
||
"/api/shipping-notice/tools/generate_shipment_excel",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
assert excel_res.status_code == 200
|
||
assert excel_res.get_json()["status"] == "ok"
|
||
|
||
with app.app_context():
|
||
task = ShippingTask.query.filter_by(task_no=task_id).first()
|
||
assert task is not None
|
||
assert task.export_file_path
|
||
|
||
status_res = client.post(
|
||
"/api/shipping-notice/tools/get_task_status",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
assert status_res.status_code == 200
|
||
assert status_res.get_json()["status"] == "ok"
|
||
|
||
|
||
def test_shipping_notice_match_shortage_needs_human(app, client):
|
||
headers = _tool_headers(app)
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_notice",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"structured": {
|
||
"customer_name": "ACME",
|
||
"lines": [{"sku": "A001", "reservation_id": "R-1", "qty": 100, "uom": "EA"}],
|
||
"confidence": 0.95,
|
||
},
|
||
},
|
||
)
|
||
client.post(
|
||
"/api/shipping-notice/tools/ensure_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id, "inventory_lines": [{"sku": "A001", "reservation_id": "R-1", "available_qty": 5}]},
|
||
)
|
||
match_res = client.post(
|
||
"/api/shipping-notice/tools/match_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
data = match_res.get_json()
|
||
assert data["status"] == "needs_human"
|
||
assert "订单" in data["message"]
|
||
assert "库存" in data["message"]
|
||
assert "缺口" in data["message"]
|
||
assert "confirm" in (data.get("human_options") or [])
|
||
|
||
confirm_res = client.post(
|
||
"/api/shipping-notice/tools/submit_decision",
|
||
headers=headers,
|
||
json={"task_id": task_id, "decision": "confirm"},
|
||
)
|
||
assert confirm_res.get_json()["status"] == "ok"
|
||
|
||
|
||
def test_openapi_spec_public(app, client):
|
||
app.config["SHIPPING_NOTICE_TOOL_API_BASE"] = "http://tool-api.example.com"
|
||
res = client.get("/api/shipping-notice/tools/openapi.json")
|
||
assert res.status_code == 200
|
||
spec = res.get_json()
|
||
assert "create_task" in str(spec)
|
||
assert spec["servers"][0]["url"] == "http://tool-api.example.com/api/shipping-notice"
|
||
props = spec["paths"]["/tools/create_task"]["post"]["requestBody"]["content"]["application/json"]["schema"]["properties"]
|
||
assert "raw_content" in props
|
||
assert "/rpa/" not in str(spec["paths"])
|
||
assert "/tools/parse_inventory" in spec["paths"]
|
||
assert "/tools/get_inventory_schema_preview" in spec["paths"]
|
||
assert "/tools/apply_inventory_mapping" in spec["paths"]
|
||
|
||
|
||
def test_parse_inventory_from_chat_text(app, client):
|
||
headers = _tool_headers(app)
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_notice",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"structured": {
|
||
"customer_name": "ACME",
|
||
"lines": [{"sku": "A001", "reservation_id": "R-1", "qty": 10, "uom": "EA"}],
|
||
"confidence": 0.95,
|
||
},
|
||
},
|
||
)
|
||
|
||
inv_text = "料号\t可用数量\nA001\t10\nB002\t5"
|
||
parse_inv = client.post(
|
||
"/api/shipping-notice/tools/parse_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id, "raw_inventory_text": inv_text},
|
||
)
|
||
assert parse_inv.status_code == 200
|
||
data = parse_inv.get_json()
|
||
assert data["status"] == "needs_human"
|
||
assert data.get("checkpoint") == "inventory_field_mapping_review"
|
||
assert data.get("stop_agent_turn") is True
|
||
assert "match_inventory" in (data.get("forbidden_next_tools") or [])
|
||
assert "field_alignment" in data["data"]
|
||
|
||
confirm_res = client.post(
|
||
"/api/shipping-notice/tools/submit_decision",
|
||
headers=headers,
|
||
json={"task_id": task_id, "decision": "confirm_inventory_mapping"},
|
||
)
|
||
confirm_data = confirm_res.get_json()
|
||
assert confirm_data["status"] == "needs_human"
|
||
assert confirm_data.get("checkpoint") == "inventory_field_mapping_review"
|
||
assert "Reservation ID" in (confirm_data.get("message") or "")
|
||
|
||
match_res = client.post(
|
||
"/api/shipping-notice/tools/match_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
assert match_res.get_json()["status"] in {"failed", "needs_human"}
|
||
|
||
|
||
def test_parse_inventory_empty_needs_human(app, client):
|
||
headers = _tool_headers(app)
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
res = client.post(
|
||
"/api/shipping-notice/tools/parse_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id, "raw_inventory_text": " "},
|
||
)
|
||
assert res.get_json()["status"] == "needs_human"
|
||
assert "reparse_inventory" in (res.get_json().get("human_options") or [])
|
||
|
||
|
||
def test_match_by_aws_sku_and_reservation_id(app, client):
|
||
headers = _tool_headers(app)
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_notice",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"structured": {
|
||
"customer_name": "AWS",
|
||
"lines": [
|
||
{"sku": "10-40594-001", "reservation_id": "AWS-311", "qty": 2, "uom": "EA"},
|
||
{"sku": "10-40594-001", "reservation_id": "ORD-3002", "qty": 1, "uom": "EA"},
|
||
],
|
||
"confidence": 0.95,
|
||
},
|
||
},
|
||
)
|
||
|
||
inv_text = (
|
||
"AWS Sku\tCOM ID\tQty Available\n"
|
||
"10-40594-001\tAWS-311\t2\n"
|
||
"10-40594-001\tORD-3002\t0\n"
|
||
)
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id, "raw_inventory_text": inv_text},
|
||
)
|
||
client.post(
|
||
"/api/shipping-notice/tools/submit_decision",
|
||
headers=headers,
|
||
json={"task_id": task_id, "decision": "confirm_inventory_mapping"},
|
||
)
|
||
|
||
match_res = client.post(
|
||
"/api/shipping-notice/tools/match_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
data = match_res.get_json()
|
||
assert data["status"] == "needs_human"
|
||
results = {r["reservation_id"]: r for r in data["data"]["line_results"]}
|
||
assert results["AWS-311"]["line_status"] == "ok"
|
||
assert results["ORD-3002"]["line_status"] == "shortage"
|
||
|
||
|
||
def test_match_inventory_blocked_before_mapping_confirm(app, client):
|
||
headers = _tool_headers(app)
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_notice",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"structured": {
|
||
"customer_name": "AWS",
|
||
"lines": [{"sku": "10-40594-001", "reservation_id": "AWS-311", "qty": 1, "uom": "EA"}],
|
||
"confidence": 0.95,
|
||
},
|
||
},
|
||
)
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_inventory",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"raw_inventory_text": "AWS Sku\tCOM ID\tQty Available\n10-40594-001\tAWS-311\t1\n",
|
||
},
|
||
)
|
||
|
||
match_res = client.post(
|
||
"/api/shipping-notice/tools/match_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
blocked = match_res.get_json()
|
||
assert blocked["status"] == "needs_human"
|
||
assert blocked.get("checkpoint") == "inventory_field_mapping_review"
|
||
assert blocked.get("stop_agent_turn") is True
|
||
assert "field_alignment" in blocked["data"]
|
||
mapping = blocked["data"]["field_alignment"]
|
||
assert mapping["inventory_columns"]["mapping"]["sku"]["source_column"] == "AWS Sku"
|
||
assert mapping["inventory_columns"]["mapping"]["reservation_id"]["source_column"] == "COM ID"
|
||
|
||
|
||
def test_match_inventory_requires_reservation_id(app, client):
|
||
headers = _tool_headers(app)
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_notice",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"structured": {
|
||
"customer_name": "ACME",
|
||
"lines": [{"sku": "A001", "qty": 1, "uom": "EA"}],
|
||
"confidence": 0.95,
|
||
},
|
||
},
|
||
)
|
||
client.post(
|
||
"/api/shipping-notice/tools/ensure_inventory",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"inventory_lines": [{"sku": "A001", "reservation_id": "R-1", "available_qty": 10}],
|
||
},
|
||
)
|
||
match_res = client.post(
|
||
"/api/shipping-notice/tools/match_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
data = match_res.get_json()
|
||
assert data["status"] == "needs_human"
|
||
assert data.get("checkpoint") == "reservation_id_required"
|
||
|
||
|
||
def test_correct_inventory_mapping_resets_to_inference(app, client, tmp_path):
|
||
headers = _tool_headers(app)
|
||
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path / "shipping")
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_notice",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"structured": {
|
||
"customer_name": "AWS",
|
||
"lines": [{"sku": "10-40594-001", "reservation_id": "AWS-311", "qty": 1, "uom": "EA"}],
|
||
"confidence": 0.95,
|
||
},
|
||
},
|
||
)
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_inventory",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"raw_inventory_text": "AWS Sku\tCOM ID\tQty Available\n10-40594-001\tAWS-311\t1\n",
|
||
},
|
||
)
|
||
|
||
correct_res = client.post(
|
||
"/api/shipping-notice/tools/submit_decision",
|
||
headers=headers,
|
||
json={"task_id": task_id, "decision": "correct_inventory_mapping"},
|
||
)
|
||
data = correct_res.get_json()
|
||
assert data["status"] == "ok"
|
||
assert data.get("checkpoint") == "inventory_mapping_inference"
|
||
assert "schema_preview" in data["data"]
|
||
assert data["data"]["schema_preview"]["headers"] == ["AWS Sku", "COM ID", "Qty Available"]
|
||
|
||
|
||
def test_rpa_inventory_callback(app, client, tmp_path, monkeypatch):
|
||
headers = _tool_headers(app)
|
||
drop = tmp_path / "drop"
|
||
trigger = tmp_path / "triggers"
|
||
drop.mkdir()
|
||
trigger.mkdir()
|
||
app.config["FREIGHT_INVENTORY_DROP_DIR"] = str(drop)
|
||
app.config["FREIGHT_RPA_INVENTORY_TRIGGER_DIR"] = str(trigger)
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
from openpyxl import Workbook
|
||
|
||
inv_file = drop / "stock.xlsx"
|
||
wb = Workbook()
|
||
ws = wb.active
|
||
ws.append(["料号", "可用数量"])
|
||
ws.append(["A001", 20])
|
||
wb.save(inv_file)
|
||
wb.close()
|
||
|
||
cb_res = client.post(
|
||
"/api/shipping-notice/rpa/inventory-ready",
|
||
headers=headers,
|
||
json={"task_no": task_id, "success": True, "file_path": str(inv_file)},
|
||
)
|
||
assert cb_res.status_code == 200
|
||
data = cb_res.get_json()
|
||
assert data["status"] == "needs_human"
|
||
assert data["data"]["process_status"] == "inventory_ready"
|
||
assert "confirm_inventory_mapping" in (data.get("human_options") or [])
|
||
|
||
|
||
def test_rpa_upload_callback(app, client, tmp_path):
|
||
headers = _tool_headers(app)
|
||
queue = tmp_path / "queue"
|
||
trigger = tmp_path / "upload_triggers"
|
||
queue.mkdir()
|
||
trigger.mkdir()
|
||
app.config["FREIGHT_UPLOAD_QUEUE_DIR"] = str(queue)
|
||
app.config["FREIGHT_RPA_UPLOAD_TRIGGER_DIR"] = str(trigger)
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_notice",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"structured": {
|
||
"customer_name": "ACME",
|
||
"lines": [{"sku": "A001", "reservation_id": "R-1", "qty": 5, "uom": "EA"}],
|
||
"confidence": 0.95,
|
||
},
|
||
},
|
||
)
|
||
client.post(
|
||
"/api/shipping-notice/tools/ensure_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id, "inventory_lines": [{"sku": "A001", "reservation_id": "R-1", "available_qty": 10}]},
|
||
)
|
||
client.post(
|
||
"/api/shipping-notice/tools/match_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
client.post(
|
||
"/api/shipping-notice/tools/generate_shipment_excel",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
|
||
cb_res = client.post(
|
||
"/api/shipping-notice/rpa/upload-complete",
|
||
headers=headers,
|
||
json={"task_no": task_id, "success": True, "freight_ref_no": "FR-001"},
|
||
)
|
||
assert cb_res.status_code == 200
|
||
assert cb_res.get_json()["status"] == "ok"
|
||
assert cb_res.get_json()["data"]["freight_ref_no"] == "FR-001"
|
||
|
||
|
||
def test_generate_shipment_excel_writes_vendor_part_and_odm_id(app, client):
|
||
headers = _tool_headers(app)
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_notice",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"structured": {
|
||
"customer_name": "Wistron",
|
||
"po_number": "SN-20260608-7FCBFA4B",
|
||
"lines": [
|
||
{
|
||
"sku": "504-010213-001",
|
||
"vendor_part_number": "504-010213-001",
|
||
"reservation_id": "1000140351",
|
||
"qty": 9,
|
||
"uom": "EA",
|
||
}
|
||
],
|
||
"confidence": 0.95,
|
||
},
|
||
},
|
||
)
|
||
client.post(
|
||
"/api/shipping-notice/tools/ensure_inventory",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"inventory_lines": [
|
||
{
|
||
"sku": "504-010213-001",
|
||
"reservation_id": "1000140351",
|
||
"available_qty": 9,
|
||
"odm_id": "16754796",
|
||
}
|
||
],
|
||
},
|
||
)
|
||
client.post(
|
||
"/api/shipping-notice/tools/match_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
|
||
excel_res = client.post(
|
||
"/api/shipping-notice/tools/generate_shipment_excel",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
assert excel_res.status_code == 200
|
||
assert excel_res.get_json()["status"] == "ok"
|
||
|
||
from openpyxl import load_workbook
|
||
|
||
with app.app_context():
|
||
task = ShippingTask.query.filter_by(task_no=task_id).first()
|
||
assert task is not None
|
||
assert task.export_file_path
|
||
wb = load_workbook(task.export_file_path, read_only=True)
|
||
header_ws = wb["PVMI Pull Request Header"]
|
||
detail_ws = wb["PVMI Pull Request Detail"]
|
||
assert header_ws.cell(4, 6).value == "16754796"
|
||
assert detail_ws.cell(4, 4).value == "504-010213-001"
|
||
wb.close()
|
||
|
||
|
||
def test_upload_inventory_file_parses_server_side(app, client, tmp_path):
|
||
headers = _tool_headers(app)
|
||
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path / "shipping")
|
||
app.config["SHIPPING_NOTICE_INVENTORY_TEXT_MAX_CHARS"] = 1000
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_notice",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"structured": {
|
||
"customer_name": "AWS",
|
||
"lines": [{"sku": "10-40594-001", "reservation_id": "AWS-311", "qty": 1, "uom": "EA"}],
|
||
"confidence": 0.95,
|
||
},
|
||
},
|
||
)
|
||
|
||
from io import BytesIO
|
||
|
||
from openpyxl import Workbook
|
||
|
||
buf = BytesIO()
|
||
wb = Workbook()
|
||
ws = wb.active
|
||
ws.append(["AWS Sku", "COM ID", "Qty Available"])
|
||
ws.append(["10-40594-001", "AWS-311", 5])
|
||
wb.save(buf)
|
||
wb.close()
|
||
buf.seek(0)
|
||
|
||
upload_res = client.post(
|
||
"/api/shipping-notice/tools/upload_inventory_file",
|
||
headers={k: v for k, v in headers.items() if k.lower() != "content-type"},
|
||
data={
|
||
"task_id": task_id,
|
||
"file": (buf, "inventory.xlsx"),
|
||
},
|
||
)
|
||
assert upload_res.status_code == 200
|
||
upload_data = upload_res.get_json()
|
||
assert upload_data["status"] == "ok"
|
||
assert upload_data.get("checkpoint") == "inventory_mapping_inference"
|
||
preview = upload_data["data"]["schema_preview"]
|
||
assert preview["headers"] == ["AWS Sku", "COM ID", "Qty Available"]
|
||
assert preview["suggested_mapping"]["sku"] == "AWS Sku"
|
||
|
||
apply_res = client.post(
|
||
"/api/shipping-notice/tools/apply_inventory_mapping",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"column_mapping_json": {
|
||
**preview["suggested_mapping"],
|
||
"match_rule": "LLM:Reservation Order 对齐订单 reservation_id,汇总 Qty Available。",
|
||
},
|
||
},
|
||
)
|
||
apply_data = apply_res.get_json()
|
||
assert apply_data["status"] == "needs_human"
|
||
assert apply_data.get("checkpoint") == "inventory_field_mapping_review"
|
||
assert apply_data["data"]["field_alignment"]["inventory_row_count"] == 1
|
||
assert apply_data["data"]["field_alignment"]["match_rule"].startswith("LLM:")
|
||
assert apply_data["data"]["field_alignment"].get("match_rule_source") == "llm"
|
||
with app.app_context():
|
||
assert ShippingInventoryMapping.query.filter_by(customer_code="GLOBAL").count() >= 1
|
||
|
||
|
||
def test_apply_inventory_mapping_fallback_match_rule(app, client, tmp_path):
|
||
headers = _tool_headers(app)
|
||
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path / "shipping")
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
from io import BytesIO
|
||
|
||
from openpyxl import Workbook
|
||
|
||
wb = Workbook()
|
||
ws = wb.active
|
||
ws.append(["AWS Sku", "COM ID", "Qty Available"])
|
||
ws.append(["504-010213-001", "1000140351", 5])
|
||
buf = BytesIO()
|
||
wb.save(buf)
|
||
buf.seek(0)
|
||
|
||
client.post(
|
||
"/api/shipping-notice/tools/upload_inventory_file",
|
||
headers={k: v for k, v in headers.items() if k.lower() != "content-type"},
|
||
data={"task_id": task_id, "file": (buf, "inventory.xlsx")},
|
||
)
|
||
|
||
apply_res = client.post(
|
||
"/api/shipping-notice/tools/apply_inventory_mapping",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"column_mapping_json": {
|
||
"sku": "AWS Sku",
|
||
"reservation_id": "COM ID",
|
||
"available_qty": "Qty Available",
|
||
},
|
||
},
|
||
)
|
||
apply_data = apply_res.get_json()
|
||
match_rule = apply_data["data"]["field_alignment"]["match_rule"]
|
||
assert "AWS Sku" in match_rule
|
||
assert "COM ID" in match_rule
|
||
assert apply_data["data"]["field_alignment"].get("match_rule_source") == "fallback"
|
||
|
||
|
||
def test_get_inventory_schema_preview(app, client, tmp_path):
|
||
headers = _tool_headers(app)
|
||
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path / "shipping")
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
missing = client.post(
|
||
"/api/shipping-notice/tools/get_inventory_schema_preview",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
assert missing.get_json()["checkpoint"] == "inventory_upload_required"
|
||
|
||
|
||
def test_apply_inventory_mapping_custom_headers(app, client, tmp_path):
|
||
headers = _tool_headers(app)
|
||
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path / "shipping")
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
from io import BytesIO
|
||
|
||
from openpyxl import Workbook
|
||
|
||
buf = BytesIO()
|
||
wb = Workbook()
|
||
ws = wb.active
|
||
ws.append(["Part No", "Resv", "On Hand"])
|
||
ws.append(["SKU-99", "R-1", 12])
|
||
wb.save(buf)
|
||
wb.close()
|
||
buf.seek(0)
|
||
|
||
client.post(
|
||
"/api/shipping-notice/tools/upload_inventory_file",
|
||
headers={k: v for k, v in headers.items() if k.lower() != "content-type"},
|
||
data={"task_id": task_id, "file": (buf, "inv.xlsx")},
|
||
)
|
||
|
||
apply_res = client.post(
|
||
"/api/shipping-notice/tools/apply_inventory_mapping",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"column_mapping_json": {
|
||
"sku": "Part No",
|
||
"reservation_id": "Resv",
|
||
"available_qty": "On Hand",
|
||
},
|
||
},
|
||
)
|
||
apply_data = apply_res.get_json()
|
||
assert apply_data["status"] == "needs_human"
|
||
assert apply_data["data"]["inventory_row_count"] == 1
|
||
sample = apply_data["data"]["field_alignment"]["sample_inventory_lines"][0]
|
||
assert sample["sku"] == "SKU-99"
|
||
assert sample["available_qty"] == 12
|
||
|
||
|
||
def test_confirm_parse_does_not_skip_inventory_match(app, client):
|
||
headers = _tool_headers(app)
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_notice",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"structured": {
|
||
"customer_name": "Wistron",
|
||
"lines": [{"sku": "504-010213-001", "qty": 9, "uom": "EA"}],
|
||
"confidence": 0.95,
|
||
},
|
||
},
|
||
)
|
||
|
||
confirm_res = client.post(
|
||
"/api/shipping-notice/tools/submit_decision",
|
||
headers=headers,
|
||
json={"task_id": task_id, "decision": "confirm"},
|
||
)
|
||
confirm_data = confirm_res.get_json()
|
||
assert confirm_data["status"] in {"ok", "running", "needs_human"}
|
||
|
||
excel_res = client.post(
|
||
"/api/shipping-notice/tools/generate_shipment_excel",
|
||
headers=headers,
|
||
json={"task_id": task_id},
|
||
)
|
||
excel_data = excel_res.get_json()
|
||
assert excel_data["status"] == "failed"
|
||
assert "匹配" in excel_data["message"]
|
||
|
||
with app.app_context():
|
||
task = ShippingTask.query.filter_by(task_no=task_id).first()
|
||
assert task is not None
|
||
assert task.process_status in {"parsed", "waiting_inventory"}
|
||
assert task.match_status is None
|
||
|
||
|
||
def test_submit_decision_by_conversation_id(app, client):
|
||
headers = _tool_headers(app)
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual", "conversation_id": "conv-test-001"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
client.post(
|
||
"/api/shipping-notice/tools/parse_notice",
|
||
headers=headers,
|
||
json={
|
||
"task_id": task_id,
|
||
"conversation_id": "conv-test-001",
|
||
"structured": {
|
||
"customer_name": "Wistron",
|
||
"lines": [{"sku": "504-010213-001", "qty": 9, "uom": "EA"}],
|
||
"confidence": 0.95,
|
||
},
|
||
},
|
||
)
|
||
|
||
confirm_res = client.post(
|
||
"/api/shipping-notice/tools/submit_decision",
|
||
headers=headers,
|
||
json={"conversation_id": "conv-test-001", "decision": "confirm"},
|
||
)
|
||
confirm_data = confirm_res.get_json()
|
||
assert confirm_data["status"] in {"ok", "running", "needs_human"}
|
||
assert confirm_data["task_id"] == task_id
|
||
|
||
dup_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual", "conversation_id": "conv-test-001"},
|
||
)
|
||
dup_data = dup_res.get_json()
|
||
assert dup_data["task_id"] == task_id
|
||
assert dup_data.get("checkpoint") == "task_already_exists"
|
||
|
||
|
||
def test_parse_inventory_rejects_oversized_text(app, client):
|
||
headers = _tool_headers(app)
|
||
app.config["SHIPPING_NOTICE_INVENTORY_TEXT_MAX_CHARS"] = 100
|
||
|
||
create_res = client.post(
|
||
"/api/shipping-notice/tools/create_task",
|
||
headers=headers,
|
||
json={"source_type": "manual"},
|
||
)
|
||
task_id = create_res.get_json()["task_id"]
|
||
|
||
res = client.post(
|
||
"/api/shipping-notice/tools/parse_inventory",
|
||
headers=headers,
|
||
json={"task_id": task_id, "raw_inventory_text": "x" * 200},
|
||
)
|
||
data = res.get_json()
|
||
assert data["status"] == "needs_human"
|
||
assert data.get("checkpoint") == "inventory_upload_required"
|
||
assert "过大" in data["message"]
|
||
|
||
|
||
def test_chat_actions_empty_when_task_rejected(app):
|
||
from app.services.shipping_notice.chat_actions import build_chat_actions_payload, human_options_for_task
|
||
|
||
with app.app_context():
|
||
task = ShippingTask(
|
||
task_no="SN-20260101-TEST0001",
|
||
source_type="manual",
|
||
process_status="rejected",
|
||
review_status="rejected",
|
||
)
|
||
assert human_options_for_task(task) == []
|
||
payload = build_chat_actions_payload(task)
|
||
assert payload["checkpoint"] == "rejected"
|
||
assert payload["action_buttons"] == []
|