"""freight_rpa_heartbeat 共享哨兵与 payload 单测。""" from __future__ import annotations import importlib.util import sys from pathlib import Path def _load_freight_rpa_heartbeat_module(): rpa_dir = Path(__file__).resolve().parents[2] / "scripts" / "rpa" if str(rpa_dir) not in sys.path: sys.path.insert(0, str(rpa_dir)) module_path = rpa_dir / "freight_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.setenv("SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID", "hxops-rpa-test-data") payload = module.build_heartbeat_payload( trigger_dir=r"C:\HxopsRpaTest\data\inventory_triggers", drop_dir=r"C:\HxopsRpaTest\data\inventory_drop", ) assert payload["shared_scope_id"] == "hxops-rpa-test-data" assert payload["trigger_dir"] == r"C:\HxopsRpaTest\data\inventory_triggers" def test_resolve_shared_scope_id_from_trigger_path(monkeypatch): module = _load_freight_rpa_heartbeat_module() monkeypatch.delenv("SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID", raising=False) monkeypatch.delenv("SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID", raising=False) monkeypatch.setenv( "SHIPPING_NOTICE_RPA_SHARED_INVENTORY_TRIGGER_DIR", r"C:\HxopsRpaTest\data\inventory_triggers", ) assert module._resolve_shared_scope_id() == "" monkeypatch.setenv( "SHIPPING_NOTICE_RPA_SHARED_INVENTORY_TRIGGER_DIR", r"C:\mount\hxops-rpa-test-data\inventory_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 / "inventory_triggers" trigger_dir.mkdir() monkeypatch.setenv("SHIPPING_NOTICE_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