Files
romm/backend/tests/tasks/test_cleanup_orphaned_resources.py
Georges-Antoine Assi 787bbba2b4 refactor(tasks): give every enqueue one home
Scheduling moved onto RQ, which left the task catalog with two identity
schemes: the registry key a job payload now carries, and the dotted func
path each PeriodicTask still declared. Nothing read the latter, so it is
gone, along with the test that guarded paths no payload names.

Four sites built the same enqueue by hand and had already drifted: the
startup backfills passed the global timeout and no result_ttl, so an
auto-enqueued conversion fell out of the task list after RQ's default
500s while the same task triggered by an admin lived for TASK_RESULT_TTL.
They now go through enqueue_task, which derives timeout, ttl and meta
from the task itself.

Scan discovery paid a Redis round trip per queued job to re-read a status
the fetch already carried, and the stale sweep read every delayed job to
find the few past due. Both now ask Redis once.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-02 21:03:30 -04:00

169 lines
6.1 KiB
Python

import os
from types import SimpleNamespace
from unittest.mock import patch
import pytest
import tasks.scheduled.cleanup_orphaned_resources as mod
from tasks.scheduled.cleanup_orphaned_resources import CleanupOrphanedResourcesTask
class TestCleanupOrphanedResourcesTask:
@pytest.fixture
def task(self):
return CleanupOrphanedResourcesTask()
def test_disabled_by_default(self, task):
# The run-task endpoint rejects a task unless both flags are set, so a
# disabled schedule also means no manual runs.
assert task.enabled is False
assert task.manual_run is True
assert task.can_run_manually is False
def test_enabled_follows_config_flag(self):
with patch.object(mod, "ENABLE_SCHEDULED_CLEANUP_ORPHANED_RESOURCES", True):
task = CleanupOrphanedResourcesTask()
assert task.enabled is True
assert task.can_run_manually is True
def test_cron_string_uses_configured_schedule(self, task):
assert task.cron_string == mod.SCHEDULED_CLEANUP_ORPHANED_RESOURCES_CRON
def test_cron_string_follows_config_override(self):
with patch.object(
mod, "SCHEDULED_CLEANUP_ORPHANED_RESOURCES_CRON", "30 2 * * *"
):
assert CleanupOrphanedResourcesTask().cron_string == "30 2 * * *"
class TestCleanupOrphanedResourcesRun:
@pytest.fixture
def resources_path(self, tmp_path, mocker):
mocker.patch.object(mod, "RESOURCES_BASE_PATH", str(tmp_path))
(tmp_path / "roms").mkdir()
return tmp_path
@staticmethod
def _make_dirs(resources_path, platform_id: int, rom_ids: list[int]) -> None:
for rom_id in rom_ids:
(resources_path / "roms" / str(platform_id) / str(rom_id)).mkdir(
parents=True
)
@staticmethod
def _mock_db(mocker, library: dict[int, list[int]]) -> None:
"""Make the database report `library`, a mapping of platform id to ROM ids."""
mocker.patch.object(
mod.db_platform_handler,
"get_platforms",
return_value=[SimpleNamespace(id=pid) for pid in library],
)
mocker.patch.object(
mod.db_rom_handler,
"get_roms_scalar",
side_effect=lambda platform_ids: [
SimpleNamespace(id=rom_id) for rom_id in library[platform_ids[0]]
],
)
async def test_skips_when_db_empty_and_filesystem_populated(
self, resources_path, mocker
):
self._make_dirs(resources_path, 1, [10])
self._make_dirs(resources_path, 2, [20])
self._mock_db(mocker, {})
stats = await CleanupOrphanedResourcesTask().run()
assert stats["platforms_in_fs"] == 2
assert stats["removed_fs_platforms"] == 0
assert stats["removed_fs_roms"] == 0
assert (resources_path / "roms" / "1" / "10").exists()
assert (resources_path / "roms" / "2" / "20").exists()
async def test_force_cleans_up_when_db_empty(self, resources_path, mocker):
self._make_dirs(resources_path, 1, [10])
self._mock_db(mocker, {})
stats = await CleanupOrphanedResourcesTask().run(force=True)
assert stats["removed_fs_platforms"] == 1
assert not (resources_path / "roms" / "1").exists()
async def test_removes_orphans_when_db_reports_platforms(
self, resources_path, mocker
):
self._make_dirs(resources_path, 1, [10, 11])
self._make_dirs(resources_path, 2, [20])
self._mock_db(mocker, {1: [10]})
stats = await CleanupOrphanedResourcesTask().run()
assert stats["removed_fs_platforms"] == 1
assert stats["removed_fs_roms"] == 2
assert (resources_path / "roms" / "1" / "10").exists()
assert not (resources_path / "roms" / "1" / "11").exists()
assert not (resources_path / "roms" / "2").exists()
async def test_empty_db_and_empty_filesystem_is_not_skipped(
self, resources_path, mocker
):
self._mock_db(mocker, {})
stats = await CleanupOrphanedResourcesTask().run()
assert stats["platforms_in_fs"] == 0
assert stats["removed_fs_platforms"] == 0
class TestScanResourceDirs:
"""The resource tree walk backing the cleanup task."""
@staticmethod
def _make_tree(root, layout: dict[int, list[int]]) -> None:
for platform_id, rom_ids in layout.items():
for rom_id in rom_ids:
(root / str(platform_id) / str(rom_id)).mkdir(parents=True)
def test_maps_platforms_to_rom_dirs(self, tmp_path):
self._make_tree(tmp_path, {1: [10, 11], 2: [20]})
assert mod._scan_resource_dirs(str(tmp_path)) == {1: {10, 11}, 2: {20}}
def test_ignores_non_numeric_names(self, tmp_path):
self._make_tree(tmp_path, {1: [10]})
(tmp_path / "not-a-platform").mkdir()
(tmp_path / "1" / "not-a-rom").mkdir()
assert mod._scan_resource_dirs(str(tmp_path)) == {1: {10}}
def test_ignores_files(self, tmp_path):
self._make_tree(tmp_path, {1: [10]})
(tmp_path / "2").write_text("stray file named like a platform")
(tmp_path / "1" / "20").write_text("stray file named like a rom")
assert mod._scan_resource_dirs(str(tmp_path)) == {1: {10}}
def test_empty_tree(self, tmp_path):
assert mod._scan_resource_dirs(str(tmp_path)) == {}
def test_platform_with_no_rom_dirs(self, tmp_path):
(tmp_path / "1").mkdir()
assert mod._scan_resource_dirs(str(tmp_path)) == {1: set()}
def test_unreadable_directory_yields_empty_set(self, tmp_path):
# A directory that disappears or denies access must not abort the walk,
# otherwise one bad platform would strand every other platform's orphans.
self._make_tree(tmp_path, {1: [10]})
real_scandir = os.scandir
def flaky_scandir(path):
if path.endswith(os.sep + "1"):
raise PermissionError(13, "Permission denied")
return real_scandir(path)
with patch.object(mod.os, "scandir", side_effect=flaky_scandir):
assert mod._scan_resource_dirs(str(tmp_path)) == {1: set()}