mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-15 22:41:28 +00:00
127 lines
4.3 KiB
Python
127 lines
4.3 KiB
Python
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_media_by_source,
|
|
remove_media_by_source,
|
|
)
|
|
|
|
|
|
class MaintenanceTests(unittest.TestCase):
|
|
def test_remove_file_source_keeps_jellyfin_media(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.executemany(
|
|
"""
|
|
INSERT INTO media (
|
|
id, source_path, title, content_sha256, size_bytes, mtime_ns,
|
|
interval_ms, model_id, embedding_dim, source_kind,
|
|
jellyfin_item_id
|
|
)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
""",
|
|
[
|
|
(
|
|
"file-1",
|
|
"/tmp/test.mp4",
|
|
"Test clip 1",
|
|
"hash-file",
|
|
100,
|
|
0,
|
|
2000,
|
|
"model",
|
|
2,
|
|
"file",
|
|
None,
|
|
),
|
|
(
|
|
"jf-1",
|
|
"/media/movie.mkv",
|
|
"Jellyfin movie",
|
|
"hash-jf",
|
|
200,
|
|
0,
|
|
10000,
|
|
"model",
|
|
2,
|
|
"jellyfin",
|
|
"item-1",
|
|
),
|
|
],
|
|
)
|
|
con.executemany(
|
|
"""
|
|
INSERT INTO frames (
|
|
id, media_id, frame_number, timestamp_ms,
|
|
thumb_path, embedding_f16
|
|
)
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
""",
|
|
[
|
|
(
|
|
"frame-file",
|
|
"file-1",
|
|
0,
|
|
0,
|
|
str(thumbs_dir / "file-1" / "frame.jpg"),
|
|
b"1234",
|
|
),
|
|
(
|
|
"frame-jf",
|
|
"jf-1",
|
|
0,
|
|
0,
|
|
str(thumbs_dir / "jf-1" / "frame.jpg"),
|
|
b"5678",
|
|
),
|
|
],
|
|
)
|
|
|
|
for media_id in ("file-1", "jf-1"):
|
|
folder = thumbs_dir / media_id
|
|
folder.mkdir(parents=True)
|
|
(folder / "frame.jpg").write_bytes(b"jpeg")
|
|
|
|
matches = list_media_by_source(db_path, "file")
|
|
self.assertEqual([item.title for item in matches], ["Test clip 1"])
|
|
|
|
removed = remove_media_by_source(db_path, thumbs_dir, "file")
|
|
self.assertEqual([item.media_id for item in removed], ["file-1"])
|
|
|
|
with connect(db_path) as con:
|
|
media_ids = [
|
|
row["id"] for row in con.execute(
|
|
"SELECT id FROM media ORDER BY id"
|
|
).fetchall()
|
|
]
|
|
frame_ids = [
|
|
row["id"] for row in con.execute(
|
|
"SELECT id FROM frames ORDER BY id"
|
|
).fetchall()
|
|
]
|
|
|
|
self.assertEqual(media_ids, ["jf-1"])
|
|
self.assertEqual(frame_ids, ["frame-jf"])
|
|
self.assertFalse((thumbs_dir / "file-1").exists())
|
|
self.assertTrue((thumbs_dir / "jf-1").exists())
|
|
|
|
def test_unknown_source_returns_empty(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
db_path = Path(tmp) / "scenes.sqlite3"
|
|
init_db(db_path)
|
|
|
|
self.assertEqual(list_media_by_source(db_path, "file"), [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|