from __future__ import annotations import tempfile import unittest from pathlib import Path from types import SimpleNamespace from unittest.mock import patch from scene_search.index_jobs import IndexJobManager from scene_search.index_report import load_index_report, save_index_report class IndexReportTests(unittest.TestCase): def test_report_persists_across_process_state(self) -> None: with tempfile.TemporaryDirectory() as tmp: fake_settings = SimpleNamespace(data_dir=Path(tmp)) report = { "available": True, "kind": "index", "status": "complete", "summary": {"failed": 1, "pruned": 1, "indexed": 1}, "items": [ {"status": "failed", "title": "Bad", "reason": "No Trickplay"}, {"status": "removed", "title": "Gone", "reason": "Removed"}, {"status": "indexed", "title": "Good", "reason": "Indexed"}, ], } with patch("scene_search.index_report.settings", fake_settings): save_index_report(report) loaded = load_index_report() self.assertTrue(loaded["available"]) self.assertEqual(loaded["summary"]["failed"], 1) self.assertEqual(len(loaded["items"]), 3) def test_manager_report_filters_and_pages_without_polling_all_items(self) -> None: manager = IndexJobManager(SimpleNamespace()) # type: ignore[arg-type] stored = { "available": True, "kind": "index", "status": "complete", "started_at": "start", "finished_at": "finish", "summary": {"failed": 3}, "items": [ {"status": "failed", "title": "A"}, {"status": "indexed", "title": "B"}, {"status": "failed", "title": "C"}, {"status": "failed", "title": "D"}, ], } with patch("scene_search.index_jobs.load_index_report", return_value=stored): page = manager.report(category="failed", offset=1, limit=1) self.assertEqual(page["total"], 3) self.assertEqual(page["offset"], 1) self.assertEqual(page["limit"], 1) self.assertEqual([item["title"] for item in page["items"]], ["C"]) if __name__ == "__main__": unittest.main()