Files
FabledCurator/tests/test_api_downloads.py
T
bvandeusen 9322c984fd feat(subs-hub): collapse /credentials + /downloads into /subscriptions hub with three GS-style subtabs
Replaces the three top-level routes with a single `/subscriptions` parent
owning the whole download-pipeline domain. Internal tab state via `?tab=`
query param, mirroring ArtistView's pattern. TopNav auto-drops the two
removed entries (route-driven via meta.title). Bookmark-safe redirects
from `/credentials` and `/downloads` route into the appropriate subtab.

**Subtab 1 — Subscriptions (default).** Carries over the existing
artist-grouped expandable table; adds (a) status filter dropdown, (b)
bulk-select column with Enable/Disable/Delete-all actions, (c) GS-style
color-coded `PlatformChip` per distinct platform in the collapsed row.
Reuses SourceRow, SourceHealthDot, SourceFormDialog, ArtistCreateDialog.

**Subtab 2 — Downloads.** Full GS dashboard. Five colored stat chips up
top (Queued/Running/Completed/Failed/Skipped, sourced from new
`GET /api/downloads/stats?window_hours=`). Popover-style filter UI
(Status/Source/FromDate/ToDate) with active-filter pills below.
Maintenance menu wraps existing /api/import/retry-failed and
/api/import/clear-stuck endpoints; Export-failed-logs item disabled with
a "v2" tooltip. Per-row Retry preserved via existing DownloadEventRow.

**Subtab 3 — Settings.** Four sections: ExtensionKeyBar (top), GS-style
per-platform CredentialCard grid (md=6 v-row/v-col, dashed border if
unset / accent border if set, expandable how-to panel), Downloader card
(rate limit, validate_files), Schedule defaults card (default interval,
event retention, failure warning threshold). The Downloader and Schedule
sections were extracted out of components/settings/ImportFiltersForm.vue
— SettingsView's Import tab now owns only image-import filters.

**Backend:** new `GET /api/downloads/stats` returns
{pending, running, ok, error, skipped} count grouped by status over the
configurable window. Status keys stay raw from the ENUM; UI does the
display-label mapping. Two integration tests pin the response shape +
window_hours validation.

**Util:** `frontend/src/utils/platformColor.js` — single source of truth
for the six platforms' color + icon + label, mirroring GS's palette
(patreon=red mdi-patreon, subscribestar=amber mdi-star,
hentaifoundry=purple mdi-palette, discord=indigo mdi-discord,
pixiv=blue mdi-alpha-p-box, deviantart=green mdi-deviantart). Unknown
platform falls back to grey + mdi-web.

Deferred (explicit non-goals): subscription import/export, "Trigger Due
Now" scheduler-tick button (needs new backend endpoint), Export Failed
Logs CSV dump.
2026-05-27 13:02:24 -04:00

129 lines
3.7 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