diff --git a/backend/handler/database/steam_library_handler.py b/backend/handler/database/steam_library_handler.py index 88f47511d..467df75f3 100644 --- a/backend/handler/database/steam_library_handler.py +++ b/backend/handler/database/steam_library_handler.py @@ -10,7 +10,7 @@ from sqlalchemy.orm import Session from decorators.database import INJECTED_SESSION, begin_session from logger.logger import log from models.platform import Platform -from models.rom import Rom +from models.rom import Rom, rom_has_file_on_disk from models.steam_library import SteamLibraryEntry from models.user import User from utils.database import is_non_blank @@ -62,7 +62,7 @@ class DBSteamLibraryHandler(DBBaseHandler): select(Rom.steam_id, Rom.id) .join(Platform, Platform.id == Rom.platform_id) .where(Platform.slug.in_(platform_slugs), Rom.steam_id.in_(chunk)) - .order_by(Rom.storefront.is_not(None), Rom.id) + .order_by(rom_has_file_on_disk().desc(), Rom.id) ).all() for app_id, rom_id in rows: # The IN filter already excludes a NULL steam_id. diff --git a/backend/models/rom.py b/backend/models/rom.py index 656f6ec55..c4296dc48 100644 --- a/backend/models/rom.py +++ b/backend/models/rom.py @@ -29,6 +29,7 @@ from sqlalchemy import ( false, func, inspect, + not_, or_, select, ) @@ -1478,6 +1479,11 @@ def rom_without_file() -> ColumnElement[bool]: return or_(Rom.is_physical.is_(True), Rom.storefront.is_not(None)) +def rom_has_file_on_disk() -> ColumnElement[bool]: + """Matches rows a readable file backs, as `Rom.has_file_on_disk` does in Python.""" + return and_(not_(rom_without_file()), Rom.missing_from_fs.is_(False)) + + def apply_file_stats(rom: Rom, files: Sequence[RomFile]) -> None: """Fill the deferred file-stat columns from an already-loaded file list.""" set_committed_value( diff --git a/backend/tests/handler/test_steam_library_handler.py b/backend/tests/handler/test_steam_library_handler.py index 8771d6bc0..9da51124a 100644 --- a/backend/tests/handler/test_steam_library_handler.py +++ b/backend/tests/handler/test_steam_library_handler.py @@ -216,6 +216,31 @@ async def test_prefers_a_rom_with_a_file_over_a_store_only_one( assert entry.rom_id == with_file.id +@pytest.mark.parametrize( + ("kind", "overrides"), + [ + ("physical", {"is_physical": True}), + ("missing", {"missing_from_fs": True}), + ], +) +async def test_prefers_a_rom_with_a_file_over_one_without( + kind: str, + overrides: dict[str, Any], + steam_api: MagicMock, + steam_user: User, + win: Platform, +): + """A copy with no readable file loses even when it was created first.""" + make_rom(make_platform("linux"), "Portal 2", steam_id=620, **overrides) + with_file = make_rom(make_platform("mac"), "Portal 2", steam_id=620) + steam_api.get_owned_games.return_value = [_game(620, "Portal 2")] + + await sync_steam_library(steam_user) + + [entry] = db_steam_library_handler.get_entries(steam_user.id) + assert entry.rom_id == with_file.id + + async def test_a_second_sync_reuses_the_store_only_rom( steam_api: MagicMock, steam_user: User, win: Platform ): diff --git a/frontend/src/components/Settings/SteamLibrarySection.test.ts b/frontend/src/components/Settings/SteamLibrarySection.test.ts index 92303c2b2..d0c195c8f 100644 --- a/frontend/src/components/Settings/SteamLibrarySection.test.ts +++ b/frontend/src/components/Settings/SteamLibrarySection.test.ts @@ -100,6 +100,26 @@ describe("SteamLibrarySection", () => { expect(wrapper.text()).toContain("settings.steam-owned-count"); }); + it("ignores a second Sync press while one is in flight", async () => { + storeAuth().setCurrentUser(userFixture({ id: 1, steam_id: STEAM_ID })); + let release!: () => void; + vi.mocked(userApi.syncSteamLibrary).mockReturnValueOnce( + new Promise((resolve) => { + release = resolve as () => void; + }), + ); + const wrapper = mountSection(); + await flushPromises(); + const syncBtn = wrapper.findAll("button")[0]!; + + await syncBtn.trigger("click"); + await syncBtn.trigger("click"); + release(); + await flushPromises(); + + expect(userApi.syncSteamLibrary).toHaveBeenCalledExactlyOnceWith({ id: 1 }); + }); + it("unlinks only once confirmed", async () => { storeAuth().setCurrentUser(userFixture({ id: 1, steam_id: STEAM_ID })); vi.mocked(userApi.unlinkSteamAccount).mockResolvedValue({ diff --git a/frontend/src/components/Settings/SteamLibrarySection.vue b/frontend/src/components/Settings/SteamLibrarySection.vue index c9c3dcb0c..e1a19dbe5 100644 --- a/frontend/src/components/Settings/SteamLibrarySection.vue +++ b/frontend/src/components/Settings/SteamLibrarySection.vue @@ -58,7 +58,7 @@ async function link() { async function sync() { const user = auth.user; - if (!user) return; + if (!user || syncing.value) return; syncing.value = true; try { await userApi.syncSteamLibrary({ id: user.id });