from types import SimpleNamespace

from src.agents.diagnostician_agent import (
    _build_degraded_description,
    _guess_category_from_snapshot,
)


def test_degraded_fallback_preserves_docker_memory_alertname() -> None:
    snapshot = SimpleNamespace(
        alert_info={
            "alert_name": "DockerContainerMemoryLimitPressure",
            "labels": {
                "alertname": "DockerContainerMemoryLimitPressure",
                "layer": "docker",
                "container_name": "gitea",
                "host": "110",
            },
        },
        evidence_summary="容器 gitea 記憶體超過 limit 85%",
    )

    category = _guess_category_from_snapshot(snapshot)
    description = _build_degraded_description(snapshot, "step_timeout", category)

    assert category == "DockerContainerMemoryLimitPressure"
    assert "KubePodOOM" not in description
    assert "保留原始告警: DockerContainerMemoryLimitPressure" in description
    assert "target=gitea" in description
    assert "host=110" in description


def test_degraded_fallback_preserves_gitea_memory_pressure() -> None:
    snapshot = SimpleNamespace(
        alert_info={
            "alert_name": "GiteaMemoryPressure",
            "labels": {
                "alertname": "GiteaMemoryPressure",
                "name": "gitea",
                "host": "110",
            },
        },
        evidence_summary="Gitea 記憶體工作集 > 85% limit",
    )

    assert _guess_category_from_snapshot(snapshot) == "GiteaMemoryPressure"


def test_degraded_fallback_uses_generic_memory_pressure_without_alertname() -> None:
    snapshot = SimpleNamespace(
        alert_info={"labels": {"layer": "docker", "container_name": "momo-pro-system"}},
        evidence_summary="container_name=momo-pro-system layer=docker memory limit pressure",
    )

    assert _guess_category_from_snapshot(snapshot) == "DockerContainerMemoryPressure"


def test_degraded_fallback_handles_chinese_memory_summary_without_alertname() -> None:
    snapshot = SimpleNamespace(
        alert_info={"labels": {"layer": "docker", "container_name": "gitea"}},
        evidence_summary="gitea 容器記憶體超過 limit 85%",
    )

    assert _guess_category_from_snapshot(snapshot) == "DockerContainerMemoryPressure"


def test_degraded_fallback_only_uses_kube_oom_for_k8s_oom_context() -> None:
    snapshot = SimpleNamespace(
        alert_info={"labels": {"pod": "awoooi-api-123"}},
        evidence_summary="Kubernetes pod terminated with OOMKilled",
    )

    assert _guess_category_from_snapshot(snapshot) == "KubePodOOM"
