"""Tests for automatic MCP reload when config.yaml mcp_servers section changes."""
from unittest.mock import MagicMock, patch

from utils import file_signature


def _make_cli(tmp_path, mcp_servers=None, extra_config=None):
    """Create a minimal HermesCLI instance with mocked config."""
    import cli as cli_mod
    obj = object.__new__(cli_mod.HermesCLI)
    cfg = {"mcp_servers": mcp_servers or {}}
    if extra_config:
        cfg.update(extra_config)
    obj.config = cfg
    obj._agent_running = False
    obj._last_config_check = 0.0
    obj._config_mcp_servers = mcp_servers or {}

    cfg_file = tmp_path / "config.yaml"
    cfg_file.write_text("mcp_servers: {}\n")
    obj._config_sig = file_signature(cfg_file.stat())

    obj._reload_mcp = MagicMock()
    obj._busy_command = MagicMock()
    obj._busy_command.return_value.__enter__ = MagicMock(return_value=None)
    obj._busy_command.return_value.__exit__ = MagicMock(return_value=False)
    obj._slow_command_status = MagicMock(return_value="reloading...")

    return obj, cfg_file


class TestMCPConfigWatch:



    def test_new_mcp_server_triggers_reload(self, tmp_path):
        """Adding a new MCP server to config triggers auto-reload."""
        import hermes_yaml as yaml
        obj, cfg_file = _make_cli(tmp_path, mcp_servers={})

        # Simulate user adding a new MCP server to config.yaml
        cfg_file.write_text(yaml.safe_dump({"mcp_servers": {"github": {"url": "https://mcp.github.com"}}}))
        obj._config_sig = None  # force stale mtime

        with patch("hermes_cli.config.get_config_path", return_value=cfg_file):
            obj._check_config_mcp_changes()

        obj._reload_mcp.assert_called_once()

    def test_removed_mcp_server_triggers_reload(self, tmp_path):
        """Removing an MCP server from config triggers auto-reload."""
        import hermes_yaml as yaml
        obj, cfg_file = _make_cli(tmp_path, mcp_servers={"github": {"url": "https://mcp.github.com"}})

        # Simulate user removing the server
        cfg_file.write_text(yaml.safe_dump({"mcp_servers": {}}))
        obj._config_sig = None

        with patch("hermes_cli.config.get_config_path", return_value=cfg_file):
            obj._check_config_mcp_changes()

        obj._reload_mcp.assert_called_once()



    def test_optout_disables_auto_reload(self, tmp_path, capsys):
        """When mcp.auto_reload_on_config_change is False, a changed
        mcp_servers section must NOT trigger an automatic reload — but the
        change is still detected and the user is told how to apply it.

        This protects the provider prompt cache: every automatic reload
        rebuilds the agent tool surface and invalidates cached prefixes.

        The toggle is the top-level ``mcp:`` section in config.yaml, and the
        watcher reads it from the same freshly-parsed file it diffs — so
        flipping the toggle and editing mcp_servers in one edit behaves
        correctly.
        """
        import hermes_yaml as yaml
        obj, cfg_file = _make_cli(
            tmp_path,
            mcp_servers={},
        )

        # Simulate a changed mcp_servers section with auto-reload opted out.
        cfg_file.write_text(yaml.safe_dump({
            "mcp": {"auto_reload_on_config_change": False},
            "mcp_servers": {"github": {"url": "https://mcp.github.com"}},
        }))
        obj._config_sig = None  # force stale mtime

        with patch("hermes_cli.config.get_config_path", return_value=cfg_file):
            obj._check_config_mcp_changes()

        obj._reload_mcp.assert_not_called()

        out = capsys.readouterr().out
        assert "/reload-mcp" in out  # tells the user how to apply it manually

    def test_optout_updates_snapshot_so_reload_mcp_applies_cleanly(self, tmp_path):
        """After an opted-out change, the watcher must not re-notify every
        tick: the snapshot is updated so the same content compares equal on
        the next pass."""
        import hermes_yaml as yaml
        obj, cfg_file = _make_cli(tmp_path, mcp_servers={})

        cfg_file.write_text(yaml.safe_dump({
            "mcp": {"auto_reload_on_config_change": False},
            "mcp_servers": {"github": {"url": "https://mcp.github.com"}},
        }))
        obj._config_sig = None

        with patch("hermes_cli.config.get_config_path", return_value=cfg_file):
            obj._check_config_mcp_changes()
            # Second pass: same file content, new mtime — no reload, no change.
            obj._last_config_check = 0.0
            obj._config_sig = None
            obj._check_config_mcp_changes()

        obj._reload_mcp.assert_not_called()
        assert obj._config_mcp_servers == {"github": {"url": "https://mcp.github.com"}}

    def test_optout_path_is_top_level_mcp_not_auxiliary(self, tmp_path):
        """Regression guard: the opt-out toggle is the top-level
        ``mcp.auto_reload_on_config_change`` key, NOT ``auxiliary.mcp``
        (which holds side-LLM task provider settings).

        A config that sets ONLY ``auxiliary.mcp.auto_reload_on_config_change:
        false`` must NOT disable the reload."""
        import hermes_yaml as yaml
        obj, cfg_file = _make_cli(
            tmp_path,
            mcp_servers={},
        )

        cfg_file.write_text(yaml.safe_dump({
            "auxiliary": {"mcp": {"auto_reload_on_config_change": False}},
            "mcp_servers": {"github": {"url": "https://mcp.github.com"}},
        }))
        obj._config_sig = None

        with patch("hermes_cli.config.get_config_path", return_value=cfg_file):
            obj._check_config_mcp_changes()

        # Reload happened because the aux-task path is not the toggle.
        obj._reload_mcp.assert_called()

    def test_env_var_templates_do_not_false_positive_on_unrelated_saves(
        self, tmp_path, monkeypatch, capsys
    ):
        """Regression for the '/reasoning triggers MCP reload' bug (#55701).

        Init snapshots mcp_servers from the loaded config, which has been
        through _expand_env_vars() — so ``${MCP_GH_API_KEY}`` is stored
        expanded.  The watcher re-parses the RAW yaml.  Without expanding the
        watcher side too, the comparison is always unequal whenever any
        template is in use, so EVERY config.yaml rewrite (e.g.
        save_config_value('agent.reasoning_effort', ...) from /reasoning)
        fired a full MCP reconnect.
        """
        import hermes_yaml as yaml
        monkeypatch.setenv("MCP_GH_API_KEY", "sekrit-token")

        raw_servers = {
            "github": {
                "url": "https://mcp.github.com",
                "headers": {"Authorization": "Bearer ${MCP_GH_API_KEY}"},
            }
        }
        expanded_servers = {
            "github": {
                "url": "https://mcp.github.com",
                "headers": {"Authorization": "Bearer sekrit-token"},
            }
        }
        # Init snapshot holds the EXPANDED form (as load_cli_config produces).
        obj, cfg_file = _make_cli(tmp_path, mcp_servers=expanded_servers)

        # Unrelated-key save: mcp_servers content identical (raw templates),
        # only reasoning_effort changed — mtime moves.
        cfg_file.write_text(yaml.safe_dump({
            "agent": {"reasoning_effort": "high"},
            "mcp_servers": raw_servers,
        }))
        obj._config_sig = None

        with patch("hermes_cli.config.get_config_path", return_value=cfg_file):
            obj._check_config_mcp_changes()

        obj._reload_mcp.assert_not_called()
        assert "MCP server config changed" not in capsys.readouterr().out


