"""Smoke test — celery runs the ping task in eager mode (no broker required)."""

import pytest

from backend.app.celery_app import celery
from backend.app.tasks.smoke import ping


@pytest.fixture(autouse=True)
def celery_eager():
    celery.conf.task_always_eager = True
    celery.conf.task_eager_propagates = True
    yield
    celery.conf.task_always_eager = False


def test_ping_returns_pong():
    result = ping.delay()
    assert result.get(timeout=1) == "pong"


def test_ping_with_value():
    result = ping.delay("hello")
    assert result.get(timeout=1) == "hello"


def test_broker_resilience_configured():
    # Workers must ride out a transient broker outage (swarm overlay blip after
    # a redeploy left Redis briefly unreachable) instead of crash-looping on the
    # initial connect and needing a manual Redis reset (2026-06-24).
    conf = celery.conf
    assert conf.broker_connection_retry_on_startup is True
    assert conf.broker_connection_max_retries is None  # retry forever
    opts = conf.broker_transport_options
    assert opts["socket_keepalive"] is True
    assert opts["socket_connect_timeout"] == 5
    assert opts["health_check_interval"] == 30
    assert conf.redis_retry_on_timeout is True
