feat(shipping-notice): 完成货代发货通知全门禁恢复MVP收口
完成全门禁恢复MVP版本交付:调整自动上传与重试配置,重构正式执行链路;修复多处代码bug,新增测试与审计工具;更新全流程文档与契约规范。
This commit is contained in:
1 parent
8e0bbf7a81
commit
c94d173f92
181 files changed
+12947
-1724
No files matched your search
@@ -9,6 +9,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
@@ -42,6 +43,16 @@ def _build_inventory_export_trigger(task_no: str) -> dict[str, object]:
|
||||
"requested_at": "2026-07-01T03:23:25.114522",
|
||||
"mode": "auto",
|
||||
"attempt": 3,
|
||||
"schema_version": "shipping_inventory_rpa.v2",
|
||||
"callback_id": f"SRC-{task_no}:C1",
|
||||
"source_code": "DSV360_AWSDU",
|
||||
"source_run_no": f"SRC-{task_no}",
|
||||
"attempt_no": 3,
|
||||
"lease_token": "lease-test",
|
||||
"candidate_set_id": 12,
|
||||
"execution_id": 88,
|
||||
"gate_id": 901,
|
||||
"expected_warehouse_id": "AWSDU",
|
||||
"rpa_inventory_source": {
|
||||
"source_code": "DSV360_AWSDU",
|
||||
"connector_type": "dsv360",
|
||||
@@ -59,6 +70,121 @@ def _build_inventory_export_trigger(task_no: str) -> dict[str, object]:
|
||||
}
|
||||
|
||||
|
||||
def test_single_inventory_trigger_missing_source_run_is_rejected_before_rpa(monkeypatch, tmp_path):
|
||||
"""缺少 source_run_no 时不生成 None:C1、不启动浏览器,并移入 failed。"""
|
||||
worker = _load_freight_rpa_worker()
|
||||
trigger_dir = tmp_path / "triggers"
|
||||
drop_dir = tmp_path / "drop"
|
||||
processed_dir = trigger_dir / "processed"
|
||||
failed_dir = trigger_dir / "failed"
|
||||
trigger_dir.mkdir()
|
||||
drop_dir.mkdir()
|
||||
trigger_path = trigger_dir / "inventory_export_invalid.json"
|
||||
payload = _build_inventory_export_trigger("SN-IDENTITY-INVALID")
|
||||
payload.pop("source_run_no")
|
||||
trigger_path.write_text(json.dumps(payload), encoding="utf-8")
|
||||
callbacks: list[tuple[str, dict]] = []
|
||||
monkeypatch.setattr(worker, "_post_callback", lambda path, body: callbacks.append((path, body)))
|
||||
monkeypatch.setattr(worker, "_download_inventory_with_browser", lambda *_args, **_kwargs: pytest.fail("RPA must not start"))
|
||||
|
||||
worker._handle_inventory_trigger(trigger_path, drop_dir, processed_dir)
|
||||
|
||||
assert callbacks == [
|
||||
(
|
||||
"/api/shipping-notice/rpa/inventory-ready",
|
||||
{
|
||||
"schema_version": "shipping_inventory_rpa.v2",
|
||||
"callback_id": None,
|
||||
"task_no": "SN-IDENTITY-INVALID",
|
||||
"business_status": "rejected",
|
||||
"status": "rejected",
|
||||
"error_code": "trigger_identity_invalid",
|
||||
"identity_errors": ["source_run_no"],
|
||||
},
|
||||
),
|
||||
]
|
||||
assert (failed_dir / trigger_path.name).is_file()
|
||||
|
||||
|
||||
def test_single_inventory_trigger_missing_callback_id_is_rejected(monkeypatch, tmp_path):
|
||||
"""v2 不允许 Worker 推导 callback_id,缺失时必须拒绝。"""
|
||||
worker = _load_freight_rpa_worker()
|
||||
trigger_dir = tmp_path / "triggers"
|
||||
drop_dir = tmp_path / "drop"
|
||||
processed_dir = trigger_dir / "processed"
|
||||
trigger_dir.mkdir()
|
||||
drop_dir.mkdir()
|
||||
trigger_path = trigger_dir / "inventory_export_missing_callback.json"
|
||||
payload = _build_inventory_export_trigger("SN-IDENTITY-CALLBACK-INVALID")
|
||||
payload.pop("callback_id", None)
|
||||
trigger_path.write_text(json.dumps(payload), encoding="utf-8")
|
||||
callbacks: list[tuple[str, dict]] = []
|
||||
monkeypatch.setattr(worker, "_post_callback", lambda path, body: callbacks.append((path, body)))
|
||||
monkeypatch.setattr(worker, "_download_inventory_with_browser", lambda *_args, **_kwargs: pytest.fail("RPA must not start"))
|
||||
|
||||
worker._handle_inventory_trigger(trigger_path, drop_dir, processed_dir)
|
||||
|
||||
assert callbacks[0][1]["callback_id"] is None
|
||||
assert callbacks[0][1]["error_code"] == "trigger_identity_invalid"
|
||||
assert "callback_id" in callbacks[0][1]["identity_errors"]
|
||||
assert (trigger_dir / "failed" / trigger_path.name).is_file()
|
||||
|
||||
|
||||
def test_batch_inventory_callback_preserves_each_source_v2_identity(monkeypatch, tmp_path):
|
||||
"""批量成功结果必须原样回传逐源 v2 身份,供后端绑定 SourceAttempt。"""
|
||||
worker = _load_freight_rpa_worker()
|
||||
trigger_dir = tmp_path / "triggers"
|
||||
drop_dir = tmp_path / "drop"
|
||||
processed_dir = trigger_dir / "processed"
|
||||
trigger_dir.mkdir()
|
||||
drop_dir.mkdir()
|
||||
trigger_path = trigger_dir / "inventory_export_batch_identity.json"
|
||||
source = {
|
||||
"source_code": "DBS",
|
||||
"warehouse_id": "WH-01",
|
||||
"task_no": "SN-BATCH-IDENTITY",
|
||||
"callback_id": "SRC-BATCH-001:C1",
|
||||
"source_run_no": "SRC-BATCH-001",
|
||||
"attempt_no": 2,
|
||||
"lease_token": "lease-batch",
|
||||
"candidate_set_id": 12,
|
||||
"execution_id": 88,
|
||||
"gate_id": 901,
|
||||
"expected_warehouse_id": "WH-01",
|
||||
}
|
||||
trigger_path.write_text(json.dumps({
|
||||
"action": "inventory_export_batch",
|
||||
"schema_version": "shipping_inventory_rpa.v2",
|
||||
"callback_id": "BATCH-001:batch:C1",
|
||||
"batch_run_no": "BATCH-001",
|
||||
"task_no": "SN-BATCH-IDENTITY",
|
||||
"inventory_sources": [source],
|
||||
}), encoding="utf-8")
|
||||
callbacks: list[tuple[str, dict]] = []
|
||||
monkeypatch.setenv("SHIPPING_NOTICE_RPA_WORKER_MOCK", "true")
|
||||
monkeypatch.setattr(worker, "_post_callback", lambda path, body: callbacks.append((path, body)))
|
||||
|
||||
worker._handle_inventory_batch_trigger(trigger_path, drop_dir, processed_dir)
|
||||
|
||||
completed = next(body for path, body in callbacks if path.endswith("/inventory-batch-ready"))
|
||||
result = completed["results"][0]
|
||||
assert {key: result[key] for key in (
|
||||
"callback_id", "task_no", "source_code", "source_run_no", "attempt_no",
|
||||
"lease_token", "candidate_set_id", "execution_id", "gate_id", "expected_warehouse_id",
|
||||
)} == {
|
||||
"callback_id": "SRC-BATCH-001:C1",
|
||||
"task_no": "SN-BATCH-IDENTITY",
|
||||
"source_code": "DBS",
|
||||
"source_run_no": "SRC-BATCH-001",
|
||||
"attempt_no": 2,
|
||||
"lease_token": "lease-batch",
|
||||
"candidate_set_id": 12,
|
||||
"execution_id": 88,
|
||||
"gate_id": 901,
|
||||
"expected_warehouse_id": "WH-01",
|
||||
}
|
||||
|
||||
|
||||
def test_env_strips_wrapping_quotes(monkeypatch):
|
||||
"""RPA 读取 .env 风格账号密码时,应自动剥掉外层成对引号。"""
|
||||
worker = _load_freight_rpa_worker()
|
||||
@@ -443,12 +569,23 @@ def test_inventory_export_trigger_2edadc77_reports_success(monkeypatch, tmp_path
|
||||
callback_path, callback_payload = callbacks[0]
|
||||
assert callback_path == "/api/shipping-notice/rpa/inventory-ready"
|
||||
assert callback_payload == {
|
||||
"schema_version": "shipping_inventory_rpa.v2",
|
||||
"callback_id": "SRC-SN-20260629-2EDADC77:C1",
|
||||
"task_no": "SN-20260629-2EDADC77",
|
||||
"source_code": "DSV360_AWSDU",
|
||||
"source_run_no": "SRC-SN-20260629-2EDADC77",
|
||||
"run_no": "SRC-SN-20260629-2EDADC77",
|
||||
"attempt_no": 3,
|
||||
"lease_token": "lease-test",
|
||||
"candidate_set_id": 12,
|
||||
"execution_id": 88,
|
||||
"gate_id": 901,
|
||||
"expected_warehouse_id": "AWSDU",
|
||||
"status": "success",
|
||||
"success": True,
|
||||
"file_path": str(drop_dir / "inventory_SN-20260629-2EDADC77_DSV360_AWSDU.xlsx"),
|
||||
"file_sha256": hashlib.sha256(b"fake inventory content").hexdigest(),
|
||||
"rpa_mode": "browser",
|
||||
"source_code": "DSV360_AWSDU",
|
||||
}
|
||||
assert not trigger_path.exists()
|
||||
assert (processed_dir / trigger_path.name).is_file()
|
||||
@@ -494,12 +631,23 @@ def test_inventory_export_trigger_2edadc77_mock_writes_inventory_excel(monkeypat
|
||||
callback_path, callback_payload = callbacks[0]
|
||||
assert callback_path == "/api/shipping-notice/rpa/inventory-ready"
|
||||
assert callback_payload == {
|
||||
"schema_version": "shipping_inventory_rpa.v2",
|
||||
"callback_id": "SRC-SN-20260629-2EDADC77:C1",
|
||||
"task_no": "SN-20260629-2EDADC77",
|
||||
"source_code": "DSV360_AWSDU",
|
||||
"source_run_no": "SRC-SN-20260629-2EDADC77",
|
||||
"run_no": "SRC-SN-20260629-2EDADC77",
|
||||
"attempt_no": 3,
|
||||
"lease_token": "lease-test",
|
||||
"candidate_set_id": 12,
|
||||
"execution_id": 88,
|
||||
"gate_id": 901,
|
||||
"expected_warehouse_id": "AWSDU",
|
||||
"status": "success",
|
||||
"success": True,
|
||||
"file_path": str(exported),
|
||||
"file_sha256": hashlib.sha256(exported.read_bytes()).hexdigest(),
|
||||
"rpa_mode": "mock",
|
||||
"source_code": "DSV360_AWSDU",
|
||||
}
|
||||
assert not trigger_path.exists()
|
||||
assert (processed_dir / trigger_path.name).is_file()
|
||||
|
||||
Reference in new issue
Block a user