mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-17 07:21:28 +00:00
135 lines
5.9 KiB
Python
135 lines
5.9 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.overview_prompts import ensure_overview_prompt_file, load_overview_prompt
|
|
from scene_search.overviews import _chat_overview, preview
|
|
from tests.test_overviews import VisualOverviewTests
|
|
|
|
|
|
class OverviewPromptFileTests(unittest.TestCase):
|
|
def test_both_styles_seed_once_and_preserve_user_edits(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
root = Path(d)
|
|
paths = {
|
|
"general": root / "nested" / "overview.txt",
|
|
"specialist": root / "nested" / "overview_specialist.txt",
|
|
}
|
|
first = {
|
|
style: ensure_overview_prompt_file(path, style=style)
|
|
for style, path in paths.items()
|
|
}
|
|
self.assertIn("visual overview", first["general"])
|
|
self.assertIn("mature or sensitive", first["specialist"])
|
|
self.assertNotEqual(first["general"], first["specialist"])
|
|
paths["general"].write_text(
|
|
"Summarise in my preferred house style.\n", encoding="utf-8"
|
|
)
|
|
self.assertEqual(
|
|
ensure_overview_prompt_file(paths["general"], style="general"),
|
|
"Summarise in my preferred house style.",
|
|
)
|
|
self.assertIn(
|
|
"mature or sensitive",
|
|
ensure_overview_prompt_file(paths["specialist"], style="specialist"),
|
|
)
|
|
paths["specialist"].write_text("\n", encoding="utf-8")
|
|
with self.assertRaisesRegex(RuntimeError, "empty"):
|
|
ensure_overview_prompt_file(paths["specialist"], style="specialist")
|
|
|
|
def test_selected_prompt_is_passed_to_ollama_as_is(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
root = Path(d)
|
|
general = root / "general.txt"
|
|
specialist = root / "specialist.txt"
|
|
general.write_text("Give a factual one-paragraph description.\n", encoding="utf-8")
|
|
specialist.write_text("Describe mature visual content neutrally.\n", encoding="utf-8")
|
|
paths = SimpleNamespace(
|
|
overview_prompt_file=general,
|
|
overview_specialist_prompt_file=specialist,
|
|
)
|
|
ok = Mock()
|
|
ok.ok = True
|
|
ok.json.return_value = {"message": {"content": "A concise visual summary."}}
|
|
with patch("scene_search.overview_prompts.settings", paths), patch(
|
|
"scene_search.overviews.requests.post", return_value=ok
|
|
) as post:
|
|
self.assertEqual(
|
|
_chat_overview(
|
|
[(0, "People standing outside")], model="text-model",
|
|
ollama_url="http://ollama.test", timeout_seconds=20,
|
|
style="general",
|
|
),
|
|
"A concise visual summary.",
|
|
)
|
|
payload = post.call_args.kwargs["json"]
|
|
self.assertTrue(
|
|
payload["messages"][0]["content"].startswith(
|
|
"Give a factual one-paragraph description.\n\n[00:00:00]"
|
|
)
|
|
)
|
|
self.assertEqual(
|
|
_chat_overview(
|
|
[(0, "People standing outside")], model="text-model",
|
|
ollama_url="http://ollama.test", timeout_seconds=20,
|
|
style="specialist",
|
|
),
|
|
"A concise visual summary.",
|
|
)
|
|
payload = post.call_args.kwargs["json"]
|
|
self.assertTrue(
|
|
payload["messages"][0]["content"].startswith(
|
|
"Describe mature visual content neutrally.\n\n[00:00:00]"
|
|
)
|
|
)
|
|
|
|
def test_editing_prompt_invalidates_only_unpublished_cached_draft(self) -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
root = Path(d)
|
|
db_path = root / "scenes.sqlite3"
|
|
VisualOverviewTests.seed(db_path)
|
|
general = root / "overview.txt"
|
|
specialist = root / "overview_specialist.txt"
|
|
general.write_text("Original instructions.", encoding="utf-8")
|
|
specialist.write_text("Specialist instructions.", encoding="utf-8")
|
|
paths = SimpleNamespace(
|
|
overview_prompt_file=general,
|
|
overview_specialist_prompt_file=specialist,
|
|
)
|
|
calls = []
|
|
def summarize(captions, **kwargs):
|
|
calls.append(kwargs["prompt_text"])
|
|
return f"Summary version {len(calls)}"
|
|
|
|
with patch("scene_search.overview_prompts.settings", paths):
|
|
first = preview(
|
|
db_path=db_path, media_id="media-1",
|
|
model="test-model", style="general", chat_fn=summarize,
|
|
)
|
|
second = preview(
|
|
db_path=db_path, media_id="media-1",
|
|
model="test-model", style="general",
|
|
chat_fn=lambda *a, **kw: self.fail("Cache should be reused"),
|
|
)
|
|
self.assertEqual(first["overview"], second["overview"])
|
|
self.assertEqual(len(calls), 1)
|
|
|
|
general.write_text("Updated instructions.", encoding="utf-8")
|
|
third = preview(
|
|
db_path=db_path, media_id="media-1",
|
|
model="test-model", style="general", chat_fn=summarize,
|
|
)
|
|
self.assertEqual(third["overview"], "Summary version 2")
|
|
self.assertEqual(calls, ["Original instructions.", "Updated instructions."])
|
|
self.assertNotEqual(
|
|
first["caption_fingerprint"], third["caption_fingerprint"]
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|