Files
romm/backend/tests/test_startup.py
Sam Dornan bd16648c9a chore(mypy): switch to strict mode with listed exceptions (#4817)
* chore(mypy): switch to strict mode with listed exceptions

Replace the hand-picked flags with `strict = True`, turning off only the
four flags that aren't clean yet (disallow_any_generics,
warn_return_any, disallow_untyped_calls, disallow_untyped_decorators).
Any other strict flag, including those future mypy releases add to the
preset, is on by default.

That turns on no_implicit_reexport, strict_equality and extra_checks:
- Import each name from the module that defines it. About 240 test
  sites patched a dependency through the module that imported it
  (`scan_module.fs_rom_handler`); they now import the same object from
  its source, so every patch targets what it did before. Tests that
  replace a module attribute are unchanged.
- handler.streaming.memory_cards re-exports store_memory_card_version
  explicitly, since endpoints call it there and tests patch it there.
- mutagen gets implicit_reexport, as it re-exports without __all__.
- Test comparisons build typed values (CoverResult, a typed expected
  dict, enum .value) and the CORS lookup casts for its identity check.
- One ss_handler ** spread keeps a coded ignore: extra_checks wants its
  NotRequired dump_* keys spelled out, and the media dict never has them.

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

* test(upload): assert cleanup against the patched upload directory

The 409 test checked config's ROM_UPLOAD_TMP_BASE while the fixture patches
the endpoint's copy to tmp_path/uploads, so it passed even if cleanup failed.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 15:45:37 -05:00

211 lines
7.0 KiB
Python

