import pytest

from backend.app.celery_app import celery

pytestmark = pytest.mark.integration


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


@pytest.mark.asyncio
async def test_trigger_thumbnail_backfill_returns_counts(client):
    """The API runs the scan synchronously now and returns
    {scanned, enqueued, ok, regenerated} — operator-flagged 2026-06-01:
    the previous fire-and-forget shape returned only a celery_task_id,
    so the admin UI couldn't show whether backfill found 0 or 5000
    candidates. \"Found nothing\" was indistinguishable from \"the
    worker isn't picking up the task.\""""
    r = await client.post("/api/thumbnails/backfill")
    assert r.status_code == 200
    body = await r.get_json()
    assert set(body) == {"scanned", "enqueued", "ok", "regenerated"}
    for key in ("scanned", "enqueued", "ok", "regenerated"):
        assert isinstance(body[key], int)
