mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-19 00:03:36 +00:00
375 lines
16 KiB
Python
375 lines
16 KiB
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock, patch
|
|
|
|
from scene_search.database import connect, init_db
|
|
from scene_search.overviews import (
|
|
AI_OVERVIEW_HEADING,
|
|
MAX_CAPTION_CHARS,
|
|
_caption_route_counts,
|
|
_chat_overview,
|
|
_format_caption_evidence,
|
|
_sample,
|
|
preview,
|
|
publish,
|
|
restore,
|
|
)
|
|
|
|
|
|
class VisualOverviewTests(unittest.TestCase):
|
|
@staticmethod
|
|
def seed(db_path: Path, *, with_window: bool = True) -> 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://film-1', 'My Film', 'hash', 0, 0,
|
|
10000, 'model', 2, 'jellyfin', 'film-1', 'library-1'
|
|
)
|
|
"""
|
|
)
|
|
for index in range(3):
|
|
con.execute(
|
|
"""
|
|
INSERT INTO frames (
|
|
id, media_id, frame_number, timestamp_ms,
|
|
thumb_path, embedding_f16, caption
|
|
)
|
|
VALUES (?, 'media-1', ?, ?, '', ?, ?)
|
|
""",
|
|
(
|
|
f"frame-{index}", index, index * 10000,
|
|
b"\0\0\0\0", f"Original legacy scene {index}",
|
|
),
|
|
)
|
|
if with_window:
|
|
con.execute(
|
|
"""
|
|
INSERT INTO caption_windows (
|
|
id, media_id, start_ms, end_ms, representative_frame_id,
|
|
caption, window_policy
|
|
)
|
|
VALUES ('cw-1', 'media-1', 0, 20000, 'frame-0',
|
|
'People crossing a plaza', 'test-policy')
|
|
"""
|
|
)
|
|
con.executemany(
|
|
"""
|
|
INSERT INTO caption_window_frames
|
|
(window_id, frame_id, position) VALUES ('cw-1', ?, ?)
|
|
""", [('frame-0', 0), ('frame-1', 1)],
|
|
)
|
|
|
|
@staticmethod
|
|
def client():
|
|
client = Mock()
|
|
remote = {
|
|
"Id": "film-1", "Type": "Movie", "Name": "My Film",
|
|
"Overview": "Original distributor synopsis.",
|
|
"Tags": ["Drama"], "Genres": ["Adventure"],
|
|
"ProviderIds": {"Tmdb": "1"},
|
|
}
|
|
live = {"item": dict(remote)}
|
|
|
|
def fetch(_):
|
|
return dict(live["item"])
|
|
|
|
def write(item, overview):
|
|
# A real Jellyfin write receives the entire DTO plus new Overview.
|
|
assert item["Tags"] == ["Drama"]
|
|
assert item["ProviderIds"] == {"Tmdb": "1"}
|
|
live["item"]["Overview"] = overview
|
|
|
|
client.item_for_update.side_effect = fetch
|
|
client.update_item_overview.side_effect = write
|
|
return client, live
|
|
|
|
def test_spaced_samples_cover_the_entire_video(self) -> None:
|
|
evidence = [(index * 1000, str(index)) for index in range(100)]
|
|
chosen = _sample(evidence, 5)
|
|
self.assertEqual(len(chosen), 5)
|
|
self.assertEqual(chosen[0], evidence[0])
|
|
self.assertEqual(chosen[-1], evidence[-1])
|
|
|
|
def test_preview_uses_window_captions_and_caches_without_jellyfin_write(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db_path = Path(d) / "scenes.sqlite3"
|
|
self.seed(db_path)
|
|
captured = []
|
|
|
|
def summarize(captions, **kwargs):
|
|
captured.extend(captions)
|
|
return "People gather outside and walk around a plaza."
|
|
|
|
with patch("scene_search.overviews._jellyfin_client") as jc:
|
|
first = preview(
|
|
db_path=db_path, title="My Film",
|
|
model="local-test-model", chat_fn=summarize,
|
|
)
|
|
second = preview(
|
|
db_path=db_path, media_id="media-1",
|
|
model="local-test-model",
|
|
chat_fn=lambda *a, **kw: self.fail("Unnecessary LLM call"),
|
|
)
|
|
jc.assert_not_called()
|
|
|
|
self.assertEqual(first["overview"], second["overview"])
|
|
self.assertEqual(first["sampled_captions"], 2)
|
|
self.assertEqual(first["total_captions"], 2)
|
|
self.assertEqual(captured[0][1], "People crossing a plaza")
|
|
self.assertIn("Original legacy scene 2", captured[1][1])
|
|
self.assertTrue(all("legacy scene 0" not in text for _, text in captured))
|
|
|
|
|
|
def test_specialist_aware_summary_uses_saved_routes_and_selected_model(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db_path = Path(d) / "scenes.sqlite3"
|
|
self.seed(db_path)
|
|
with connect(db_path) as con:
|
|
con.execute(
|
|
"UPDATE caption_windows SET caption_route = 'specialist' "
|
|
"WHERE id = 'cw-1'"
|
|
)
|
|
con.execute(
|
|
"UPDATE frames SET caption_route = 'general' "
|
|
"WHERE id = 'frame-2'"
|
|
)
|
|
|
|
self.assertEqual(
|
|
_caption_route_counts(db_path, "media-1"),
|
|
{"general": 1, "specialist": 1, "unknown": 0},
|
|
)
|
|
observed = []
|
|
|
|
def summarize(captions, **kwargs):
|
|
observed.append(kwargs)
|
|
return "A neutral description of the visible adults and setting."
|
|
|
|
fake_settings = SimpleNamespace(
|
|
overview_model="general-text-model",
|
|
overview_specialist_model="specialist-text-model",
|
|
overview_timeout_seconds=120,
|
|
ollama_url="http://ollama.test",
|
|
)
|
|
with patch("scene_search.overviews.settings", fake_settings):
|
|
first = preview(
|
|
db_path=db_path, title="My Film", chat_fn=summarize
|
|
)
|
|
self.assertEqual(first["style"], "specialist")
|
|
self.assertEqual(first["model"], "specialist-text-model")
|
|
self.assertEqual(observed[-1]["style"], "specialist")
|
|
|
|
# Changing the requested style invalidates the old draft
|
|
# even if caption data has not changed.
|
|
second = preview(
|
|
db_path=db_path, title="My Film",
|
|
style="general", chat_fn=summarize,
|
|
)
|
|
self.assertEqual(second["style"], "general")
|
|
self.assertEqual(second["model"], "general-text-model")
|
|
self.assertEqual(len(observed), 2)
|
|
|
|
# Same configuration + captions hits the cached draft.
|
|
preview(
|
|
db_path=db_path, title="My Film", style="general",
|
|
chat_fn=lambda *a, **kw: self.fail("Should use cached draft"),
|
|
)
|
|
self.assertEqual(len(observed), 2)
|
|
|
|
def test_general_video_stays_on_general_model_and_prompt(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db_path = Path(d) / "scenes.sqlite3"
|
|
self.seed(db_path)
|
|
observed = []
|
|
|
|
def summarize(captions, **kwargs):
|
|
observed.append(kwargs)
|
|
return "A group of people outside."
|
|
|
|
settings = SimpleNamespace(
|
|
overview_model="general-model",
|
|
overview_specialist_model="specialist-model",
|
|
overview_timeout_seconds=120,
|
|
ollama_url="http://ollama.test",
|
|
)
|
|
with patch("scene_search.overviews.settings", settings):
|
|
result = preview(
|
|
db_path=db_path, title="My Film", chat_fn=summarize
|
|
)
|
|
|
|
self.assertEqual(result["style"], "general")
|
|
self.assertEqual(result["model"], "general-model")
|
|
self.assertEqual(observed[-1]["style"], "general")
|
|
|
|
def test_evidence_budget_preserves_last_timestamp_for_long_specialist_captions(self) -> None:
|
|
long_evidence = [
|
|
(index * 30000, "Detailed caption " + ("a scene " * 500))
|
|
for index in range(64)
|
|
]
|
|
rendered = _format_caption_evidence(long_evidence)
|
|
self.assertLessEqual(len(rendered), MAX_CAPTION_CHARS)
|
|
self.assertEqual(len(rendered.splitlines()), 64)
|
|
self.assertIn("[00:00:00]", rendered)
|
|
self.assertIn("[00:31:30]", rendered)
|
|
self.assertTrue(rendered.splitlines()[-1].startswith("[00:31:30]"))
|
|
|
|
def test_specialist_chat_prompt_remains_factual_without_vague_euphemisms(self) -> None:
|
|
response = Mock()
|
|
response.ok = True
|
|
response.json.return_value = {"message": {"content": "A factual visual summary."}}
|
|
|
|
with patch("scene_search.overviews.requests.post", return_value=response) as post:
|
|
description = _chat_overview(
|
|
[(0, "Adults in a room."), (30000, "An intimate scene.")],
|
|
model="text-model", ollama_url="http://ollama.test",
|
|
timeout_seconds=20, style="specialist",
|
|
)
|
|
|
|
self.assertEqual(description, "A factual visual summary.")
|
|
payload = post.call_args.kwargs["json"]
|
|
prompt = payload["messages"][0]["content"]
|
|
self.assertIn("non-graphic language", prompt)
|
|
self.assertIn("intimate", prompt)
|
|
self.assertIn("[00:00:30]", prompt)
|
|
self.assertEqual(payload["model"], "text-model")
|
|
self.assertNotIn("images", payload["messages"][0])
|
|
|
|
def test_publish_appends_only_after_preview_and_restore_returns_exact_original(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db_path = Path(d) / "scenes.sqlite3"
|
|
self.seed(db_path)
|
|
client, live = self.client()
|
|
with self.assertRaisesRegex(ValueError, "Generate and review"):
|
|
publish(db_path=db_path, title="My Film", client=client)
|
|
client.update_item_overview.assert_not_called()
|
|
|
|
preview(
|
|
db_path=db_path, title="My Film",
|
|
model="test", chat_fn=lambda *args, **kw: "People cross a plaza.",
|
|
)
|
|
published = publish(db_path=db_path, title="My Film", client=client)
|
|
self.assertEqual(published["status"], "applied")
|
|
self.assertTrue(
|
|
live["item"]["Overview"].startswith("Original distributor synopsis.")
|
|
)
|
|
self.assertIn(AI_OVERVIEW_HEADING, live["item"]["Overview"])
|
|
self.assertIn("People cross a plaza.", live["item"]["Overview"])
|
|
self.assertEqual(publish(
|
|
db_path=db_path, title="My Film", client=client
|
|
)["status"], "already_applied")
|
|
self.assertEqual(client.update_item_overview.call_count, 1)
|
|
|
|
result = restore(db_path=db_path, title="My Film", client=client)
|
|
self.assertEqual(result["status"], "restored")
|
|
self.assertEqual(live["item"]["Overview"], "Original distributor synopsis.")
|
|
self.assertEqual(client.update_item_overview.call_count, 2)
|
|
|
|
def test_original_overview_backup_survives_media_reindex(self) -> None:
|
|
# Jellyfin metadata changes update the ETag. The indexer currently
|
|
# deletes and recreates the media row, so the backup cannot cascade.
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db_path = Path(d) / "scenes.sqlite3"
|
|
self.seed(db_path)
|
|
client, _ = self.client()
|
|
preview(
|
|
db_path=db_path, title="My Film", model="test",
|
|
chat_fn=lambda *args, **kw: "A street with a crowd.",
|
|
)
|
|
publish(db_path=db_path, title="My Film", client=client)
|
|
with connect(db_path) as con:
|
|
con.execute("DELETE FROM media WHERE id = 'media-1'")
|
|
backup = con.execute(
|
|
"SELECT original_overview FROM jellyfin_overview_drafts "
|
|
"WHERE media_id = 'media-1'"
|
|
).fetchone()
|
|
self.assertIsNotNone(backup)
|
|
self.assertEqual(
|
|
backup["original_overview"], "Original distributor synopsis."
|
|
)
|
|
|
|
def test_out_of_band_metadata_edits_block_restore(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db_path = Path(d) / "scenes.sqlite3"
|
|
self.seed(db_path)
|
|
client, live = self.client()
|
|
preview(
|
|
db_path=db_path, title="My Film",
|
|
model="test", chat_fn=lambda *args, **kw: "A quiet courtyard.",
|
|
)
|
|
publish(db_path=db_path, title="My Film", client=client)
|
|
live["item"]["Overview"] += " New manual changes."
|
|
with self.assertRaisesRegex(RuntimeError, "edited after publishing"):
|
|
restore(db_path=db_path, title="My Film", client=client)
|
|
self.assertTrue(live["item"]["Overview"].endswith("New manual changes."))
|
|
|
|
def test_mismatched_remote_id_blocks_publish(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db_path = Path(d) / "scenes.sqlite3"
|
|
self.seed(db_path)
|
|
client, live = self.client()
|
|
live["item"]["Id"] = "wrong-item"
|
|
preview(
|
|
db_path=db_path, title="My Film", model="test",
|
|
chat_fn=lambda *args, **kw: "People passing through a courtyard.",
|
|
)
|
|
with self.assertRaisesRegex(RuntimeError, "different item ID"):
|
|
publish(db_path=db_path, title="My Film", client=client)
|
|
client.update_item_overview.assert_not_called()
|
|
|
|
def test_ambiguous_title_requires_media_id(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db_path = Path(d) / "scenes.sqlite3"
|
|
self.seed(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
|
|
)
|
|
VALUES ('media-2', 'jellyfin://film-2', 'My Film',
|
|
'hash2', 0, 0, 10000, 'model', 2,
|
|
'jellyfin', 'film-2')
|
|
"""
|
|
)
|
|
with self.assertRaisesRegex(ValueError, "ambiguous"):
|
|
preview(db_path=db_path, title="My Film", model="test")
|
|
|
|
def test_partial_api_failure_preserves_restore_backup(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
db_path = Path(d) / "scenes.sqlite3"
|
|
self.seed(db_path)
|
|
client, live = self.client()
|
|
preview(
|
|
db_path=db_path, title="My Film", model="test",
|
|
chat_fn=lambda *args, **kw: "Courtyard with passers-by.",
|
|
)
|
|
client.update_item_overview.side_effect = RuntimeError("network failure")
|
|
with self.assertRaisesRegex(RuntimeError, "network failure"):
|
|
publish(db_path=db_path, title="My Film", client=client)
|
|
with connect(db_path) as con:
|
|
row = con.execute(
|
|
"SELECT original_overview, applied_overview "
|
|
"FROM jellyfin_overview_drafts WHERE media_id = 'media-1'"
|
|
).fetchone()
|
|
self.assertEqual(row["original_overview"], "Original distributor synopsis.")
|
|
self.assertIn("Courtyard with passers-by.", row["applied_overview"])
|
|
# Restoring a write that never occurred is a safe no-op.
|
|
restore(db_path=db_path, title="My Film", client=client)
|
|
self.assertEqual(live["item"]["Overview"], "Original distributor synopsis.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|