from grantflow.api import runtime_state
from grantflow.core.config import config

from grantflow.tests.queue_dispatch_helpers import client


def test_generate_returns_503_when_redis_queue_is_unavailable(monkeypatch):
    monkeypatch.setattr(config.job_runner, "mode", "redis_queue")
    monkeypatch.setattr(runtime_state.JOB_RUNNER, "submit", lambda *args, **kwargs: False)

    response = client.post(
        "/generate",
        json={
            "donor_id": "usaid",
            "input_context": {"project": "Redis queue unavailable", "country": "Kenya"},
            "llm_mode": False,
            "hitl_enabled": False,
        },
    )
    assert response.status_code == 503
    assert response.json()["detail"]["code"] == "job_queue_full"
