from types import SimpleNamespace
from unittest.mock import AsyncMock

import pytest

from src.jobs.incident_lifecycle_reconciler import (
    LifecycleCandidate,
    reconcile_stuck_incidents,
)


@pytest.mark.asyncio
async def test_reconcile_stuck_incidents_resolves_strong_evidence(monkeypatch):
    service = SimpleNamespace(resolve_incident=AsyncMock(return_value=object()))

    monkeypatch.setattr(
        "src.jobs.incident_lifecycle_reconciler._fetch_candidates",
        AsyncMock(
            return_value=[
                LifecycleCandidate(
                    incident_id="INC-EXEC-SUCCESS",
                    resolution_type="auto_repair",
                    reason="approval_execution_success",
                ),
                LifecycleCandidate(
                    incident_id="INC-TIMEOUT",
                    resolution_type="timeout",
                    reason="approval_expired",
                ),
            ]
        ),
    )
    monkeypatch.setattr(
        "src.services.incident_service.get_incident_service",
        lambda: service,
    )

    resolved, errors = await reconcile_stuck_incidents(limit=2)

    assert (resolved, errors) == (2, 0)
    assert service.resolve_incident.await_args_list[0].args == (
        "INC-EXEC-SUCCESS",
    )
    assert service.resolve_incident.await_args_list[0].kwargs == {
        "resolution_type": "auto_repair",
        "emit_postmortem": False,
    }
    assert service.resolve_incident.await_args_list[1].args == ("INC-TIMEOUT",)
    assert service.resolve_incident.await_args_list[1].kwargs == {
        "resolution_type": "timeout",
        "emit_postmortem": False,
    }
