e597a9300e
trigger enqueues a Celery scan_directory task and returns 202 with the celery task id (the actual scan happens asynchronously; status surfaces via /api/import/status and /api/system/stats). list_tasks supports status filter and cursor pagination. retry-failed resets failed tasks to queued and re-enqueues. clear-completed deletes finished tasks older than the supplied age (used by the Settings UI's cleanup control). mode='deep' is explicitly rejected with 400 until FC-2d ships it; the frontend's UI in FC-2a only sends 'quick'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
91 lines
2.6 KiB
Python
91 lines
2.6 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
|
|
import pytest
|
|
|
|
from backend.app import create_app
|
|
from backend.app.celery_app import celery
|
|
from backend.app.models import ImportBatch, ImportTask
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def eager():
|
|
celery.conf.task_always_eager = True
|
|
yield
|
|
celery.conf.task_always_eager = False
|
|
|
|
|
|
@pytest.fixture
|
|
async def app():
|
|
return create_app()
|
|
|
|
|
|
@pytest.fixture
|
|
async def client(app):
|
|
async with app.test_client() as c:
|
|
yield c
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_trigger_rejects_unknown_mode(client):
|
|
resp = await client.post("/api/import/trigger", json={"mode": "deep"})
|
|
assert resp.status_code == 400
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_status_when_idle(client):
|
|
resp = await client.get("/api/import/status")
|
|
body = await resp.get_json()
|
|
assert body["active_batch"] is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_tasks_with_status_filter(client, db):
|
|
batch = ImportBatch(triggered_by="manual", source_path="/import", scan_mode="quick")
|
|
db.add(batch)
|
|
await db.flush()
|
|
for status in ("complete", "failed", "complete"):
|
|
db.add(ImportTask(
|
|
batch_id=batch.id, source_path="/x", task_type="media", status=status,
|
|
finished_at=datetime.now(timezone.utc),
|
|
))
|
|
await db.commit()
|
|
|
|
resp = await client.get("/api/import/tasks?status=failed")
|
|
body = await resp.get_json()
|
|
assert len(body["tasks"]) == 1
|
|
assert body["tasks"][0]["status"] == "failed"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_retry_failed(client, db):
|
|
batch = ImportBatch(triggered_by="manual", source_path="/import", scan_mode="quick")
|
|
db.add(batch)
|
|
await db.flush()
|
|
db.add(ImportTask(batch_id=batch.id, source_path="/x", task_type="media", status="failed"))
|
|
await db.commit()
|
|
|
|
resp = await client.post("/api/import/retry-failed")
|
|
body = await resp.get_json()
|
|
assert body["retried"] == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_clear_completed(client, db):
|
|
batch = ImportBatch(triggered_by="manual", source_path="/import", scan_mode="quick")
|
|
db.add(batch)
|
|
await db.flush()
|
|
long_ago = datetime.now(timezone.utc) - timedelta(days=30)
|
|
db.add(ImportTask(
|
|
batch_id=batch.id, source_path="/x", task_type="media",
|
|
status="complete", finished_at=long_ago,
|
|
))
|
|
db.add(ImportTask(
|
|
batch_id=batch.id, source_path="/y", task_type="media",
|
|
status="complete", finished_at=datetime.now(timezone.utc),
|
|
))
|
|
await db.commit()
|
|
|
|
resp = await client.post("/api/import/clear-completed", json={"age_days": 7})
|
|
body = await resp.get_json()
|
|
assert body["deleted"] == 1
|