From 49d1ae71b2ae7e1e5318ab2bd3f073f773eef151 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sat, 25 Jul 2026 13:53:02 -0400 Subject: [PATCH] Fix smart collections dropping newer filters Smart collections silently ignored several filters at both ends: - The backend never applied has_soundtrack, has_saves, has_states, or player_counts from stored criteria, so a "has soundtrack" smart collection contained every game. get_roms_scalar also dropped has_soundtrack before it reached filter_roms. - The v2 criteria serializer had no fields for has_saves/has_states and only stored tri-state filters when true, so "unmatched" or "has saves" produced empty criteria and the no-filters guard blocked the save. Criteria now carry saves/states filters and explicit false states, the backend applies them, and summaries render negative states with the existing "show-not-*" labels. Fixes #3888 Co-Authored-By: Claude Fable 5 --- .../handler/database/collections_handler.py | 5 ++ backend/handler/database/roms_handler.py | 1 + .../database/test_collections_handler.py | 62 ++++++++++++++++ .../handler/database/test_roms_handler.py | 33 +++++++++ .../Dialogs/CreateSmartCollectionDialog.vue | 2 + .../v2/utils/smartCollectionCriteria.test.ts | 56 +++++++++++++++ .../src/v2/utils/smartCollectionCriteria.ts | 71 ++++++++++++++++--- 7 files changed, 222 insertions(+), 8 deletions(-) diff --git a/backend/handler/database/collections_handler.py b/backend/handler/database/collections_handler.py index f38e7c874..33826df86 100644 --- a/backend/handler/database/collections_handler.py +++ b/backend/handler/database/collections_handler.py @@ -511,6 +511,9 @@ class DBCollectionsHandler(DBBaseHandler): duplicate=criteria.get("duplicate"), playable=criteria.get("playable"), has_ra=criteria.get("has_ra"), + has_saves=criteria.get("has_saves"), + has_states=criteria.get("has_states"), + has_soundtrack=criteria.get("has_soundtrack"), missing=criteria.get("missing"), verified=criteria.get("verified"), genres=genres, @@ -521,6 +524,7 @@ class DBCollectionsHandler(DBBaseHandler): statuses=statuses, regions=regions, languages=languages, + player_counts=criteria.get("player_counts"), tags=tags, metadata_providers=criteria.get("metadata_providers"), # Logic operators for multi-value filters @@ -531,6 +535,7 @@ class DBCollectionsHandler(DBBaseHandler): age_ratings_logic=criteria.get("age_ratings_logic", "any"), regions_logic=criteria.get("regions_logic", "any"), languages_logic=criteria.get("languages_logic", "any"), + player_counts_logic=criteria.get("player_counts_logic", "any"), statuses_logic=criteria.get("statuses_logic", "any"), metadata_providers_logic=criteria.get("metadata_providers_logic", "any"), tags_logic=criteria.get("tags_logic", "any"), diff --git a/backend/handler/database/roms_handler.py b/backend/handler/database/roms_handler.py index c64c9865e..51a319d26 100644 --- a/backend/handler/database/roms_handler.py +++ b/backend/handler/database/roms_handler.py @@ -1360,6 +1360,7 @@ class DBRomsHandler(DBBaseHandler): has_ra=kwargs.get("has_ra", None), has_saves=kwargs.get("has_saves", None), has_states=kwargs.get("has_states", None), + has_soundtrack=kwargs.get("has_soundtrack", None), missing=kwargs.get("missing", None), verified=kwargs.get("verified", None), genres=kwargs.get("genres", None), diff --git a/backend/tests/handler/database/test_collections_handler.py b/backend/tests/handler/database/test_collections_handler.py index fb86a6c50..903404960 100644 --- a/backend/tests/handler/database/test_collections_handler.py +++ b/backend/tests/handler/database/test_collections_handler.py @@ -179,6 +179,68 @@ def test_get_smart_collection_roms_passes_metadata_providers(admin_user: User, m assert get_roms_scalar.call_args.kwargs["metadata_providers_logic"] == "all" +def test_get_smart_collection_roms_passes_asset_and_soundtrack_filters( + admin_user: User, mocker +): + get_roms_scalar = mocker.patch("handler.database.db_rom_handler.get_roms_scalar") + smart_collection = SmartCollection( + name="Games with saves and music", + description="", + user_id=admin_user.id, + filter_criteria={ + "has_saves": True, + "has_states": True, + "has_soundtrack": True, + }, + ) + + db_collection_handler.get_smart_collection_roms( + smart_collection, user_id=admin_user.id + ) + + assert get_roms_scalar.call_args.kwargs["has_saves"] is True + assert get_roms_scalar.call_args.kwargs["has_states"] is True + assert get_roms_scalar.call_args.kwargs["has_soundtrack"] is True + + +def test_get_smart_collection_roms_passes_negative_boolean_filters( + admin_user: User, mocker +): + get_roms_scalar = mocker.patch("handler.database.db_rom_handler.get_roms_scalar") + smart_collection = SmartCollection( + name="Unmatched games", + description="", + user_id=admin_user.id, + filter_criteria={"matched": False}, + ) + + db_collection_handler.get_smart_collection_roms( + smart_collection, user_id=admin_user.id + ) + + assert get_roms_scalar.call_args.kwargs["matched"] is False + + +def test_get_smart_collection_roms_passes_player_counts(admin_user: User, mocker): + get_roms_scalar = mocker.patch("handler.database.db_rom_handler.get_roms_scalar") + smart_collection = SmartCollection( + name="Multiplayer games", + description="", + user_id=admin_user.id, + filter_criteria={ + "player_counts": ["2", "4"], + "player_counts_logic": "any", + }, + ) + + db_collection_handler.get_smart_collection_roms( + smart_collection, user_id=admin_user.id + ) + + assert get_roms_scalar.call_args.kwargs["player_counts"] == ["2", "4"] + assert get_roms_scalar.call_args.kwargs["player_counts_logic"] == "any" + + def test_get_smart_collection_roms_passes_tags(admin_user: User, mocker): get_roms_scalar = mocker.patch("handler.database.db_rom_handler.get_roms_scalar") smart_collection = SmartCollection( diff --git a/backend/tests/handler/database/test_roms_handler.py b/backend/tests/handler/database/test_roms_handler.py index 34f4ddeb0..274efef7b 100644 --- a/backend/tests/handler/database/test_roms_handler.py +++ b/backend/tests/handler/database/test_roms_handler.py @@ -192,6 +192,39 @@ def _sync(rom: Rom, scanned: list[RomFile]) -> list[RomFile]: return db_rom_handler.sync_rom_files(rom.id, scanned).files +class TestHasSoundtrackFilter: + """Smart collections resolve their criteria through `get_roms_scalar`, so + it must forward has_soundtrack to filter_roms like the other flags.""" + + def _with_soundtrack(self, rom: Rom) -> None: + _sync( + rom, + [_scanned_file(rom, "track01.flac", category=RomFileCategory.SOUNDTRACK)], + ) + + def test_has_soundtrack_true_matches_only_roms_with_tracks( + self, rom: Rom, platform: Platform + ): + other = db_rom_handler.add_rom(_make_rom(platform, "No Music.gba")) + self._with_soundtrack(rom) + + ids = {r.id for r in db_rom_handler.get_roms_scalar(has_soundtrack=True)} + + assert rom.id in ids + assert other.id not in ids + + def test_has_soundtrack_false_excludes_roms_with_tracks( + self, rom: Rom, platform: Platform + ): + other = db_rom_handler.add_rom(_make_rom(platform, "No Music.gba")) + self._with_soundtrack(rom) + + ids = {r.id for r in db_rom_handler.get_roms_scalar(has_soundtrack=False)} + + assert rom.id not in ids + assert other.id in ids + + class TestSyncRomFiles: """A rescan reconciles the file rows in place, so ids survive it. Anything keyed on a file id (track metadata, persisted soundtrack covers) stays diff --git a/frontend/src/v2/components/Dialogs/CreateSmartCollectionDialog.vue b/frontend/src/v2/components/Dialogs/CreateSmartCollectionDialog.vue index bb0296510..3ab673828 100644 --- a/frontend/src/v2/components/Dialogs/CreateSmartCollectionDialog.vue +++ b/frontend/src/v2/components/Dialogs/CreateSmartCollectionDialog.vue @@ -89,6 +89,8 @@ const openHandler = () => { filterDuplicates: galleryFilter.filterDuplicates, filterPlayables: galleryFilter.filterPlayables, filterRA: galleryFilter.filterRA, + filterSaves: galleryFilter.filterSaves, + filterStates: galleryFilter.filterStates, filterSoundtrack: galleryFilter.filterSoundtrack, filterMissing: galleryFilter.filterMissing, filterVerified: galleryFilter.filterVerified, diff --git a/frontend/src/v2/utils/smartCollectionCriteria.test.ts b/frontend/src/v2/utils/smartCollectionCriteria.test.ts index 2fc04aaa9..52fc2ec7d 100644 --- a/frontend/src/v2/utils/smartCollectionCriteria.test.ts +++ b/frontend/src/v2/utils/smartCollectionCriteria.test.ts @@ -20,6 +20,8 @@ function emptySnapshot(): GalleryFilterSnapshot { filterDuplicates: null, filterPlayables: null, filterRA: null, + filterSaves: null, + filterStates: null, filterSoundtrack: null, filterMissing: null, filterVerified: null, @@ -131,3 +133,57 @@ describe("buildSmartFilterCriteria — soundtrack", () => { ); }); }); + +describe("buildSmartFilterCriteria — saves and states", () => { + it("serializes the saves and states filters", () => { + const out = buildSmartFilterCriteria({ + ...emptySnapshot(), + filterSaves: true, + filterStates: true, + }); + + expect(out.has_saves).toBe(true); + expect(out.has_states).toBe(true); + }); + + it("surfaces saves and states in the human-readable summary", () => { + const rows = summarizeSmartFilterCriteria( + { has_saves: true, has_states: true }, + tStub, + ); + + expect(rows).toContainEqual(expect.objectContaining({ key: "has_saves" })); + expect(rows).toContainEqual(expect.objectContaining({ key: "has_states" })); + }); +}); + +describe("buildSmartFilterCriteria — negative tri-state filters", () => { + it("serializes `false` states instead of dropping them", () => { + const out = buildSmartFilterCriteria({ + ...emptySnapshot(), + filterMatched: false, + filterSaves: false, + }); + + expect(out.matched).toBe(false); + expect(out.has_saves).toBe(false); + }); + + it("omits tri-state keys only when the filter is unset (null)", () => { + const out = buildSmartFilterCriteria(emptySnapshot()); + + expect(out.matched).toBeUndefined(); + expect(out.favorite).toBeUndefined(); + expect(out.has_saves).toBeUndefined(); + expect(out.has_states).toBeUndefined(); + expect(out.has_soundtrack).toBeUndefined(); + }); + + it("renders `false` values with their negative label in the summary", () => { + const rows = summarizeSmartFilterCriteria({ matched: false }, tStub); + + const row = rows.find((r) => r.key === "matched"); + expect(row).toBeDefined(); + expect(row?.label).toBe("Show unmatched"); + }); +}); diff --git a/frontend/src/v2/utils/smartCollectionCriteria.ts b/frontend/src/v2/utils/smartCollectionCriteria.ts index d2b102485..5c6da5da0 100644 --- a/frontend/src/v2/utils/smartCollectionCriteria.ts +++ b/frontend/src/v2/utils/smartCollectionCriteria.ts @@ -32,6 +32,8 @@ export interface SmartFilterCriteria { duplicate?: boolean; playable?: boolean; has_ra?: boolean; + has_saves?: boolean; + has_states?: boolean; has_soundtrack?: boolean; missing?: boolean; verified?: boolean; @@ -69,6 +71,8 @@ export interface GalleryFilterSnapshot { filterDuplicates: boolean | null; filterPlayables: boolean | null; filterRA: boolean | null; + filterSaves: boolean | null; + filterStates: boolean | null; filterSoundtrack: boolean | null; filterMissing: boolean | null; filterVerified: boolean | null; @@ -142,14 +146,19 @@ export function buildSmartFilterCriteria( out.smart_collection_id = context.currentSmartCollectionId; } - if (snap.filterMatched) out.matched = true; - if (snap.filterFavorites) out.favorite = true; - if (snap.filterDuplicates) out.duplicate = true; - if (snap.filterPlayables) out.playable = true; - if (snap.filterRA) out.has_ra = true; - if (snap.filterSoundtrack) out.has_soundtrack = true; - if (snap.filterMissing) out.missing = true; - if (snap.filterVerified) out.verified = true; + // Tri-state filters: `false` is a real criterion ("show unmatched"), + // only `null` means "not filtering on this". + if (snap.filterMatched !== null) out.matched = snap.filterMatched; + if (snap.filterFavorites !== null) out.favorite = snap.filterFavorites; + if (snap.filterDuplicates !== null) out.duplicate = snap.filterDuplicates; + if (snap.filterPlayables !== null) out.playable = snap.filterPlayables; + if (snap.filterRA !== null) out.has_ra = snap.filterRA; + if (snap.filterSaves !== null) out.has_saves = snap.filterSaves; + if (snap.filterStates !== null) out.has_states = snap.filterStates; + if (snap.filterSoundtrack !== null) + out.has_soundtrack = snap.filterSoundtrack; + if (snap.filterMissing !== null) out.missing = snap.filterMissing; + if (snap.filterVerified !== null) out.verified = snap.filterVerified; if (snap.selectedGenres.length > 0) { out.genres = snap.selectedGenres; @@ -236,6 +245,9 @@ interface FieldSpec { icon: string; labelKey: string; defaultLabel: string; + /** "bool" fields only: label shown when the stored value is `false`. */ + negLabelKey?: string; + negDefaultLabel?: string; kind: FieldKind; } @@ -281,6 +293,8 @@ const FIELDS: FieldSpec[] = [ icon: "mdi-star", labelKey: "platform.show-favorites", defaultLabel: "Show favourites", + negLabelKey: "platform.show-not-favorites-only", + negDefaultLabel: "Show non-favourite ROMs only", kind: "bool", }, { @@ -288,6 +302,8 @@ const FIELDS: FieldSpec[] = [ icon: "mdi-check-decagram", labelKey: "platform.show-matched", defaultLabel: "Show matched", + negLabelKey: "platform.show-unmatched", + negDefaultLabel: "Show unmatched", kind: "bool", }, { @@ -295,6 +311,8 @@ const FIELDS: FieldSpec[] = [ icon: "mdi-content-duplicate", labelKey: "platform.show-duplicates", defaultLabel: "Show versions", + negLabelKey: "platform.show-not-duplicates-only", + negDefaultLabel: "Show ROMs without versions only", kind: "bool", }, { @@ -302,6 +320,8 @@ const FIELDS: FieldSpec[] = [ icon: "mdi-gamepad-variant-outline", labelKey: "platform.show-playables", defaultLabel: "Show playables", + negLabelKey: "platform.show-not-playables-only", + negDefaultLabel: "Show non-playable ROMs only", kind: "bool", }, { @@ -309,6 +329,26 @@ const FIELDS: FieldSpec[] = [ icon: "mdi-trophy-outline", labelKey: "platform.show-ra", defaultLabel: "Show RetroAchievements", + negLabelKey: "platform.show-not-ra-only", + negDefaultLabel: "Show ROMs without RetroAchievements only", + kind: "bool", + }, + { + storage: "has_saves", + icon: "mdi-content-save-outline", + labelKey: "platform.has-saves", + defaultLabel: "Has saves", + negLabelKey: "platform.show-not-saves-only", + negDefaultLabel: "Show ROMs without saves only", + kind: "bool", + }, + { + storage: "has_states", + icon: "mdi-camera-outline", + labelKey: "platform.has-states", + defaultLabel: "Has save states", + negLabelKey: "platform.show-not-states-only", + negDefaultLabel: "Show ROMs without save states only", kind: "bool", }, { @@ -316,6 +356,8 @@ const FIELDS: FieldSpec[] = [ icon: "mdi-music-note", labelKey: "platform.has-soundtrack", defaultLabel: "Has soundtrack", + negLabelKey: "platform.show-no-soundtrack-only", + negDefaultLabel: "Show ROMs without soundtracks only", kind: "bool", }, { @@ -323,6 +365,8 @@ const FIELDS: FieldSpec[] = [ icon: "mdi-file-alert-outline", labelKey: "platform.show-missing", defaultLabel: "Show missing", + negLabelKey: "platform.show-not-missing-only", + negDefaultLabel: "Show non-missing ROMs only", kind: "bool", }, { @@ -330,6 +374,8 @@ const FIELDS: FieldSpec[] = [ icon: "mdi-shield-check-outline", labelKey: "platform.show-verified", defaultLabel: "Show verified", + negLabelKey: "platform.show-not-verified-only", + negDefaultLabel: "Show non-verified ROMs only", kind: "bool", }, { @@ -462,6 +508,15 @@ export function summarizeSmartFilterCriteria( } else if (f.kind === "bool") { if (raw === true) { out.push({ key: f.storage, icon: f.icon, label }); + } else if (raw === false) { + out.push({ + key: f.storage, + icon: f.icon, + label: t( + f.negLabelKey ?? f.labelKey, + f.negDefaultLabel ?? f.defaultLabel, + ), + }); } } else if (f.kind === "platforms") { if (Array.isArray(raw) && raw.length > 0) {