diff --git a/tests/test_incremental_caption_sync.py b/tests/test_incremental_caption_sync.py new file mode 100644 index 0000000..3ff12b6 --- /dev/null +++ b/tests/test_incremental_caption_sync.py @@ -0,0 +1,144 @@ +from __future__ import annotations + +import tempfile +import unittest +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import MagicMock, patch + +from scene_search.database import connect, init_db +from scene_search.sync import CaptionMeiliSync, sync_frame_metadata + + +class IncrementalCaptionSyncTests(unittest.TestCase): + def _seed(self, db_path: Path, *, count: int = 2) -> None: + init_db(db_path) + 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-1", + "jellyfin://item-1", + "Movie", + "hash", + 0, + 0, + 10000, + "model", + 2, + "jellyfin", + "item-1", + "lib-1", + ), + ) + for index in range(count): + con.execute( + """ + INSERT INTO frames ( + id, media_id, frame_number, timestamp_ms, + thumb_path, embedding_f16, + caption, caption_model, captioned_at, + caption_route, caption_route_reason, + route_score, route_model, route_policy, routed_at, + meili_synced_at + ) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, + ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, NULL) + """, + ( + f"frame-{index}", + "media-1", + index, + index * 10000, + f"jellyfin://item-1/trickplay/320/{index}", + b"\x00\x00\x00\x00", + f"Caption {index}", + "caption-model", + "specialist", + "score", + 0.9, + "router-model", + "policy-v1", + ), + ) + + def test_sync_updates_only_caption_metadata_and_marks_frame_synced(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + db_path = Path(tmp) / "scenes.sqlite3" + self._seed(db_path, count=1) + fake_settings = SimpleNamespace(db_path=db_path) + meili = MagicMock() + + with patch("scene_search.sync.settings", fake_settings): + count = sync_frame_metadata(["frame-0"], meili=meili) + + self.assertEqual(count, 1) + meili.update_documents.assert_called_once() + args, kwargs = meili.update_documents.call_args + self.assertTrue(kwargs["wait"]) + self.assertEqual(args[0][0]["id"], "frame-0") + self.assertEqual(args[0][0]["caption"], "Caption 0") + self.assertNotIn("_vectors", args[0][0]) + + with connect(db_path) as con: + row = con.execute( + "SELECT meili_synced_at FROM frames WHERE id = ?", + ("frame-0",), + ).fetchone() + + self.assertIsNotNone(row["meili_synced_at"]) + + def test_failed_meili_update_leaves_frame_unsynced(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + db_path = Path(tmp) / "scenes.sqlite3" + self._seed(db_path, count=1) + fake_settings = SimpleNamespace(db_path=db_path) + meili = MagicMock() + meili.update_documents.side_effect = RuntimeError("Meili unavailable") + + with ( + patch("scene_search.sync.settings", fake_settings), + self.assertRaisesRegex(RuntimeError, "Meili unavailable"), + ): + sync_frame_metadata(["frame-0"], meili=meili) + + with connect(db_path) as con: + row = con.execute( + "SELECT meili_synced_at FROM frames WHERE id = ?", + ("frame-0",), + ).fetchone() + + self.assertIsNone(row["meili_synced_at"]) + + def test_batcher_catches_up_unsynced_captions_in_small_batches(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + db_path = Path(tmp) / "scenes.sqlite3" + self._seed(db_path, count=2) + fake_settings = SimpleNamespace(db_path=db_path) + meili = MagicMock() + progress: list[dict] = [] + + with patch("scene_search.sync.settings", fake_settings): + syncer = CaptionMeiliSync( + ["lib-1"], + meili=meili, + batch_size=1, + progress=progress.append, + ) + caught_up = syncer.catch_up() + + self.assertEqual(caught_up, 2) + self.assertEqual(syncer.updated_total, 2) + self.assertEqual(meili.update_documents.call_count, 2) + self.assertEqual(progress[-1]["meili_sync_pending"], 0) + + +if __name__ == "__main__": + unittest.main()