diff --git a/tests/test_reindex_annotations.py b/tests/test_reindex_annotations.py new file mode 100644 index 0000000..a018035 --- /dev/null +++ b/tests/test_reindex_annotations.py @@ -0,0 +1,171 @@ +from __future__ import annotations + +import tempfile +import unittest +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import patch + +from scene_search.database import connect, init_db +from scene_search.jellyfin_index import ( + _load_frame_annotations, + _restore_frame_annotations, +) + + +class ReindexAnnotationPreservationTests(unittest.TestCase): + def test_caption_and_routing_metadata_survive_matching_frame_rebuild(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + db_path = Path(tmp) / "scenes.sqlite3" + init_db(db_path) + fake_settings = SimpleNamespace(db_path=db_path) + + with patch("scene_search.jellyfin_index.settings", fake_settings): + 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", + 100, + 0, + 10000, + "model", + 2, + "jellyfin", + "item-1", + "lib-1", + ), + ) + 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 + ) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + """, + ( + "frame-same", + "media-1", + 0, + 0, + "jellyfin://item-1/trickplay/320/0", + b"\x00\x00", + "A person stands in a kitchen.", + "general-model", + "2026-10-07 10:00:00", + "general", + "score", + 0.12, + "router-model", + "policy-v1", + "2026-10-07 09:59:00", + ), + ) + + annotations = _load_frame_annotations("media-1") + + with connect(db_path) as con: + con.execute("DELETE FROM media WHERE id = ?", ("media-1",)) + 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", + "new-hash", + 100, + 0, + 10000, + "model", + 2, + "jellyfin", + "item-1", + "lib-1", + ), + ) + con.execute( + """ + INSERT INTO frames ( + id, media_id, frame_number, timestamp_ms, + thumb_path, embedding_f16 + ) + VALUES (?, ?, ?, ?, ?, ?) + """, + ( + "frame-same", + "media-1", + 0, + 0, + "jellyfin://item-1/trickplay/320/0", + b"\x01\x01", + ), + ) + con.execute( + """ + INSERT INTO frames ( + id, media_id, frame_number, timestamp_ms, + thumb_path, embedding_f16 + ) + VALUES (?, ?, ?, ?, ?, ?) + """, + ( + "frame-new", + "media-1", + 1, + 10000, + "jellyfin://item-1/trickplay/320/1", + b"\x02\x02", + ), + ) + preserved = _restore_frame_annotations( + con, + annotations, + {"frame-same", "frame-new"}, + ) + + self.assertEqual(preserved, 1) + + with connect(db_path) as con: + same = con.execute( + "SELECT * FROM frames WHERE id = ?", + ("frame-same",), + ).fetchone() + new = con.execute( + "SELECT * FROM frames WHERE id = ?", + ("frame-new",), + ).fetchone() + + self.assertEqual( + same["caption"], + "A person stands in a kitchen.", + ) + self.assertEqual(same["caption_model"], "general-model") + self.assertEqual(same["caption_route"], "general") + self.assertEqual(same["route_policy"], "policy-v1") + self.assertIsNone(new["caption"]) + self.assertIsNone(new["caption_route"]) + + +if __name__ == "__main__": + unittest.main()