From e32c8c68cd60692457536dd15ebbba2c1bafaaa3 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sat, 29 Aug 2026 15:42:22 -0400 Subject: [PATCH] refactor(firmware): reuse shared helpers in the missing firmware code Swap the hand-rolled platform picker in MissingFirmwareSection for the shared PlatformSelect, and fold the triplicated firmware builder in the tests into an add_firmware factory fixture. Keep Platform out of list_firmware: the relationship is lazy="joined", so every row also ran Platform's rom_count and fs_size_bytes aggregate subqueries over roms, for data no caller reads. In useTaskCompletion, the resolver already identifies the live wait, so drop the parallel generation counter and the unused cancel export. Co-Authored-By: Claude Opus 5 (1M context) --- backend/handler/database/firmware_handler.py | 11 ++- backend/tests/conftest.py | 48 ++++++----- backend/tests/endpoints/test_firmware.py | 8 +- .../handler/database/test_firmware_handler.py | 70 +++++---------- .../tasks/test_cleanup_missing_firmware.py | 57 ++++--------- .../Settings/MissingFirmwareSection.vue | 85 +++---------------- .../Settings/MissingGamesSection.vue | 2 - .../v2/composables/useTaskCompletion/index.ts | 40 +++------ frontend/src/v2/utils/playerFirmware.ts | 9 +- 9 files changed, 105 insertions(+), 225 deletions(-) diff --git a/backend/handler/database/firmware_handler.py b/backend/handler/database/firmware_handler.py index 20fa3f35f..3977cb74a 100644 --- a/backend/handler/database/firmware_handler.py +++ b/backend/handler/database/firmware_handler.py @@ -1,7 +1,7 @@ from collections.abc import Sequence from sqlalchemy import and_, delete, select, update -from sqlalchemy.orm import QueryableAttribute, Session, load_only +from sqlalchemy.orm import QueryableAttribute, Session, load_only, noload from decorators.database import begin_session from models.firmware import Firmware @@ -37,7 +37,14 @@ class DBFirmwareHandler(DBBaseHandler): hidden_platform_ids: Sequence[int] | None = None, session: Session = None, # type: ignore ) -> Sequence[Firmware]: - query = select(Firmware).order_by(Firmware.file_name.asc()) + # `Firmware.platform` is `lazy="joined"`, which drags in Platform's + # rom_count / fs_size_bytes aggregate subqueries over `roms`. No + # caller here reads the relationship, so keep it out of the query. + query = ( + select(Firmware) + .options(noload(Firmware.platform)) + .order_by(Firmware.file_name.asc()) + ) if platform_id: query = query.filter_by(platform_id=platform_id) diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 1a092d413..714576cdf 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -127,33 +127,43 @@ def platform(): return db_platform_handler.add_platform(platform) -def _build_firmware(platform: Platform, file_name: str, missing: bool) -> Firmware: - return Firmware( - platform_id=platform.id, - file_name=file_name, - file_path=f"{platform.fs_slug}/bios", - file_size_bytes=1024, - crc_hash="crc", - md5_hash="md5", - sha1_hash="sha1", - missing_from_fs=missing, - ) +@pytest.fixture +def other_platform(): + platform = Platform(name="other", slug="other_slug", fs_slug="other_slug") + return db_platform_handler.add_platform(platform) @pytest.fixture -def firmware(platform: Platform): +def add_firmware(): + """Factory for firmware rows, defaulting to a file still on disk.""" + + def _add(platform: Platform, file_name: str, missing: bool = False) -> Firmware: + return db_firmware_handler.add_firmware( + Firmware( + platform_id=platform.id, + file_name=file_name, + file_path=f"{platform.fs_slug}/bios", + file_size_bytes=1024, + crc_hash="crc", + md5_hash="md5", + sha1_hash="sha1", + missing_from_fs=missing, + ) + ) + + return _add + + +@pytest.fixture +def firmware(platform: Platform, add_firmware): """Firmware whose file is still on disk.""" - return db_firmware_handler.add_firmware( - _build_firmware(platform, "present.bin", missing=False) - ) + return add_firmware(platform, "present.bin") @pytest.fixture -def missing_firmware(platform: Platform): +def missing_firmware(platform: Platform, add_firmware): """Firmware flagged by a scan as gone from the filesystem.""" - return db_firmware_handler.add_firmware( - _build_firmware(platform, "gone.bin", missing=True) - ) + return add_firmware(platform, "gone.bin", missing=True) @pytest.fixture diff --git a/backend/tests/endpoints/test_firmware.py b/backend/tests/endpoints/test_firmware.py index b117ba118..a523ba0c3 100644 --- a/backend/tests/endpoints/test_firmware.py +++ b/backend/tests/endpoints/test_firmware.py @@ -1,10 +1,4 @@ -"""Tests for `GET /api/firmware`. - -Issue #4075: the endpoint returned every row with no way to select on -`missing_from_fs`, so the player's BIOS list offered (and auto-selected) -firmware whose file was gone, and nothing could list missing firmware -library-wide. -""" +"""Tests for `GET /api/firmware`.""" from fastapi import status diff --git a/backend/tests/handler/database/test_firmware_handler.py b/backend/tests/handler/database/test_firmware_handler.py index 6cf4e048e..bfccf71f4 100644 --- a/backend/tests/handler/database/test_firmware_handler.py +++ b/backend/tests/handler/database/test_firmware_handler.py @@ -1,74 +1,48 @@ -"""Tests for the firmware list filters. +"""Tests for the firmware list filters.""" -A scan flags firmware whose file vanished with `missing_from_fs`, but until -issue #4075 nothing could select on that flag, so every consumer (the player's -BIOS list, the platform firmware count) had to take the whole set. -""" - -from handler.database import db_firmware_handler, db_platform_handler -from models.firmware import Firmware -from models.platform import Platform - - -def _add(platform: Platform, file_name: str, missing: bool) -> Firmware: - return db_firmware_handler.add_firmware( - Firmware( - platform_id=platform.id, - file_name=file_name, - file_path=f"{platform.fs_slug}/bios", - file_size_bytes=1, - crc_hash="crc", - md5_hash="md5", - sha1_hash="sha1", - missing_from_fs=missing, - ) - ) - - -def _other_platform() -> Platform: - return db_platform_handler.add_platform( - Platform(name="other", slug="other_slug", fs_slug="other_slug") - ) +from handler.database import db_firmware_handler class TestListFirmwareMissingFilter: - def test_lists_everything_without_the_filter(self, platform): - _add(platform, "present.bin", missing=False) - _add(platform, "gone.bin", missing=True) + def test_lists_everything_without_the_filter(self, platform, add_firmware): + add_firmware(platform, "present.bin") + add_firmware(platform, "gone.bin", missing=True) names = [f.file_name for f in db_firmware_handler.list_firmware()] assert names == ["gone.bin", "present.bin"] - def test_missing_true_returns_only_flagged_firmware(self, platform): - _add(platform, "present.bin", missing=False) - _add(platform, "gone.bin", missing=True) + def test_missing_true_returns_only_flagged_firmware(self, platform, add_firmware): + add_firmware(platform, "present.bin") + add_firmware(platform, "gone.bin", missing=True) firmware = db_firmware_handler.list_firmware(missing=True) assert [f.file_name for f in firmware] == ["gone.bin"] - def test_missing_false_excludes_flagged_firmware(self, platform): - _add(platform, "present.bin", missing=False) - _add(platform, "gone.bin", missing=True) + def test_missing_false_excludes_flagged_firmware(self, platform, add_firmware): + add_firmware(platform, "present.bin") + add_firmware(platform, "gone.bin", missing=True) firmware = db_firmware_handler.list_firmware(missing=False) assert [f.file_name for f in firmware] == ["present.bin"] - def test_combines_with_the_platform_filter(self, platform): - other = _other_platform() - _add(platform, "gone.bin", missing=True) - _add(other, "other-gone.bin", missing=True) + def test_combines_with_the_platform_filter( + self, platform, other_platform, add_firmware + ): + add_firmware(platform, "gone.bin", missing=True) + add_firmware(other_platform, "other-gone.bin", missing=True) firmware = db_firmware_handler.list_firmware( platform_id=platform.id, missing=True ) assert [f.file_name for f in firmware] == ["gone.bin"] - def test_combines_with_the_hidden_platform_filter(self, platform): - other = _other_platform() - _add(platform, "gone.bin", missing=True) - _add(other, "other-gone.bin", missing=True) + def test_combines_with_the_hidden_platform_filter( + self, platform, other_platform, add_firmware + ): + add_firmware(platform, "gone.bin", missing=True) + add_firmware(other_platform, "other-gone.bin", missing=True) firmware = db_firmware_handler.list_firmware( - missing=True, hidden_platform_ids=[other.id] + missing=True, hidden_platform_ids=[other_platform.id] ) assert [f.file_name for f in firmware] == ["gone.bin"] diff --git a/backend/tests/tasks/test_cleanup_missing_firmware.py b/backend/tests/tasks/test_cleanup_missing_firmware.py index 1ba5392e7..4e3410cab 100644 --- a/backend/tests/tasks/test_cleanup_missing_firmware.py +++ b/backend/tests/tasks/test_cleanup_missing_firmware.py @@ -1,43 +1,14 @@ -"""Tests for CleanupMissingFirmwareTask. - -The ROM side has had a bulk cleanup for missing rows since forever; firmware -flagged by `mark_missing_firmware` had no counterpart, so stale BIOS entries -could only be removed one platform tab at a time (issue #4075). -""" +"""Tests for CleanupMissingFirmwareTask.""" import pytest -from handler.database import db_firmware_handler, db_platform_handler -from models.firmware import Firmware -from models.platform import Platform +from handler.database import db_firmware_handler from tasks.manual.cleanup_missing_firmware import ( CleanupMissingFirmwareTask, cleanup_missing_firmware_task, ) -def _add(platform: Platform, file_name: str, missing: bool) -> Firmware: - return db_firmware_handler.add_firmware( - Firmware( - platform_id=platform.id, - file_name=file_name, - file_path=f"{platform.fs_slug}/bios", - file_size_bytes=1, - crc_hash="crc", - md5_hash="md5", - sha1_hash="sha1", - missing_from_fs=missing, - ) - ) - - -@pytest.fixture -def other_platform() -> Platform: - return db_platform_handler.add_platform( - Platform(name="other", slug="other_slug", fs_slug="other_slug") - ) - - class TestCleanupMissingFirmwareTask: @pytest.fixture def task(self) -> CleanupMissingFirmwareTask: @@ -52,9 +23,9 @@ class TestCleanupMissingFirmwareTask: assert task.can_run_manually is True assert task.cron_string is None - async def test_deletes_only_missing_firmware(self, task, platform): - present = _add(platform, "present.bin", missing=False) - gone = _add(platform, "gone.bin", missing=True) + async def test_deletes_only_missing_firmware(self, task, platform, add_firmware): + present = add_firmware(platform, "present.bin") + gone = add_firmware(platform, "gone.bin", missing=True) stats = await task.run() @@ -64,9 +35,11 @@ class TestCleanupMissingFirmwareTask: assert db_firmware_handler.get_firmware(gone.id) is None assert db_firmware_handler.get_firmware(present.id) is not None - async def test_scopes_to_a_single_platform(self, task, platform, other_platform): - mine = _add(platform, "gone.bin", missing=True) - theirs = _add(other_platform, "other-gone.bin", missing=True) + async def test_scopes_to_a_single_platform( + self, task, platform, other_platform, add_firmware + ): + mine = add_firmware(platform, "gone.bin", missing=True) + theirs = add_firmware(other_platform, "other-gone.bin", missing=True) stats = await task.run(platform_id=platform.id) @@ -75,9 +48,9 @@ class TestCleanupMissingFirmwareTask: assert db_firmware_handler.get_firmware(mine.id) is None assert db_firmware_handler.get_firmware(theirs.id) is not None - async def test_counts_delete_failures(self, task, platform, mocker): - _add(platform, "gone-a.bin", missing=True) - _add(platform, "gone-b.bin", missing=True) + async def test_counts_delete_failures(self, task, platform, mocker, add_firmware): + add_firmware(platform, "gone-a.bin", missing=True) + add_firmware(platform, "gone-b.bin", missing=True) mocker.patch( "tasks.manual.cleanup_missing_firmware.db_firmware_handler.delete_firmware", side_effect=RuntimeError("boom"), @@ -89,8 +62,8 @@ class TestCleanupMissingFirmwareTask: assert stats["firmware_deleted"] == 0 assert stats["errors"] == 2 - async def test_no_missing_firmware_is_a_no_op(self, task, platform): - _add(platform, "present.bin", missing=False) + async def test_no_missing_firmware_is_a_no_op(self, task, platform, add_firmware): + add_firmware(platform, "present.bin") stats = await task.run() diff --git a/frontend/src/v2/components/Settings/MissingFirmwareSection.vue b/frontend/src/v2/components/Settings/MissingFirmwareSection.vue index 917b8446f..987b19a74 100644 --- a/frontend/src/v2/components/Settings/MissingFirmwareSection.vue +++ b/frontend/src/v2/components/Settings/MissingFirmwareSection.vue @@ -1,42 +1,22 @@