Files
syops/backend/tests/test_freight_rpa_heartbeat.py
T
hugoandCursor 39fe3c05f2 feat(freight-rpa): 库存/收料改名在手/入站,报表归属RPA,类名统一FreightRpa
把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与
枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa
持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀,
上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-19 10:46:06 +08:00

131 lines
5.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""freight_rpa_heartbeat 共享哨兵与 payload 单测。"""
from __future__ import annotations
import importlib.util
import sys
import threading
import time
from pathlib import Path
def _load_freight_rpa_heartbeat_module():
rpa_dir = Path(__file__).resolve().parents[2] / "scripts" / "freight_rpa"
if str(rpa_dir) not in sys.path:
sys.path.insert(0, str(rpa_dir))
module_path = rpa_dir / "common" / "rpa_heartbeat.py"
spec = importlib.util.spec_from_file_location("freight_rpa_heartbeat_test", module_path)
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(module)
return module
def test_build_heartbeat_payload_includes_shared_scope_id(monkeypatch):
module = _load_freight_rpa_heartbeat_module()
monkeypatch.delenv("FREIGHT_RPA_WORKER_ID", raising=False)
monkeypatch.setenv("FREIGHT_RPA_SHARED_SCOPE_ID", "hxops-rpa-test-data")
payload = module.build_heartbeat_payload(
trigger_dir=r"C:\HxopsRpaTest\data\onhand_triggers",
drop_dir=r"C:\HxopsRpaTest\data\onhand_drop",
)
assert payload["shared_scope_id"] == "hxops-rpa-test-data"
assert payload["trigger_dir"] == r"C:\HxopsRpaTest\data\onhand_triggers"
assert payload["capabilities"] == ["onhand_export"]
def test_build_heartbeat_payload_inbound_worker_declares_inbound_export(monkeypatch):
"""inbound Worker(*-inbound 或 inbound trigger 路径)须声明 inbound_export。"""
module = _load_freight_rpa_heartbeat_module()
monkeypatch.setenv("FREIGHT_RPA_WORKER_ID", "WIN-RPA-01-inbound")
payload = module.build_heartbeat_payload(
trigger_dir=r"C:\HxopsRpaTest\data\freight_rpa\inbound\triggers",
drop_dir=r"C:\HxopsRpaTest\data\freight_rpa\inbound\drop",
)
assert payload["capabilities"] == ["inbound_export"]
assert payload["worker_id"].endswith("-inbound")
def test_build_heartbeat_payload_adds_freight_rpa_upload_when_upload_enabled(monkeypatch):
"""开启上传时心跳能力是 freight_rpa_upload,不再上报 freight_upload。"""
module = _load_freight_rpa_heartbeat_module()
monkeypatch.setenv("FREIGHT_RPA_WORKER_UPLOAD_ENABLED", "true")
payload = module.build_heartbeat_payload(
trigger_dir=r"C:\HxopsRpaTest\data\onhand_triggers",
drop_dir=r"C:\HxopsRpaTest\data\onhand_drop",
)
assert payload["capabilities"] == ["onhand_export", "freight_rpa_upload"]
def test_resolve_shared_scope_id_from_trigger_path(monkeypatch):
module = _load_freight_rpa_heartbeat_module()
monkeypatch.delenv("FREIGHT_RPA_SHARED_SCOPE_ID", raising=False)
monkeypatch.delenv("FREIGHT_RPA_SHARED_SCOPE_ID", raising=False)
monkeypatch.setenv(
"FREIGHT_RPA_SHARED_ONHAND_TRIGGER_DIR",
r"C:\HxopsRpaTest\data\onhand_triggers",
)
assert module._resolve_shared_scope_id() == ""
monkeypatch.setenv(
"FREIGHT_RPA_SHARED_ONHAND_TRIGGER_DIR",
r"C:\mount\hxops-rpa-test-data\onhand_triggers",
)
assert module._resolve_shared_scope_id() == "hxops-rpa-test-data"
def test_write_shared_mount_sentinel_writes_scope_file(tmp_path, monkeypatch):
module = _load_freight_rpa_heartbeat_module()
trigger_dir = tmp_path / "onhand_triggers"
trigger_dir.mkdir()
monkeypatch.setenv("FREIGHT_RPA_SHARED_SCOPE_ID", "hxops-rpa-test-data")
module.write_shared_mount_sentinel(trigger_dir=trigger_dir, hostname="WIN-RPA-01")
sentinel = trigger_dir / ".hxops_rpa_shared_mount"
assert sentinel.is_file()
content = sentinel.read_text(encoding="utf-8")
assert "scope_id=hxops-rpa-test-data" in content
assert "host=WIN-RPA-01" in content
def test_maybe_send_heartbeat_does_not_block_trigger_loop_when_http_stalls(tmp_path, monkeypatch):
"""断言底层 HTTP 卡住时,主 Worker 仍能立即继续扫描 Trigger。"""
module = _load_freight_rpa_heartbeat_module()
release_request = threading.Event()
request_started = threading.Event()
calls: list[dict] = []
def stalled_post(payload):
calls.append(payload)
request_started.set()
release_request.wait(timeout=2)
return False
monkeypatch.setattr(module, "post_heartbeat", stalled_post)
monkeypatch.setenv("FREIGHT_RPA_WORKER_HEARTBEAT_SECONDS", "15")
started = time.monotonic()
sent_at = module.maybe_send_heartbeat(
trigger_dir=tmp_path,
drop_dir=tmp_path,
last_sent_mono=0.0,
force=True,
)
# 核心断言:投递而非等待卡住的 HTTP 请求,扫描循环可立即继续。
assert time.monotonic() - started < 0.2
assert sent_at > 0
assert request_started.wait(timeout=0.5)
module.maybe_send_heartbeat(
trigger_dir=tmp_path,
drop_dir=tmp_path,
last_sent_mono=0.0,
force=True,
)
# 同一时刻只允许一个后台请求,避免后端异常时线程无限累积。
assert len(calls) == 1
release_request.set()
assert module._HEARTBEAT_THREAD is not None
module._HEARTBEAT_THREAD.join(timeout=1)