"""Tests for Phase 1b identity-based agent routing."""

from lib.agent_routing import AGENT_ORDER, classify_pr_route


def _diff_for(*paths_and_lines: tuple[str, str] | str) -> str:
    chunks = []
    for item in paths_and_lines:
        if isinstance(item, tuple):
            path, line = item
        else:
            path, line = item, "+content"
        chunks.append(f"diff --git a/{path} b/{path}\n{line}")
    return "\n".join(chunks)


def test_six_primary_domains_route_to_expected_agents():
    expected = {
        "grand-strategy": "Leo",
        "ai-alignment": "Theseus",
        "internet-finance": "Rio",
        "health": "Vida",
        "entertainment": "Clay",
        "space-development": "Astra",
    }

    for domain, agent in expected.items():
        route = classify_pr_route(_diff_for(f"domains/{domain}/claim.md"))
        assert route.primary_agent == agent
        assert route.required_agents == (agent,)
        assert route.route_kind == "single"
        assert route.fallback is False


def test_broadened_identity_domains_route_to_owners():
    expected = {
        "ai-systems": "Theseus",
        "living-agents": "Theseus",
        "living-capital": "Rio",
        "collective-intelligence": "Leo",
        "cultural-dynamics": "Clay",
        "energy": "Astra",
        "robotics": "Astra",
        "manufacturing": "Astra",
        "advanced-manufacturing": "Astra",
    }

    for domain, agent in expected.items():
        route = classify_pr_route(_diff_for(f"foundations/{domain}/claim.md"))
        assert route.primary_agent == agent
        assert route.required_agents == (agent,)


def test_cross_domain_ai_and_x402_requires_theseus_and_rio():
    route = classify_pr_route(
        _diff_for(
            ("domains/ai-alignment/agent-wallets.md", "+AI systems route agents around x402 payments"),
            ("domains/internet-finance/x402.md", "+x402 payment rail for onchain agent transactions"),
        )
    )

    assert route.primary_agent == "Rio"
    assert set(route.required_agents) == {"Theseus", "Rio"}
    assert len(route.required_agents) == 2
    assert route.route_kind == "multi"


def test_collective_ai_goals_routes_to_leo_and_theseus():
    route = classify_pr_route(
        _diff_for(
            (
                "foundations/collective-intelligence/collective-ai-goals.md",
                "+Collective AI goals and AI systems self-understanding need review.",
            )
        )
    )

    assert route.primary_agent == "Leo"
    assert route.required_agents == ("Leo", "Theseus")
    assert route.route_kind == "multi"


def test_too_many_touched_domains_caps_at_two_and_marks_escalated():
    route = classify_pr_route(
        _diff_for(
            "domains/internet-finance/a.md",
            "domains/internet-finance/b.md",
            "domains/health/c.md",
            "domains/entertainment/d.md",
            "domains/space-development/e.md",
        )
    )

    assert route.primary_agent == "Rio"
    assert route.required_agents == ("Rio", "Vida")
    assert route.route_kind == "escalated"
    assert len(route.required_agents) == 2


def test_branch_prefix_used_when_diff_has_no_route_path():
    route = classify_pr_route(_diff_for("inbox/archive/source.md"), branch="vida/research-glp1")

    assert route.primary_agent == "Vida"
    assert route.required_agents == ("Vida",)
    assert route.route_kind == "single"


def test_unknown_route_falls_back_to_leo():
    route = classify_pr_route(_diff_for("docs/readme.md"), branch="misc/update")

    assert route.primary_agent == "Leo"
    assert route.required_agents == ("Leo",)
    assert route.route_kind == "fallback"
    assert route.fallback is True


def test_routing_is_deterministic_for_repeated_inputs():
    diff = _diff_for(
        ("domains/health/agent-care.md", "+AI systems and health medicine review"),
        ("domains/ai-systems/care-agent.md", "+clinical model behavior"),
    )
    first = classify_pr_route(diff)

    for _ in range(100):
        assert classify_pr_route(diff) == first


def test_agent_order_is_stable():
    assert AGENT_ORDER == ("Leo", "Theseus", "Rio", "Vida", "Clay", "Astra")
