"""Tests for the CampaignRuntime startup recovery interface."""

import asyncio
from types import SimpleNamespace

from agenteval.evaluation import campaign_runner
from agenteval.evaluation.campaign_runner import CampaignRuntime
from agenteval.models import CampaignStatus


async def test_runtime_recovery_repairs_before_relaunch(monkeypatch):
    calls: list[str] = []

    class FakeSession:
        def close(self):
            calls.append("close")

    class FakeRuns:
        def __init__(self, _session):
            pass

        def mark_orphans_failed(self):
            calls.append("runs")
            return 2

    class FakeCampaigns:
        def __init__(self, _session):
            pass

        def list_all(self):
            calls.append("campaigns")
            return [SimpleNamespace(id="campaign-1", status=CampaignStatus.RUNNING)]

    async def fake_loop(campaign_id, cancel, **kwargs):
        calls.append(f"launch:{campaign_id}")

    monkeypatch.setattr(campaign_runner, "RunRepository", FakeRuns)
    monkeypatch.setattr(campaign_runner, "CampaignRepository", FakeCampaigns)
    monkeypatch.setattr(campaign_runner, "_run_campaign_loop", fake_loop)
    runtime = CampaignRuntime(session_factory=FakeSession)

    summary = runtime.recover()
    await asyncio.sleep(0)
    await runtime.shutdown()

    assert summary.interrupted_runs == 2
    assert summary.resumed_campaigns == 1
    assert calls[:3] == ["runs", "campaigns", "close"]
    assert "launch:campaign-1" in calls
