from __future__ import annotations import tempfile import unittest from pathlib import Path from types import SimpleNamespace from unittest.mock import patch from scene_search.cli import _route_policy, _select_caption_rows from scene_search.database import connect, init_db class CaptionSelectionTests(unittest.TestCase): def test_resume_selects_only_frames_needing_caption_in_requested_library(self) -> None: with tempfile.TemporaryDirectory() as tmp: db_path = Path(tmp) / "scenes.sqlite3" init_db(db_path) fake_settings = SimpleNamespace( db_path=db_path, caption_general_model="general-model", caption_specialist_model="specialist-model", caption_force_specialist=False, caption_router_enabled=False, jellyfin_force_specialist_tag="scene-search:specialist", jellyfin_force_general_tag="scene-search:general", jellyfin_specialist_tags=(), ) with patch("scene_search.cli.settings", fake_settings): policy = _route_policy() with connect(db_path) as con: for media_id, library_id in [ ("media-a", "lib-a"), ("media-b", "lib-b"), ]: con.execute( """ INSERT INTO media ( id, source_path, title, content_sha256, size_bytes, mtime_ns, interval_ms, model_id, embedding_dim, source_kind, jellyfin_item_id, jellyfin_library_id ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( media_id, f"jellyfin://{media_id}", media_id, "hash", 0, 0, 10000, "embed-model", 1, "jellyfin", media_id, library_id, ), ) con.execute( """ INSERT INTO frames ( id, media_id, frame_number, timestamp_ms, thumb_path, embedding_f16, caption, caption_model, caption_route, route_policy ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( "done", "media-a", 0, 0, "/tmp/done.jpg", b"\x00\x00", "already captioned", "general-model", "general", policy, ), ) con.execute( """ INSERT INTO frames ( id, media_id, frame_number, timestamp_ms, thumb_path, embedding_f16 ) VALUES (?, ?, ?, ?, ?, ?) """, ( "resume-me", "media-a", 1, 10000, "/tmp/resume.jpg", b"\x00\x00", ), ) con.execute( """ INSERT INTO frames ( id, media_id, frame_number, timestamp_ms, thumb_path, embedding_f16 ) VALUES (?, ?, ?, ?, ?, ?) """, ( "other-library", "media-b", 0, 0, "/tmp/other.jpg", b"\x00\x00", ), ) args = SimpleNamespace( force=False, media_id=None, title=None, limit=None, library_ids=["lib-a"], ) rows = _select_caption_rows(args, only_needing_caption=True) self.assertEqual([row["id"] for row in rows], ["resume-me"]) def test_resume_recaptions_when_saved_route_and_caption_model_disagree(self) -> None: with tempfile.TemporaryDirectory() as tmp: db_path = Path(tmp) / "scenes.sqlite3" init_db(db_path) fake_settings = SimpleNamespace( db_path=db_path, caption_general_model="general-model", caption_specialist_model="specialist-model", caption_force_specialist=False, caption_router_enabled=True, caption_router_model="router-model", caption_router_threshold=0.5, caption_router_context_seconds=6, jellyfin_force_specialist_tag="scene-search:specialist", jellyfin_force_general_tag="scene-search:general", jellyfin_specialist_tags=(), ) with patch("scene_search.cli.settings", fake_settings): policy = _route_policy() with connect(db_path) as con: con.execute( """ INSERT INTO media ( id, source_path, title, content_sha256, size_bytes, mtime_ns, interval_ms, model_id, embedding_dim, source_kind, jellyfin_item_id, jellyfin_library_id ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( "media-a", "jellyfin://media-a", "Movie", "hash", 0, 0, 10000, "embed-model", 1, "jellyfin", "media-a", "lib-a", ), ) con.execute( """ INSERT INTO frames ( id, media_id, frame_number, timestamp_ms, thumb_path, embedding_f16, caption, caption_model, caption_route, route_policy ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( "wrong-model", "media-a", 0, 0, "/tmp/frame.jpg", b"\x00\x00", "old general caption", "general-model", "specialist", policy, ), ) args = SimpleNamespace( force=False, media_id=None, title=None, limit=None, library_ids=["lib-a"], ) rows = _select_caption_rows(args, only_needing_caption=True) self.assertEqual([row["id"] for row in rows], ["wrong-model"]) if __name__ == "__main__": unittest.main()