Files
FabledCurator/tests/test_api_downloads.py
bvandeusen 77e9859da3 fix(downloads): rewire MaintenanceMenu to the downloads pipeline
The maintenance dropdown in Subscriptions → Downloads was wired to the
filesystem-import pipeline (POST /api/import/retry-failed +
POST /api/import/clear-stuck) — the subtitles even said so ("Re-enqueue
every failed import task"), but it was contextually misplaced. From the
Downloads view "Retry failed" queued nothing the operator could see
because the action operated on import_task rows, not download_event
rows. Import-pipeline maintenance is already reachable from Settings →
Imports (ImportTaskList.vue), so removing the import wiring loses
nothing.

Rewired:
- "Retry failed" → bulk-retries the failing-sources list, same loop as
  FailingSourcesCard's RETRY ALL (sourcesStore.checkNow per source).
  Subtitle now matches: "Re-queue every currently failing source".
- "Force recovery sweep" → triggers recover_stalled_download_events on
  demand via a new POST /api/downloads/recover-stalled endpoint. The
  sweep also runs every 5 min on Beat; this is the manual fallback so
  the operator doesn't have to wait for the next tick to clear newly
  stranded events.

MaintenanceMenu is now stateless — emits retry-failed and recover-
stalled. DownloadsTab owns the handlers (reuses the existing
onRetryAll; new onRecoverStalled with a delayed refresh so swept rows
land in the failing rollup).

Operator-flagged 2026-05-29 — "the retry failed button in the
maintenance dropdown doesn't appear to queue anything but manual
requeues works."

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-30 00:59:05 -04:00

157 lines
4.9 KiB
Python

import pytest
from backend.app.models import Artist, DownloadEvent, Source
pytestmark = pytest.mark.integration
@pytest.fixture
async def seed(db):
artist = Artist(name="Alice", slug="alice")
db.add(artist)
await db.flush()
source = Source(
artist_id=artist.id, platform="patreon",
url="https://patreon.com/alice", enabled=True,
config_overrides={},
)
db.add(source)
await db.flush()
events = [
DownloadEvent(
source_id=source.id, status="ok",
files_count=3, bytes_downloaded=100000,
metadata_={
"run_stats": {"downloaded_count": 3, "skipped_count": 1, "quarantined_count": 0},
"duration_seconds": 10.5, "error_type": None,
},
),
DownloadEvent(
source_id=source.id, status="error", error="auth failed",
metadata_={
"run_stats": {"downloaded_count": 0, "skipped_count": 0, "quarantined_count": 0},
"duration_seconds": 2.1, "error_type": "auth_error",
},
),
]
db.add_all(events)
await db.commit()
return artist, source, events
@pytest.mark.asyncio
async def test_list_returns_newest_first(client, seed):
resp = await client.get("/api/downloads")
assert resp.status_code == 200
body = await resp.get_json()
assert len(body) == 2
assert body[0]["id"] > body[1]["id"]
assert body[0]["summary"]["error_type"] == "auth_error"
assert body[0]["artist_name"] == "Alice"
@pytest.mark.asyncio
async def test_list_filter_by_status(client, seed):
resp = await client.get("/api/downloads?status=ok")
body = await resp.get_json()
assert all(r["status"] == "ok" for r in body)
@pytest.mark.asyncio
async def test_list_filter_by_source_id(client, seed):
_, source, _ = seed
resp = await client.get(f"/api/downloads?source_id={source.id}")
body = await resp.get_json()
assert all(r["source_id"] == source.id for r in body)
@pytest.mark.asyncio
async def test_list_rejects_invalid_status(client):
resp = await client.get("/api/downloads?status=bogus")
assert resp.status_code == 400
@pytest.mark.asyncio
async def test_list_before_keyset(client, seed):
_, _, events = seed
middle_id = events[0].id
resp = await client.get(f"/api/downloads?before={middle_id}")
body = await resp.get_json()
assert all(r["id"] < middle_id for r in body)
@pytest.mark.asyncio
async def test_detail_returns_full_metadata(client, seed):
_, _, events = seed
target = events[1]
resp = await client.get(f"/api/downloads/{target.id}")
assert resp.status_code == 200
body = await resp.get_json()
assert body["metadata"]["error_type"] == "auth_error"
assert body["metadata"]["duration_seconds"] == 2.1
@pytest.mark.asyncio
async def test_detail_404(client):
resp = await client.get("/api/downloads/99999")
assert resp.status_code == 404
@pytest.mark.asyncio
async def test_stats_returns_full_status_set(client, seed):
resp = await client.get("/api/downloads/stats")
assert resp.status_code == 200
body = await resp.get_json()
assert set(body) == {"pending", "running", "ok", "error", "skipped"}
assert body["ok"] == 1
assert body["error"] == 1
assert body["pending"] == 0
@pytest.mark.asyncio
async def test_stats_window_hours_rejects_out_of_range(client):
resp = await client.get("/api/downloads/stats?window_hours=0")
assert resp.status_code == 400
resp = await client.get("/api/downloads/stats?window_hours=bogus")
assert resp.status_code == 400
@pytest.mark.asyncio
async def test_activity_returns_fixed_bucket_array(client, seed):
resp = await client.get("/api/downloads/activity")
assert resp.status_code == 200
body = await resp.get_json()
assert body["hours"] == 24
assert len(body["buckets"]) == 24
assert sum(b["total"] for b in body["buckets"]) == 2
# Both seed events were created "now" → land in the newest bucket.
newest = body["buckets"][-1]
assert newest["ok"] == 1
assert newest["error"] == 1
@pytest.mark.asyncio
async def test_activity_rejects_bogus_hours(client):
resp = await client.get("/api/downloads/activity?hours=bogus")
assert resp.status_code == 400
@pytest.mark.asyncio
async def test_recover_stalled_dispatches_task(client, monkeypatch):
"""POST /api/downloads/recover-stalled returns 202 and dispatches the
recover_stalled_download_events task. The task body itself is exercised
in test_maintenance.py — this just covers the API → Celery hand-off."""
from backend.app.tasks import maintenance
called: list[tuple] = []
monkeypatch.setattr(
maintenance.recover_stalled_download_events, "delay",
lambda *a, **kw: called.append((a, kw)),
)
resp = await client.post("/api/downloads/recover-stalled")
assert resp.status_code == 202
body = await resp.get_json()
assert body == {"queued": True}
assert len(called) == 1