from datetime import UTC, datetime

from src.models.incident import Incident, Severity, Signal
from src.services.decision_manager import _incident_alertname_for_dedup


def test_telegram_dedup_alertname_uses_signal_not_missing_title() -> None:
    incident = Incident(
        incident_id="INC-TEST",
        severity=Severity.P3,
        signals=[
            Signal(
                alert_name="FallbackAlertName",
                severity=Severity.P3,
                source="alertmanager",
                fired_at=datetime.now(UTC),
                labels={"alertname": "DockerContainerMemoryLimitPressure"},
            )
        ],
        affected_services=["node-exporter-110"],
    )

    assert not hasattr(incident, "title")
    assert _incident_alertname_for_dedup(incident) == "DockerContainerMemoryLimitPressure"


def test_telegram_dedup_alertname_falls_back_to_incident_id() -> None:
    incident = Incident(
        incident_id="INC-NO-SIGNAL",
        severity=Severity.P3,
        signals=[],
        affected_services=[],
    )

    assert _incident_alertname_for_dedup(incident) == "INC-NO-SIGNAL"
