Files
bvandeusen 5c3f8ebd70
CI / lint (push) Successful in 3s
CI / frontend-build (push) Successful in 22s
CI / backend-lint-and-test (push) Successful in 35s
CI / integration (push) Successful in 3m7s
fix(aliases): store modal alias under raw model key + make aliases visible/manageable
The headline bug: aliases created from the modal NEVER resolved. Create
sent the normalized display name ('Sword', 'Uchiha Sasuke') while
resolution keys on the raw booru model key ('sword', 'uchiha_sasuke',
case-sensitive) — so the mapping was stored under a key nothing looks up,
and the prediction kept reappearing unaliased. The raw key wasn't even in
the /suggestions response, so the modal couldn't send it.

- Suggestion now carries raw_name (the model key an alias must use) and
  via_alias (surfaced via an operator alias); both serialized by the API.
- Modal alias-create sends raw_name, not display_name (the fix). Aliased
  suggestions show an 'alias' badge and a 'Remove alias' action; 'Treat as
  alias for…' is hidden for centroid hits (no model key) and already-aliased
  rows.
- Tag-side management: TagCard ⋮ → 'Aliases…' opens a dialog listing the
  model keys that fold into a tag, with remove (GET /api/tags/<id>/aliases +
  AliasService.list_for_tag). Creation stays in the modal suggestion flow.

Tests: full API round-trip locking the raw-key contract (raw_name exposed →
alias authored with it → resolves + via_alias on a later image);
list_for_tag (service + API); via_alias/raw_name on the existing service
suggestion tests. No migration.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 13:05:58 -04:00

101 lines
3.4 KiB
Python

import pytest
from backend.app.models import TagKind
from backend.app.services.ml.aliases import AliasService
from backend.app.services.tag_service import TagService
pytestmark = pytest.mark.integration
@pytest.mark.asyncio
async def test_create_and_resolve(db):
tags = TagService(db)
canonical = await tags.find_or_create("Sasuke Uchiha", TagKind.character)
aliases = AliasService(db)
await aliases.create("uchiha_sasuke", "character", canonical.id)
resolved = await aliases.resolve("uchiha_sasuke", "character")
assert resolved is not None
assert resolved.id == canonical.id
@pytest.mark.asyncio
async def test_resolve_returns_none_when_no_alias(db):
aliases = AliasService(db)
assert await aliases.resolve("nonexistent", "general") is None
@pytest.mark.asyncio
async def test_category_scoped(db):
tags = TagService(db)
char = await tags.find_or_create("Naruto", TagKind.character)
copy = await tags.find_or_create("Naruto", TagKind.fandom)
aliases = AliasService(db)
await aliases.create("naruto", "character", char.id)
await aliases.create("naruto", "copyright", copy.id)
assert (await aliases.resolve("naruto", "character")).id == char.id
assert (await aliases.resolve("naruto", "copyright")).id == copy.id
@pytest.mark.asyncio
async def test_create_idempotent(db):
tags = TagService(db)
t = await tags.find_or_create("X", TagKind.general)
aliases = AliasService(db)
await aliases.create("x_alias", "general", t.id)
await aliases.create("x_alias", "general", t.id) # no error
assert (await aliases.resolve("x_alias", "general")).id == t.id
@pytest.mark.asyncio
async def test_resolve_many(db):
tags = TagService(db)
a = await tags.find_or_create("Alice", TagKind.character)
b = await tags.find_or_create("Bob", TagKind.artist)
aliases = AliasService(db)
await aliases.create("alice_x", "character", a.id)
await aliases.create("bob_x", "artist", b.id)
out = await aliases.resolve_many(
[("alice_x", "character"), ("bob_x", "artist"), ("unknown", "general")]
)
assert out[("alice_x", "character")].id == a.id
assert out[("bob_x", "artist")].id == b.id
assert ("unknown", "general") not in out
@pytest.mark.asyncio
async def test_remove(db):
tags = TagService(db)
t = await tags.find_or_create("Y", TagKind.general)
aliases = AliasService(db)
await aliases.create("y_alias", "general", t.id)
await aliases.remove("y_alias", "general")
assert await aliases.resolve("y_alias", "general") is None
@pytest.mark.asyncio
async def test_list_all(db):
tags = TagService(db)
t = await tags.find_or_create("Z", TagKind.general)
aliases = AliasService(db)
await aliases.create("z1", "general", t.id)
rows = await aliases.list_all()
assert any(r.alias_string == "z1" and r.canonical_tag_name == "Z" for r in rows)
@pytest.mark.asyncio
async def test_list_for_tag(db):
tags = TagService(db)
keep = await tags.find_or_create("Keeper", TagKind.general)
other = await tags.find_or_create("Other", TagKind.general)
aliases = AliasService(db)
await aliases.create("k1", "general", keep.id)
await aliases.create("k2", "general", keep.id)
await aliases.create("o1", "general", other.id)
rows = await aliases.list_for_tag(keep.id)
assert {r.alias_string for r in rows} == {"k1", "k2"}
assert all(r.canonical_tag_id == keep.id for r in rows)