Files
ewoooc/tests/test_auto_heal_safety.py
OoO f0e044aa48
All checks were successful
CD Pipeline / deploy (push) Successful in 1m16s
補齊自癒稽核與 OpenClaw 記憶閉環
2026-04-29 23:02:06 +08:00

43 lines
1.5 KiB
Python

def test_auto_heal_blocks_unsafe_ssh_commands():
from services.auto_heal_service import AutoHealService
svc = AutoHealService()
assert svc._is_allowed_ssh_argv(["docker", "ps"]) is True
assert svc._is_allowed_ssh_argv(["df", "-h"]) is True
assert svc._is_allowed_ssh_argv(["docker", "restart", "momo-pro-system"]) is False
assert svc._is_allowed_ssh_argv(["docker", "logs", "momo-db"]) is False
assert svc._is_allowed_ssh_argv(["bash", "-lc", "whoami"]) is False
def test_auto_heal_code_fix_writes_audit(monkeypatch):
from services.auto_heal_service import AutoHealResult, AutoHealService
svc = AutoHealService()
calls = []
monkeypatch.setattr(
svc,
"_handle_code_fix",
lambda playbook, context: AutoHealResult(True, "CODE_FIX", "fixed"),
)
monkeypatch.setattr(
svc,
"_alert_and_store",
lambda playbook, context, result, duration_ms=0.0: calls.append((playbook, context, result, duration_ms)),
)
monkeypatch.setattr("services.auto_heal_service._find_playbook", lambda error_type: {
"id": 99,
"name": "Code fix",
"action_type": "CODE_FIX",
"action_params": {},
"max_retries": 1,
"cooldown_min": 0,
})
monkeypatch.setattr(svc, "_ensure_incident", lambda error_type, context: 123)
result = svc.handle_exception("code_exception", {"target_file": "services/example.py"})
assert result.success is True
assert calls
assert calls[0][2].action == "CODE_FIX"