mirror of
https://github.com/rommapp/romm.git
synced 2026-12-04 12:52:19 +01:00
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:
@@ -10,7 +10,7 @@ from sqlalchemy.orm import Session
|
|||||||
from decorators.database import INJECTED_SESSION, begin_session
|
from decorators.database import INJECTED_SESSION, begin_session
|
||||||
from logger.logger import log
|
from logger.logger import log
|
||||||
from models.platform import Platform
|
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.steam_library import SteamLibraryEntry
|
||||||
from models.user import User
|
from models.user import User
|
||||||
from utils.database import is_non_blank
|
from utils.database import is_non_blank
|
||||||
@@ -62,7 +62,7 @@ class DBSteamLibraryHandler(DBBaseHandler):
|
|||||||
select(Rom.steam_id, Rom.id)
|
select(Rom.steam_id, Rom.id)
|
||||||
.join(Platform, Platform.id == Rom.platform_id)
|
.join(Platform, Platform.id == Rom.platform_id)
|
||||||
.where(Platform.slug.in_(platform_slugs), Rom.steam_id.in_(chunk))
|
.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()
|
).all()
|
||||||
for app_id, rom_id in rows:
|
for app_id, rom_id in rows:
|
||||||
# The IN filter already excludes a NULL steam_id.
|
# The IN filter already excludes a NULL steam_id.
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ from sqlalchemy import (
|
|||||||
false,
|
false,
|
||||||
func,
|
func,
|
||||||
inspect,
|
inspect,
|
||||||
|
not_,
|
||||||
or_,
|
or_,
|
||||||
select,
|
select,
|
||||||
)
|
)
|
||||||
@@ -1478,6 +1479,11 @@ def rom_without_file() -> ColumnElement[bool]:
|
|||||||
return or_(Rom.is_physical.is_(True), Rom.storefront.is_not(None))
|
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:
|
def apply_file_stats(rom: Rom, files: Sequence[RomFile]) -> None:
|
||||||
"""Fill the deferred file-stat columns from an already-loaded file list."""
|
"""Fill the deferred file-stat columns from an already-loaded file list."""
|
||||||
set_committed_value(
|
set_committed_value(
|
||||||
|
|||||||
@@ -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
|
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(
|
async def test_a_second_sync_reuses_the_store_only_rom(
|
||||||
steam_api: MagicMock, steam_user: User, win: Platform
|
steam_api: MagicMock, steam_user: User, win: Platform
|
||||||
):
|
):
|
||||||
|
|||||||
@@ -100,6 +100,26 @@ describe("SteamLibrarySection", () => {
|
|||||||
expect(wrapper.text()).toContain("settings.steam-owned-count");
|
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 () => {
|
it("unlinks only once confirmed", async () => {
|
||||||
storeAuth().setCurrentUser(userFixture({ id: 1, steam_id: STEAM_ID }));
|
storeAuth().setCurrentUser(userFixture({ id: 1, steam_id: STEAM_ID }));
|
||||||
vi.mocked(userApi.unlinkSteamAccount).mockResolvedValue({
|
vi.mocked(userApi.unlinkSteamAccount).mockResolvedValue({
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ async function link() {
|
|||||||
|
|
||||||
async function sync() {
|
async function sync() {
|
||||||
const user = auth.user;
|
const user = auth.user;
|
||||||
if (!user) return;
|
if (!user || syncing.value) return;
|
||||||
syncing.value = true;
|
syncing.value = true;
|
||||||
try {
|
try {
|
||||||
await userApi.syncSteamLibrary({ id: user.id });
|
await userApi.syncSteamLibrary({ id: user.id });
|
||||||
|
|||||||
Reference in New Issue
Block a user