Files
FabledCurator/tests/test_api_downloads.py
T
bvandeusen 2358cedf3e feat(dashboards): scheduler health strip, failing-source rollup, 24h activity sparkline, credential staleness nudge
D1 scheduler visibility: AppSetting last-tick stamp on every Beat tick +
GET /api/sources/schedule-status (last_tick_at/next_due_at/due_now/auto_sources)
+ SchedulerStatusBar on the Subscriptions tab (re-polled every 30s).

D2 failing-source rollup: ?failing=true on the sources list + FailingSourcesCard
on Downloads with per-source and bulk "retry" (re-runs the feed via /check).

D3 activity sparkline: GET /api/downloads/activity hourly buckets + CSS bar
chart by the stat chips (failures stacked in error color); refreshes on live poll.

D4 credential staleness: surface last_verified age + "re-verify recommended"
warning past 30d; also fixes the dead last_verified_at field-name mismatch so
the verification row renders at all.

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

149 lines
4.4 KiB
Python

import pytest
from backend.app import create_app
from backend.app.models import Artist, DownloadEvent, Source
pytestmark = pytest.mark.integration
@pytest.fixture
async def app():
return create_app()
@pytest.fixture
async def client(app):
async with app.test_client() as c:
yield c
@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