a444cf82d1
Every tag suggestion is a canonical DB tag now (tagging-v2 #114: heads + CCIP score EXISTING concept tags). The pre-heads apparatus for model-predicted tags that didn't exist in the DB — creates_new_tag / raw_name / via_alias, the /suggestions/alias endpoint + add_alias_and_accept, AliasPickerDialog, and the store's aliasAccept/removeAlias — was dead and is removed. The type-to-add dropdown was TWO row sources (server autocomplete + the image's ML suggestions) merged with a dedup that dropped the %-bearing suggestion row when the debounced server hit landed — the operator's "confidence % flickers then vanishes". Now it's ONE list of DB-tag matches, each annotated with the model's confidence (join by canonical_tag_id) when the tag was scored for this image. No dedup, no flicker; picking a suggested tag still records acceptance via TagPanel.findPending. Single per-image fetch: score_image now reports above_threshold per row (computed vs the head's own suggest cut, separate from the inclusion floor), so the rail makes ONE min=0 request and derives the panel (above_threshold) and the dropdown (all, text-filtered) client-side — the two /suggestions calls collapse to one. Manual "Create 'X' as <kind>" (novel typed names) is unchanged; the alias table + tag-side alias admin + auto-apply alias matching are untouched. Tests: gate/serializer assertions updated (above_threshold; dropped dead-field + alias-endpoint checks); frontend spec seeds via the single load and covers the byCategory/aboveByCategory split. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CgZP9v2otxVJymiYsnVuMy
118 lines
4.0 KiB
Python
118 lines
4.0 KiB
Python
import pytest
|
|
from sqlalchemy import select
|
|
|
|
from backend.app.celery_app import celery
|
|
from backend.app.models import ImageRecord, MLSettings, TagHead, TagKind
|
|
from backend.app.services.tag_service import TagService
|
|
|
|
pytestmark = pytest.mark.integration
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def eager():
|
|
celery.conf.task_always_eager = True
|
|
yield
|
|
celery.conf.task_always_eager = False
|
|
|
|
|
|
async def _img(db, sha="s" * 64):
|
|
img = ImageRecord(
|
|
path=f"/images/{sha}.jpg", sha256=sha, size_bytes=1,
|
|
mime="image/jpeg", width=1, height=1,
|
|
origin="imported_filesystem", integrity_status="unknown",
|
|
)
|
|
db.add(img)
|
|
await db.commit()
|
|
return img
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_suggestions(client, db):
|
|
# Suggestions come from a trained head now (Camie/centroid removed): an image
|
|
# whose embedding aligns with the head surfaces that concept.
|
|
s = (await db.execute(select(MLSettings).where(MLSettings.id == 1))).scalar_one()
|
|
img = ImageRecord(
|
|
path="/images/headsug.jpg", sha256="h" * 64, size_bytes=1,
|
|
mime="image/jpeg", width=1, height=1, origin="imported_filesystem",
|
|
integrity_status="unknown", siglip_embedding=[3.0] + [0.0] * 1151,
|
|
)
|
|
db.add(img)
|
|
await db.flush()
|
|
tag = await TagService(db).find_or_create("sword", TagKind.general)
|
|
db.add(TagHead(
|
|
tag_id=tag.id, embedding_version=s.embedder_model_version,
|
|
weights=[1.0] + [0.0] * 1151, bias=0.0, suggest_threshold=0.5,
|
|
auto_apply_threshold=None, n_pos=10, n_neg=30,
|
|
ap=0.8, precision_cv=0.9, recall=0.6,
|
|
))
|
|
await db.commit()
|
|
resp = await client.get(f"/api/images/{img.id}/suggestions")
|
|
assert resp.status_code == 200
|
|
body = await resp.get_json()
|
|
general = body["by_category"].get("general", [])
|
|
s2 = next(x for x in general if x["canonical_tag_id"] == tag.id)
|
|
assert s2["source"] == "head"
|
|
assert s2["above_threshold"] is True # ~0.73 clears the 0.5 suggest cut
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_accept_requires_tag_id(client, db):
|
|
img = await _img(db)
|
|
resp = await client.post(
|
|
f"/api/images/{img.id}/suggestions/accept", json={}
|
|
)
|
|
assert resp.status_code == 400
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_accept_applies_tag_to_image(client, db):
|
|
# Camie/allowlist retired (#1189): accept applies the tag to THIS image
|
|
# (source='ml_accepted', a head-training positive) — no bulk allowlist
|
|
# fan-out anymore.
|
|
from backend.app.models.tag import image_tag
|
|
|
|
img = await _img(db)
|
|
tag = await TagService(db).find_or_create("AcceptMe", TagKind.character)
|
|
await db.commit()
|
|
resp = await client.post(
|
|
f"/api/images/{img.id}/suggestions/accept", json={"tag_id": tag.id}
|
|
)
|
|
assert resp.status_code == 200
|
|
assert (await resp.get_json())["accepted"] is True
|
|
src = (await db.execute(
|
|
select(image_tag.c.source)
|
|
.where(image_tag.c.image_record_id == img.id)
|
|
.where(image_tag.c.tag_id == tag.id)
|
|
)).scalar_one()
|
|
assert src == "ml_accepted"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_dismiss(client, db):
|
|
img = await _img(db)
|
|
tag = await TagService(db).find_or_create("DismissMe", TagKind.general)
|
|
await db.commit()
|
|
resp = await client.post(
|
|
f"/api/images/{img.id}/suggestions/dismiss", json={"tag_id": tag.id}
|
|
)
|
|
assert resp.status_code == 204
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_undismiss_reverses_rejection(client, db):
|
|
img = await _img(db)
|
|
tag = await TagService(db).find_or_create("UndismissMe", TagKind.general)
|
|
await db.commit()
|
|
await client.post(
|
|
f"/api/images/{img.id}/suggestions/dismiss", json={"tag_id": tag.id}
|
|
)
|
|
resp = await client.post(
|
|
f"/api/images/{img.id}/suggestions/undismiss", json={"tag_id": tag.id}
|
|
)
|
|
assert resp.status_code == 204
|
|
# Idempotent: un-rejecting again (nothing to clear) is still a 204.
|
|
resp2 = await client.post(
|
|
f"/api/images/{img.id}/suggestions/undismiss", json={"tag_id": tag.id}
|
|
)
|
|
assert resp2.status_code == 204
|