Files
FabledCurator/tests/test_api_ml_admin.py
bvandeusen ab63d94249
CI / lint (push) Successful in 3s
CI / frontend-build (push) Successful in 20s
CI / backend-lint-and-test (push) Successful in 38s
CI / integration (push) Successful in 3m47s
feat(ml): presentation auto-hide settings + review table (#141 step 3)
MLSettings gains presentation_auto_apply_enabled / _threshold (default 0.90) +
presentation_conflict_threshold (default 0.50): banner/editor auto-hide with a
FLAT threshold (decoupled from content-head graduation), plus the "also looks
like content" conflict cut. New presentation_review table (image, presentation
tag, conflict tag + score, created/resolved_at) records auto-hides flagged for
review. Migration 0082 (columns + table), ml_admin API (editable + get_settings
+ _validate bounds), settings roundtrip/bounds test. The sweep that reads these
knobs + the Settings UI land in step 4.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CDgx8bQS5YrGRK76v8HUnM
2026-07-06 22:59:00 -04:00

172 lines
6.4 KiB
Python

import pytest
from backend.app.celery_app import celery
from backend.app.models import 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
@pytest.mark.asyncio
async def test_get_and_patch_settings(client):
resp = await client.get("/api/ml/settings")
assert resp.status_code == 200
body = await resp.get_json()
assert body["head_min_positives"] == 8
# Retired tagger/suggestion-threshold columns are gone from the payload
# (Camie retirement #1189/#1199).
assert "suggestion_threshold_general" not in body
assert "tagger_store_floor" not in body
assert "tagger_model_version" not in body
resp = await client.patch(
"/api/ml/settings", json={"head_min_positives": 12}
)
assert resp.status_code == 200
assert (await resp.get_json())["head_min_positives"] == 12
@pytest.mark.asyncio
async def test_embedder_model_default_settable_and_empty_rejected(client):
# #1203: fresh installs default to SigLIP 2 (migration 0069, no embeddings).
# The model is operator-settable (a swap) and neither field may be blanked.
body = await (await client.get("/api/ml/settings")).get_json()
assert body["embedder_model_name"] == "google/siglip2-so400m-patch16-512"
ok = await client.patch("/api/ml/settings", json={
"embedder_model_name": "google/siglip-so400m-patch14-384",
"embedder_model_version": "siglip-so400m-patch14-384",
})
assert ok.status_code == 200
out = await ok.get_json()
assert out["embedder_model_name"] == "google/siglip-so400m-patch14-384"
bad = await client.patch("/api/ml/settings", json={"embedder_model_name": " "})
assert bad.status_code == 400
@pytest.mark.asyncio
async def test_detector_settings_defaults_patch_and_validation(client):
# #134: crop-proposer config is exposed + editable here (announced to the
# agent via the lease). Defaults are all-on with the pinned weights.
body = await (await client.get("/api/ml/settings")).get_json()
assert body["detector_person_enabled"] is True
assert body["detector_anatomy_enabled"] is True
assert "yolov11m_aa22" in body["detector_anatomy_weights"]
assert body["detector_panel_weights"].endswith("::best.pt")
assert body["detector_max_regions"] == 128
ok = await client.patch("/api/ml/settings", json={
"detector_anatomy_enabled": False,
"detector_person_conf": 0.5,
"detector_max_panels": 4,
})
assert ok.status_code == 200
out = await ok.get_json()
assert out["detector_anatomy_enabled"] is False
assert out["detector_person_conf"] == 0.5
assert out["detector_max_panels"] == 4
# Out-of-range confidence + non-positive cap are rejected.
assert (await client.patch(
"/api/ml/settings", json={"detector_panel_conf": 1.5})).status_code == 400
assert (await client.patch(
"/api/ml/settings", json={"detector_max_regions": 0})).status_code == 400
@pytest.mark.asyncio
async def test_presentation_settings_defaults_patch_and_validation(client):
# #141: presentation-chrome auto-hide knobs (banner/editor auto-apply +
# the "also looks like content" conflict threshold) are exposed + editable.
body = await (await client.get("/api/ml/settings")).get_json()
assert body["presentation_auto_apply_enabled"] is True
assert body["presentation_auto_apply_threshold"] == 0.90
assert body["presentation_conflict_threshold"] == 0.50
ok = await client.patch("/api/ml/settings", json={
"presentation_auto_apply_enabled": False,
"presentation_auto_apply_threshold": 0.95,
"presentation_conflict_threshold": 0.4,
})
assert ok.status_code == 200
out = await ok.get_json()
assert out["presentation_auto_apply_enabled"] is False
assert out["presentation_auto_apply_threshold"] == 0.95
assert out["presentation_conflict_threshold"] == 0.4
# Auto-apply threshold below the floor + conflict cut above 1 are rejected.
assert (await client.patch(
"/api/ml/settings",
json={"presentation_auto_apply_threshold": 0.4})).status_code == 400
assert (await client.patch(
"/api/ml/settings",
json={"presentation_conflict_threshold": 1.5})).status_code == 400
@pytest.mark.asyncio
async def test_embedder_models_list(client):
# #1203: the dropdown reads the supported-model list from the server.
body = await (await client.get("/api/ml/embedder-models")).get_json()
assert any(
m["name"] == "google/siglip2-so400m-patch16-512" for m in body["models"]
)
assert all({"name", "version", "label"} <= set(m) for m in body["models"])
@pytest.mark.asyncio
async def test_video_settings_default_and_patch(client):
"""#747: video frame-sampling knobs are exposed + patchable."""
body = await (await client.get("/api/ml/settings")).get_json()
assert body["video_frame_interval_seconds"] == pytest.approx(4.0)
assert body["video_max_frames"] == 64
resp = await client.patch(
"/api/ml/settings",
json={"video_frame_interval_seconds": 5, "video_max_frames": 40},
)
assert resp.status_code == 200
out = await resp.get_json()
assert out["video_frame_interval_seconds"] == pytest.approx(5.0)
assert out["video_max_frames"] == 40
@pytest.mark.asyncio
async def test_video_max_frames_below_one_rejected(client):
resp = await client.patch(
"/api/ml/settings", json={"video_max_frames": 0},
)
assert resp.status_code == 400
assert "video_max_frames" in (await resp.get_json())["error"]
@pytest.mark.asyncio
async def test_backfill_trigger(client):
r1 = await client.post("/api/ml/backfill")
assert r1.status_code == 202
@pytest.mark.asyncio
async def test_rename_tag(client, db):
tag = await TagService(db).find_or_create("oldname", TagKind.character)
await db.commit()
resp = await client.patch(f"/api/tags/{tag.id}", json={"name": "New Name"})
assert resp.status_code == 200
assert (await resp.get_json())["name"] == "New Name"
@pytest.mark.asyncio
async def test_rename_collision_409(client, db):
svc = TagService(db)
await svc.find_or_create("Taken", TagKind.character)
other = await svc.find_or_create("Mover", TagKind.character)
await db.commit()
resp = await client.patch(f"/api/tags/{other.id}", json={"name": "Taken"})
assert resp.status_code == 409