fix(steam): prefer a rom with a file, and guard a second sync press

Two findings from Copilot's review of the combined test branch.

`get_rom_ids_by_steam_app_id` ordered by `storefront IS NOT NULL`, which
only sorts store-only roms last. A physical or missing copy also has no
file and a lower id won over a downloadable one, against the docstring.
A new `rom_has_file_on_disk()` gives `Rom.has_file_on_disk` its SQL twin,
beside the `never_has_file` and `rom_without_file()` pair, and the query
orders by it.

`SteamLibrarySection.sync()` set its busy flag without checking it first,
and `RBtn`'s `loading` only changes how the button looks, so a second
press queued another sync and the first reply cleared the flag under the
second. It now returns early while a sync is in flight, as `link()` does.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NH5s4gb7QNE7xa4pRUsBzx
This commit is contained in:
Claude
2026-11-03 21:17:02 +00:00
parent fbc2ad7341
commit f3ca7b0869
5 changed files with 54 additions and 3 deletions

View File

@@ -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.

View File

@@ -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(

View File

@@ -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
):

View File

@@ -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<never>((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({

View File

@@ -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 });