Files
romm/backend/tests/models/test_base.py
Tyler Vigario bbb7d98f53 fix(scan): look a file named by several titles up by each of them (#5478)
* fix(scan): look a file named by several titles up by each of them

No-Intro and Redump name a game released under several titles by all of
them, joined by " ~ ". Each file-name lookup now tries the full name
first and then each title, tags and extension kept, until one matches;
SteamGridDB searches the titles too. Only the title is split, never a
tag group.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Eky1CPt1rz6BWLAqWVNmU9

* fix(scan): search each SteamGridDB name once

From Greptile's review: a title can repeat a name a provider already
gave, so SteamGridDB's candidate names are deduplicated, first position
kept, with a test. The new test module's docstring is one sentence, as
AGENTS.md asks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Eky1CPt1rz6BWLAqWVNmU9

* fix(scan): keep the full-name answer on a miss and cap title lookups

pr-ready step 2 (code-review xhigh --fix): return the full name's result when
every title misses, dedupe and cap titles at 4, and stop wrapping CSDb, whose
lookup only reads a (csdb-N) tag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(scan): give SteamGridDB the bare titles and inline lookup args

pr-ready step 3 (simplify): add compute_titles so SteamGridDB stops re-attaching
and re-stripping tags, inline the Moby and SS platform ids, and merge the two
all-miss tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(scan): keep the narrowed Moby and SS platform ids for mypy

pr-ready step 4 (review-polish): the locals inlined in step 3 narrow the
optional platform ids, which a lambda body cannot do; restore them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(scan): reword the title cap comment

pr-ready step 5 (humanizer).

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>
2026-11-08 16:40:43 -05:00

133 lines
4.4 KiB
Python

import pytest
from config.config_manager import config_manager as cm
from models.assets import Save
from models.base import compute_file_name_parts, compute_title_file_names
from models.firmware import Firmware
from models.rom import Rom, compute_name_sort_key
@pytest.mark.parametrize(
("file_name", "no_tags", "no_ext", "extension"),
[
("Sonic (USA) [!].md", "Sonic", "Sonic (USA) [!]", "md"),
("game.tar.gz", "game", "game", "tar.gz"),
("README", "README", "README", ""),
(
"Final Fantasy VII (Disc 1).bin",
"Final Fantasy VII",
"Final Fantasy VII (Disc 1)",
"bin",
),
],
)
def test_compute_file_name_parts(file_name, no_tags, no_ext, extension):
parts = compute_file_name_parts(file_name)
assert parts.no_tags == no_tags
assert parts.no_ext == no_ext
assert parts.extension == extension
@pytest.mark.parametrize(
("file_name", "title_file_names"),
[
(
"Sonic Wings 2 ~ Aero Fighters 2 (World) (En,Ja).chd",
[
"Sonic Wings 2 (World) (En,Ja).chd",
"Aero Fighters 2 (World) (En,Ja).chd",
],
),
(
"Dunk Dream ~ Street Slam ~ Street Hoop (World).zip",
[
"Dunk Dream (World).zip",
"Street Slam (World).zip",
"Street Hoop (World).zip",
],
),
("Mickey Mouse ~ Egg", ["Mickey Mouse", "Egg"]),
("Aero Fighters 2 (World).chd", []),
("Puzzle Bobble ~ Puzzle Bobble (Japan).zip", ["Puzzle Bobble (Japan).zip"]),
("A ~ B ~ C ~ D ~ E ~ F.zip", ["A.zip", "B.zip", "C.zip", "D.zip"]),
# A tag naming the compilation's titles leaves the game's title whole
(
"Sonic The Hedgehog 2 (World) (Rev B) (Sonic Compilation ~ Sonic Classics).md",
[],
),
],
)
def test_compute_title_file_names(file_name, title_file_names):
assert compute_title_file_names(file_name) == title_file_names
@pytest.mark.parametrize(
("name", "expected"),
[
("The Legend of Zelda 2", "legend of zelda 000000000002"),
("A Bug's Life", "bug's life"),
("Final Fantasy 10", "final fantasy 000000000010"),
("Die Hard Arcade", "die hard arcade"),
("Le Mans 24 Hours", "le mans 000000000024 hours"),
(None, ""),
],
)
def test_compute_name_sort_key(name, expected):
assert compute_name_sort_key(name) == expected
def test_compute_name_sort_key_drops_the_configured_articles(mocker):
mocker.patch.object(cm.config, "SORT_ARTICLES", ["der", "die", "das"])
assert compute_name_sort_key("Die Siedler") == "siedler"
assert compute_name_sort_key("The Legend of Zelda") == "the legend of zelda"
def test_a_custom_sort_key_keeps_its_leading_article():
assert compute_name_sort_key("The Legend", articles=()) == "the legend"
rom = Rom(name="The Legend of Zelda", name_sort_key="The Legend")
assert rom.name_sort_key == "the legend"
def test_rom_validator_derives_fs_name_parts_on_construction():
rom = Rom(fs_name="The Legend of Zelda (USA) (Rev 1).z64")
assert rom.fs_name_no_tags == "The Legend of Zelda"
assert rom.fs_name_no_ext == "The Legend of Zelda (USA) (Rev 1)"
assert rom.fs_extension == "z64"
# The validator only fires on assignment; name was never set, so its
# derived column keeps the column default rather than being computed.
assert rom.name_sort_key is None
def test_rom_validator_resyncs_on_mutation():
rom = Rom(fs_name="old.zip", name="The Old Game")
assert rom.name_sort_key == "old game"
rom.fs_name = "Sonic (Europe).md"
rom.name = "A New Game 2"
assert rom.fs_name_no_tags == "Sonic"
assert rom.fs_extension == "md"
assert rom.name_sort_key == "new game 000000000002"
def test_asset_validator_derives_file_name_parts():
# Defined on the abstract BaseAsset, inherited by every asset subclass.
save = Save(file_name="Sonic [!] (proto).srm")
assert save.file_name_no_tags == "Sonic"
assert save.file_name_no_ext == "Sonic [!] (proto)"
assert save.file_extension == "srm"
def test_firmware_validator_derives_file_name_parts():
firmware = Firmware(file_name="bios (world).bin")
assert firmware.file_name_no_tags == "bios"
assert firmware.file_name_no_ext == "bios (world)"
assert firmware.file_extension == "bin"