from __future__ import annotations

import importlib
import sys
from pathlib import Path


_ROOT = Path(__file__).resolve().parent.parent
_SOFIIA_PATH = _ROOT / "services" / "sofiia-console"
if str(_SOFIIA_PATH) not in sys.path:
    sys.path.insert(0, str(_SOFIIA_PATH))


def test_get_router_url_noda2_uses_router_url_when_node_id_unset(monkeypatch):
    monkeypatch.delenv("NODE_ID", raising=False)
    monkeypatch.delenv("NODES_NODA2_ROUTER_URL", raising=False)
    monkeypatch.delenv("NODES_NODA1_ROUTER_URL", raising=False)
    monkeypatch.setenv("ROUTER_URL", "http://example:9102")

    import app.config as config_mod  # type: ignore

    importlib.reload(config_mod)
    assert config_mod.get_router_url("NODA2") == "http://example:9102"


def test_get_router_url_noda2_prefers_explicit_node_override(monkeypatch):
    monkeypatch.delenv("NODE_ID", raising=False)
    monkeypatch.setenv("ROUTER_URL", "http://fallback-router:9102")
    monkeypatch.setenv("NODES_NODA2_ROUTER_URL", "http://explicit-noda2:9102")

    import app.config as config_mod  # type: ignore

    importlib.reload(config_mod)
    assert config_mod.get_router_url("NODA2") == "http://explicit-noda2:9102"

