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, _route_rows from scene_search.database import init_db class SavedRouteReuseTests(unittest.TestCase): def test_current_saved_route_skips_classifier(self) -> None: fake_settings = SimpleNamespace( 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_device="cuda", caption_router_batch_size=32, 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() row = { "id": "frame-1", "media_id": "media-1", "timestamp_ms": 10000, "caption_route": "specialist", "caption_route_reason": "score", "route_score": 0.91, "route_model": "router-model", "route_policy": policy, } with patch( "scene_search.cli.ExplicitContentRouter", side_effect=AssertionError("classifier should not be loaded"), ): routed, missing = _route_rows([row]) self.assertEqual(missing, 0) self.assertEqual(len(routed), 1) self.assertEqual(routed[0][1], "specialist") self.assertAlmostEqual(routed[0][2], 0.91) self.assertEqual(routed[0][3], "score") def test_global_specialist_override_skips_classifier(self) -> None: fake_settings = SimpleNamespace( db_path=Path(self._testMethodName + ".sqlite3"), caption_general_model="general-model", caption_specialist_model="specialist-model", caption_force_specialist=True, caption_router_enabled=True, caption_router_model="router-model", caption_router_device="cuda", caption_router_batch_size=32, 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 tempfile.TemporaryDirectory() as tmp: fake_settings.db_path = Path(tmp) / "scenes.sqlite3" init_db(fake_settings.db_path) row = { "id": "frame-1", "media_id": "media-1", "timestamp_ms": 10000, "caption_route": None, "caption_route_reason": None, "route_score": None, "route_model": None, "route_policy": None, "jellyfin_tags": "[]", } with patch("scene_search.cli.settings", fake_settings): with patch( "scene_search.cli.ExplicitContentRouter", side_effect=AssertionError("classifier should not be loaded"), ): routed, missing = _route_rows([row]) self.assertEqual(missing, 0) self.assertEqual(routed[0][1], "specialist") self.assertEqual(routed[0][3], "global-specialist") if __name__ == "__main__": unittest.main()