"""Internal tests for main.py helpers. Targets: - _resolve_token branches (arg, dict result, str result, exception). - _auth outcomes (not required, missing, invalid, valid). - main() mode selection (HTTP vs stdio) and invocation args. These tests exercise internal logic for confidence without modifying production code. They patch imported helpers to isolate behavior. """ from __future__ import annotations import importlib import pytest mod = importlib.import_module("sme_external_terminal_mcp_server.main") @pytest.fixture(autouse=True) def restore_globals(): """Restore mutated globals after each test.""" orig_require = mod.REQUIRE_TOKEN yield mod.REQUIRE_TOKEN = orig_require # type: ignore def test_resolve_token_prefers_argument(monkeypatch): """Passed token returned immediately without env lookup.""" called = False def fake_get_env_var_or_request(*a, **kw): # pragma: no cover nonlocal called called = True return {"value": "envtok", "source": "env"} monkeypatch.setattr( mod, "get_env_var_or_request", fake_get_env_var_or_request ) token, source = mod._resolve_token("argtok") # type: ignore assert token == "argtok" and source == "arg" assert called is False def test_resolve_token_dict_result(monkeypatch): """Dictionary form from helper is normalized.""" def fake_get_env_var_or_request(field, ctx): # noqa: D401 return {"value": "envtok", "source": "env"} monkeypatch.setattr( mod, "get_env_var_or_request", fake_get_env_var_or_request ) token, source = mod._resolve_token(None) # type: ignore assert token == "envtok" and source == "env" def test_resolve_token_string_result(monkeypatch): """String result becomes fallback source.""" def fake_get_env_var_or_request(field, ctx): # noqa: D401 return "plain" monkeypatch.setattr( mod, "get_env_var_or_request", fake_get_env_var_or_request ) token, source = mod._resolve_token(None) # type: ignore assert token == "plain" and source == "fallback" def test_resolve_token_exception(monkeypatch): """Exception yields (None, 'none').""" def fake_get_env_var_or_request(field, ctx): # noqa: D401 raise RuntimeError("boom") monkeypatch.setattr( mod, "get_env_var_or_request", fake_get_env_var_or_request ) token, source = mod._resolve_token(None) # type: ignore assert token is None and source == "none" def test_auth_not_required_and_no_token(monkeypatch): """If auth not required and no token, passes.""" mod.REQUIRE_TOKEN = False # type: ignore err = mod._auth(None) # type: ignore assert err is None def test_auth_required_missing(monkeypatch): """Missing token when required -> error.""" mod.REQUIRE_TOKEN = True # type: ignore def fake_resolve(passed, ctx=None): # noqa: D401 return None, "none" monkeypatch.setattr(mod, "_resolve_token", fake_resolve) err = mod._auth(None) # type: ignore assert err and "missing" in err["error"].lower() def test_auth_required_invalid(monkeypatch): """Invalid token triggers error.""" mod.REQUIRE_TOKEN = True # type: ignore def fake_resolve(passed, ctx=None): # noqa: D401 return "wrong", "arg" def fake_check(field, token): # noqa: D401 return False monkeypatch.setattr(mod, "_resolve_token", fake_resolve) monkeypatch.setattr(mod, "check_env_var", fake_check) err = mod._auth("wrong") # type: ignore assert err and "invalid" in err["error"].lower() def test_auth_required_valid(monkeypatch): """Valid token accepted.""" mod.REQUIRE_TOKEN = True # type: ignore def fake_resolve(passed, ctx=None): # noqa: D401 return "good", "arg" def fake_check(field, token): # noqa: D401 return True monkeypatch.setattr(mod, "_resolve_token", fake_resolve) monkeypatch.setattr(mod, "check_env_var", fake_check) err = mod._auth("good") # type: ignore assert err is None def test_main_mode_http(monkeypatch): """main() calls mcp.run with HTTP transport when flag true.""" monkeypatch.setattr(mod, "USE_STREAMABLE_HTTP", True, raising=False) called = {} def fake_run(*a, **kw): # noqa: D401 called["args"] = a called["kwargs"] = kw monkeypatch.setattr(mod.mcp, "run", fake_run) # type: ignore mod.main() assert called.get("kwargs", {}).get("transport") == "streamable-http" def test_main_mode_stdio(monkeypatch): """main() calls mcp.run without args when flag false.""" monkeypatch.setattr(mod, "USE_STREAMABLE_HTTP", False, raising=False) called = {} def fake_run(*a, **kw): # noqa: D401 called["args"] = a called["kwargs"] = kw monkeypatch.setattr(mod.mcp, "run", fake_run) # type: ignore mod.main() assert "transport" not in called.get("kwargs", {})