"""Tests for startup-time auto-enqueue of the recompute task."""
from unittest.mock import AsyncMock
import pytest
import startup
from rq.exceptions import DuplicateJobError
from rq.job import JOB_ID_PATTERN
from handler.database import db_save_handler
from handler.metadata.launchbox_handler.types import LAUNCHBOX_METADATA_STORE
from handler.redis_handler import default_queue, high_prio_queue, low_prio_queue
from tasks.registry import get_task
from tasks.scheduled.update_switch_titledb import SWITCH_TITLEDB_STORE
from utils.cache import VersionedCacheStore
@pytest.fixture
def enqueue_task(mocker):
return mocker.patch.object(startup, "enqueue_task")
def test_enqueue_recompute_skips_when_no_missing_hashes(mocker, enqueue_task):
"""Saves all have content_hash -> no enqueue."""
mocker.patch.object(
db_save_handler, "count_saves_missing_content_hash", return_value=0
)
startup._enqueue_recompute_save_hashes_if_needed()
enqueue_task.assert_not_called()
def test_enqueue_recompute_fires_when_missing_hashes_present(mocker, enqueue_task):
"""At least one Save row has NULL content_hash -> enqueue exactly once."""
mocker.patch.object(
db_save_handler, "count_saves_missing_content_hash", return_value=42
)
startup._enqueue_recompute_save_hashes_if_needed()
enqueue_task.assert_called_once_with(
"recompute_save_content_hashes",
job_id=startup.RECOMPUTE_SAVE_HASHES_JOB_ID,
# RQ settles the duplicate check and the enqueue in one round trip
unique=True,
)
def test_enqueue_convert_webp_fires_when_not_queued(enqueue_task):
"""No in-flight bootstrap job -> enqueue the backfill exactly once."""
startup._enqueue_convert_images_to_webp()
enqueue_task.assert_called_once_with(
"convert_images_to_webp",
job_id=startup.CONVERT_IMAGES_TO_WEBP_JOB_ID,
unique=True,
)
@pytest.mark.parametrize(
"job_id",
(startup.RECOMPUTE_SAVE_HASHES_JOB_ID, startup.CONVERT_IMAGES_TO_WEBP_JOB_ID),
)
def test_backfill_job_ids_are_valid_rq_ids(job_id):
"""RQ rejects any job_id not matching [A-Za-z0-9_-]+ (ValueError in set_id),
which the broad except here would swallow -> backfill silently never
enqueues. A colon was the original culprit."""
assert JOB_ID_PATTERN.fullmatch(job_id)
def test_both_backfills_name_a_registered_task():
"""The name in the payload is all the runner gets, so it has to resolve."""
assert get_task("recompute_save_content_hashes") is not None
assert get_task("convert_images_to_webp") is not None
@pytest.mark.parametrize(
"error", (DuplicateJobError("exists"), RuntimeError("redis gone"))
)
def test_a_failed_backfill_enqueue_does_not_crash_startup(mocker, error):
"""An in-flight job from a previous restart, or a Redis outage, is survivable."""
mocker.patch.object(
db_save_handler, "count_saves_missing_content_hash", return_value=10
)
mocker.patch.object(startup, "enqueue_task", side_effect=error)
startup._enqueue_recompute_save_hashes_if_needed()
startup._enqueue_convert_images_to_webp()
def test_enqueue_recompute_swallows_count_error(mocker, enqueue_task):
"""A failed COUNT query must not crash startup."""
mocker.patch.object(
db_save_handler,
"count_saves_missing_content_hash",
side_effect=RuntimeError("db gone"),
)
startup._enqueue_recompute_save_hashes_if_needed()
enqueue_task.assert_not_called()
class TestDropLegacySchedulerState:
"""The old scheduler's keys go on the first start after the migration."""
@pytest.fixture
def redis(self, mocker):
redis = mocker.patch.object(startup, "redis_client")
redis.scan_iter.return_value = []
return redis
def test_removes_the_scheduler_keys_with_no_jobs_left_behind(self, redis):
redis.zrange.return_value = []
startup._drop_legacy_scheduler_state()
redis.delete.assert_called_once_with(*startup.LEGACY_SCHEDULER_KEYS)
def test_deletes_orphaned_jobs_but_not_queued_ones(self, mocker, redis):
redis.zrange.return_value = [b"orphan", b"queued"]
for queue in (high_prio_queue, default_queue):
mocker.patch.object(queue, "get_job_ids", return_value=[])
mocker.patch.object(low_prio_queue, "get_job_ids", return_value=["queued"])
startup._drop_legacy_scheduler_state()
assert redis.delete.call_args_list[0].args == ("rq:job:orphan",)
assert redis.delete.call_args_list[-1].args == startup.LEGACY_SCHEDULER_KEYS
def test_survives_a_redis_failure(self, redis):
redis.zrange.side_effect = RuntimeError("redis gone")
startup._drop_legacy_scheduler_state()
class TestRebuildOutdatedMetadataStores:
"""A store an older release wrote is dropped, and its rebuild queued."""
@pytest.fixture
def drop_stale(self, mocker):
"""Report only the stores named as stale, whatever order they run in."""
def patch_drop(*stale: VersionedCacheStore):
async def drop(_cache, store):
return store in stale
return mocker.patch.object(
startup, "drop_stale_cache_store", new=AsyncMock(side_effect=drop)
)
return patch_drop
async def test_stale_launchbox_store_is_dropped_and_rebuilt(
self, mocker, drop_stale, enqueue_task
):
mocker.patch.object(startup, "LAUNCHBOX_API_ENABLED", True)
drop_stale(LAUNCHBOX_METADATA_STORE)
await startup._rebuild_outdated_metadata_stores()
enqueue_task.assert_called_once_with(
"update_launchbox_metadata",
job_id=startup.UPDATE_LAUNCHBOX_METADATA_JOB_ID,
unique=True,
)
async def test_launchbox_rebuild_waits_on_the_provider_being_enabled(
self, mocker, drop_stale, enqueue_task
):
"""Dropping reclaims the memory; refilling it would serve nobody."""
mocker.patch.object(startup, "LAUNCHBOX_API_ENABLED", False)
mocker.patch.object(
startup,
"VERSIONED_METADATA_STORES",
(
(
LAUNCHBOX_METADATA_STORE,
"update_launchbox_metadata",
startup.UPDATE_LAUNCHBOX_METADATA_JOB_ID,
False,
),
),
)
drop_stale(LAUNCHBOX_METADATA_STORE)
await startup._rebuild_outdated_metadata_stores()
enqueue_task.assert_not_called()
async def test_stale_switch_store_is_dropped_and_rebuilt(
self, drop_stale, enqueue_task
):
drop_stale(SWITCH_TITLEDB_STORE)
await startup._rebuild_outdated_metadata_stores()
enqueue_task.assert_called_once_with(
"update_switch_titledb",
job_id=startup.UPDATE_SWITCH_TITLEDB_JOB_ID,
unique=True,
)
async def test_current_stores_queue_nothing(self, drop_stale, enqueue_task):
drop_stale()
await startup._rebuild_outdated_metadata_stores()
enqueue_task.assert_not_called()