- MeTubeClient encapsulates all MeTube HTTP/Socket.IO calls (verified against
the real MeTube source: /add returns no job id, GET /history gives a queue
snapshot for reconciliation, filenames arrive already relative, percent is
a 0-100 float)
- download_jobs table + service: request/dedup active downloads, apply live
Socket.IO events (added/updated/completed/canceled/cleared) matched by
canonical YouTube URL, safe relative-path -> public media URL construction
- Reconciliation on startup against MeTube's live queue/done state (section 19):
non-terminal jobs recovered where possible, else marked "unknown"; already
completed jobs are left untouched
- POST/GET /api/videos/{id}/download(-status), recheck-local; feed/video
detail now report real local availability instead of a stub
- Frontend: download button with live status polling (queued/downloading %/
postprocessing/completed/failed+retry), local <video> playback with
YouTube fallback on playback error
- health.py now delegates to MeTubeClient (single place for MeTube calls)
26 new backend tests (63 total). Verified live: Socket.IO connects
successfully to the real MeTube instance on deploy.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
120 lines
4.4 KiB
Python
120 lines
4.4 KiB
Python
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.core.auth_dependency import require_session
|
|
from app.db import get_db
|
|
from app.main import app
|
|
from app.models.channel import Channel
|
|
|
|
|
|
@pytest.fixture
|
|
def client(db_session):
|
|
def _get_db_override():
|
|
yield db_session
|
|
|
|
app.dependency_overrides[get_db] = _get_db_override
|
|
app.dependency_overrides[require_session] = lambda: None
|
|
# Deliberately not using `with TestClient(app)`: that runs the app's
|
|
# lifespan, which would try to reach the real MeTube instance and DB.
|
|
yield TestClient(app)
|
|
del app.dependency_overrides[get_db]
|
|
del app.dependency_overrides[require_session]
|
|
|
|
|
|
def _create_channel(db_session, youtube_channel_id="chanA", title="Channel A"):
|
|
channel = Channel(youtube_channel_id=youtube_channel_id, title=title, subscribed=True)
|
|
db_session.add(channel)
|
|
db_session.commit()
|
|
db_session.refresh(channel)
|
|
return channel
|
|
|
|
|
|
def test_category_crud(client):
|
|
created = client.post("/api/categories", json={"name": "Linux"}).json()
|
|
assert created["name"] == "Linux"
|
|
assert created["slug"] == "linux"
|
|
assert created["channel_count"] == 0
|
|
|
|
listed = client.get("/api/categories").json()
|
|
assert len(listed) == 1
|
|
|
|
updated = client.patch(f"/api/categories/{created['id']}", json={"name": "Linux/BSD"}).json()
|
|
assert updated["name"] == "Linux/BSD"
|
|
|
|
resp = client.delete(f"/api/categories/{created['id']}")
|
|
assert resp.status_code == 204
|
|
assert client.get("/api/categories").json() == []
|
|
|
|
|
|
def test_category_name_must_be_unique(client):
|
|
client.post("/api/categories", json={"name": "Шахматы"})
|
|
resp = client.post("/api/categories", json={"name": "шахматы"})
|
|
assert resp.status_code == 409
|
|
|
|
|
|
def test_cyrillic_slug_transliterated(client):
|
|
created = client.post("/api/categories", json={"name": "Научпоп"}).json()
|
|
assert created["slug"] == "nauchpop"
|
|
|
|
|
|
def test_delete_category_does_not_delete_channel(client, db_session):
|
|
channel = _create_channel(db_session)
|
|
category = client.post("/api/categories", json={"name": "IT"}).json()
|
|
|
|
client.put(f"/api/channels/{channel.id}/categories", json={"category_ids": [category["id"]]})
|
|
client.delete(f"/api/categories/{category['id']}")
|
|
|
|
resp = client.get(f"/api/channels/{channel.id}").json()
|
|
assert resp["id"] == channel.id
|
|
assert resp["category_ids"] == []
|
|
|
|
|
|
def test_channel_can_belong_to_multiple_categories(client, db_session):
|
|
channel = _create_channel(db_session)
|
|
cat1 = client.post("/api/categories", json={"name": "Linux"}).json()
|
|
cat2 = client.post("/api/categories", json={"name": "IT"}).json()
|
|
|
|
resp = client.put(
|
|
f"/api/channels/{channel.id}/categories",
|
|
json={"category_ids": [cat1["id"], cat2["id"]]},
|
|
).json()
|
|
|
|
assert sorted(resp["category_ids"]) == sorted([cat1["id"], cat2["id"]])
|
|
|
|
|
|
def test_assign_unknown_category_rejected(client, db_session):
|
|
channel = _create_channel(db_session)
|
|
resp = client.put(f"/api/channels/{channel.id}/categories", json={"category_ids": [9999]})
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_channels_filter_by_category_and_uncategorized(client, db_session):
|
|
channel_a = _create_channel(db_session, "chanA", "Channel A")
|
|
channel_b = _create_channel(db_session, "chanB", "Channel B")
|
|
category = client.post("/api/categories", json={"name": "Linux"}).json()
|
|
client.put(f"/api/channels/{channel_a.id}/categories", json={"category_ids": [category["id"]]})
|
|
|
|
in_category = client.get(f"/api/channels?category_id={category['id']}").json()
|
|
assert [c["id"] for c in in_category] == [channel_a.id]
|
|
|
|
uncategorized = client.get("/api/channels?uncategorized=true").json()
|
|
assert [c["id"] for c in uncategorized] == [channel_b.id]
|
|
|
|
|
|
def test_reorder_categories(client):
|
|
cat1 = client.post("/api/categories", json={"name": "A"}).json()
|
|
cat2 = client.post("/api/categories", json={"name": "B"}).json()
|
|
cat3 = client.post("/api/categories", json={"name": "C"}).json()
|
|
|
|
reordered = client.post(
|
|
"/api/categories/reorder",
|
|
json={"category_ids": [cat3["id"], cat1["id"], cat2["id"]]},
|
|
).json()
|
|
|
|
assert [c["id"] for c in reordered] == [cat3["id"], cat1["id"], cat2["id"]]
|
|
|
|
|
|
def test_reorder_rejects_mismatched_ids(client):
|
|
client.post("/api/categories", json={"name": "A"})
|
|
resp = client.post("/api/categories/reorder", json={"category_ids": [9999]})
|
|
assert resp.status_code == 400
|