Files
romm/backend/tests/tasks/test_scan_library.py
Georges-Antoine Assi e62c452ea9 fix(tasks): code-review pass on the task catalog split
Loads every model before a job resolves its task, so the mappers
configure in a work horse that imported a single task module. Moves the
httpx2 import into RemoteFilePullTask.run, builds the push-pull job meta
from its spec, gates the scheduled scan on its spec, and widens the
loaded-modules test to cover task modules and httpx2.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-01 11:00:39 -04:00

80 lines
2.8 KiB
Python

from dataclasses import replace
from unittest.mock import AsyncMock, MagicMock
import pytest
from config import SCAN_TIMEOUT
from handler import metadata
from handler.scan_handler import MetadataSource, ScanType
from tasks.scheduled.scan_library import ScanLibraryTask, scan_library_task
class TestScanLibraryTask:
@pytest.fixture
def task(self):
return ScanLibraryTask()
@pytest.fixture
def providers(self, mocker):
"""Every metadata provider off, so one configured in the environment
cannot add itself to the scan."""
handlers = {
name: handler
for name, handler in vars(metadata).items()
if name.startswith("meta_") and hasattr(handler, "is_enabled")
}
for handler in handlers.values():
mocker.patch.object(handler, "is_enabled", return_value=False)
return handlers
def test_init(self, task):
"""Test task initialization"""
assert task.spec.description == "Rescans the entire library"
async def test_run_enabled(self, task, mocker, providers):
"""Test run when scheduled rescan is enabled"""
for name in ("meta_ra_handler", "meta_launchbox_handler"):
mocker.patch.object(providers[name], "is_enabled", return_value=True)
task.spec = replace(task.spec, enabled=True)
scan_result = MagicMock()
mock_scan_platforms = mocker.patch(
"tasks.scheduled.scan_library.scan_platforms",
side_effect=AsyncMock(return_value=scan_result),
)
mock_log = mocker.patch("tasks.scheduled.scan_library.log")
await task.run()
mock_log.info.assert_any_call("Scheduled library scan started...")
mock_scan_platforms.assert_called_once_with(
platform_ids=[],
metadata_sources=[MetadataSource.RA, MetadataSource.LAUNCHBOX],
scan_type=ScanType.QUICK,
)
mock_log.info.assert_any_call("Scheduled library scan done")
async def test_run_disabled(self, task, mocker):
"""Test run when scheduled rescan is disabled"""
task.spec = replace(task.spec, enabled=False)
mock_scan_platforms = mocker.patch(
"tasks.scheduled.scan_library.scan_platforms"
)
mock_log = mocker.patch("tasks.scheduled.scan_library.log")
await task.run()
mock_log.info.assert_called_once_with(
"Scheduled library scan not enabled, skipping..."
)
mock_scan_platforms.assert_not_called()
def test_task_instance(self):
"""Test that the module-level task instance is created correctly"""
assert isinstance(scan_library_task, ScanLibraryTask)
def test_scheduled_rescan_gets_the_scan_timeout():
"""It inherits the five-minute task timeout otherwise, which kills it."""
assert scan_library_task.spec.timeout == SCAN_TIMEOUT