Files
romm/backend/tests/tasks/test_cleanup_missing_roms.py
kynrek f9c8c8ab2a perf(roms): page grouped galleries from the cached id index (#5636)
* perf(roms): page grouped galleries from the cached id index

With group_by_meta_id, every gallery page ran the sibling-dedup window
over the whole library, because the frontend fetches pages with
with_rom_id_index=false. Grouped pages for the whole library or a plain
platform list now slice the versioned, cached id index instead, so only
the first page after a change pays for the window. Filtered, searched and
collection pages still run live.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q367p1Ri2c1dKeYCpQgHeh

* fix(roms): share one cached key for grouped platform sidecars (pr-ready: code-review)

Grouped platform-only sets read their char index, id index and pages
from one key, deleting a platform invalidates the cached lists, and the
platform-only gate derives from has_scope().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(roms): simplify grouped sidecar caching (pr-ready: simplify)

Add RomFilterParams.has_only_platform_scope, share one str key base
between the sidecar key builders, collapse the total and index
branches, and dedupe the platform-key tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(roms): cover has_only_platform_scope (pr-ready: review-polish)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(roms): page grouped galleries from a Redis list, bump on deletes

- Store the cached id index as a Redis list. A grouped page reads its
  window with LRANGE/LLEN instead of decoding the whole list.
- Page requests only read the cache; on a miss they run the live page
  query, so only the index request rebuilds the list.
- Cache grouped platform sets only for a single platform, and never
  store an empty list, so client-chosen platform ids can't grow Redis.
- delete_rom and delete_platform queue the after-commit version bump,
  replacing the hand-placed calls in the endpoints and cleanup task.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
2026-11-18 18:48:02 -05:00

175 lines
5.8 KiB
Python

"""Tests for CleanupMissingRomsTask."""
from unittest.mock import AsyncMock
import pytest
from pytest_mock import MockerFixture
from tests.factories import make_rom
from handler.database import db_collection_handler, db_rom_handler
from handler.database.roms_handler import DBRomsHandler
from models.collection import SmartCollection
from models.platform import Platform
from models.rom import Rom
from models.user import User
from tasks.manual.cleanup_missing_roms import (
CleanupMissingRomsTask,
cleanup_missing_roms_task,
)
def _add_rom(platform: Platform, name: str, *, missing: bool) -> Rom:
return make_rom(platform, name, missing_from_fs=missing)
class TestCleanupMissingRomsTask:
@pytest.fixture
def task(self) -> CleanupMissingRomsTask:
return CleanupMissingRomsTask()
@pytest.fixture(autouse=True)
def resources(self, mocker: MockerFixture) -> AsyncMock:
return mocker.patch(
"tasks.manual.cleanup_missing_roms.fs_resource_handler.remove_directory",
new=AsyncMock(),
)
def test_module_singleton_exists(self):
assert isinstance(cleanup_missing_roms_task, CleanupMissingRomsTask)
async def test_deletes_only_missing_roms(
self, task: CleanupMissingRomsTask, platform: Platform
):
present = _add_rom(platform, "present", missing=False)
gone = _add_rom(platform, "gone", missing=True)
stats = await task.run()
assert stats["roms_found"] == 1
assert stats["roms_deleted"] == 1
assert stats["errors"] == 0
assert db_rom_handler.get_rom(gone.id) is None
assert db_rom_handler.get_rom(present.id) is not None
async def test_drops_cached_filter_values(
self, task: CleanupMissingRomsTask, platform: Platform, mocker: MockerFixture
):
_add_rom(platform, "gone", missing=True)
invalidate = mocker.spy(DBRomsHandler, "invalidate_filter_values_cache")
await task.run()
invalidate.assert_called_once_with()
async def test_deleted_roms_leave_smart_collections(
self,
task: CleanupMissingRomsTask,
platform: Platform,
admin_user: User,
):
present = _add_rom(platform, "present", missing=False)
_add_rom(platform, "gone", missing=True)
collection = db_collection_handler.add_smart_collection(
SmartCollection(
name="Everything here",
description="",
user_id=admin_user.id,
filter_criteria={"platform_ids": [platform.id]},
)
)
seeded = db_collection_handler.refresh_smart_collection(collection.id)
assert seeded is not None and seeded.rom_count == 2
await task.run()
after = db_collection_handler.get_smart_collection(collection.id)
assert after is not None
assert after.rom_ids == [present.id]
assert after.rom_count == 1
async def test_nothing_deleted_leaves_the_caches_alone(
self, task: CleanupMissingRomsTask, platform: Platform, mocker: MockerFixture
):
_add_rom(platform, "present", missing=False)
invalidate = mocker.spy(DBRomsHandler, "invalidate_filter_values_cache")
refresh = mocker.spy(
db_collection_handler, "refresh_smart_collections_for_roms"
)
await task.run()
invalidate.assert_not_called()
refresh.assert_not_called()
async def test_a_failed_delete_is_counted_and_not_refreshed(
self, task: CleanupMissingRomsTask, platform: Platform, mocker: MockerFixture
):
_add_rom(platform, "gone", missing=True)
mocker.patch(
"tasks.manual.cleanup_missing_roms.db_rom_handler.delete_rom",
side_effect=RuntimeError("boom"),
)
refresh = mocker.spy(
db_collection_handler, "refresh_smart_collections_for_roms"
)
stats = await task.run()
assert stats["errors"] == 1
assert stats["roms_deleted"] == 0
refresh.assert_not_called()
async def test_a_resource_failure_still_refreshes_what_was_deleted(
self,
task: CleanupMissingRomsTask,
platform: Platform,
resources: AsyncMock,
mocker: MockerFixture,
):
gone = _add_rom(platform, "gone", missing=True)
resources.side_effect = PermissionError("read-only")
invalidate = mocker.spy(DBRomsHandler, "invalidate_filter_values_cache")
refresh = mocker.spy(
db_collection_handler, "refresh_smart_collections_for_roms"
)
with pytest.raises(PermissionError):
await task.run()
assert db_rom_handler.get_rom(gone.id) is None
invalidate.assert_called_once_with()
refresh.assert_called_once_with([gone.id])
async def test_a_refresh_failure_does_not_fail_the_run(
self, task: CleanupMissingRomsTask, platform: Platform, mocker: MockerFixture
):
_add_rom(platform, "gone", missing=True)
mocker.patch.object(
db_collection_handler,
"refresh_smart_collections_for_roms",
side_effect=RuntimeError("boom"),
)
stats = await task.run()
assert stats["roms_deleted"] == 1
assert stats["errors"] == 0
async def test_a_cache_failure_still_refreshes_smart_collections(
self, task: CleanupMissingRomsTask, platform: Platform, mocker: MockerFixture
):
rom = _add_rom(platform, "gone", missing=True)
mocker.patch.object(
DBRomsHandler,
"invalidate_filter_values_cache",
side_effect=RuntimeError("redis down"),
)
refresh = mocker.patch.object(
db_collection_handler, "refresh_smart_collections_for_roms"
)
stats = await task.run()
refresh.assert_called_once_with([rom.id])
assert stats["roms_deleted"] == 1