mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* fix(platforms): count only the roms the caller can see in rom_count and fs_size_bytes Platform.rom_count and fs_size_bytes are column properties over every rom row on the platform, and the platform endpoints only filter hidden platforms. So the platform header's "In library" count and "On disk" size included roms hidden from the caller (directly, or by an age limit), while the gallery left them out. Server Stats was off the same way: its per-platform metadata coverage divides /api/stats' visibility-filtered match counts by the unfiltered rom_count, so a restricted user saw coverage too low and platform counts and sizes too high. For a restricted caller, the list, detail and update endpoints now subtract the totals of the roms RomVisibilityFilter hides (DBStatsHandler.get_hidden_rom_totals_by_platform, next to the other per-platform aggregates). Direct hides are matched by primary key, so a few hidden roms cost a few lookups rather than a pass over every rom on the platform. Admins and other unrestricted callers keep the model's values and skip the query. On a generated library of 50 platforms and 100k roms (MariaDB), the platform list takes ~44 ms; this adds ~4 ms with 100 roms hidden, ~44 ms with 5,000, and ~60 ms with an age limit (min_age isn't indexed, so that case scans the platforms' roms). Fixes #5312 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(platforms): read the hidden roms' totals in the platform query Addresses Greptile's review of #5313: - Counts can become negative: the hidden totals came from a second query, so a rom added between the two could be subtracted without having been counted. They now come from a grouped subquery outer-joined into the statement that computes the full totals, so both read one snapshot (on PostgreSQL's READ COMMITTED too, which a shared transaction wouldn't give). - Counting rules live in endpoints: the endpoint helper is gone. get_platform and get_platforms take the caller's RomVisibilityFilter (unrestricted by default) and return platforms with the visible totals; DBStatsHandler.get_hidden_rom_totals_by_platform is removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(platforms): code-review fixes for visible platform totals - Re-read rows with populate_existing so a shared session never subtracts twice - Narrow the hidden-totals subquery to the one platform get_platform reads - Re-read after update only for restricted callers, and 404 instead of leaking full totals - Test the update endpoint's restricted totals and the shared-session read Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(platforms): simplify pass on visible platform totals Gate the row clause in one condition with a why comment, fold the MariaDB narrowing reason into the docstring, and drop restating comments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(platforms): count only visible roms in /supported and RetroArch listings - Move the hidden-ROM predicate into RomVisibilityFilter.hidden_on_visible_platforms_clause, with the age rule spelled as ranges an index on min_age can serve - get_platforms reads hidden platforms from the visibility filter instead of a second hidden_platform_ids argument - /api/platforms/supported lists a hidden platform as unmatched and counts only the caller's ROMs on the rest - RetroArch WebDAV skips a platform whose ROMs are all hidden from the caller Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf(roms): index platform_id, min_age, fs_size_bytes for age-limited totals On a 73k-ROM copy of a real library (MariaDB 13), GET /api/platforms for an age-limited user went from 208 ms to 55 ms: the hidden-totals aggregate is now an index-only scan instead of a full scan of the wide roms rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
174 lines
5.9 KiB
Python
174 lines
5.9 KiB
Python
import pytest
|
|
from tests.factories import make_platform, make_rom
|
|
|
|
from handler.auth.rom_visibility import UNRESTRICTED, RomVisibilityFilter
|
|
from handler.database import db_platform_handler
|
|
from handler.database.base_handler import sync_session
|
|
from models.platform import Platform
|
|
|
|
|
|
def test_lookup_finds_a_folder_renamed_only_in_case():
|
|
"""A rescan must reuse the row, not add a second platform beside it."""
|
|
stored = make_platform("n64", fs_slug="nintendo 64")
|
|
|
|
found = db_platform_handler.get_platform_by_fs_slug("Nintendo 64")
|
|
|
|
assert found is not None
|
|
assert found.id == stored.id
|
|
|
|
|
|
def test_lookup_returns_the_exactly_named_folder_when_one_exists():
|
|
"""The exact spelling is tried first, for backends that can tell them apart.
|
|
|
|
MariaDB and MySQL match case-insensitively by collation, so only the
|
|
exactly-named row is asserted here; PostgreSQL additionally distinguishes
|
|
the sibling.
|
|
"""
|
|
exact = make_platform("psx", name="PlayStation")
|
|
|
|
found = db_platform_handler.get_platform_by_fs_slug("psx")
|
|
assert found is not None
|
|
assert found.id == exact.id
|
|
|
|
|
|
def test_rescan_preserves_user_authored_fields(platform):
|
|
"""A rescan merges a freshly built Platform over the existing row.
|
|
|
|
The scan pipeline never populates `custom_name` or `description`, so both
|
|
must survive the merge rather than being reset to their column defaults.
|
|
"""
|
|
db_platform_handler.update_platform(
|
|
platform.id,
|
|
{
|
|
"custom_name": "Sega - Genesis/ Mega Drive (Unofficial)",
|
|
"description": "Aftermarket only",
|
|
},
|
|
)
|
|
|
|
# Mirrors endpoints/sockets/scan.py: build a platform from scan metadata,
|
|
# graft the existing id onto it, then merge.
|
|
scanned = Platform(
|
|
name=platform.name,
|
|
slug=platform.slug,
|
|
fs_slug=platform.fs_slug,
|
|
)
|
|
scanned.id = platform.id
|
|
|
|
merged = db_platform_handler.add_platform(scanned) # noqa: TID251
|
|
|
|
assert merged.custom_name == "Sega - Genesis/ Mega Drive (Unofficial)"
|
|
assert merged.description == "Aftermarket only"
|
|
|
|
|
|
def _pegi(rating: str) -> dict[str, list[dict[str, str]]]:
|
|
return {"age_ratings": [{"category": "PEGI", "rating": rating}]}
|
|
|
|
|
|
PEGI_18 = _pegi("18")
|
|
|
|
|
|
def _totals(platform):
|
|
return platform.rom_count, platform.fs_size_bytes
|
|
|
|
|
|
def test_totals_count_only_the_roms_the_visibility_shows():
|
|
platform_a = make_platform("platform_a")
|
|
platform_b = make_platform("platform_b")
|
|
make_rom(platform_a, "a1", fs_size_bytes=10)
|
|
hidden = make_rom(platform_a, "a2", fs_size_bytes=5)
|
|
make_rom(platform_b, "b1", fs_size_bytes=7)
|
|
visibility = RomVisibilityFilter(hidden_rom_ids=frozenset({hidden.id}))
|
|
|
|
listed = {
|
|
p.id: _totals(p)
|
|
for p in db_platform_handler.get_platforms(visibility=visibility)
|
|
}
|
|
detail = db_platform_handler.get_platform(platform_a.id, visibility=visibility)
|
|
|
|
assert listed[platform_a.id] == (1, 10)
|
|
assert listed[platform_b.id] == (1, 7)
|
|
assert detail is not None and _totals(detail) == (1, 10)
|
|
|
|
|
|
def test_totals_leave_out_roms_above_the_age_limit():
|
|
platform = make_platform("platform_a")
|
|
make_rom(platform, "a1", fs_size_bytes=10)
|
|
make_rom(platform, "a2", fs_size_bytes=5, igdb_metadata=PEGI_18)
|
|
|
|
found = db_platform_handler.get_platform(
|
|
platform.id, visibility=RomVisibilityFilter(age_limit=16)
|
|
)
|
|
|
|
assert found is not None and _totals(found) == (1, 10)
|
|
|
|
|
|
def test_a_rom_hidden_both_ways_is_left_out_once():
|
|
platform = make_platform("platform_a")
|
|
make_rom(platform, "a1", fs_size_bytes=10)
|
|
hidden = make_rom(platform, "a2", fs_size_bytes=5, igdb_metadata=PEGI_18)
|
|
|
|
found = db_platform_handler.get_platform(
|
|
platform.id,
|
|
visibility=RomVisibilityFilter(
|
|
hidden_rom_ids=frozenset({hidden.id}), age_limit=16
|
|
),
|
|
)
|
|
|
|
assert found is not None and _totals(found) == (1, 10)
|
|
|
|
|
|
def test_a_shared_session_rereads_the_totals():
|
|
platform = make_platform("platform_a")
|
|
make_rom(platform, "a1", fs_size_bytes=10)
|
|
hidden = make_rom(platform, "a2", fs_size_bytes=5)
|
|
visibility = RomVisibilityFilter(hidden_rom_ids=frozenset({hidden.id}))
|
|
|
|
reads = []
|
|
with sync_session.begin() as session:
|
|
# Each read returns the session's one instance, so take its totals at once.
|
|
for read_visibility in (visibility, visibility, UNRESTRICTED):
|
|
found = db_platform_handler.get_platform(
|
|
platform.id, visibility=read_visibility, session=session
|
|
)
|
|
assert found is not None
|
|
reads.append(_totals(found))
|
|
|
|
assert reads == [(1, 10), (1, 10), (2, 15)]
|
|
|
|
|
|
def test_totals_are_whole_without_a_visibility():
|
|
platform = make_platform("platform_a")
|
|
make_rom(platform, "a1", fs_size_bytes=10)
|
|
make_rom(platform, "a2", fs_size_bytes=5)
|
|
|
|
found = db_platform_handler.get_platform(platform.id)
|
|
|
|
assert found is not None and _totals(found) == (2, 15)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"visibility",
|
|
[
|
|
RomVisibilityFilter(age_limit=12),
|
|
RomVisibilityFilter(hide_unrated_roms=True),
|
|
RomVisibilityFilter(age_limit=12, hide_unrated_roms=True),
|
|
RomVisibilityFilter(age_limit=12, hidden_rom_ids=frozenset({-1})),
|
|
],
|
|
ids=["limit", "unrated", "limit-and-unrated", "limit-and-hides"],
|
|
)
|
|
def test_totals_agree_with_the_visibility_rule(visibility: RomVisibilityFilter):
|
|
platform = make_platform("platform_a")
|
|
roms = [
|
|
make_rom(platform, "unrated", fs_size_bytes=1),
|
|
make_rom(platform, "pegi3", fs_size_bytes=2, igdb_metadata=_pegi("3")),
|
|
make_rom(platform, "pegi12", fs_size_bytes=4, igdb_metadata=_pegi("12")),
|
|
make_rom(platform, "pegi18", fs_size_bytes=8, igdb_metadata=_pegi("18")),
|
|
]
|
|
shown = [rom for rom in roms if visibility.allows(rom)]
|
|
|
|
found = db_platform_handler.get_platform(platform.id, visibility=visibility)
|
|
|
|
assert 0 < len(shown) < len(roms)
|
|
assert found is not None
|
|
assert _totals(found) == (len(shown), sum(rom.fs_size_bytes for rom in shown))
|