feat(p1-2): DetectionTimer erkennt Plugin-State-Datei (background_tasks > 0 → active)
This commit is contained in:
@@ -1909,3 +1909,143 @@ class TestPluginStatePersistence:
|
||||
|
||||
state = _read_plugin_state(state_path)
|
||||
assert state["background_tasks"] == 0
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# P1-2: DetectionTimer erkennt Plugin-State-Datei
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestDetectionTimerPluginState:
|
||||
"""P1-2: DetectionTimer erkennt Plugin-State-Datei."""
|
||||
|
||||
def test_plugin_state_background_tasks_positive_detected(self, tmp_path):
|
||||
"""Plugin-State mit background_tasks=3 → check() returns True."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer, _write_plugin_state
|
||||
|
||||
state_path = str(tmp_path / "hermes_rocket_plugin.json")
|
||||
os.makedirs(os.path.dirname(state_path), exist_ok=True)
|
||||
_write_plugin_state(state_path, {
|
||||
"background_tasks": 3,
|
||||
"last_update_iso": "2026-01-01T00:00:00",
|
||||
"source": "plugin_internal",
|
||||
"schema_version": 1,
|
||||
})
|
||||
|
||||
dt = DetectionTimer(
|
||||
gateway_state_path="/tmp/nonexistent_gateway_state.json",
|
||||
detection_interval=0.1,
|
||||
plugin_state_path=state_path,
|
||||
)
|
||||
result = dt.check()
|
||||
assert result is True
|
||||
|
||||
def test_plugin_state_background_tasks_zero_not_detected(self, tmp_path):
|
||||
"""Plugin-State mit background_tasks=0 → check() fällt auf pgrep zurück."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer, _write_plugin_state
|
||||
|
||||
state_path = str(tmp_path / "hermes_rocket_plugin.json")
|
||||
os.makedirs(os.path.dirname(state_path), exist_ok=True)
|
||||
_write_plugin_state(state_path, {
|
||||
"background_tasks": 0,
|
||||
"last_update_iso": "2026-01-01T00:00:00",
|
||||
"source": "plugin_internal",
|
||||
"schema_version": 1,
|
||||
})
|
||||
|
||||
own_pid = os.getpid()
|
||||
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
|
||||
mock_run.return_value = MagicMock(
|
||||
stdout=f"{own_pid}\n",
|
||||
stderr="",
|
||||
returncode=0,
|
||||
)
|
||||
dt = DetectionTimer(
|
||||
gateway_state_path="/tmp/nonexistent_gateway_state.json",
|
||||
detection_interval=0.1,
|
||||
plugin_state_path=state_path,
|
||||
)
|
||||
result = dt.check()
|
||||
# background_tasks=0, gateway_state fehlt, pgrep nur self → False
|
||||
assert result is False
|
||||
|
||||
def test_plugin_state_missing_file_no_error(self):
|
||||
"""Plugin-State-Datei fehlt → kein Fehler, fällt zurück auf gateway_state+pgrep."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
|
||||
own_pid = os.getpid()
|
||||
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
|
||||
mock_run.return_value = MagicMock(
|
||||
stdout=f"{own_pid}\n99999\n",
|
||||
stderr="",
|
||||
returncode=0,
|
||||
)
|
||||
dt = DetectionTimer(
|
||||
gateway_state_path="/tmp/nonexistent_gateway_state.json",
|
||||
detection_interval=0.1,
|
||||
plugin_state_path="/tmp/nonexistent_plugin_state.json",
|
||||
)
|
||||
result = dt.check()
|
||||
# pgrep findet anderen Prozess → True
|
||||
assert result is True
|
||||
|
||||
def test_plugin_state_corrupt_json_no_crash(self, tmp_path):
|
||||
"""Korrupte Plugin-State-Datei → kein Crash, graceful fallback."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
|
||||
state_path = str(tmp_path / "hermes_rocket_plugin.json")
|
||||
os.makedirs(os.path.dirname(state_path), exist_ok=True)
|
||||
# Schreibe korruptes JSON
|
||||
with open(state_path, 'w') as f:
|
||||
f.write("{invalid json {{{")
|
||||
|
||||
own_pid = os.getpid()
|
||||
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
|
||||
mock_run.return_value = MagicMock(
|
||||
stdout=f"{own_pid}\n99999\n",
|
||||
stderr="",
|
||||
returncode=0,
|
||||
)
|
||||
dt = DetectionTimer(
|
||||
gateway_state_path="/tmp/nonexistent_gateway_state.json",
|
||||
detection_interval=0.1,
|
||||
plugin_state_path=state_path,
|
||||
)
|
||||
result = dt.check()
|
||||
# Sollte nicht crashen, pgrep findet anderen Prozess → True
|
||||
assert result is True
|
||||
|
||||
def test_plugin_state_priority_after_gateway(self, tmp_path):
|
||||
"""Plugin-State wird NACH gateway_state geprüft (gateway hat Vorrang)."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer, _write_plugin_state
|
||||
import tempfile, json
|
||||
|
||||
# gateway_state mit active_agents=3 (>1 → PRIMARY entscheidet)
|
||||
state = {"active_agents": 3, "profiles": ["default", "miguel", "anna"]}
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False, mode="w") as f:
|
||||
json.dump(state, f)
|
||||
gw_path = f.name
|
||||
|
||||
try:
|
||||
# Plugin-State mit background_tasks=0 (würde allein nicht reichen)
|
||||
plugin_path = str(tmp_path / "hermes_rocket_plugin.json")
|
||||
os.makedirs(os.path.dirname(plugin_path), exist_ok=True)
|
||||
_write_plugin_state(plugin_path, {
|
||||
"background_tasks": 0,
|
||||
"last_update_iso": "2026-01-01T00:00:00",
|
||||
"source": "plugin_internal",
|
||||
"schema_version": 1,
|
||||
})
|
||||
|
||||
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
|
||||
dt = DetectionTimer(
|
||||
gateway_state_path=gw_path,
|
||||
detection_interval=0.1,
|
||||
plugin_state_path=plugin_path,
|
||||
)
|
||||
result = dt.check()
|
||||
# gateway_state=3 > 1 → PRIMARY → True (pgrep NICHT aufgerufen)
|
||||
assert result is True
|
||||
mock_run.assert_not_called()
|
||||
finally:
|
||||
os.unlink(gw_path)
|
||||
|
||||
Reference in New Issue
Block a user