from __future__ import annotations import tempfile import unittest from pathlib import Path from scene_search.database import connect, init_db from scene_search.maintenance import ( list_legacy_jellyfin_thumbs, remove_legacy_jellyfin_thumbs, ) class LegacyThumbCleanupTests(unittest.TestCase): def test_cleanup_removes_only_local_files_not_canonical_rows(self) -> None: with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) db_path = root / "scenes.sqlite3" thumbs_dir = root / "thumbs" 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, trickplay_width ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( "media-1", "jellyfin://item-1", "Movie", "hash", 0, 0, 10000, "model", 1, "jellyfin", "item-1", "lib-1", 320, ), ) con.execute( """ INSERT INTO frames ( id, media_id, frame_number, timestamp_ms, thumb_path, embedding_f16 ) VALUES (?, ?, ?, ?, ?, ?) """, ( "frame-1", "media-1", 0, 0, str(thumbs_dir / "media-1" / "frame_000001.jpg"), b"\x00\x00", ), ) directory = thumbs_dir / "media-1" directory.mkdir(parents=True) (directory / "frame_000001.jpg").write_bytes(b"duplicate") found = list_legacy_jellyfin_thumbs(db_path, thumbs_dir) self.assertEqual(len(found), 1) self.assertEqual(found[0].files, 1) removed = remove_legacy_jellyfin_thumbs(db_path, thumbs_dir) self.assertEqual(len(removed), 1) self.assertFalse(directory.exists()) with connect(db_path) as con: media_count = con.execute( "SELECT COUNT(*) FROM media" ).fetchone()[0] frame_count = con.execute( "SELECT COUNT(*) FROM frames" ).fetchone()[0] self.assertEqual(media_count, 1) self.assertEqual(frame_count, 1) if __name__ == "__main__": unittest.main()