"""Focused tests for Hermes-to-Herdr routing and the shared auth patch."""

from __future__ import annotations

import importlib.util
import sys
from pathlib import Path

import pytest


SCRIPTS = Path(__file__).parents[2] / "services/hermes/scripts"


def _load(name: str):
    spec = importlib.util.spec_from_file_location(name, SCRIPTS / f"{name}.py")
    assert spec and spec.loader
    module = importlib.util.module_from_spec(spec)
    sys.modules[spec.name] = module
    spec.loader.exec_module(module)
    return module


dispatch = _load("herdr_dispatch")
auth_patch = _load("patch_hermes_auth")


def test_herdr_plan_chooses_task_shape_and_caps_effort():
    status = {
        "routes": {
            "codex-high": [
                "openai-codex/gpt-5.6-sol",
                "anthropic/claude-opus-5",
                "custom/qwen2.5:14b-instruct-q4_0",
            ],
            "claude-medium": [
                "anthropic/claude-sonnet-5",
                "openai-codex/gpt-5.6-terra",
            ],
        }
    }
    implementation = dispatch.select_plan(status, "implementation", "high")
    architecture = dispatch.select_plan(status, "architecture", "medium")
    assert implementation["worker"] == "codex"
    assert implementation["model"] == "gpt-5.6-sol"
    assert architecture["worker"] == "claude"
    assert architecture["model"] == "claude-sonnet-5"
    with pytest.raises(ValueError, match="effort"):
        dispatch.select_plan(status, "review", "max")


def test_auth_patch_honors_explicit_shared_store(tmp_path: Path):
    source = tmp_path / "auth.py"
    destination = tmp_path / "patched" / "auth.py"
    source.write_text(
        "from pathlib import Path\nimport os\n\n"
        "def _auth_file_path() -> Path:\n"
        "    path = get_hermes_home() / \"auth.json\"\n"
        "    return path\n",
        encoding="utf-8",
    )
    auth_patch.patch(source, destination)
    content = destination.read_text(encoding="utf-8")
    assert 'os.environ.get("HERMES_AUTH_FILE"' in content
    assert "Path(configured)" in content


def test_auth_patch_fails_closed_on_upstream_drift(tmp_path: Path):
    source = tmp_path / "auth.py"
    source.write_text("def changed():\n    pass\n", encoding="utf-8")
    with pytest.raises(RuntimeError, match="context changed"):
        auth_patch.patch(source, tmp_path / "patched.py")
