- 新增后端完整业务支持:数据库模型、API接口、核心服务与RPA对接逻辑 - 新增前端工作台页面、路由适配与多语言国际化 - 新增Dify自定义工具OpenAPI规范、Agent配置脚本与部署文档 - 新增发货Excel模板生成脚本与默认模板 - 修复测试配置文件中的环境变量命名错误 - 更新本地开发环境变量配置
223 lines
7.0 KiB
Python
223 lines
7.0 KiB
Python
from app import db
|
|
from app.models import ShippingCase
|
|
|
|
|
|
def _tool_headers(app):
|
|
key = app.config.get("SHIPPING_NOTICE_TOOL_API_KEY") or app.config.get("INTEGRATION_API_KEY") or "test-tool-key"
|
|
app.config["SHIPPING_NOTICE_TOOL_API_KEY"] = key
|
|
app.config["INTEGRATION_API_KEY"] = key
|
|
return {
|
|
"Content-Type": "application/json",
|
|
"X-Shipping-Tool-Key": key,
|
|
}
|
|
|
|
|
|
def test_shipping_notice_tool_flow(app, client):
|
|
headers = _tool_headers(app)
|
|
|
|
create_res = client.post(
|
|
"/api/shipping-notice/tools/create_case",
|
|
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"
|
|
case_id = create_data["case_id"]
|
|
|
|
parse_res = client.post(
|
|
"/api/shipping-notice/tools/parse_notice",
|
|
headers=headers,
|
|
json={"case_id": case_id, "raw_text": "客户:ACME 料号A001 x 10"},
|
|
)
|
|
assert parse_res.status_code == 200
|
|
assert parse_res.get_json()["status"] in {"ok", "needs_human"}
|
|
|
|
inv_res = client.post(
|
|
"/api/shipping-notice/tools/ensure_inventory",
|
|
headers=headers,
|
|
json={
|
|
"case_id": case_id,
|
|
"inventory_lines": [{"sku": "A001", "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={"case_id": case_id},
|
|
)
|
|
assert match_res.status_code == 200
|
|
assert match_res.get_json()["status"] == "ok"
|
|
|
|
excel_res = client.post(
|
|
"/api/shipping-notice/tools/generate_shipment_excel",
|
|
headers=headers,
|
|
json={"case_id": case_id},
|
|
)
|
|
assert excel_res.status_code == 200
|
|
assert excel_res.get_json()["status"] == "ok"
|
|
|
|
with app.app_context():
|
|
case = ShippingCase.query.filter_by(case_no=case_id).first()
|
|
assert case is not None
|
|
assert case.export_file_path
|
|
|
|
status_res = client.post(
|
|
"/api/shipping-notice/tools/get_case_status",
|
|
headers=headers,
|
|
json={"case_id": case_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_case",
|
|
headers=headers,
|
|
json={"source_type": "manual"},
|
|
)
|
|
case_id = create_res.get_json()["case_id"]
|
|
|
|
client.post(
|
|
"/api/shipping-notice/tools/parse_notice",
|
|
headers=headers,
|
|
json={
|
|
"case_id": case_id,
|
|
"structured": {
|
|
"customer_name": "ACME",
|
|
"lines": [{"sku": "A001", "qty": 100, "uom": "EA"}],
|
|
"confidence": 0.95,
|
|
},
|
|
},
|
|
)
|
|
client.post(
|
|
"/api/shipping-notice/tools/ensure_inventory",
|
|
headers=headers,
|
|
json={"case_id": case_id, "inventory_lines": [{"sku": "A001", "available_qty": 5}]},
|
|
)
|
|
match_res = client.post(
|
|
"/api/shipping-notice/tools/match_inventory",
|
|
headers=headers,
|
|
json={"case_id": case_id},
|
|
)
|
|
data = match_res.get_json()
|
|
assert data["status"] == "needs_human"
|
|
assert "confirm" in (data.get("human_options") or [])
|
|
|
|
confirm_res = client.post(
|
|
"/api/shipping-notice/tools/submit_decision",
|
|
headers=headers,
|
|
json={"case_id": case_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_case" in str(spec)
|
|
assert spec["servers"][0]["url"] == "http://tool-api.example.com/api/shipping-notice"
|
|
props = spec["paths"]["/tools/create_case"]["post"]["requestBody"]["content"]["application/json"]["schema"]["properties"]
|
|
assert "raw_content" in props
|
|
assert "ShippingToolKey" in spec["components"]["securitySchemes"]
|
|
assert "/rpa/" not in str(spec["paths"])
|
|
|
|
|
|
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_case",
|
|
headers=headers,
|
|
json={"source_type": "manual"},
|
|
)
|
|
case_id = create_res.get_json()["case_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={"case_no": case_id, "success": True, "file_path": str(inv_file)},
|
|
)
|
|
assert cb_res.status_code == 200
|
|
data = cb_res.get_json()
|
|
assert data["status"] == "ok"
|
|
assert data["data"]["process_status"] == "inventory_ready"
|
|
|
|
|
|
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_case",
|
|
headers=headers,
|
|
json={"source_type": "manual"},
|
|
)
|
|
case_id = create_res.get_json()["case_id"]
|
|
|
|
client.post(
|
|
"/api/shipping-notice/tools/parse_notice",
|
|
headers=headers,
|
|
json={
|
|
"case_id": case_id,
|
|
"structured": {
|
|
"customer_name": "ACME",
|
|
"lines": [{"sku": "A001", "qty": 5, "uom": "EA"}],
|
|
"confidence": 0.95,
|
|
},
|
|
},
|
|
)
|
|
client.post(
|
|
"/api/shipping-notice/tools/ensure_inventory",
|
|
headers=headers,
|
|
json={"case_id": case_id, "inventory_lines": [{"sku": "A001", "available_qty": 10}]},
|
|
)
|
|
client.post(
|
|
"/api/shipping-notice/tools/match_inventory",
|
|
headers=headers,
|
|
json={"case_id": case_id},
|
|
)
|
|
client.post(
|
|
"/api/shipping-notice/tools/generate_shipment_excel",
|
|
headers=headers,
|
|
json={"case_id": case_id},
|
|
)
|
|
|
|
cb_res = client.post(
|
|
"/api/shipping-notice/rpa/upload-complete",
|
|
headers=headers,
|
|
json={"case_no": case_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"
|