Replace the committed channels/<slug>.json files as the runtime source of truth with a local SQLite database at the repo root (clipwright.db), overridable via CLIPWRIGHT_DB. Existing JSON profiles are imported once on first bootstrap, then the store is authoritative. - add clipwright/storage/channel_profiles.py (stdlib sqlite3, WAL + lock) - add Profile/TitleRules/ThumbnailRules.to_dict() for round-tripping - load_profile/list_profiles read the store; channels_dir stays a file seam - /api/channels CRUD plus import/export endpoints - untrack channels/*.json, ignore the DB files - seed a temp DB in tests/conftest.py
204 lines
7 KiB
Python
204 lines
7 KiB
Python
"""GET/POST/PUT/DELETE /api/channels (PLAN #1, dashboard profile selector).
|
|
|
|
Pins the response shape ``{"channels": [{"slug", "name"}]}``, that the seeded
|
|
profiles show up with their display names, and the full CRUD + import/export
|
|
surface backed by the SQLite store (``tests/conftest.py`` seeds a temp DB).
|
|
"""
|
|
import asyncio
|
|
import json
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
app_module = pytest.importorskip("clipwright.api.app")
|
|
|
|
|
|
def _request(method, path, **kwargs):
|
|
async def _do():
|
|
transport = httpx.ASGITransport(app=app_module.app)
|
|
async with httpx.AsyncClient(transport=transport,
|
|
base_url="http://testserver") as client:
|
|
return await client.request(method, path, **kwargs)
|
|
return asyncio.run(_do())
|
|
|
|
|
|
def _get(path):
|
|
return _request("GET", path)
|
|
|
|
|
|
def test_returns_seeded_profiles_with_display_names():
|
|
resp = _get("/api/channels")
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert set(data) == {"channels"}
|
|
by_slug = {item["slug"]: item["name"] for item in data["channels"]}
|
|
assert by_slug["cseea"] == "cseea"
|
|
assert by_slug["pedro"] == "Pedro Bento"
|
|
assert all(set(item) == {"slug", "name"} for item in data["channels"])
|
|
|
|
|
|
def test_list_reflects_store_changes():
|
|
created = _request(
|
|
"POST", "/api/channels",
|
|
json={"slug": "listed", "name": "Listed One"})
|
|
assert created.status_code == 200, created.text
|
|
try:
|
|
listed = {i["slug"]: i["name"] for i in _get("/api/channels").json()["channels"]}
|
|
assert listed["listed"] == "Listed One"
|
|
finally:
|
|
_request("DELETE", "/api/channels/listed")
|
|
listed = {i["slug"] for i in _get("/api/channels").json()["channels"]}
|
|
assert "listed" not in listed
|
|
|
|
|
|
# --- read one ---------------------------------------------------------------
|
|
|
|
def test_get_one_profile_returns_full_payload():
|
|
resp = _get("/api/channels/cseea")
|
|
assert resp.status_code == 200
|
|
body = resp.json()
|
|
assert body["slug"] == "cseea"
|
|
assert body["tone"] == "nostalgic and calm; curious, never hype"
|
|
assert body["title_rules"]["mobile_chars"] == 38
|
|
assert body["thumbnail"]["palette"] == [
|
|
"mossy green", "cobblestone grey", "torch orange"]
|
|
|
|
|
|
def test_get_missing_profile_is_404():
|
|
resp = _get("/api/channels/does-not-exist")
|
|
assert resp.status_code == 404
|
|
assert "not found" in resp.json()["detail"]
|
|
|
|
|
|
# --- create -----------------------------------------------------------------
|
|
|
|
def test_create_profile_returns_payload():
|
|
body = {"slug": "created", "name": "Created", "tone": "calm"}
|
|
resp = _request("POST", "/api/channels", json=body)
|
|
assert resp.status_code == 200, resp.text
|
|
assert resp.json()["slug"] == "created"
|
|
assert resp.json()["tone"] == "calm"
|
|
_request("DELETE", "/api/channels/created")
|
|
|
|
|
|
def test_create_duplicate_is_409():
|
|
resp = _request("POST", "/api/channels", json={"slug": "cseea", "name": "Dup"})
|
|
assert resp.status_code == 409
|
|
assert "already exists" in resp.json()["detail"]
|
|
|
|
|
|
def test_create_without_slug_is_400():
|
|
resp = _request("POST", "/api/channels", json={"name": "No Slug"})
|
|
assert resp.status_code == 400
|
|
assert "slug is required" in resp.json()["detail"]
|
|
|
|
|
|
def test_create_with_bad_slug_is_400():
|
|
resp = _request("POST", "/api/channels", json={"slug": "../evil", "name": "Bad"})
|
|
assert resp.status_code == 400
|
|
|
|
|
|
# --- update -----------------------------------------------------------------
|
|
|
|
def test_put_updates_existing_profile():
|
|
_request("PUT", "/api/channels/updateme", json={"name": "Before", "tone": "a"})
|
|
try:
|
|
resp = _request(
|
|
"PUT", "/api/channels/updateme",
|
|
json={"name": "After", "tone": "b"})
|
|
assert resp.status_code == 200, resp.text
|
|
assert resp.json()["name"] == "After"
|
|
assert _get("/api/channels/updateme").json()["tone"] == "b"
|
|
finally:
|
|
_request("DELETE", "/api/channels/updateme")
|
|
|
|
|
|
def test_put_ignores_body_slug():
|
|
try:
|
|
resp = _request(
|
|
"PUT", "/api/channels/pathed",
|
|
json={"slug": "other", "name": "Pathed"})
|
|
assert resp.status_code == 200, resp.text
|
|
assert resp.json()["slug"] == "pathed"
|
|
assert _get("/api/channels/other").status_code == 404
|
|
finally:
|
|
_request("DELETE", "/api/channels/pathed")
|
|
|
|
|
|
# --- delete -----------------------------------------------------------------
|
|
|
|
def test_delete_removes_profile():
|
|
_request("POST", "/api/channels", json={"slug": "deleteme", "name": "D"})
|
|
resp = _request("DELETE", "/api/channels/deleteme")
|
|
assert resp.status_code == 200
|
|
assert resp.json() == {"deleted": True}
|
|
assert _get("/api/channels/deleteme").status_code == 404
|
|
|
|
|
|
def test_delete_missing_is_404():
|
|
resp = _request("DELETE", "/api/channels/nope-nope")
|
|
assert resp.status_code == 404
|
|
|
|
|
|
# --- export / import --------------------------------------------------------
|
|
|
|
def test_export_all_shape_and_headers():
|
|
resp = _get("/api/channels/export")
|
|
assert resp.status_code == 200
|
|
assert resp.headers["content-disposition"] == (
|
|
'attachment; filename="channel-profiles.json"')
|
|
body = resp.json()
|
|
assert body["version"] == 1
|
|
slugs = {p["slug"] for p in body["profiles"]}
|
|
assert {"cseea", "pedro"} <= slugs
|
|
assert all(p["slug"] for p in body["profiles"])
|
|
|
|
|
|
def test_export_single_profile_and_missing_404():
|
|
resp = _get("/api/channels/export?slug=cseea")
|
|
assert resp.status_code == 200
|
|
assert resp.headers["content-disposition"] == (
|
|
'attachment; filename="channel-profile-cseea.json"')
|
|
body = resp.json()
|
|
assert [p["slug"] for p in body["profiles"]] == ["cseea"]
|
|
|
|
missing = _get("/api/channels/export?slug=absent")
|
|
assert missing.status_code == 404
|
|
|
|
|
|
def test_import_valid_payload():
|
|
payload = {"version": 1, "profiles": [
|
|
{"slug": "imported", "name": "Imported", "tone": "fresh"},
|
|
]}
|
|
resp = _request("POST", "/api/channels/import", json=payload)
|
|
assert resp.status_code == 200, resp.text
|
|
assert resp.json() == {"imported": 1, "failed": []}
|
|
try:
|
|
assert _get("/api/channels/imported").json()["tone"] == "fresh"
|
|
finally:
|
|
_request("DELETE", "/api/channels/imported")
|
|
|
|
|
|
def test_import_partial_failure_does_not_abort():
|
|
payload = [
|
|
{"slug": "good-import", "name": "Good"},
|
|
{"name": "missing slug"},
|
|
{"slug": "bad-enum", "title_rules": {"required_signal": "profit"}},
|
|
]
|
|
resp = _request("POST", "/api/channels/import", json=payload)
|
|
assert resp.status_code == 200, resp.text
|
|
result = resp.json()
|
|
assert result["imported"] == 1
|
|
assert [f["slug"] for f in result["failed"]] == ["", "bad-enum"]
|
|
try:
|
|
assert _get("/api/channels/good-import").status_code == 200
|
|
finally:
|
|
_request("DELETE", "/api/channels/good-import")
|
|
|
|
|
|
def test_import_non_object_payload_is_400():
|
|
resp = _request(
|
|
"POST", "/api/channels/import",
|
|
content=json.dumps("not-a-profile"),
|
|
headers={"content-type": "application/json"})
|
|
assert resp.status_code == 400
|