"""Tests for scripts/self_healing.py safe CLI behavior.""" from __future__ import annotations import importlib.util from pathlib import Path from unittest.mock import MagicMock import pytest REPO_ROOT = Path(__file__).parent.parent spec = importlib.util.spec_from_file_location("self_healing", REPO_ROOT / "scripts" / "self_healing.py") sh = importlib.util.module_from_spec(spec) spec.loader.exec_module(sh) class TestMainCli: def test_help_exits_without_running_healer(self, monkeypatch, capsys): healer_cls = MagicMock() monkeypatch.setattr(sh, "SelfHealer", healer_cls) with pytest.raises(SystemExit) as excinfo: sh.main(["--help"]) assert excinfo.value.code == 0 healer_cls.assert_not_called() out = capsys.readouterr().out assert "--execute" in out assert "--help-safe" in out def test_help_safe_exits_without_running_healer(self, monkeypatch, capsys): healer_cls = MagicMock() monkeypatch.setattr(sh, "SelfHealer", healer_cls) with pytest.raises(SystemExit) as excinfo: sh.main(["--help-safe"]) assert excinfo.value.code == 0 healer_cls.assert_not_called() out = capsys.readouterr().out assert "DRY-RUN" in out assert "--confirm-kill" in out def test_default_invocation_runs_in_dry_run_mode(self, monkeypatch): healer = MagicMock() healer_cls = MagicMock(return_value=healer) monkeypatch.setattr(sh, "SelfHealer", healer_cls) sh.main([]) healer_cls.assert_called_once_with(dry_run=True, confirm_kill=False, yes=False) healer.run.assert_called_once_with() def test_execute_flag_disables_dry_run(self, monkeypatch): healer = MagicMock() healer_cls = MagicMock(return_value=healer) monkeypatch.setattr(sh, "SelfHealer", healer_cls) sh.main(["--execute", "--yes", "--confirm-kill"]) healer_cls.assert_called_once_with(dry_run=False, confirm_kill=True, yes=True) healer.run.assert_called_once_with()