Files
romm/backend/tests/utils/test_switch.py
Georges-Antoine Assi a3f45158e1 fix(scan): skip extracted Switch files when reading title ids (#5839)
* fix(scan): skip extracted Switch files when reading title ids

Sigil reads a Switch title id only from an NSP or XCI container, so no
file under an extracted title's exefs or romfs folder can yield one. A
scan still handed every such file to sigil, and the Switch family never
reuses stored ids while a file lacks one, so every quick scan re-read an
extracted game's whole tree (one Switch 2 game in the report holds ~160k
files) and kept each path in the scan's needs-key set for the summary.

Files inside an exefs or romfs folder of a Switch ROM are no longer
title id sources. On a 50k-file extracted game, a quick rescan goes from
50,002 sigil reads and 50,002 retained needs-key paths to none, and its
peak allocation drops from 193 MB to 110 MB.

Refs #5706

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

* test(scan): check row reuse by identity in the extracted Switch rescan test

Unsaved rows all have a None id, so comparing ids could not tell reused
rows from rebuilt ones. Addresses Greptile's review.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 08:23:01 -05:00

126 lines
4.2 KiB
Python

from pathlib import PurePath
import pytest
from models.rom import RomIdentity, SaveTargetLayout
from utils import switch
class TestIsBaseTitleId:
@pytest.mark.parametrize(
"title_id",
["0100ABCD12340000", "0100F4700B2E0000", "0100abcd12340000"],
)
def test_base_ids_are_recognized(self, title_id: str):
assert switch.is_base_title_id(title_id)
@pytest.mark.parametrize(
"title_id",
[
"0100ABCD12340800", # update: odd program-index nibble
"0100ABCD12341001", # DLC: low bits set
"",
"000",
"0100ABCD1234ZZZ0", # non-hex nibble
],
)
def test_non_base_ids_are_rejected(self, title_id: str):
assert not switch.is_base_title_id(title_id)
class TestIsExtractedContent:
@pytest.mark.parametrize(
"rel_path",
["romfs/data/a.bfres", "exefs/main", "ExeFS/main.npdm", "v65536/RomFS/b.arc"],
)
def test_files_in_an_extracted_tree(self, rel_path: str):
assert switch.is_extracted_content(PurePath(rel_path))
@pytest.mark.parametrize(
"rel_path", ["Game.nsp", "updates/Game.nsp", "romfs", "romfs.nsp", "."]
)
def test_files_outside_an_extracted_tree(self, rel_path: str):
assert not switch.is_extracted_content(PurePath(rel_path))
class TestDeriveBaseTitleId:
def test_update_resolves_to_its_base(self):
assert switch.derive_base_title_id("0100ABCD12340800") == "0100ABCD12340000"
def test_dlc_resolves_to_its_base(self):
assert switch.derive_base_title_id("0100ABCD12341001") == "0100ABCD12340000"
def test_a_base_id_is_left_alone(self):
assert switch.derive_base_title_id("0100ABCD12340000") == "0100ABCD12340000"
def test_case_of_the_program_index_nibble_is_preserved(self):
assert switch.derive_base_title_id("0100abcd1234e000") == "0100abcd1234e000"
assert switch.derive_base_title_id("0100ABCD1234E000") == "0100ABCD1234E000"
@pytest.mark.parametrize("title_id", ["", "000", "0100ABCD1234ZZZ0"])
def test_unparseable_ids_yield_nothing(self, title_id: str):
assert switch.derive_base_title_id(title_id) is None
_BASE_FOLDER = RomIdentity(
title_id="0100ABCD12340000",
save_target="0100ABCD12340000",
save_target_layout=SaveTargetLayout.FOLDER_EXACT,
)
class TestNormalizeIdentity:
@pytest.mark.parametrize(
"title_id",
[
pytest.param("0100ABCD12340800", id="update"),
pytest.param("0100ABCD12340000", id="base-without-save-target"),
],
)
def test_switch_ids_key_saves_by_the_base_id(self, title_id: str):
identity = RomIdentity(title_id=title_id)
assert switch.normalize_identity(True, identity) == _BASE_FOLDER
def test_a_base_id_keeps_its_save_target(self):
identity = RomIdentity(
title_id="0100ABCD12340000",
save_target="sigil-target",
save_target_layout=SaveTargetLayout.FOLDER_EXACT,
)
assert switch.normalize_identity(True, identity) == identity
@pytest.mark.parametrize(
("is_switch", "title_id"),
[
pytest.param(False, "0100ABCD12340800", id="not-switch"),
pytest.param(True, "ZZZ", id="not-derivable"),
pytest.param(True, None, id="no-id"),
],
)
def test_other_identities_are_left_alone(
self, is_switch: bool, title_id: str | None
):
identity = RomIdentity(title_id=title_id)
assert switch.normalize_identity(is_switch, identity) == identity
class TestTitleIdRegexes:
def test_a_bare_id_matches_the_plain_regex(self):
assert switch.TITLE_ID_REGEX.fullmatch("0100ABCD12340000")
@pytest.mark.parametrize(
"value",
["0100ABCD1234000", "0100ABCD123400001", "[0100ABCD12340000]", "xyz"],
)
def test_anything_else_does_not(self, value: str):
assert not switch.TITLE_ID_REGEX.fullmatch(value)
def test_an_embedded_id_is_found_anywhere_in_a_name(self):
assert switch.TITLE_ID_BRACKET_REGEX.search("Game [0100ABCD12340000][v0].nsp")
def test_a_name_without_one_is_not_matched(self):
assert not switch.TITLE_ID_BRACKET_REGEX.search("Game [USA][v0].nsp")