from datetime import UTC, datetime

from worker_watcher.agent import AgentOutbox, RemoteAgentClient
from worker_watcher.models import AgentRegistration


def registration() -> AgentRegistration:
    return AgentRegistration("agent.local.alice", "Alice laptop", "0.1", "local", "alice-laptop", "python", ("heartbeat",))


def test_agent_outbox_retains_event_when_watcher_unreachable(tmp_path) -> None:
    outbox = AgentOutbox(tmp_path / "agent.sqlite")
    client = RemoteAgentClient("http://127.0.0.1:1", registration(), outbox, timeout_seconds=1)
    try:
        assert not client.emit("worker.alpha", "ai.pipeline.task", "heartbeat",
                               {"display_name": "AI pipeline", "stale_after_seconds": 60},
                               datetime(2026, 8, 1, 12, 0, tzinfo=UTC))
        assert outbox.size() == 1
        assert outbox.pending()[0]["attempts"] == 1
    finally:
        outbox.close()
