feat(shipping-notice): 新增发货通知自动上传功能与候选资格评估

- 在环境配置中新增 SHIPPING_NOTICE_AUTO_UPLOAD_ENABLED 选项,默认关闭,需显式开启以支持自动上传。
- 引入新的 pytest.ini 文件,增加测试标记以支持自动化测试。
- 删除不再使用的 seed_shipping_notice_agents.py 文件,简化代码库。
- 更新相关 API,增强发货通知与候选资格评估功能,支持多种筛选条件。
- 增强文档与代码注释,反映新的业务逻辑与功能要求。
This commit is contained in:
hugo committed 2026-08-02 19:26:35 +08:00
1 parent cef3d5b26a
commit 3a9fbac2fc
226 files changed
+25010 -2098

No files matched your search

+76 -2
View File
@@ -1,9 +1,9 @@
"""
backend/tests/test_rpa_worker_heartbeat.py
用途:货代库存 RPA Worker 心跳存储、共享目录校验、就绪检查与 API 契约单测。
用途:货代库存/上传 RPA Worker 心跳存储、共享目录校验、就绪检查与 API 契约单测。
依赖:rpa_worker_heartbeat、shipping_notice_tools heartbeat 路由。
逻辑:覆盖跨平台 scope、哨兵、Redis 策略、环境匹配与 fail-fast 断言。
逻辑:覆盖跨平台 scope、哨兵、Redis 策略、环境匹配、货代上传就绪与 fail-fast 断言。
"""
from __future__ import annotations
@@ -15,10 +15,14 @@ import pytest
from app.core.errors import ServiceUnavailableError
from app.services.shipping_notice.rpa_worker_heartbeat import (
FREIGHT_UPLOAD_CAPABILITY,
INVENTORY_EXPORT_CAPABILITY,
RPA_FREIGHT_UPLOAD_NOT_READY_MESSAGE,
RPA_HEARTBEAT_STORAGE_UNAVAILABLE_MESSAGE,
RPA_WORKER_OFFLINE_MESSAGE,
assert_freight_upload_rpa_ready,
assert_inventory_rpa_worker_ready,
check_freight_upload_rpa_ready,
check_inventory_rpa_worker_ready,
record_rpa_worker_heartbeat,
resolve_backend_rpa_environment,
@@ -371,3 +375,73 @@ def test_shared_scope_id_derived_from_mount_path_when_env_missing(app, tmp_path)
with app.app_context():
assert shared_scope_id() == "hxops-rpa-test-data"
def _enable_freight_upload_checks(app, tmp_path, *, scope_id: str = "hxops-rpa-test-data"):
"""配置货代上传 trigger/queue 与心跳策略,供上传就绪单测复用。"""
upload_trigger = tmp_path / "upload_triggers"
upload_queue = tmp_path / "upload_queue"
upload_trigger.mkdir()
upload_queue.mkdir()
_write_shared_sentinel(upload_trigger, scope_id=scope_id)
app.config["ENV_NAME"] = "test"
app.config["SHIPPING_NOTICE_RPA_SHARED_UPLOAD_TRIGGER_DIR"] = str(upload_trigger)
app.config["SHIPPING_NOTICE_RPA_SHARED_UPLOAD_QUEUE_DIR"] = str(upload_queue)
app.config["SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID"] = scope_id
app.config["SHIPPING_NOTICE_RPA_CREDENTIAL_KEY"] = "dGVzdC1rZXktMzItYnl0ZXMtZm9yLWZlcm5ldCE="
app.config["INTERNAL_API_BASE_URL"] = "http://127.0.0.1:5052"
app.config["SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_REQUIRED"] = True
app.config["SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_REQUIRE_REDIS"] = False
app.config["SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_ALLOW_MEMORY_FALLBACK"] = True
app.config["SHIPPING_NOTICE_RPA_WORKER_MOCK"] = "false"
return upload_trigger
def test_check_freight_upload_rpa_ready_requires_freight_upload_capability(app, tmp_path):
"""仅 inventory_export 能力时货代上传不得 ready。"""
upload_trigger = _enable_freight_upload_checks(app, tmp_path)
with app.app_context():
record_rpa_worker_heartbeat(
_heartbeat_payload(
trigger_dir=str(upload_trigger),
shared_scope_id="hxops-rpa-test-data",
capabilities=[INVENTORY_EXPORT_CAPABILITY],
)
)
result = check_freight_upload_rpa_ready()
# 关键断言:缺少 freight_upload 能力必须阻断自动上传。
assert result.ready is False
assert any("missing_freight_upload_capability" in reason for reason in result.reasons)
def test_check_freight_upload_rpa_ready_when_worker_and_dirs_ok(app, tmp_path):
"""Worker 声明 freight_upload 且目录/凭据齐全时应 ready。"""
upload_trigger = _enable_freight_upload_checks(app, tmp_path)
with app.app_context():
record_rpa_worker_heartbeat(
_heartbeat_payload(
trigger_dir=str(upload_trigger),
shared_scope_id="hxops-rpa-test-data",
capabilities=[INVENTORY_EXPORT_CAPABILITY, FREIGHT_UPLOAD_CAPABILITY],
)
)
result = check_freight_upload_rpa_ready()
assert result.ready is True
def test_assert_freight_upload_rpa_ready_includes_reasons(app, tmp_path):
"""上传 fail-fast 须带 reasons,供建单前/触发时展示阻断原因。"""
from app.services.shipping_notice.rpa_worker_heartbeat import (
reset_rpa_worker_heartbeat_state_for_tests,
)
_enable_freight_upload_checks(app, tmp_path)
# 去掉凭据主密钥:即使 Redis 残留心跳,静态配置缺失也必须阻断。
app.config.pop("SHIPPING_NOTICE_RPA_CREDENTIAL_KEY", None)
reset_rpa_worker_heartbeat_state_for_tests()
with app.app_context():
with pytest.raises(ServiceUnavailableError) as exc_info:
assert_freight_upload_rpa_ready()
assert exc_info.value.message == RPA_FREIGHT_UPLOAD_NOT_READY_MESSAGE
reasons = (exc_info.value.details or {}).get("reasons", [])
assert "credential_key_missing" in reasons