feat(fc2c-i): tag directory service and /api/tags/directory

This commit is contained in:
2026-05-15 15:50:41 -04:00
parent a74b313596
commit 8484cb9aaa
4 changed files with 247 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
import pytest
from backend.app import create_app
from backend.app.models import Tag, TagKind
pytestmark = pytest.mark.integration
@pytest.fixture
async def client():
app = create_app()
async with app.test_client() as c:
yield c
@pytest.mark.asyncio
async def test_directory_endpoint(client, db):
db.add(Tag(name="api_tag", kind=TagKind.general))
await db.flush()
await db.commit()
resp = await client.get("/api/tags/directory?limit=10")
assert resp.status_code == 200
body = await resp.get_json()
assert "cards" in body and "next_cursor" in body
@pytest.mark.asyncio
async def test_directory_bad_limit(client):
resp = await client.get("/api/tags/directory?limit=nan")
assert resp.status_code == 400
+72
View File
@@ -0,0 +1,72 @@
import pytest
from backend.app.models import ImageRecord, Tag, TagKind
from backend.app.models.tag import image_tag
from backend.app.services.tag_directory_service import TagDirectoryService
pytestmark = pytest.mark.integration
async def _img(db, i):
r = ImageRecord(
path=f"/images/td/{i}.jpg", sha256=f"d{i:063d}",
size_bytes=1, mime="image/jpeg", width=1, height=1,
origin="imported_filesystem", integrity_status="unknown",
)
db.add(r)
await db.flush()
return r
@pytest.mark.asyncio
async def test_directory_lists_tags_with_counts_and_previews(db):
t = Tag(name="sunset", kind=TagKind.general)
db.add(t)
await db.flush()
for i in range(4):
img = await _img(db, i)
await db.execute(image_tag.insert().values(
image_record_id=img.id, tag_id=t.id, source="manual"))
await db.flush()
svc = TagDirectoryService(db)
page = await svc.list_tags(kind=None, q=None, cursor=None, limit=10)
card = next(c for c in page.cards if c["name"] == "sunset")
assert card["image_count"] == 4
assert len(card["preview_thumbnails"]) == 3
assert card["preview_thumbnails"][0].startswith("/images/thumbs/")
@pytest.mark.asyncio
async def test_directory_kind_filter_and_search(db):
db.add_all([
Tag(name="artist_a", kind=TagKind.artist),
Tag(name="char_b", kind=TagKind.character),
Tag(name="char_zoom", kind=TagKind.character),
])
await db.flush()
svc = TagDirectoryService(db)
only_char = await svc.list_tags(kind="character", q=None, cursor=None, limit=10)
assert {c["name"] for c in only_char.cards} == {"char_b", "char_zoom"}
searched = await svc.list_tags(kind=None, q="zoom", cursor=None, limit=10)
assert {c["name"] for c in searched.cards} == {"char_zoom"}
@pytest.mark.asyncio
async def test_directory_cursor_pagination(db):
for i in range(5):
db.add(Tag(name=f"t{i:02d}", kind=TagKind.general))
await db.flush()
svc = TagDirectoryService(db)
first = await svc.list_tags(kind=None, q=None, cursor=None, limit=2)
assert len(first.cards) == 2
assert first.next_cursor is not None
second = await svc.list_tags(kind=None, q=None, cursor=first.next_cursor, limit=2)
assert len(second.cards) == 2
assert {c["id"] for c in first.cards}.isdisjoint({c["id"] for c in second.cards})
@pytest.mark.asyncio
async def test_directory_rejects_bad_limit(db):
svc = TagDirectoryService(db)
with pytest.raises(ValueError):
await svc.list_tags(kind=None, q=None, cursor=None, limit=0)