def test_tui_init_run_state_seeds_config_sig_when_config_exists(monkeypatch):
    """REPL init must seed _config_sig from on-disk config.yaml.

    file_signature is only evaluated when the file exists (short-circuit
    otherwise). Isolated-home tests without a config file therefore never
    exercised the call, and a missing import crashed every real CLI launch.
    """
    from hermes_cli.config import get_config_path
    import cli as cli_mod

    # Bare object: skip the tool-callback / security wiring at the end of the init.
    monkeypatch.setenv("HERMES_DEFER_AGENT_STARTUP", "1")
    cfg_file = get_config_path()
    cfg_file.parent.mkdir(parents=True, exist_ok=True)
    cfg_file.write_text("mcp_servers: {}\n")

    obj = object.__new__(cli_mod.HermesCLI)
    obj.config = {"mcp_servers": {}}
    obj._tui_init_run_state()

    assert obj._config_sig == file_signature(cfg_file.stat())


def test_pinned_mtime_same_size_replacement_triggers_reload(tmp_path):
    """#111105: cp -p / rsync -t style replacement (same mtime, same size) must still reload."""
    import os
    import shutil

    obj, cfg_file = _make_cli(tmp_path, mcp_servers={"bb": {"command": "b"}})
    cfg_file.write_text("mcp_servers:\n  bb: {command: b}\n")
    obj._config_sig = file_signature(cfg_file.stat())
    other = tmp_path / "other.yaml"
    other.write_text("mcp_servers:\n  aa: {command: a}\n")
    shutil.copy2(other, cfg_file)
    os.utime(cfg_file, ns=(obj._config_sig[0], obj._config_sig[0]))

    with patch("hermes_cli.config.get_config_path", return_value=cfg_file):
        obj._check_config_mcp_changes()

    obj._reload_mcp.assert_called_once()
    assert obj._config_mcp_servers == {"aa": {"command": "a"}}
