mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
Rename `FirmwareTemplate.platform_path` to `firmware_dir`: it returns the firmware folder, while the same name on `StructureTemplate` returns the platform folder. Name the watcher's index for the segment it points at. Fold the two reads of a raw structure key into `_raw_template`, and drop the docstrings that only restated a one-line delegation or a signature. Remove `footer-create-summary-default`, which no component references and whose copy still named Structure A. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
3134 lines
118 KiB
Python
3134 lines
118 KiB
Python
import hashlib
|
|
import os
|
|
import shutil
|
|
import struct
|
|
import tempfile
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, Mock, patch
|
|
|
|
import pytest
|
|
from hypothesis import assume, given
|
|
from hypothesis import strategies as st
|
|
from tests._zipfile_shim import reload_zipfile
|
|
|
|
from adapters.services.sigil import SigilExtractionResult
|
|
from config.config_manager import (
|
|
DEFAULT_EXCLUDED_EXTENSIONS,
|
|
LIBRARY_BASE_PATH,
|
|
Config,
|
|
)
|
|
from handler.filesystem.base_handler import (
|
|
LANGUAGES_BY_SHORTCODE,
|
|
REGIONS_BY_SHORTCODE,
|
|
)
|
|
from handler.filesystem.roms_handler import (
|
|
FileHash,
|
|
FSRomsHandler,
|
|
category_matches,
|
|
mtime_matches,
|
|
)
|
|
from models.platform import Platform
|
|
from models.rom import Rom, RomFile, RomFileCategory, SaveTargetLayout
|
|
from utils.archives import extract_chd_hash
|
|
|
|
|
|
class TestFSRomsHandler:
|
|
"""Test suite for FSRomsHandler class"""
|
|
|
|
@pytest.fixture
|
|
def handler(self):
|
|
return FSRomsHandler()
|
|
|
|
@pytest.fixture
|
|
def config(self):
|
|
return Config(
|
|
EXCLUDED_PLATFORMS=[],
|
|
EXCLUDED_SINGLE_EXT=["tmp"],
|
|
EXCLUDED_SINGLE_FILES=["excluded_test.tmp"],
|
|
EXCLUDED_MULTI_FILES=["excluded_multi"],
|
|
EXCLUDED_MULTI_PARTS_EXT=["tmp"],
|
|
EXCLUDED_MULTI_PARTS_FILES=["excluded_part.bin"],
|
|
PLATFORMS_BINDING={},
|
|
PLATFORMS_VERSIONS={},
|
|
STRUCTURE_TEMPLATES={
|
|
"default": "{platform}/roms/{game}",
|
|
"firmware": "{platform}/bios",
|
|
},
|
|
)
|
|
|
|
@pytest.fixture
|
|
def platform(self):
|
|
return Platform(name="Nintendo 64", slug="n64", fs_slug="n64")
|
|
|
|
@pytest.fixture
|
|
def rom_single(self, platform: Platform):
|
|
return Rom(
|
|
id=1,
|
|
fs_name="Paper Mario (USA).z64",
|
|
fs_path="n64/roms",
|
|
fs_extension="z64",
|
|
platform=platform,
|
|
full_path="n64/roms/Paper Mario (USA).z64",
|
|
)
|
|
|
|
@pytest.fixture
|
|
def rom_single_nested(self, platform: Platform):
|
|
return Rom(
|
|
id=3,
|
|
fs_name="Sonic (EU) [T]",
|
|
fs_path="n64/roms",
|
|
fs_extension="",
|
|
platform=platform,
|
|
full_path="n64/roms/Sonic (EU) [T]",
|
|
files=[
|
|
RomFile(
|
|
id=1,
|
|
file_name="Sonic (EU) [T].n64",
|
|
file_path="n64/roms/Sonic (EU) [T]",
|
|
),
|
|
RomFile(
|
|
id=2,
|
|
file_name="Sonic (EU) [T-En].z64",
|
|
file_path="n64/roms/Sonic (EU) [T]/translation",
|
|
),
|
|
],
|
|
)
|
|
|
|
@pytest.fixture
|
|
def rom_multi(self, platform: Platform):
|
|
return Rom(
|
|
id=2,
|
|
fs_name="Super Mario 64 (J) (Rev A)",
|
|
fs_path="n64/roms",
|
|
fs_extension="",
|
|
platform=platform,
|
|
files=[
|
|
RomFile(
|
|
id=1,
|
|
file_name="Super Mario 64 (J) (Rev A) [Part 1].z64",
|
|
file_path="n64/roms",
|
|
),
|
|
RomFile(
|
|
id=2,
|
|
file_name="Super Mario 64 (J) (Rev A) [Part 2].z64",
|
|
file_path="n64/roms",
|
|
),
|
|
],
|
|
)
|
|
|
|
def test_init_uses_library_base_path(self, handler: FSRomsHandler):
|
|
"""Test that FSRomsHandler initializes with LIBRARY_BASE_PATH"""
|
|
assert handler.base_path == Path(LIBRARY_BASE_PATH).resolve()
|
|
|
|
@pytest.mark.parametrize(
|
|
("template", "expected"),
|
|
[
|
|
("{platform}/roms/{game}", "n64/roms"),
|
|
("roms/{platform}/{game}", "roms/n64"),
|
|
("games/all/{platform}/{game}", "games/all/n64"),
|
|
],
|
|
)
|
|
def test_get_roms_fs_structure(self, handler: FSRomsHandler, template, expected):
|
|
"""The platform folder is whatever the template puts `{platform}` in."""
|
|
cnfg = Config(STRUCTURE_TEMPLATES={"default": template})
|
|
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: cnfg)
|
|
|
|
assert handler.get_roms_fs_structure("n64") == expected
|
|
|
|
def test_parse_tags_regions_and_languages(self, handler: FSRomsHandler):
|
|
"""Test parse_tags method with regions and languages"""
|
|
fs_name = "Zelda (USA) (Rev 1) [En,Fr] [Test].n64"
|
|
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
|
|
assert "USA" in parsed_tags.regions
|
|
assert parsed_tags.revision == "1"
|
|
assert parsed_tags.version == ""
|
|
assert "English" in parsed_tags.languages
|
|
assert "French" in parsed_tags.languages
|
|
assert "Test" in parsed_tags.other_tags
|
|
|
|
def test_parse_tags_complex_tags(self, handler: FSRomsHandler):
|
|
"""Test parse_tags with complex tag structures"""
|
|
fs_name = "Game (Europe) (En,De,Fr,Es,It) (Rev A) [Reg-PAL] [Beta].rom"
|
|
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
|
|
assert "Europe" in parsed_tags.regions
|
|
assert "PAL" in parsed_tags.regions
|
|
assert parsed_tags.revision == "A"
|
|
assert parsed_tags.version == ""
|
|
assert "English" in parsed_tags.languages
|
|
assert "German" in parsed_tags.languages
|
|
assert "French" in parsed_tags.languages
|
|
assert "Spanish" in parsed_tags.languages
|
|
assert "Italian" in parsed_tags.languages
|
|
assert "Beta" in parsed_tags.other_tags
|
|
|
|
def test_parse_tags_no_tags(self, handler: FSRomsHandler):
|
|
"""Test parse_tags with no tags"""
|
|
fs_name = "Simple Game.rom"
|
|
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
|
|
assert parsed_tags.regions == []
|
|
assert parsed_tags.revision == ""
|
|
assert parsed_tags.version == ""
|
|
assert parsed_tags.languages == []
|
|
assert parsed_tags.other_tags == []
|
|
|
|
def test_parse_tags_version(self, handler: FSRomsHandler):
|
|
"""Test parse_tags method with version tags"""
|
|
fs_name = "stardew_valley(v1.5.6.1988831614)(53038).exe"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == "1.5.6.1988831614"
|
|
assert "53038" in parsed_tags.other_tags
|
|
assert parsed_tags.regions == []
|
|
assert parsed_tags.revision == ""
|
|
assert parsed_tags.languages == []
|
|
|
|
fs_name = "My Game (Version 1.2.3).rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == "1.2.3"
|
|
|
|
fs_name = "My Game (Ver-1.2.3).rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == "1.2.3"
|
|
|
|
fs_name = "My Game (v_1.2.3).rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == "1.2.3"
|
|
|
|
fs_name = "My Game (v 1.2.3).rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == "1.2.3"
|
|
|
|
fs_name = "My Game (version B).rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == "B"
|
|
|
|
fs_name = "My Game (v1.2a).rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == "1.2a"
|
|
|
|
# A separator with no version after it must yield "" (str), never None.
|
|
fs_name = "My Game (v-).rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == ""
|
|
|
|
# A trailing separator leaves leading whitespace the group must strip.
|
|
fs_name = "My Game (v 1.2.3).rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == "1.2.3"
|
|
|
|
# A dot separator (e.g. "v.1.0", "Ver. 1.00") must not leak into the
|
|
# version as a leading dot.
|
|
fs_name = "My Game (v.1.2.3).rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == "1.2.3"
|
|
|
|
fs_name = "My Game (Ver.1.2.3).rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == "1.2.3"
|
|
|
|
fs_name = "My Game (Ver. 1.00).rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == "1.00"
|
|
|
|
fs_name = "My Game (Version.2).rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == "2"
|
|
|
|
def test_parse_tags_non_version_tags_starting_with_v(self, handler: FSRomsHandler):
|
|
"""Test parse_tags keeps non-version tags starting with v as generic tags"""
|
|
fs_name = "Rom [2026] [Variation].rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == ""
|
|
assert "2026" in parsed_tags.other_tags
|
|
assert "Variation" in parsed_tags.other_tags
|
|
|
|
fs_name = "Rom [versionB].rom"
|
|
parsed_tags = handler.parse_tags(fs_name)
|
|
assert parsed_tags.version == ""
|
|
assert "versionB" in parsed_tags.other_tags
|
|
|
|
def test_parse_tags_reg_prefix_resolves_shortcode(self, handler: FSRomsHandler):
|
|
"""A "Reg-" prefixed shortcode resolves to its full region name."""
|
|
assert "Japan" in handler.parse_tags("Game [Reg-J].rom").regions
|
|
assert "USA" in handler.parse_tags("Game [Reg-U].rom").regions
|
|
assert "Netherlands" in handler.parse_tags("Game [Reg-NL].rom").regions
|
|
|
|
# A value that is not a shortcode is kept verbatim.
|
|
assert "PAL" in handler.parse_tags("Game [Reg-PAL].rom").regions
|
|
|
|
# A space after the separator isn't part of the value.
|
|
assert handler.parse_tags("Game [Reg- PAL].rom").regions == ["PAL"]
|
|
assert handler.parse_tags("Game [Reg- U].rom").regions == ["USA"]
|
|
assert handler.parse_tags("Game [Reg- PAL ].rom").regions == ["PAL"]
|
|
|
|
# A prefix with no value behind it is not a region.
|
|
for fs_name in ("Game [Reg-].rom", "Game [Reg- ].rom"):
|
|
parsed = handler.parse_tags(fs_name)
|
|
assert parsed.regions == []
|
|
assert parsed.other_tags == ["Reg-"]
|
|
|
|
def test_parse_tags_region_casing_is_normalized(self, handler: FSRomsHandler):
|
|
"""Region names collapse to one canonical spelling regardless of casing."""
|
|
for fs_name in ("Game (USA).rom", "Game (usa).rom", "Game (Usa).rom"):
|
|
assert handler.parse_tags(fs_name).regions == ["USA"]
|
|
|
|
assert handler.parse_tags("Game (europe).rom").regions == ["Europe"]
|
|
assert handler.parse_tags("Game (hong kong).rom").regions == ["Hong Kong"]
|
|
assert handler.parse_tags("Game (u).rom").regions == ["USA"]
|
|
|
|
# An unknown tag is still just a tag.
|
|
parsed = handler.parse_tags("Game (Nonsense).rom")
|
|
assert parsed.regions == []
|
|
assert "Nonsense" in parsed.other_tags
|
|
|
|
def test_parse_tags_region_codes_do_not_shadow_languages(
|
|
self, handler: FSRomsHandler
|
|
):
|
|
"""Nl/No are languages; NL/NO are regions. Case decides."""
|
|
assert handler.parse_tags("Game (Nl).rom").languages == ["Dutch"]
|
|
assert handler.parse_tags("Game (No).rom").languages == ["Norwegian"]
|
|
assert handler.parse_tags("Game (NL).rom").regions == ["Netherlands"]
|
|
assert handler.parse_tags("Game (NO).rom").regions == ["Norway"]
|
|
|
|
# Lowercased, the region wins; neither table can claim both.
|
|
assert handler.parse_tags("Game (nl).rom").regions == ["Netherlands"]
|
|
assert handler.parse_tags("Game (no).rom").regions == ["Norway"]
|
|
|
|
def test_parse_tags_language_casing_is_normalized(self, handler: FSRomsHandler):
|
|
"""Language names collapse to one canonical spelling regardless of casing."""
|
|
for fs_name in (
|
|
"Game (English).rom",
|
|
"Game (english).rom",
|
|
"Game (ENGLISH).rom",
|
|
):
|
|
assert handler.parse_tags(fs_name).languages == ["English"]
|
|
|
|
assert handler.parse_tags("Game (japanese).rom").languages == ["Japanese"]
|
|
assert handler.parse_tags("Game (no language).rom").languages == ["No Language"]
|
|
|
|
def test_parse_tags_language_codes_are_case_insensitive(
|
|
self, handler: FSRomsHandler
|
|
):
|
|
"""A differently-cased language shortcode still resolves."""
|
|
assert handler.parse_tags("Game (en).rom").languages == ["English"]
|
|
assert handler.parse_tags("Game (EN).rom").languages == ["English"]
|
|
assert handler.parse_tags("Game (de).rom").languages == ["German"]
|
|
assert handler.parse_tags("Game [ja,FR].rom").languages == [
|
|
"Japanese",
|
|
"French",
|
|
]
|
|
|
|
def test_exclude_multi_roms_filters_excluded(self, handler: FSRomsHandler, config):
|
|
"""Test exclude_multi_roms filters out excluded multi-file ROMs"""
|
|
roms = ["Game1", "excluded_multi", "Game2", "Game3"]
|
|
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
|
|
result = handler.exclude_multi_roms(roms)
|
|
expected = ["Game1", "Game2", "Game3"]
|
|
|
|
assert result == expected
|
|
|
|
def test_exclude_multi_roms_no_exclusions(self, handler: FSRomsHandler):
|
|
"""Test exclude_multi_roms with no exclusions"""
|
|
roms = ["Game1", "Game2", "Game3"]
|
|
config = Config(
|
|
EXCLUDED_PLATFORMS=[],
|
|
EXCLUDED_SINGLE_EXT=[],
|
|
EXCLUDED_SINGLE_FILES=[],
|
|
EXCLUDED_MULTI_FILES=[],
|
|
EXCLUDED_MULTI_PARTS_EXT=[],
|
|
EXCLUDED_MULTI_PARTS_FILES=[],
|
|
PLATFORMS_BINDING={},
|
|
PLATFORMS_VERSIONS={},
|
|
STRUCTURE_TEMPLATES={
|
|
"default": "{platform}/roms/{game}",
|
|
"firmware": "{platform}/bios",
|
|
},
|
|
)
|
|
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
|
|
result = handler.exclude_multi_roms(roms)
|
|
assert result == roms
|
|
|
|
def test_exclude_multi_roms_drops_hidden_folders(
|
|
self, handler: FSRomsHandler, config
|
|
):
|
|
"""A hidden folder is never a rom, with or without a structure template."""
|
|
roms = ["Game1", ".hidden", "Game2"]
|
|
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
|
|
assert handler.exclude_multi_roms(roms) == ["Game1", "Game2"]
|
|
|
|
def test_exclude_multi_roms_case_insensitive(self, handler: FSRomsHandler, config):
|
|
"""Test exclude_multi_roms ignores case in excluded names"""
|
|
roms = ["Game1", "Manuals", "Game2"]
|
|
config.EXCLUDED_MULTI_FILES = ["manuals"]
|
|
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
|
|
result = handler.exclude_multi_roms(roms)
|
|
assert result == ["Game1", "Game2"]
|
|
|
|
def test_exclude_multi_roms_ignores_whitespace(
|
|
self, handler: FSRomsHandler, config
|
|
):
|
|
"""Test exclude_multi_roms trims accidental surrounding whitespace"""
|
|
roms = ["Game1", "covers", "Game2"]
|
|
config.EXCLUDED_MULTI_FILES = [" covers "]
|
|
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
|
|
result = handler.exclude_multi_roms(roms)
|
|
assert result == ["Game1", "Game2"]
|
|
|
|
def test_exclude_multi_roms_wildcard_patterns(self, handler: FSRomsHandler, config):
|
|
"""Test exclude_multi_roms keeps wildcard matching with normalized config"""
|
|
roms = ["Game1", "Manuals", "manuals-fr", "Game2"]
|
|
config.EXCLUDED_MULTI_FILES = [" manuals* "]
|
|
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
|
|
result = handler.exclude_multi_roms(roms)
|
|
assert result == ["Game1", "Game2"]
|
|
|
|
def test_build_rom_file_single_file(self, rom_single: Rom, handler: FSRomsHandler):
|
|
"""Test _build_rom_file with actual single ROM file"""
|
|
rom_path = Path(rom_single.fs_path)
|
|
file_name = rom_single.fs_name
|
|
file_hash = FileHash(
|
|
{
|
|
"crc_hash": "ABCD1234",
|
|
"md5_hash": "def456",
|
|
"sha1_hash": "789ghi",
|
|
"chd_sha1_hash": "654321",
|
|
}
|
|
)
|
|
|
|
rom_file = handler._build_rom_file(rom_single, rom_path, file_name, file_hash)
|
|
|
|
assert isinstance(rom_file, RomFile)
|
|
assert rom_file.file_name == file_name
|
|
assert rom_file.file_path == str(rom_path)
|
|
assert rom_file.crc_hash == "ABCD1234"
|
|
assert rom_file.md5_hash == "def456"
|
|
assert rom_file.sha1_hash == "789ghi"
|
|
assert rom_file.file_size_bytes > 0 # Should have actual file size
|
|
assert rom_file.last_modified is not None
|
|
assert rom_file.category is None # No category matching for this path
|
|
|
|
def test_build_rom_file_with_category(self, rom_multi: Rom, handler: FSRomsHandler):
|
|
"""Test _build_rom_file with category detection"""
|
|
# Test with DLC category
|
|
rom_path = Path(rom_multi.fs_path, "dlc")
|
|
file_name = "test_dlc.n64"
|
|
file_hash = FileHash(
|
|
{
|
|
"crc_hash": "12345678",
|
|
"md5_hash": "abcdef",
|
|
"sha1_hash": "123456",
|
|
"chd_sha1_hash": "654321",
|
|
}
|
|
)
|
|
|
|
# Create the test file
|
|
os.makedirs(handler.base_path / rom_path, exist_ok=True)
|
|
test_file = handler.base_path / rom_path / file_name
|
|
test_file.write_text("Test DLC content")
|
|
|
|
try:
|
|
rom_file = handler._build_rom_file(
|
|
rom_multi, rom_path, file_name, file_hash
|
|
)
|
|
|
|
assert rom_file.category == RomFileCategory.DLC
|
|
assert rom_file.file_name == file_name
|
|
assert rom_file.file_size_bytes > 0
|
|
finally:
|
|
# Clean up
|
|
if test_file.exists():
|
|
test_file.unlink()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_roms(self, handler: FSRomsHandler, platform, config):
|
|
"""Test get_roms with actual files in the filesystem"""
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
m.setattr("os.path.exists", lambda x: False)
|
|
|
|
result = await handler.get_roms(platform)
|
|
|
|
assert isinstance(result, list)
|
|
assert len(result) > 0
|
|
|
|
# Check that we have both single and multi ROMs
|
|
single_roms = [r for r in result if not r["flat"]]
|
|
multi_roms = [r for r in result if not r["flat"]]
|
|
|
|
assert len(single_roms) > 0
|
|
assert len(multi_roms) > 0
|
|
|
|
# Check specific files exist
|
|
rom_names = [r["fs_name"] for r in result]
|
|
assert "Paper Mario (USA).z64" in rom_names
|
|
assert "Super Mario 64 (J) (Rev A)" in rom_names
|
|
assert "Zelda (USA) (Rev 1) [En,Fr] [Test].n64" in rom_names
|
|
|
|
# Check excluded files are not present
|
|
assert "excluded_test.tmp" not in rom_names
|
|
|
|
def _make_structure_config(self, templates: dict[str, str | list[str]]) -> Config:
|
|
return Config(
|
|
EXCLUDED_PLATFORMS=[],
|
|
EXCLUDED_SINGLE_EXT=["tmp"],
|
|
EXCLUDED_SINGLE_FILES=[],
|
|
EXCLUDED_MULTI_FILES=["@eaDir"],
|
|
EXCLUDED_MULTI_PARTS_EXT=["tmp"],
|
|
EXCLUDED_MULTI_PARTS_FILES=[],
|
|
PLATFORMS_BINDING={},
|
|
PLATFORMS_VERSIONS={},
|
|
STRUCTURE_TEMPLATES={"default": "{platform}/roms/{game}", **templates},
|
|
)
|
|
|
|
def _build_structure_library(self, tmp_path: Path, base: str) -> None:
|
|
"""Library exercising literal/wildcard levels, file-vs-folder terminals,
|
|
a name collision across folders, a hidden folder, and depth > 1."""
|
|
roms = tmp_path / base
|
|
roms.mkdir(parents=True)
|
|
(roms / "Top.zip").write_text("t")
|
|
hacks = roms / "Hacks"
|
|
hacks.mkdir()
|
|
(hacks / "Shared.zip").write_text("a")
|
|
(hacks / "HackOnly.zip").write_text("b")
|
|
inner = hacks / "Inner"
|
|
inner.mkdir()
|
|
(inner / "x.bin").write_text("x")
|
|
trans = roms / "Translations"
|
|
trans.mkdir()
|
|
(trans / "Shared.zip").write_text("c") # collides with Hacks/Shared.zip
|
|
hidden = roms / ".hidden"
|
|
hidden.mkdir()
|
|
(hidden / "secret.zip").write_text("s")
|
|
region = roms / "Region"
|
|
region.mkdir()
|
|
usa = region / "USA"
|
|
usa.mkdir()
|
|
(usa / "usagame.zip").write_text("u")
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_roms_no_structure_is_default(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""Without a template, only top-level files and folders are surfaced
|
|
(each file a flat rom, each folder a single multi-file rom), and hidden
|
|
folders are skipped as they are under a template."""
|
|
handler = FSRomsHandler()
|
|
handler.base_path = tmp_path
|
|
with patch(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: self._make_structure_config({}),
|
|
):
|
|
base = handler.get_roms_fs_structure(platform.fs_slug)
|
|
self._build_structure_library(tmp_path, base)
|
|
roms = await handler.get_roms(platform)
|
|
count = await handler.count_roms(platform)
|
|
|
|
keys = {(r["fs_path"], r["fs_name"]) for r in roms}
|
|
assert (base, "Top.zip") in keys
|
|
assert (base, "Hacks") in keys
|
|
assert (base, "Translations") in keys
|
|
assert (base, "Region") in keys
|
|
assert (base, ".hidden") not in keys
|
|
# Nothing nested is surfaced.
|
|
assert all(r["fs_path"] == base for r in roms)
|
|
assert len(roms) == 4
|
|
assert count == len(roms)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_roms_structure_wildcard_level(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""A wildcard level descends one folder and the `{game}` terminal takes
|
|
both the files and the folders it finds there; hidden folders are skipped
|
|
and identically-named games in different folders stay distinct."""
|
|
handler = FSRomsHandler()
|
|
handler.base_path = tmp_path
|
|
with patch(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: self._make_structure_config(
|
|
{platform.fs_slug: "{platform}/roms/{category}/{game}"}
|
|
),
|
|
):
|
|
base = handler.get_roms_fs_structure(platform.fs_slug)
|
|
self._build_structure_library(tmp_path, base)
|
|
roms = await handler.get_roms(platform)
|
|
count = await handler.count_roms(platform)
|
|
|
|
by_key = {(r["fs_path"], r["fs_name"]): r for r in roms}
|
|
keys = set(by_key)
|
|
# A file at the terminal level is a game of its own...
|
|
assert by_key[(f"{base}/Hacks", "Shared.zip")]["flat"] is True
|
|
assert (f"{base}/Hacks", "HackOnly.zip") in keys
|
|
assert (f"{base}/Translations", "Shared.zip") in keys # collision kept
|
|
# ...and a folder there is one multi-file game, kept whole.
|
|
assert by_key[(f"{base}/Hacks", "Inner")]["flat"] is False
|
|
assert (f"{base}/Hacks/Inner", "x.bin") not in keys
|
|
# Top-level file isn't surfaced (template requires one level down).
|
|
assert (base, "Top.zip") not in keys
|
|
# Hidden folder is never descended into.
|
|
assert not any(r["fs_path"].endswith("/.hidden") for r in roms)
|
|
full_paths = [f"{r['fs_path']}/{r['fs_name']}" for r in roms]
|
|
assert len(full_paths) == len(set(full_paths))
|
|
assert sum(1 for r in roms if r["fs_name"] == "Shared.zip") == 2
|
|
assert count == len(roms)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_roms_structure_two_wildcard_levels(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""Multiple wildcard levels descend depth-first to the terminal."""
|
|
handler = FSRomsHandler()
|
|
handler.base_path = tmp_path
|
|
with patch(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: self._make_structure_config(
|
|
{platform.fs_slug: "{platform}/roms/{region}/{system}/{game}"}
|
|
),
|
|
):
|
|
base = handler.get_roms_fs_structure(platform.fs_slug)
|
|
self._build_structure_library(tmp_path, base)
|
|
roms = await handler.get_roms(platform)
|
|
count = await handler.count_roms(platform)
|
|
|
|
keys = {(r["fs_path"], r["fs_name"]) for r in roms}
|
|
assert (f"{base}/Hacks/Inner", "x.bin") in keys
|
|
assert (f"{base}/Region/USA", "usagame.zip") in keys
|
|
# Depth-1 files are not surfaced at a depth-2 terminal.
|
|
assert (f"{base}/Hacks", "Shared.zip") not in keys
|
|
assert count == len(roms)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_roms_structure_literal_level(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""A literal section matches that exact folder only; sibling folders are
|
|
ignored."""
|
|
handler = FSRomsHandler()
|
|
handler.base_path = tmp_path
|
|
with patch(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: self._make_structure_config(
|
|
{platform.fs_slug: "{platform}/roms/Hacks/{game}"}
|
|
),
|
|
):
|
|
base = handler.get_roms_fs_structure(platform.fs_slug)
|
|
self._build_structure_library(tmp_path, base)
|
|
roms = await handler.get_roms(platform)
|
|
count = await handler.count_roms(platform)
|
|
|
|
keys = {(r["fs_path"], r["fs_name"]) for r in roms}
|
|
assert (f"{base}/Hacks", "Shared.zip") in keys
|
|
assert (f"{base}/Hacks", "HackOnly.zip") in keys
|
|
# The literal only matched "Hacks"; "Translations" is ignored.
|
|
assert (f"{base}/Translations", "Shared.zip") not in keys
|
|
assert all(r["fs_path"] == f"{base}/Hacks" for r in roms)
|
|
assert count == len(roms)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_roms_structure_list_unions_loose_and_grouped(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""A list of templates unions their discovery: games in the platform
|
|
folder plus games inside grouping subfolders, the common mixed layout,
|
|
without dropping either, deduplicated by full path."""
|
|
handler = FSRomsHandler()
|
|
handler.base_path = tmp_path
|
|
with patch(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: self._make_structure_config(
|
|
{
|
|
platform.fs_slug: [
|
|
"{platform}/roms/{game}",
|
|
"{platform}/roms/{category}/{game}",
|
|
]
|
|
}
|
|
),
|
|
):
|
|
base = handler.get_roms_fs_structure(platform.fs_slug)
|
|
self._build_structure_library(tmp_path, base)
|
|
roms = await handler.get_roms(platform)
|
|
count = await handler.count_roms(platform)
|
|
|
|
keys = {(r["fs_path"], r["fs_name"]) for r in roms}
|
|
# Loose game in the platform folder.
|
|
assert (base, "Top.zip") in keys
|
|
# Grouped games one level down.
|
|
assert (f"{base}/Hacks", "Shared.zip") in keys
|
|
assert (f"{base}/Hacks", "HackOnly.zip") in keys
|
|
assert (f"{base}/Translations", "Shared.zip") in keys
|
|
# Hidden folder still skipped; no duplicate full paths.
|
|
assert not any(r["fs_path"].endswith("/.hidden") for r in roms)
|
|
full_paths = [f"{r['fs_path']}/{r['fs_name']}" for r in roms]
|
|
assert len(full_paths) == len(set(full_paths))
|
|
assert count == len(roms)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_roms_grouping_folder_is_not_also_a_game(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""A `{game}` at the root must not claim a folder another template
|
|
descends into: `Hacks` is a category here, not a multi-file game."""
|
|
handler = FSRomsHandler()
|
|
handler.base_path = tmp_path
|
|
with patch(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: self._make_structure_config(
|
|
{
|
|
platform.fs_slug: [
|
|
"{platform}/roms/{game}",
|
|
"{platform}/roms/{category}/{game}",
|
|
]
|
|
}
|
|
),
|
|
):
|
|
base = handler.get_roms_fs_structure(platform.fs_slug)
|
|
self._build_structure_library(tmp_path, base)
|
|
roms = await handler.get_roms(platform)
|
|
count = await handler.count_roms(platform)
|
|
|
|
keys = {(r["fs_path"], r["fs_name"]) for r in roms}
|
|
# Descended into, so categories rather than games.
|
|
assert (base, "Hacks") not in keys
|
|
assert (base, "Translations") not in keys
|
|
assert (base, "Region") not in keys
|
|
# Their contents are the games, files and folders alike.
|
|
assert (f"{base}/Hacks", "HackOnly.zip") in keys
|
|
assert (f"{base}/Hacks", "Inner") in keys
|
|
assert (f"{base}/Region", "USA") in keys
|
|
# A loose file in the platform folder is still a game of its own.
|
|
assert (base, "Top.zip") in keys
|
|
assert count == len(roms)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_roms_default_layout_nested_under_a_category(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""One `{category}/{game}` template reproduces the default scan a level
|
|
down: each file a game, each folder a game."""
|
|
handler = FSRomsHandler()
|
|
handler.base_path = tmp_path
|
|
with patch(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: self._make_structure_config(
|
|
{platform.fs_slug: "{platform}/roms/{category}/{game}"}
|
|
),
|
|
):
|
|
base = handler.get_roms_fs_structure(platform.fs_slug)
|
|
self._build_structure_library(tmp_path, base)
|
|
roms = await handler.get_roms(platform)
|
|
count = await handler.count_roms(platform)
|
|
|
|
by_key = {(r["fs_path"], r["fs_name"]): r for r in roms}
|
|
assert by_key[(f"{base}/Hacks", "HackOnly.zip")]["flat"] is True
|
|
assert by_key[(f"{base}/Hacks", "Inner")]["flat"] is False
|
|
assert by_key[(f"{base}/Region", "USA")]["flat"] is False
|
|
# The top level holds no games under this template.
|
|
assert not any(r["fs_path"] == base for r in roms)
|
|
assert count == len(roms)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_roms_structure_wildcard_skips_excluded_dirs(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""A wildcard level must not descend into folders that are never a game
|
|
(scraper media output, NAS metadata), or their contents read as roms."""
|
|
handler = FSRomsHandler()
|
|
handler.base_path = tmp_path
|
|
with patch(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: self._make_structure_config(
|
|
{platform.fs_slug: "{platform}/roms/{category}/{game}"}
|
|
),
|
|
):
|
|
base = handler.get_roms_fs_structure(platform.fs_slug)
|
|
roms_dir = tmp_path / base
|
|
(roms_dir / "Hacks").mkdir(parents=True)
|
|
(roms_dir / "Hacks" / "HackOnly.zip").write_text("h")
|
|
(roms_dir / "@eaDir").mkdir()
|
|
(roms_dir / "@eaDir" / "thumb.zip").write_text("t")
|
|
roms = await handler.get_roms(platform)
|
|
|
|
keys = {(r["fs_path"], r["fs_name"]) for r in roms}
|
|
assert keys == {(f"{base}/Hacks", "HackOnly.zip")}
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_roms_structure_literal_level_matches_an_excluded_name(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""Naming the folder outright is an explicit opt-in, so the exclusion
|
|
list that guards wildcard levels does not override it."""
|
|
handler = FSRomsHandler()
|
|
handler.base_path = tmp_path
|
|
with patch(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: self._make_structure_config(
|
|
{platform.fs_slug: "{platform}/roms/@eaDir/{game}"}
|
|
),
|
|
):
|
|
base = handler.get_roms_fs_structure(platform.fs_slug)
|
|
roms_dir = tmp_path / base
|
|
(roms_dir / "@eaDir").mkdir(parents=True)
|
|
(roms_dir / "@eaDir" / "game.zip").write_text("g")
|
|
roms = await handler.get_roms(platform)
|
|
|
|
assert {(r["fs_path"], r["fs_name"]) for r in roms} == {
|
|
(f"{base}/@eaDir", "game.zip")
|
|
}
|
|
|
|
def test_upload_path_without_a_structure_is_the_roms_folder(
|
|
self, platform: Platform
|
|
):
|
|
handler = FSRomsHandler()
|
|
with patch(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: self._make_structure_config({}),
|
|
):
|
|
assert handler.get_roms_upload_path(
|
|
platform.fs_slug
|
|
) == handler.get_roms_fs_structure(platform.fs_slug)
|
|
|
|
def test_upload_path_follows_the_literal_levels_of_a_structure(
|
|
self, platform: Platform
|
|
):
|
|
"""A file dropped at the roms root would not be discovered, so the next
|
|
scan would flag the upload missing."""
|
|
handler = FSRomsHandler()
|
|
with patch(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: self._make_structure_config(
|
|
{platform.fs_slug: "{platform}/roms/Games/Loose/{game}"}
|
|
),
|
|
):
|
|
base = handler.get_roms_fs_structure(platform.fs_slug)
|
|
assert (
|
|
handler.get_roms_upload_path(platform.fs_slug) == f"{base}/Games/Loose"
|
|
)
|
|
|
|
def test_upload_path_prefers_a_template_an_upload_can_satisfy(
|
|
self, platform: Platform
|
|
):
|
|
"""Only literal levels name the folder an upload has to go in, so a
|
|
template with a wildcard is skipped for the next one that pins it down."""
|
|
handler = FSRomsHandler()
|
|
with patch(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: self._make_structure_config(
|
|
{
|
|
platform.fs_slug: [
|
|
"{platform}/roms/{category}/{game}",
|
|
"{platform}/roms/Loose/{game}",
|
|
]
|
|
}
|
|
),
|
|
):
|
|
base = handler.get_roms_fs_structure(platform.fs_slug)
|
|
assert handler.get_roms_upload_path(platform.fs_slug) == f"{base}/Loose"
|
|
|
|
def test_upload_path_refuses_when_only_the_user_can_pick_the_folder(
|
|
self, platform: Platform
|
|
):
|
|
handler = FSRomsHandler()
|
|
with patch(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: self._make_structure_config(
|
|
{platform.fs_slug: "{platform}/roms/{category}/{game}"}
|
|
),
|
|
):
|
|
with pytest.raises(ValueError, match="custom library structure"):
|
|
handler.get_roms_upload_path(platform.fs_slug)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_single_rom(
|
|
self, handler: FSRomsHandler, rom_single, config
|
|
):
|
|
"""Test get_rom_files with a single ROM file"""
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
m.setattr("os.path.exists", lambda x: False)
|
|
|
|
parsed_rom_files = await handler.get_rom_files(rom_single)
|
|
|
|
assert len(parsed_rom_files.rom_files) == 1
|
|
assert isinstance(parsed_rom_files.rom_files[0], RomFile)
|
|
assert parsed_rom_files.rom_files[0].file_name == "Paper Mario (USA).z64"
|
|
assert parsed_rom_files.rom_files[0].file_path == "n64/roms"
|
|
assert parsed_rom_files.rom_files[0].file_size_bytes > 0
|
|
|
|
assert parsed_rom_files.crc_hash == "efb5af2e"
|
|
assert parsed_rom_files.md5_hash == "0f343b0931126a20f133d67c2b018a3b"
|
|
assert (
|
|
parsed_rom_files.sha1_hash == "60cacbf3d72e1e7834203da608037b1bf83b40e8"
|
|
)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_multi_rom(
|
|
self, handler: FSRomsHandler, rom_multi, config
|
|
):
|
|
"""Test get_rom_files with a multi-part ROM"""
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
m.setattr("os.path.exists", lambda x: False)
|
|
|
|
parsed_rom_files = await handler.get_rom_files(rom_multi)
|
|
|
|
assert len(parsed_rom_files.rom_files) >= 2 # Should have multiple parts
|
|
|
|
file_names = [rf.file_name for rf in parsed_rom_files.rom_files]
|
|
assert "Super Mario 64 (J) (Rev A) [Part 1].z64" in file_names
|
|
assert "Super Mario 64 (J) (Rev A) [Part 2].z64" in file_names
|
|
|
|
for rom_file in parsed_rom_files.rom_files:
|
|
assert isinstance(rom_file, RomFile)
|
|
assert rom_file.file_size_bytes > 0
|
|
assert rom_file.last_modified is not None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_single_rom_hashes_match_its_only_file(
|
|
self, handler: FSRomsHandler, rom_single, config
|
|
):
|
|
"""A single-file ROM's hashes are its one file's hashes.
|
|
|
|
The ROM-level values reuse that file's hashers instead of streaming the
|
|
same bytes through a second set, so pin the identity that allows it.
|
|
"""
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
m.setattr("os.path.exists", lambda x: False)
|
|
|
|
parsed_rom_files = await handler.get_rom_files(rom_single)
|
|
|
|
assert len(parsed_rom_files.rom_files) == 1
|
|
only_file = parsed_rom_files.rom_files[0]
|
|
assert parsed_rom_files.crc_hash == only_file.crc_hash
|
|
assert parsed_rom_files.md5_hash == only_file.md5_hash
|
|
assert parsed_rom_files.sha1_hash == only_file.sha1_hash
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_multi_rom_hash_spans_every_part(
|
|
self, handler: FSRomsHandler, rom_multi, config
|
|
):
|
|
"""A multi-file ROM hashes the concatenation of its top-level files.
|
|
|
|
That composite cannot be derived from the per-file digests, so the parts
|
|
are streamed through a second set of hashers. Pin that it still covers
|
|
every part rather than collapsing to a single file's hash.
|
|
"""
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
m.setattr("os.path.exists", lambda x: False)
|
|
|
|
parsed_rom_files = await handler.get_rom_files(rom_multi)
|
|
|
|
rom_dir = Path(LIBRARY_BASE_PATH, "n64", "roms", rom_multi.fs_name)
|
|
expected_md5 = hashlib.md5(usedforsecurity=False)
|
|
for rom_file in parsed_rom_files.rom_files:
|
|
expected_md5.update((rom_dir / rom_file.file_name).read_bytes())
|
|
|
|
assert len(parsed_rom_files.rom_files) >= 2
|
|
assert parsed_rom_files.md5_hash == expected_md5.hexdigest()
|
|
assert parsed_rom_files.md5_hash not in {
|
|
rom_file.md5_hash for rom_file in parsed_rom_files.rom_files
|
|
}
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_multi_rom_multi_dot_exclusion(
|
|
self, handler: FSRomsHandler, rom_multi
|
|
):
|
|
"""Multi-dot filenames in a multi-part dir are excluded by simple or compound ext rules."""
|
|
multi_dot_file = (
|
|
handler.base_path / "n64/roms/Super Mario 64 (J) (Rev A)/game.n64.hash.txt"
|
|
)
|
|
multi_dot_file.write_text("hash data")
|
|
|
|
try:
|
|
# Exclude by the last single extension "txt"
|
|
config_txt = Config(
|
|
EXCLUDED_PLATFORMS=[],
|
|
EXCLUDED_SINGLE_EXT=[],
|
|
EXCLUDED_SINGLE_FILES=[],
|
|
EXCLUDED_MULTI_FILES=[],
|
|
EXCLUDED_MULTI_PARTS_EXT=["txt"],
|
|
EXCLUDED_MULTI_PARTS_FILES=[],
|
|
PLATFORMS_BINDING={},
|
|
PLATFORMS_VERSIONS={},
|
|
STRUCTURE_TEMPLATES={
|
|
"default": "{platform}/roms/{game}",
|
|
"firmware": "{platform}/bios",
|
|
},
|
|
)
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr(
|
|
"handler.filesystem.roms_handler.cm.get_config", lambda: config_txt
|
|
)
|
|
parsed = await handler.get_rom_files(rom_multi)
|
|
file_names = [rf.file_name for rf in parsed.rom_files]
|
|
assert "game.n64.hash.txt" not in file_names
|
|
assert "Super Mario 64 (J) (Rev A) [Part 1].z64" in file_names
|
|
|
|
# Exclude by the compound extension "hash.txt"
|
|
config_compound = Config(
|
|
EXCLUDED_PLATFORMS=[],
|
|
EXCLUDED_SINGLE_EXT=[],
|
|
EXCLUDED_SINGLE_FILES=[],
|
|
EXCLUDED_MULTI_FILES=[],
|
|
EXCLUDED_MULTI_PARTS_EXT=["hash.txt"],
|
|
EXCLUDED_MULTI_PARTS_FILES=[],
|
|
PLATFORMS_BINDING={},
|
|
PLATFORMS_VERSIONS={},
|
|
STRUCTURE_TEMPLATES={
|
|
"default": "{platform}/roms/{game}",
|
|
"firmware": "{platform}/bios",
|
|
},
|
|
)
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr(
|
|
"handler.filesystem.roms_handler.cm.get_config",
|
|
lambda: config_compound,
|
|
)
|
|
parsed = await handler.get_rom_files(rom_multi)
|
|
file_names = [rf.file_name for rf in parsed.rom_files]
|
|
assert "game.n64.hash.txt" not in file_names
|
|
assert "Super Mario 64 (J) (Rev A) [Part 1].z64" in file_names
|
|
finally:
|
|
multi_dot_file.unlink(missing_ok=True)
|
|
|
|
async def test_rename_fs_rom_same_name(self, handler: FSRomsHandler):
|
|
"""Test rename_fs_rom when old and new names are the same"""
|
|
old_name = "test_rom.n64"
|
|
new_name = "test_rom.n64"
|
|
fs_path = "n64/roms"
|
|
|
|
# Should not raise any exception
|
|
await handler.rename_fs_rom(old_name, new_name, fs_path)
|
|
|
|
async def test_rename_fs_rom_different_name_target_exists(
|
|
self, handler: FSRomsHandler
|
|
):
|
|
"""Test rename_fs_rom when target file already exists"""
|
|
old_name = "Paper Mario (USA).z64"
|
|
new_name = "test_game.n64" # This file exists
|
|
fs_path = "n64/roms"
|
|
|
|
from exceptions.fs_exceptions import RomAlreadyExistsException
|
|
|
|
with pytest.raises(RomAlreadyExistsException):
|
|
await handler.rename_fs_rom(old_name, new_name, fs_path)
|
|
|
|
async def test_rename_fs_rom_successful_rename(self, handler: FSRomsHandler):
|
|
"""Test successful ROM file rename"""
|
|
# Create a test file to rename
|
|
test_file = handler.base_path / "n64/roms/test_rename.n64"
|
|
test_file.write_text("Test ROM content")
|
|
|
|
old_name = "test_rename.n64"
|
|
new_name = "renamed_rom.n64"
|
|
fs_path = "n64/roms"
|
|
|
|
try:
|
|
await handler.rename_fs_rom(old_name, new_name, fs_path)
|
|
|
|
# Check that old file is gone and new file exists
|
|
old_path = handler.base_path / fs_path / old_name
|
|
new_path = handler.base_path / fs_path / new_name
|
|
|
|
assert not old_path.exists()
|
|
assert new_path.exists()
|
|
assert new_path.read_text() == "Test ROM content"
|
|
finally:
|
|
# Clean up
|
|
new_path = handler.base_path / fs_path / new_name
|
|
if new_path.exists():
|
|
new_path.unlink()
|
|
|
|
def test_integration_with_base_handler_methods(self, handler: FSRomsHandler):
|
|
"""Test that FSRomsHandler properly inherits from FSHandler"""
|
|
# Test that handler has base methods
|
|
assert hasattr(handler, "validate_path")
|
|
assert hasattr(handler, "list_files")
|
|
assert hasattr(handler, "list_directories")
|
|
assert hasattr(handler, "file_exists")
|
|
assert hasattr(handler, "move_file_or_folder")
|
|
assert hasattr(handler, "stream_file")
|
|
assert hasattr(handler, "exclude_single_files")
|
|
|
|
async def test_exclude_single_files_integration(
|
|
self, handler: FSRomsHandler, config
|
|
):
|
|
"""Test that exclude_single_files works with actual ROM files"""
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
|
|
# Get all files in the ROM directory
|
|
all_files = await handler.list_files(path="n64/roms")
|
|
|
|
# Should include .tmp files before exclusion
|
|
assert "excluded_test.tmp" in all_files
|
|
assert "Paper Mario (USA).z64" in all_files
|
|
|
|
# After exclusion, .tmp files should be removed
|
|
filtered_files = handler.exclude_single_files(all_files)
|
|
assert "excluded_test.tmp" not in filtered_files
|
|
assert "Paper Mario (USA).z64" in filtered_files
|
|
|
|
async def test_file_operations_with_actual_structure(self, handler: FSRomsHandler):
|
|
"""Test that file operations work with the actual ROM directory structure"""
|
|
# Test that we can list files
|
|
n64_files = await handler.list_files("n64/roms")
|
|
assert len(n64_files) > 0
|
|
|
|
n64_dirs = await handler.list_directories("n64/roms")
|
|
assert len(n64_dirs) > 0
|
|
|
|
# Test that we can check file existence
|
|
assert await handler.file_exists("n64/roms/Paper Mario (USA).z64")
|
|
assert await handler.file_exists("n64/roms/test_game.n64")
|
|
assert not await handler.file_exists("n64/roms/nonexistent.rom")
|
|
|
|
async def test_stream_file_with_actual_roms(self, handler: FSRomsHandler):
|
|
"""Test streaming actual ROM files"""
|
|
async with await handler.stream_file("n64/roms/Paper Mario (USA).z64") as f:
|
|
content = await f.read()
|
|
assert len(content) > 0
|
|
|
|
async with await handler.stream_file("n64/roms/test_game.n64") as f:
|
|
content = await f.read()
|
|
assert len(content) > 0
|
|
assert b"Test N64 ROM" in content
|
|
|
|
def test_tag_parsing_edge_cases(self, handler: FSRomsHandler):
|
|
"""Test tag parsing with edge cases"""
|
|
# Test with comma-separated tags
|
|
parsed_tags = handler.parse_tags("Game (USA,Europe) [En,Fr,De].rom")
|
|
assert "USA" in parsed_tags.regions
|
|
assert "Europe" in parsed_tags.regions
|
|
assert "English" in parsed_tags.languages
|
|
assert "French" in parsed_tags.languages
|
|
assert "German" in parsed_tags.languages
|
|
assert parsed_tags.version == ""
|
|
|
|
# Test with reg- prefix
|
|
parsed_tags = handler.parse_tags("Game [Reg-NTSC].rom")
|
|
assert "NTSC" in parsed_tags.regions
|
|
assert parsed_tags.version == ""
|
|
|
|
# Test with rev- prefix
|
|
parsed_tags = handler.parse_tags("Game [Rev-B].rom")
|
|
assert parsed_tags.revision == "B"
|
|
assert parsed_tags.version == ""
|
|
|
|
def test_platform_specific_behavior(self, handler: FSRomsHandler, config):
|
|
"""Test platform-specific behavior differences"""
|
|
# Create mock platforms - one hashable, one non-hashable
|
|
hashable_platform = Mock(spec=Platform)
|
|
hashable_platform.fs_slug = "gba"
|
|
hashable_platform.slug = "gba"
|
|
|
|
non_hashable_platform = Mock(spec=Platform)
|
|
non_hashable_platform.fs_slug = "n64"
|
|
non_hashable_platform.slug = "nintendo-64"
|
|
|
|
config.has_structure_path_b = True
|
|
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
|
|
hashable_path = handler.get_roms_fs_structure(hashable_platform.fs_slug)
|
|
non_hashable_path = handler.get_roms_fs_structure(
|
|
non_hashable_platform.fs_slug
|
|
)
|
|
|
|
assert hashable_path == f"{hashable_platform.fs_slug}/roms"
|
|
assert non_hashable_path == f"{non_hashable_platform.fs_slug}/roms"
|
|
|
|
async def test_multi_rom_directory_handling(self, handler: FSRomsHandler, config):
|
|
"""Test handling of multi-ROM directories with actual structure"""
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
|
|
# List directories in the ROM path
|
|
directories = await handler.list_directories("n64/roms")
|
|
|
|
# Should include our multi-ROM directories
|
|
assert "Super Mario 64 (J) (Rev A)" in directories
|
|
assert "Test Multi Rom [USA]" in directories
|
|
|
|
# After exclusion, normal directories should remain
|
|
filtered_dirs = handler.exclude_multi_roms(directories)
|
|
assert "Super Mario 64 (J) (Rev A)" in filtered_dirs
|
|
assert "Test Multi Rom [USA]" in filtered_dirs
|
|
|
|
def test_rom_fs_structure_consistency(self, handler: FSRomsHandler, config):
|
|
"""Test that ROM filesystem structure is consistent across methods"""
|
|
with pytest.MonkeyPatch.context() as m:
|
|
m.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: config)
|
|
|
|
assert handler.get_roms_fs_structure("gba") == "gba/roms"
|
|
assert handler.get_roms_upload_path("gba") == "gba/roms"
|
|
|
|
def test_actual_file_hash_calculation(self, handler: FSRomsHandler):
|
|
"""Test hash calculation with actual files"""
|
|
# Create a test file with known content for hash verification
|
|
test_content = b"Test ROM content for hashing"
|
|
test_file = handler.base_path / "n64/roms/hash_test.n64"
|
|
test_file.write_bytes(test_content)
|
|
|
|
try:
|
|
# Calculate expected hashes
|
|
import binascii
|
|
import hashlib
|
|
|
|
expected_crc = binascii.crc32(test_content)
|
|
expected_md5 = hashlib.md5(test_content, usedforsecurity=False).hexdigest()
|
|
expected_sha1 = hashlib.sha1(
|
|
test_content, usedforsecurity=False
|
|
).hexdigest()
|
|
|
|
# Test the hash calculation method
|
|
crc_result, _, md5_result, _, sha1_result, _ = (
|
|
handler._calculate_rom_hashes(
|
|
test_file,
|
|
0,
|
|
hashlib.md5(usedforsecurity=False),
|
|
hashlib.sha1(usedforsecurity=False),
|
|
)
|
|
)
|
|
|
|
assert crc_result == expected_crc
|
|
assert md5_result.hexdigest() == expected_md5
|
|
assert sha1_result.hexdigest() == expected_sha1
|
|
|
|
finally:
|
|
# Clean up
|
|
if test_file.exists():
|
|
test_file.unlink()
|
|
|
|
async def test_compressed_file_handling(self, handler: FSRomsHandler):
|
|
"""Test handling of compressed ROM files"""
|
|
# Test with the ZIP file
|
|
psx_files = await handler.list_files("psx/roms")
|
|
assert "PaRappa the Rapper.zip" in psx_files
|
|
|
|
# Verify we can stream the compressed file
|
|
async with await handler.stream_file("psx/roms/PaRappa the Rapper.zip") as f:
|
|
content = await f.read()
|
|
assert len(content) > 0
|
|
|
|
async def test_top_level_files_only_in_main_hash(
|
|
self, handler: FSRomsHandler, rom_single_nested
|
|
):
|
|
"""Test that only top-level files contribute to main ROM hash calculation"""
|
|
parsed_rom_files = await handler.get_rom_files(rom_single_nested)
|
|
|
|
# Verify we have multiple files (base game + translation)
|
|
assert len(parsed_rom_files.rom_files) == 2
|
|
|
|
base_game_rom_file = None
|
|
translation_rom_file = None
|
|
|
|
for rom_file in parsed_rom_files.rom_files:
|
|
if rom_file.file_name == "Sonic (EU) [T].n64":
|
|
base_game_rom_file = rom_file
|
|
elif rom_file.file_name == "Sonic (EU) [T-En].z64":
|
|
translation_rom_file = rom_file
|
|
|
|
assert base_game_rom_file is not None, "Base game file not found"
|
|
assert translation_rom_file is not None, "Translation file not found"
|
|
|
|
# Verify file categories
|
|
assert base_game_rom_file.category is None
|
|
assert translation_rom_file.category == RomFileCategory.TRANSLATION
|
|
|
|
# The main ROM hash should be different from the translation file hash
|
|
# (this verifies that the translation is not included in the main hash)
|
|
|
|
assert (
|
|
parsed_rom_files.md5_hash == base_game_rom_file.md5_hash
|
|
), "Main ROM hash should include base game file"
|
|
assert (
|
|
parsed_rom_files.md5_hash != translation_rom_file.md5_hash
|
|
), "Main ROM hash should not include translation file"
|
|
|
|
assert (
|
|
parsed_rom_files.sha1_hash == base_game_rom_file.sha1_hash
|
|
), "Main ROM hash should include base game file"
|
|
assert (
|
|
parsed_rom_files.sha1_hash != translation_rom_file.sha1_hash
|
|
), "Main ROM hash should not include translation file"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_with_chd_v5_uses_internal_hash(
|
|
self, handler: FSRomsHandler, platform, tmp_path
|
|
):
|
|
"""Test that a CHD v5 file stores the header SHA1 in chd_sha1_hash.
|
|
|
|
CHD files are hashed like any other file type (CRC32, MD5, SHA1 from
|
|
raw bytes). The embedded disc-data SHA1 from the CHD v5 header is
|
|
separately stored in chd_sha1_hash for metadata providers that need it.
|
|
"""
|
|
# Create a mock CHD v5 file in a temporary directory
|
|
chd_file = tmp_path / "test.chd"
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(5).to_bytes(4, "big")
|
|
internal_sha1 = "0123456789abcdef0123456789abcdef01234567"
|
|
header[84:104] = bytes.fromhex(internal_sha1)
|
|
chd_file.write_bytes(
|
|
header + b"This is extra file data to ensure file is not empty"
|
|
)
|
|
|
|
# Set up handler and rom object to point to the mock file
|
|
roms_path = tmp_path / platform.fs_slug / "roms"
|
|
roms_path.mkdir(parents=True)
|
|
shutil.copy(chd_file, roms_path / "test.chd")
|
|
|
|
# Create a new handler instance with temp base path
|
|
test_handler = FSRomsHandler()
|
|
test_handler.base_path = tmp_path
|
|
|
|
rom = Rom(
|
|
id=1,
|
|
fs_name="test.chd",
|
|
fs_extension="chd",
|
|
fs_path=str(roms_path.relative_to(tmp_path)),
|
|
platform=platform,
|
|
)
|
|
|
|
# Run the hashing process
|
|
parsed_rom_files = await test_handler.get_rom_files(rom)
|
|
|
|
# All three raw-file hashes should be populated
|
|
assert len(parsed_rom_files.rom_files) == 1
|
|
assert parsed_rom_files.crc_hash != "", "CRC should be computed from raw bytes"
|
|
assert parsed_rom_files.md5_hash != "", "MD5 should be computed from raw bytes"
|
|
assert (
|
|
parsed_rom_files.sha1_hash != ""
|
|
), "SHA1 should be computed from raw bytes"
|
|
|
|
# Raw file SHA1 is NOT the header SHA1
|
|
assert parsed_rom_files.sha1_hash != internal_sha1
|
|
|
|
# Header SHA1 stored separately in chd_sha1_hash
|
|
assert parsed_rom_files.rom_files[0].chd_sha1_hash == internal_sha1
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_folder_extracts_chd_hash_per_file(
|
|
self, tmp_path: Path
|
|
):
|
|
"""Every CHD in a folder-based ROM keeps its own header SHA1."""
|
|
disc_platform = Platform(name="PlayStation", slug="psx", fs_slug="psx")
|
|
roms_path = tmp_path / disc_platform.fs_slug / "roms"
|
|
game_dir = roms_path / "Final Fantasy VII"
|
|
game_dir.mkdir(parents=True)
|
|
|
|
sha1_by_disc = {
|
|
"Final Fantasy VII (Disc 1).chd": "0123456789abcdef0123456789abcdef01234567",
|
|
"Final Fantasy VII (Disc 2).chd": "89abcdef0123456789abcdef0123456789abcdef",
|
|
}
|
|
for file_name, internal_sha1 in sha1_by_disc.items():
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(5).to_bytes(4, "big")
|
|
header[84:104] = bytes.fromhex(internal_sha1)
|
|
(game_dir / file_name).write_bytes(header + file_name.encode())
|
|
|
|
(game_dir / "Final Fantasy VII.m3u").write_text("\n".join(sha1_by_disc) + "\n")
|
|
|
|
test_handler = FSRomsHandler()
|
|
test_handler.base_path = tmp_path
|
|
|
|
rom = Rom(
|
|
id=1,
|
|
fs_name="Final Fantasy VII",
|
|
fs_extension="",
|
|
fs_path=str(roms_path.relative_to(tmp_path)),
|
|
platform=disc_platform,
|
|
)
|
|
|
|
with patch(
|
|
"adapters.services.rahasher.RAHasherService.calculate_hash",
|
|
return_value="",
|
|
):
|
|
parsed_rom_files = await test_handler.get_rom_files(rom)
|
|
|
|
chd_hash_by_file = {
|
|
rom_file.file_name: rom_file.chd_sha1_hash
|
|
for rom_file in parsed_rom_files.rom_files
|
|
}
|
|
assert chd_hash_by_file == {
|
|
**sha1_by_disc,
|
|
"Final Fantasy VII.m3u": "",
|
|
}
|
|
|
|
@staticmethod
|
|
def _setup_archive_rom(
|
|
tmp_path: Path, platform: Platform, fs_name: str, fs_extension: str, data: bytes
|
|
) -> tuple[FSRomsHandler, Rom]:
|
|
roms_path = tmp_path / platform.fs_slug / "roms"
|
|
roms_path.mkdir(parents=True, exist_ok=True)
|
|
(roms_path / fs_name).write_bytes(data)
|
|
test_handler = FSRomsHandler()
|
|
test_handler.base_path = tmp_path
|
|
rom = Rom(
|
|
id=1,
|
|
fs_name=fs_name,
|
|
fs_extension=fs_extension,
|
|
fs_path=str(roms_path.relative_to(tmp_path)),
|
|
platform=platform,
|
|
)
|
|
return test_handler, rom
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_zip_composite_hash_sorted_order(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""Zip member bytes are hashed in ASCII path order regardless of insertion order."""
|
|
import hashlib
|
|
import io
|
|
import zipfile
|
|
|
|
contents = {
|
|
"a.bin": b"AAA content for first file",
|
|
"b.bin": b"BBB content for second file",
|
|
"c.bin": b"CCC content for third file",
|
|
}
|
|
|
|
reload_zipfile()
|
|
buf = io.BytesIO()
|
|
with zipfile.ZipFile(buf, "w") as zf:
|
|
# Insert in reverse to ensure sorting is what governs order
|
|
for name in ("c.bin", "b.bin", "a.bin"):
|
|
zf.writestr(name, contents[name])
|
|
|
|
test_handler, rom = self._setup_archive_rom(
|
|
tmp_path, platform, "game.zip", "zip", buf.getvalue()
|
|
)
|
|
|
|
parsed = await test_handler.get_rom_files(rom)
|
|
|
|
concat = b"".join(contents[k] for k in sorted(contents))
|
|
assert parsed.md5_hash == hashlib.md5(concat, usedforsecurity=False).hexdigest()
|
|
assert (
|
|
parsed.sha1_hash == hashlib.sha1(concat, usedforsecurity=False).hexdigest()
|
|
)
|
|
|
|
# Only one RomFile (the archive itself) is surfaced, not one per member.
|
|
# Per-member hashes are stored on `archive_members`.
|
|
assert len(parsed.rom_files) == 1
|
|
archive_rom_file = parsed.rom_files[0]
|
|
assert archive_rom_file.file_name == "game.zip"
|
|
assert archive_rom_file.md5_hash == parsed.md5_hash
|
|
# full_path resolves to a file that actually exists on disk
|
|
assert (Path(test_handler.base_path) / archive_rom_file.full_path).is_file()
|
|
|
|
assert archive_rom_file.archive_members is not None
|
|
# ASCII-sorted ordering, and each member has the right size + hashes
|
|
assert [m["name"] for m in archive_rom_file.archive_members] == sorted(contents)
|
|
for member in archive_rom_file.archive_members:
|
|
data = contents[member["name"]]
|
|
assert member["size"] == len(data)
|
|
assert (
|
|
member["md5_hash"]
|
|
== hashlib.md5(data, usedforsecurity=False).hexdigest()
|
|
)
|
|
assert (
|
|
member["sha1_hash"]
|
|
== hashlib.sha1(data, usedforsecurity=False).hexdigest()
|
|
)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_zip_ordering_invariant(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""Two zips with the same members in different insertion order hash identically."""
|
|
import io
|
|
import zipfile
|
|
|
|
members = [("a.bin", b"AAA"), ("b.bin", b"BBB"), ("c.bin", b"CCC")]
|
|
|
|
reload_zipfile()
|
|
|
|
def build_zip(order: list[tuple[str, bytes]]) -> bytes:
|
|
buf = io.BytesIO()
|
|
with zipfile.ZipFile(buf, "w") as zf:
|
|
for name, data in order:
|
|
zf.writestr(name, data)
|
|
return buf.getvalue()
|
|
|
|
forward = build_zip(members)
|
|
reverse = build_zip(list(reversed(members)))
|
|
|
|
handler_f, rom_f = self._setup_archive_rom(
|
|
tmp_path, platform, "forward.zip", "zip", forward
|
|
)
|
|
handler_r, rom_r = self._setup_archive_rom(
|
|
tmp_path, platform, "reverse.zip", "zip", reverse
|
|
)
|
|
|
|
parsed_f = await handler_f.get_rom_files(rom_f)
|
|
parsed_r = await handler_r.get_rom_files(rom_r)
|
|
|
|
assert parsed_f.md5_hash == parsed_r.md5_hash
|
|
assert parsed_f.sha1_hash == parsed_r.sha1_hash
|
|
assert parsed_f.crc_hash == parsed_r.crc_hash
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_tar_gz_composite_hash_compound_ext(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""Compound .tar.gz extension routes through the tar reader and composites members."""
|
|
import hashlib
|
|
import io
|
|
import tarfile
|
|
|
|
members = {"a.bin": b"first member bytes", "b.bin": b"second member bytes"}
|
|
|
|
buf = io.BytesIO()
|
|
with tarfile.open(fileobj=buf, mode="w:gz") as tf:
|
|
for name in ("b.bin", "a.bin"): # reverse insertion
|
|
info = tarfile.TarInfo(name=name)
|
|
info.size = len(members[name])
|
|
tf.addfile(info, io.BytesIO(members[name]))
|
|
|
|
test_handler, rom = self._setup_archive_rom(
|
|
tmp_path, platform, "game.tar.gz", "tar.gz", buf.getvalue()
|
|
)
|
|
|
|
parsed = await test_handler.get_rom_files(rom)
|
|
|
|
concat = b"".join(members[k] for k in sorted(members))
|
|
assert parsed.md5_hash == hashlib.md5(concat, usedforsecurity=False).hexdigest()
|
|
assert len(parsed.rom_files) == 1
|
|
assert parsed.rom_files[0].file_name == "game.tar.gz"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_malformed_zip_falls_back_to_raw_bytes(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""A file with .zip extension that is not a valid zip falls back to raw-file hashing."""
|
|
import hashlib
|
|
|
|
junk = b"Definitely not a zip file. Just arbitrary bytes for fallback."
|
|
test_handler, rom = self._setup_archive_rom(
|
|
tmp_path, platform, "fake.zip", "zip", junk
|
|
)
|
|
|
|
parsed = await test_handler.get_rom_files(rom)
|
|
|
|
# On a malformed zip, the reader raises ArchiveReadError and the
|
|
# fallback path hashes the archive file itself.
|
|
assert parsed.md5_hash == hashlib.md5(junk, usedforsecurity=False).hexdigest()
|
|
assert parsed.sha1_hash == hashlib.sha1(junk, usedforsecurity=False).hexdigest()
|
|
assert len(parsed.rom_files) == 1
|
|
assert parsed.rom_files[0].file_name == "fake.zip"
|
|
assert parsed.rom_files[0].archive_members is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_zip_with_only_excluded_entries_falls_back(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""A zip whose entries are all default-excluded hashes the archive's raw bytes."""
|
|
import hashlib
|
|
import io
|
|
import zipfile
|
|
|
|
reload_zipfile()
|
|
buf = io.BytesIO()
|
|
with zipfile.ZipFile(buf, "w") as zf:
|
|
zf.writestr("foo.tmp", b"X" * 256) # excluded by extension
|
|
zf.writestr(".DS_Store", b"Y" * 8) # excluded by name
|
|
zip_bytes = buf.getvalue()
|
|
|
|
test_handler, rom = self._setup_archive_rom(
|
|
tmp_path, platform, "only_excluded.zip", "zip", zip_bytes
|
|
)
|
|
|
|
parsed = await test_handler.get_rom_files(rom)
|
|
|
|
assert (
|
|
parsed.md5_hash == hashlib.md5(zip_bytes, usedforsecurity=False).hexdigest()
|
|
)
|
|
assert (
|
|
parsed.sha1_hash
|
|
== hashlib.sha1(zip_bytes, usedforsecurity=False).hexdigest()
|
|
)
|
|
assert len(parsed.rom_files) == 1
|
|
assert parsed.rom_files[0].file_name == "only_excluded.zip"
|
|
assert parsed.rom_files[0].archive_members is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_empty_zip_falls_back_to_raw_bytes(
|
|
self, platform: Platform, tmp_path: Path
|
|
):
|
|
"""A zip with zero entries hashes the archive's raw bytes."""
|
|
import hashlib
|
|
import io
|
|
import zipfile
|
|
|
|
reload_zipfile()
|
|
buf = io.BytesIO()
|
|
with zipfile.ZipFile(buf, "w"):
|
|
pass
|
|
zip_bytes = buf.getvalue()
|
|
|
|
test_handler, rom = self._setup_archive_rom(
|
|
tmp_path, platform, "empty.zip", "zip", zip_bytes
|
|
)
|
|
|
|
parsed = await test_handler.get_rom_files(rom)
|
|
|
|
assert (
|
|
parsed.md5_hash == hashlib.md5(zip_bytes, usedforsecurity=False).hexdigest()
|
|
)
|
|
assert (
|
|
parsed.sha1_hash
|
|
== hashlib.sha1(zip_bytes, usedforsecurity=False).hexdigest()
|
|
)
|
|
assert len(parsed.rom_files) == 1
|
|
assert parsed.rom_files[0].file_name == "empty.zip"
|
|
assert parsed.rom_files[0].archive_members is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_with_non_v5_chd_fallback_to_std_hashing(
|
|
self, handler: FSRomsHandler, platform, tmp_path
|
|
):
|
|
"""Test that non-v5 CHD files fall back to standard file hashing.
|
|
|
|
This ensures backward compatibility: if a .chd file is not version 5
|
|
or doesn't have a valid v5 header, it should be treated as a regular
|
|
file and all hashes (CRC32, MD5, SHA1) are calculated from content.
|
|
"""
|
|
# Create a CHD v4 file (should not use internal hash logic)
|
|
chd_file = tmp_path / "old_format.chd"
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(4).to_bytes(4, "big") # Version 4, not 5
|
|
|
|
# Add some content
|
|
content = header + b"This is CHD v4 data that should be hashed as a normal file"
|
|
chd_file.write_bytes(content)
|
|
|
|
# Set up handler and rom object
|
|
roms_path = tmp_path / platform.fs_slug / "roms"
|
|
roms_path.mkdir(parents=True)
|
|
shutil.copy(chd_file, roms_path / "old_format.chd")
|
|
|
|
test_handler = FSRomsHandler()
|
|
test_handler.base_path = tmp_path
|
|
|
|
rom = Rom(
|
|
id=1,
|
|
fs_name="old_format.chd",
|
|
fs_extension="chd",
|
|
fs_path=str(roms_path.relative_to(tmp_path)),
|
|
platform=platform,
|
|
)
|
|
|
|
# Run the hashing process
|
|
parsed_rom_files = await test_handler.get_rom_files(rom)
|
|
|
|
# All hashes should be populated (calculated from file content)
|
|
assert len(parsed_rom_files.rom_files) == 1
|
|
assert (
|
|
parsed_rom_files.crc_hash != ""
|
|
), "CRC hash should be calculated for non-v5 CHD"
|
|
assert (
|
|
parsed_rom_files.md5_hash != ""
|
|
), "MD5 hash should be calculated for non-v5 CHD"
|
|
assert (
|
|
parsed_rom_files.sha1_hash != ""
|
|
), "SHA1 hash should be calculated for non-v5 CHD"
|
|
|
|
# Verify they're actual hash values (not from an internal header)
|
|
assert parsed_rom_files.rom_files[0].crc_hash == parsed_rom_files.crc_hash
|
|
assert parsed_rom_files.rom_files[0].md5_hash == parsed_rom_files.md5_hash
|
|
assert parsed_rom_files.rom_files[0].sha1_hash == parsed_rom_files.sha1_hash
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_archive_computes_ra_hash_for_cartridge_platform(
|
|
self, tmp_path: Path
|
|
):
|
|
"""RA hash is computed for cartridge-platform archives (buffer hashing supported)."""
|
|
import io
|
|
import zipfile
|
|
|
|
from tests._zipfile_shim import reload_zipfile
|
|
|
|
cartridge_platform = Platform(
|
|
name="Game Boy Advance", slug="gba", fs_slug="gba"
|
|
)
|
|
|
|
reload_zipfile()
|
|
buf = io.BytesIO()
|
|
with zipfile.ZipFile(buf, "w") as zf:
|
|
zf.writestr("game.gba", b"fake GBA ROM content")
|
|
|
|
test_handler, rom = self._setup_archive_rom(
|
|
tmp_path, cartridge_platform, "game.zip", "zip", buf.getvalue()
|
|
)
|
|
|
|
with patch(
|
|
"adapters.services.rahasher.RAHasherService.calculate_hash",
|
|
return_value="abcdef1234567890abcdef1234567890",
|
|
):
|
|
parsed = await test_handler.get_rom_files(rom)
|
|
|
|
assert parsed.ra_hash == "abcdef1234567890abcdef1234567890"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_rom_files_archive_skips_ra_hash_for_disc_platform(
|
|
self, tmp_path: Path
|
|
):
|
|
"""RA hash is not computed for disc-platform archives (buffer hashing unsupported)."""
|
|
import io
|
|
import zipfile
|
|
|
|
from tests._zipfile_shim import reload_zipfile
|
|
|
|
disc_platform = Platform(name="PlayStation", slug="psx", fs_slug="psx")
|
|
|
|
reload_zipfile()
|
|
buf = io.BytesIO()
|
|
with zipfile.ZipFile(buf, "w") as zf:
|
|
zf.writestr("game.bin", b"fake PSX disc content")
|
|
|
|
test_handler, rom = self._setup_archive_rom(
|
|
tmp_path, disc_platform, "game.zip", "zip", buf.getvalue()
|
|
)
|
|
|
|
with patch(
|
|
"adapters.services.rahasher.RAHasherService.calculate_hash",
|
|
return_value="",
|
|
) as mock_calculate:
|
|
parsed = await test_handler.get_rom_files(rom)
|
|
|
|
mock_calculate.assert_called_once()
|
|
assert parsed.ra_hash == ""
|
|
|
|
|
|
SIGIL_PATCH_TARGET = "adapters.services.sigil.SigilService.extract_title_id"
|
|
|
|
SWITCH_PLATFORM = Platform(name="Nintendo Switch", slug="switch", fs_slug="switch")
|
|
|
|
|
|
@pytest.fixture
|
|
def sigil_config(monkeypatch):
|
|
cnfg = Config(
|
|
EXCLUDED_PLATFORMS=[],
|
|
EXCLUDED_SINGLE_EXT=[],
|
|
EXCLUDED_SINGLE_FILES=[],
|
|
EXCLUDED_MULTI_FILES=[],
|
|
EXCLUDED_MULTI_PARTS_EXT=[],
|
|
EXCLUDED_MULTI_PARTS_FILES=[],
|
|
PLATFORMS_BINDING={},
|
|
PLATFORMS_VERSIONS={},
|
|
STRUCTURE_TEMPLATES={
|
|
"default": "{platform}/roms/{game}",
|
|
"firmware": "{platform}/bios",
|
|
},
|
|
)
|
|
monkeypatch.setattr("handler.filesystem.roms_handler.cm.get_config", lambda: cnfg)
|
|
return cnfg
|
|
|
|
|
|
@pytest.fixture
|
|
def stub_ra_hasher(monkeypatch):
|
|
"""Keep the hashable-platform cases off the real RAHasher binary."""
|
|
monkeypatch.setattr(
|
|
"adapters.services.rahasher.RAHasherService.calculate_hash",
|
|
AsyncMock(return_value=""),
|
|
)
|
|
|
|
|
|
def make_sigil_handler(tmp_path: Path) -> FSRomsHandler:
|
|
handler = FSRomsHandler()
|
|
handler.base_path = tmp_path
|
|
return handler
|
|
|
|
|
|
def make_single_file_rom(tmp_path: Path, platform: Platform, fs_name: str) -> Rom:
|
|
roms_path = tmp_path / platform.fs_slug / "roms"
|
|
roms_path.mkdir(parents=True, exist_ok=True)
|
|
(roms_path / fs_name).write_bytes(b"rom-bytes")
|
|
return Rom(
|
|
id=1,
|
|
fs_name=fs_name,
|
|
fs_path=f"{platform.fs_slug}/roms",
|
|
platform=platform,
|
|
)
|
|
|
|
|
|
def make_multi_part_rom(
|
|
tmp_path: Path, platform: Platform, fs_name: str, file_names: list[str]
|
|
) -> Rom:
|
|
rom_dir = tmp_path / platform.fs_slug / "roms" / fs_name
|
|
rom_dir.mkdir(parents=True, exist_ok=True)
|
|
for file_name in file_names:
|
|
file_path = rom_dir / file_name
|
|
file_path.parent.mkdir(parents=True, exist_ok=True)
|
|
file_path.write_bytes(b"rom-bytes")
|
|
return Rom(
|
|
id=1,
|
|
fs_name=fs_name,
|
|
fs_extension="",
|
|
fs_path=f"{platform.fs_slug}/roms",
|
|
platform=platform,
|
|
)
|
|
|
|
|
|
async def switch_family_extract(
|
|
platform_slug: str, file_path: str
|
|
) -> SigilExtractionResult:
|
|
"""Stand in for sigil over a base/update/DLC folder, keyed by file name."""
|
|
if "update" in file_path:
|
|
return SigilExtractionResult(
|
|
title_id="0100ABCD12340800",
|
|
save_target="0100ABCD12340800",
|
|
usage="folder-exact",
|
|
content_type="patch",
|
|
version=196608,
|
|
)
|
|
if "dlc" in file_path:
|
|
return SigilExtractionResult(
|
|
title_id="0100ABCD12341001",
|
|
save_target="0100ABCD12341001",
|
|
usage="folder-exact",
|
|
content_type="addon",
|
|
version=0,
|
|
)
|
|
return SigilExtractionResult(
|
|
title_id="0100ABCD12340000",
|
|
save_target="0100ABCD12340000",
|
|
usage="folder-exact",
|
|
content_type="application",
|
|
version=0,
|
|
)
|
|
|
|
|
|
class TestSigilTitleIdExtraction:
|
|
"""Title id extraction via the sigil adapter during get_rom_files."""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_single_file_extraction_attaches_values(
|
|
self, tmp_path: Path, sigil_config: Config
|
|
):
|
|
handler = make_sigil_handler(tmp_path)
|
|
rom = make_single_file_rom(tmp_path, SWITCH_PLATFORM, "Game.nsp")
|
|
|
|
extraction = SigilExtractionResult(
|
|
title_id="0100ABCD12340000",
|
|
save_target="0100ABCD12340000",
|
|
usage="folder-exact",
|
|
)
|
|
mock_extract = AsyncMock(return_value=extraction)
|
|
|
|
with patch(SIGIL_PATCH_TARGET, mock_extract):
|
|
parsed = await handler.get_rom_files(rom)
|
|
|
|
assert len(parsed.rom_files) == 1
|
|
assert parsed.identity.title_id == "0100ABCD12340000"
|
|
assert parsed.identity.save_target == "0100ABCD12340000"
|
|
assert parsed.identity.save_target_layout == SaveTargetLayout.FOLDER_EXACT
|
|
mock_extract.assert_awaited_once_with(
|
|
"switch",
|
|
str(tmp_path / "switch/roms/Game.nsp"),
|
|
)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_multi_part_extraction_covers_nested_files(
|
|
self, tmp_path: Path, sigil_config: Config
|
|
):
|
|
handler = make_sigil_handler(tmp_path)
|
|
rom = make_multi_part_rom(
|
|
tmp_path,
|
|
SWITCH_PLATFORM,
|
|
"Zelda",
|
|
[
|
|
"Zelda [base].nsp",
|
|
"updates/Zelda [update].nsp",
|
|
"dlc/Zelda [dlc].nsp",
|
|
],
|
|
)
|
|
|
|
mock_extract = AsyncMock(side_effect=switch_family_extract)
|
|
|
|
with patch(SIGIL_PATCH_TARGET, mock_extract):
|
|
parsed = await handler.get_rom_files(rom)
|
|
|
|
by_name = {rf.file_name: rf for rf in parsed.rom_files}
|
|
|
|
assert by_name["Zelda [base].nsp"].category == RomFileCategory.GAME
|
|
assert by_name["Zelda [update].nsp"].category == RomFileCategory.UPDATE
|
|
assert by_name["Zelda [dlc].nsp"].category == RomFileCategory.DLC
|
|
|
|
# All three files, including the nested update/DLC, are extracted.
|
|
assert mock_extract.await_count == 3
|
|
|
|
# The base game is still selected for the rom-level values.
|
|
assert parsed.identity.title_id == "0100ABCD12340000"
|
|
assert parsed.identity.save_target == "0100ABCD12340000"
|
|
assert parsed.identity.save_target_layout == SaveTargetLayout.FOLDER_EXACT
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_content_type_overrides_folder_category(
|
|
self, tmp_path: Path, sigil_config: Config
|
|
):
|
|
handler = make_sigil_handler(tmp_path)
|
|
# Folder name "dlc" would otherwise derive category DLC, but the binary
|
|
# content type says this is the base application.
|
|
rom = make_multi_part_rom(
|
|
tmp_path, SWITCH_PLATFORM, "Zelda", ["dlc/Zelda [base].nsp"]
|
|
)
|
|
|
|
mock_extract = AsyncMock(
|
|
return_value=SigilExtractionResult(
|
|
title_id="0100ABCD12340000",
|
|
save_target="0100ABCD12340000",
|
|
usage="folder-exact",
|
|
content_type="application",
|
|
version=0,
|
|
)
|
|
)
|
|
|
|
with patch(SIGIL_PATCH_TARGET, mock_extract):
|
|
parsed = await handler.get_rom_files(rom)
|
|
|
|
assert parsed.rom_files[0].category == RomFileCategory.GAME
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_folder_category_preserved_when_content_type_absent(
|
|
self, tmp_path: Path, sigil_config: Config
|
|
):
|
|
handler = make_sigil_handler(tmp_path)
|
|
rom = make_multi_part_rom(
|
|
tmp_path, SWITCH_PLATFORM, "Zelda", ["dlc/Zelda [addon].nsp"]
|
|
)
|
|
|
|
# CNMT miss: title id is present but content type is None.
|
|
mock_extract = AsyncMock(
|
|
return_value=SigilExtractionResult(
|
|
title_id="0100ABCD12341001",
|
|
save_target="0100ABCD12341001",
|
|
usage="folder-exact",
|
|
content_type=None,
|
|
version=None,
|
|
)
|
|
)
|
|
|
|
with patch(SIGIL_PATCH_TARGET, mock_extract):
|
|
parsed = await handler.get_rom_files(rom)
|
|
|
|
# The folder-derived category survives when the binary offers none.
|
|
assert parsed.rom_files[0].category == RomFileCategory.DLC
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_non_rom_files_are_not_read(
|
|
self, tmp_path: Path, sigil_config: Config
|
|
):
|
|
handler = make_sigil_handler(tmp_path)
|
|
rom = make_multi_part_rom(
|
|
tmp_path,
|
|
SWITCH_PLATFORM,
|
|
"Zelda",
|
|
[
|
|
"Zelda [base].nsp",
|
|
"manuals/Zelda.pdf",
|
|
"soundtracks/Zelda.mp3",
|
|
"screenshots/Zelda.png",
|
|
],
|
|
)
|
|
|
|
mock_extract = AsyncMock(side_effect=switch_family_extract)
|
|
|
|
with patch(SIGIL_PATCH_TARGET, mock_extract):
|
|
await handler.get_rom_files(rom)
|
|
|
|
# Only the ROM file itself is worth a native parse.
|
|
assert mock_extract.await_count == 1
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
("fs_name", "title_id", "expected"),
|
|
[
|
|
# A patch id carries bitmask 0x800.
|
|
("Zelda [update].nsp", "0100ABCD12340800", "0100ABCD12340000"),
|
|
# A DLC id carries an odd program-index nibble.
|
|
("Game [DLC].nsp", "0100ABCD12351064", "0100ABCD12350000"),
|
|
],
|
|
)
|
|
async def test_switch_base_id_is_derived_when_no_base_file_is_present(
|
|
self,
|
|
tmp_path: Path,
|
|
sigil_config: Config,
|
|
fs_name: str,
|
|
title_id: str,
|
|
expected: str,
|
|
):
|
|
handler = make_sigil_handler(tmp_path)
|
|
rom = make_single_file_rom(tmp_path, SWITCH_PLATFORM, fs_name)
|
|
|
|
mock_extract = AsyncMock(
|
|
return_value=SigilExtractionResult(
|
|
title_id=title_id,
|
|
save_target=title_id,
|
|
usage="folder-exact",
|
|
)
|
|
)
|
|
|
|
with patch(SIGIL_PATCH_TARGET, mock_extract):
|
|
parsed = await handler.get_rom_files(rom)
|
|
|
|
# Switch saves are keyed by the base title id itself.
|
|
assert parsed.identity.title_id == expected
|
|
assert parsed.identity.save_target == expected
|
|
assert parsed.identity.save_target_layout == SaveTargetLayout.FOLDER_EXACT
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
("platform", "fs_name", "extraction", "expected_layout"),
|
|
[
|
|
(
|
|
Platform(name="PlayStation Portable", slug="psp", fs_slug="psp"),
|
|
"Game.iso",
|
|
SigilExtractionResult(
|
|
title_id="ULUS-10041",
|
|
save_target="ULUS10041",
|
|
usage="folder-prefix",
|
|
),
|
|
SaveTargetLayout.FOLDER_PREFIX,
|
|
),
|
|
(
|
|
Platform(name="Nintendo 3DS", slug="3ds", fs_slug="3ds"),
|
|
"Game.3ds",
|
|
SigilExtractionResult(
|
|
title_id="0004000E0011C500",
|
|
# The split path in the emulator's case, not the flat id.
|
|
save_target="0004000e/0011c500",
|
|
usage="folder-split",
|
|
),
|
|
SaveTargetLayout.FOLDER_SPLIT,
|
|
),
|
|
(
|
|
Platform(name="Dreamcast", slug="dc", fs_slug="dc"),
|
|
"Game.chd",
|
|
SigilExtractionResult(
|
|
title_id="MK-51035",
|
|
save_target="MK-51035",
|
|
usage="file-prefix",
|
|
),
|
|
SaveTargetLayout.FILE_PREFIX,
|
|
),
|
|
],
|
|
)
|
|
async def test_non_switch_platform_uses_first_extraction_verbatim(
|
|
self,
|
|
tmp_path: Path,
|
|
sigil_config: Config,
|
|
stub_ra_hasher: None,
|
|
platform: Platform,
|
|
fs_name: str,
|
|
extraction: SigilExtractionResult,
|
|
expected_layout: SaveTargetLayout,
|
|
):
|
|
handler = make_sigil_handler(tmp_path)
|
|
rom = make_single_file_rom(tmp_path, platform, fs_name)
|
|
|
|
with patch(SIGIL_PATCH_TARGET, AsyncMock(return_value=extraction)):
|
|
parsed = await handler.get_rom_files(rom)
|
|
|
|
assert parsed.identity.title_id == extraction.title_id
|
|
assert parsed.identity.save_target == extraction.save_target
|
|
assert parsed.identity.save_target_layout == expected_layout
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
("platform", "fs_name", "calculate_hashes", "extract_title_ids"),
|
|
[
|
|
# Not a platform sigil covers.
|
|
(
|
|
Platform(name="Nintendo 64", slug="n64", fs_slug="n64"),
|
|
"Game.z64",
|
|
False,
|
|
True,
|
|
),
|
|
# Turned off by config.
|
|
(SWITCH_PLATFORM, "Game.nsp", True, False),
|
|
# An archive holds no ROM binary sigil can read.
|
|
(SWITCH_PLATFORM, "game.zip", True, True),
|
|
],
|
|
)
|
|
async def test_extraction_is_skipped(
|
|
self,
|
|
tmp_path: Path,
|
|
sigil_config: Config,
|
|
platform: Platform,
|
|
fs_name: str,
|
|
calculate_hashes: bool,
|
|
extract_title_ids: bool,
|
|
):
|
|
handler = make_sigil_handler(tmp_path)
|
|
rom = make_single_file_rom(tmp_path, platform, fs_name)
|
|
|
|
mock_extract = AsyncMock()
|
|
|
|
with patch(SIGIL_PATCH_TARGET, mock_extract):
|
|
parsed = await handler.get_rom_files(
|
|
rom,
|
|
calculate_hashes=calculate_hashes,
|
|
extract_title_ids=extract_title_ids,
|
|
)
|
|
|
|
mock_extract.assert_not_awaited()
|
|
assert parsed.identity.title_id is None
|
|
assert parsed.identity.save_target_layout is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_hashable_archive_rom_skips_extraction(
|
|
self, tmp_path: Path, sigil_config: Config, stub_ra_hasher: None
|
|
):
|
|
import io
|
|
import zipfile
|
|
|
|
reload_zipfile()
|
|
buf = io.BytesIO()
|
|
with zipfile.ZipFile(buf, "w") as zf:
|
|
zf.writestr("game.bin", b"fake PSX disc content")
|
|
|
|
platform = Platform(name="PlayStation", slug="psx", fs_slug="psx")
|
|
handler = make_sigil_handler(tmp_path)
|
|
roms_path = tmp_path / "psx" / "roms"
|
|
roms_path.mkdir(parents=True)
|
|
(roms_path / "game.zip").write_bytes(buf.getvalue())
|
|
rom = Rom(
|
|
id=1,
|
|
fs_name="game.zip",
|
|
fs_path="psx/roms",
|
|
platform=platform,
|
|
)
|
|
|
|
mock_extract = AsyncMock()
|
|
|
|
with patch(SIGIL_PATCH_TARGET, mock_extract):
|
|
parsed = await handler.get_rom_files(rom)
|
|
|
|
mock_extract.assert_not_awaited()
|
|
assert parsed.identity.title_id is None
|
|
|
|
|
|
class TestEmbedSwitchTitleIdInName:
|
|
"""The rename helper that embeds a Switch title id into a filename."""
|
|
|
|
@pytest.fixture
|
|
def handler(self, tmp_path: Path) -> FSRomsHandler:
|
|
(tmp_path / "switch/roms").mkdir(parents=True)
|
|
return make_sigil_handler(tmp_path)
|
|
|
|
@staticmethod
|
|
def _write(handler: FSRomsHandler, name: str, content: bytes = b"rom") -> Path:
|
|
path = handler.base_path / "switch/roms" / name
|
|
path.write_bytes(content)
|
|
return path
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
("name", "title_id", "version", "expected"),
|
|
[
|
|
(
|
|
"Moonlighter.xci",
|
|
"0100f4700b2e0000",
|
|
3,
|
|
"Moonlighter [0100F4700B2E0000][v3].xci",
|
|
),
|
|
# A missing version is a base game's version 0.
|
|
(
|
|
"Game.nsp",
|
|
"0100ABCD12340000",
|
|
None,
|
|
"Game [0100ABCD12340000][v0].nsp",
|
|
),
|
|
(
|
|
"Game (USA) (Rev 1).xci",
|
|
"0100ABCD12340000",
|
|
0,
|
|
"Game (USA) (Rev 1) [0100ABCD12340000][v0].xci",
|
|
),
|
|
],
|
|
)
|
|
async def test_embeds_id_and_version(
|
|
self,
|
|
handler: FSRomsHandler,
|
|
name: str,
|
|
title_id: str,
|
|
version: int | None,
|
|
expected: str,
|
|
):
|
|
src = self._write(handler, name)
|
|
|
|
new_name = await handler._embed_switch_title_id_in_name(
|
|
Path("switch/roms", name), title_id, version
|
|
)
|
|
|
|
assert new_name == expected
|
|
assert (handler.base_path / "switch/roms" / expected).exists()
|
|
assert not src.exists()
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
("name", "title_id"),
|
|
[
|
|
# Already embedded, so renaming again would stack the tags.
|
|
("Game [0100ABCD12340000][v0].nsp", "0100ABCD12340000"),
|
|
("Game.nsp", "NOT-A-TITLE-ID"),
|
|
],
|
|
)
|
|
async def test_file_is_left_alone(
|
|
self, handler: FSRomsHandler, name: str, title_id: str
|
|
):
|
|
src = self._write(handler, name)
|
|
|
|
assert (
|
|
await handler._embed_switch_title_id_in_name(
|
|
Path("switch/roms", name), title_id, 0
|
|
)
|
|
is None
|
|
)
|
|
assert src.exists()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_collision_skips_and_leaves_both_files(self, handler: FSRomsHandler):
|
|
src = self._write(handler, "Game.nsp", b"original")
|
|
target = self._write(handler, "Game [0100ABCD12340000][v0].nsp", b"existing")
|
|
|
|
assert (
|
|
await handler._embed_switch_title_id_in_name(
|
|
Path("switch/roms/Game.nsp"), "0100ABCD12340000", 0
|
|
)
|
|
is None
|
|
)
|
|
assert src.read_bytes() == b"original"
|
|
assert target.read_bytes() == b"existing"
|
|
|
|
|
|
class TestSwitchTitleIdEmbedding:
|
|
"""`embed_switch_title_ids`, the rename step the scan runs after parsing."""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_parsing_alone_leaves_the_file_unchanged(
|
|
self, tmp_path: Path, sigil_config: Config
|
|
):
|
|
handler = make_sigil_handler(tmp_path)
|
|
rom = make_single_file_rom(tmp_path, SWITCH_PLATFORM, "Game.nsp")
|
|
|
|
extraction = SigilExtractionResult(
|
|
title_id="0100ABCD12340000",
|
|
save_target="0100ABCD12340000",
|
|
usage="folder-exact",
|
|
)
|
|
|
|
with patch(SIGIL_PATCH_TARGET, AsyncMock(return_value=extraction)):
|
|
parsed = await handler.get_rom_files(rom)
|
|
|
|
assert parsed.rom_files[0].file_name == "Game.nsp"
|
|
assert (tmp_path / "switch/roms/Game.nsp").exists()
|
|
# The rename is offered to the caller, not performed.
|
|
assert [c.extraction.title_id for c in parsed.embed_candidates] == [
|
|
"0100ABCD12340000"
|
|
]
|
|
assert parsed.embed_candidates[0].is_rom_level
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_single_file_renamed_and_reconciled(
|
|
self, tmp_path: Path, sigil_config: Config
|
|
):
|
|
handler = make_sigil_handler(tmp_path)
|
|
rom = make_single_file_rom(tmp_path, SWITCH_PLATFORM, "Moonlighter.xci")
|
|
|
|
extraction = SigilExtractionResult(
|
|
title_id="0100F4700B2E0000",
|
|
save_target="0100F4700B2E0000",
|
|
usage="folder-exact",
|
|
version=0,
|
|
)
|
|
|
|
with patch(SIGIL_PATCH_TARGET, AsyncMock(return_value=extraction)):
|
|
parsed = await handler.get_rom_files(rom)
|
|
renamed_rom_fs_name = await handler.embed_switch_title_ids(parsed)
|
|
|
|
new_name = "Moonlighter [0100F4700B2E0000][v0].xci"
|
|
assert renamed_rom_fs_name == new_name
|
|
assert parsed.rom_files[0].file_name == new_name
|
|
assert parsed.rom_files[0].file_path == "switch/roms"
|
|
assert (tmp_path / "switch/roms" / new_name).exists()
|
|
assert not (tmp_path / "switch/roms/Moonlighter.xci").exists()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_non_switch_platform_offers_no_candidates(
|
|
self, tmp_path: Path, sigil_config: Config, stub_ra_hasher: None
|
|
):
|
|
platform = Platform(name="PlayStation Portable", slug="psp", fs_slug="psp")
|
|
handler = make_sigil_handler(tmp_path)
|
|
rom = make_single_file_rom(tmp_path, platform, "Game.iso")
|
|
|
|
extraction = SigilExtractionResult(
|
|
title_id="0100ABCD12340000",
|
|
save_target="0100ABCD12340000",
|
|
usage="folder-exact",
|
|
)
|
|
|
|
with patch(SIGIL_PATCH_TARGET, AsyncMock(return_value=extraction)):
|
|
parsed = await handler.get_rom_files(rom)
|
|
|
|
assert parsed.embed_candidates == []
|
|
assert await handler.embed_switch_title_ids(parsed) is None
|
|
assert (tmp_path / "psp/roms/Game.iso").exists()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_no_title_id_not_renamed(self, tmp_path: Path, sigil_config: Config):
|
|
handler = make_sigil_handler(tmp_path)
|
|
rom = make_single_file_rom(tmp_path, SWITCH_PLATFORM, "Game.nsp")
|
|
|
|
with patch(SIGIL_PATCH_TARGET, AsyncMock(return_value=None)):
|
|
parsed = await handler.get_rom_files(rom)
|
|
|
|
assert parsed.embed_candidates == []
|
|
assert await handler.embed_switch_title_ids(parsed) is None
|
|
assert parsed.rom_files[0].file_name == "Game.nsp"
|
|
assert (tmp_path / "switch/roms/Game.nsp").exists()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_multi_part_nested_files_renamed(
|
|
self, tmp_path: Path, sigil_config: Config
|
|
):
|
|
handler = make_sigil_handler(tmp_path)
|
|
rom = make_multi_part_rom(
|
|
tmp_path,
|
|
SWITCH_PLATFORM,
|
|
"Zelda",
|
|
["Zelda base.nsp", "updates/Zelda update.nsp", "dlc/Zelda dlc.nsp"],
|
|
)
|
|
|
|
with patch(SIGIL_PATCH_TARGET, AsyncMock(side_effect=switch_family_extract)):
|
|
parsed = await handler.get_rom_files(rom)
|
|
renamed_rom_fs_name = await handler.embed_switch_title_ids(parsed)
|
|
|
|
names = {rf.file_name for rf in parsed.rom_files}
|
|
assert "Zelda base [0100ABCD12340000][v0].nsp" in names
|
|
assert "Zelda update [0100ABCD12340800][v196608].nsp" in names
|
|
assert "Zelda dlc [0100ABCD12341001][v0].nsp" in names
|
|
# A multi-part rom's folder name is not changed by inner-file renames.
|
|
assert renamed_rom_fs_name is None
|
|
rom_dir = tmp_path / "switch/roms/Zelda"
|
|
assert (rom_dir / "Zelda base [0100ABCD12340000][v0].nsp").exists()
|
|
assert (
|
|
rom_dir / "updates/Zelda update [0100ABCD12340800][v196608].nsp"
|
|
).exists()
|
|
|
|
|
|
class TestExtractCHDHash:
|
|
"""Test suite for extract_chd_hash function"""
|
|
|
|
def test_extract_chd_hash_v5_valid(self, tmp_path):
|
|
"""Test extracting hash from a valid CHD v5 file"""
|
|
chd_file = tmp_path / "test_v5.chd"
|
|
|
|
# CHD v5 header structure (124 bytes minimum):
|
|
# Bytes 0-7: "MComprHD" magic signature
|
|
# Bytes 12-15: Version (5 in big-endian)
|
|
# Bytes 84-103: SHA1 hash (20 bytes)
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(5).to_bytes(4, "big")
|
|
# Use a test SHA1 hash
|
|
header[84:104] = bytes.fromhex("0123456789abcdef0123456789abcdef01234567")
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result
|
|
assert isinstance(result, str)
|
|
assert len(result) == 40 # SHA1 hex is 40 characters
|
|
assert result == "0123456789abcdef0123456789abcdef01234567"
|
|
|
|
def test_extract_chd_hash_v1_rejected(self, tmp_path):
|
|
"""Test that CHD v1 files are rejected"""
|
|
chd_file = tmp_path / "test_v1.chd"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(1).to_bytes(4, "big") # Version 1
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result == ""
|
|
|
|
def test_extract_chd_hash_v2_rejected(self, tmp_path):
|
|
"""Test that CHD v2 files are rejected"""
|
|
chd_file = tmp_path / "test_v2.chd"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(2).to_bytes(4, "big") # Version 2
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result == ""
|
|
|
|
def test_extract_chd_hash_v3_rejected(self, tmp_path):
|
|
"""Test that CHD v3 files are rejected"""
|
|
chd_file = tmp_path / "test_v3.chd"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(3).to_bytes(4, "big") # Version 3
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result == ""
|
|
|
|
def test_extract_chd_hash_v4_rejected(self, tmp_path):
|
|
"""Test that CHD v4 files are rejected"""
|
|
chd_file = tmp_path / "test_v4.chd"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(4).to_bytes(4, "big") # Version 4
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result == ""
|
|
|
|
def test_extract_chd_hash_invalid_magic(self, tmp_path):
|
|
"""Test that files without CHD magic signature are rejected"""
|
|
chd_file = tmp_path / "invalid_magic.bin"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"BadMagic" # Not "MComprHD"
|
|
header[12:16] = int(5).to_bytes(4, "big")
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result == ""
|
|
|
|
def test_extract_chd_hash_truncated_header(self, tmp_path):
|
|
"""Test that CHD v5 file with truncated header is rejected"""
|
|
chd_file = tmp_path / "truncated.chd"
|
|
|
|
# Only write 100 bytes instead of required 124
|
|
header = bytearray(100)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(5).to_bytes(4, "big")
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result == ""
|
|
|
|
def test_extract_chd_hash_nonexistent_file(self, tmp_path):
|
|
"""Test that non-existent files are handled gracefully"""
|
|
nonexistent = tmp_path / "does_not_exist.chd"
|
|
|
|
result = extract_chd_hash(nonexistent)
|
|
|
|
assert result == ""
|
|
|
|
def test_extract_chd_hash_empty_file(self, tmp_path):
|
|
"""Test that empty files are rejected"""
|
|
chd_file = tmp_path / "empty.chd"
|
|
chd_file.write_bytes(b"")
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result == ""
|
|
|
|
def test_extract_chd_hash_sha1_format(self, tmp_path):
|
|
"""Test that SHA1 hash is correctly formatted as hex"""
|
|
chd_file = tmp_path / "test_format.chd"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(5).to_bytes(4, "big")
|
|
|
|
# Use a known SHA1 value
|
|
test_sha1 = bytes.fromhex("356a192b7913b04c54574d18c28d46e6395428ab")
|
|
header[84:104] = test_sha1
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result == "356a192b7913b04c54574d18c28d46e6395428ab"
|
|
# Verify it's lowercase hex
|
|
assert result == result.lower()
|
|
# Verify it's 40 characters (SHA1 is 20 bytes = 40 hex chars)
|
|
assert len(result) == 40
|
|
|
|
def test_extract_chd_hash_unknown_version(self, tmp_path):
|
|
"""Test that unknown CHD versions are rejected"""
|
|
chd_file = tmp_path / "test_unknown.chd"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(99).to_bytes(4, "big") # Unknown version
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result == ""
|
|
|
|
def test_extract_chd_hash_multiple_different_hashes(self, tmp_path):
|
|
"""Test that different SHA1 hashes are correctly extracted"""
|
|
test_cases = [
|
|
"0000000000000000000000000000000000000000",
|
|
"ffffffffffffffffffffffffffffffffffffffff",
|
|
"356a192b7913b04c54574d18c28d46e6395428ab",
|
|
"da39a3ee5e6b4b0d3255bfef95601890afd80709",
|
|
]
|
|
|
|
for i, test_hash in enumerate(test_cases):
|
|
chd_file = tmp_path / f"test_hash_{i}.chd"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(5).to_bytes(4, "big")
|
|
header[84:104] = bytes.fromhex(test_hash)
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result == test_hash, f"Hash mismatch for test case {i}"
|
|
|
|
def test_extract_chd_hash_version_boundary_cases(self, tmp_path):
|
|
"""Test version checking at boundaries (0, 1, 4, 5, 6)"""
|
|
test_versions = [
|
|
(0, ""), # Version 0 should return ""
|
|
(1, ""), # Version 1 should return ""
|
|
(4, ""), # Version 4 should return ""
|
|
(5, "0123456789abcdef0123456789abcdef01234567"), # Version 5 should work
|
|
(6, ""), # Version 6 should return ""
|
|
]
|
|
|
|
for version, expected in test_versions:
|
|
chd_file = tmp_path / f"test_v{version}.chd"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(version).to_bytes(4, "big")
|
|
header[84:104] = bytes.fromhex("0123456789abcdef0123456789abcdef01234567")
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result == expected, f"Version {version} should return {expected!r}"
|
|
|
|
def test_extract_chd_hash_file_too_short_for_magic(self, tmp_path):
|
|
"""Test file that's too short to even contain magic + version"""
|
|
chd_file = tmp_path / "too_short.chd"
|
|
|
|
# Only 8 bytes - has magic but no version
|
|
header = bytearray(8)
|
|
header[0:8] = b"MComprHD"
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result == ""
|
|
|
|
def test_extract_chd_hash_permission_error(self, tmp_path):
|
|
"""Test graceful handling of permission errors"""
|
|
chd_file = tmp_path / "no_read_permission.chd"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(5).to_bytes(4, "big")
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
# Remove read permissions
|
|
chd_file.chmod(0o000)
|
|
|
|
try:
|
|
result = extract_chd_hash(chd_file)
|
|
assert result == ""
|
|
finally:
|
|
# Restore permissions for cleanup
|
|
chd_file.chmod(0o644)
|
|
|
|
def test_extract_chd_hash_real_header(self, tmp_path):
|
|
"""Test extracting hash from real Pebble Beach Golf Links CHD v5 header
|
|
|
|
This uses the actual 128-byte header from:
|
|
Pebble Beach Golf Links - Stadler ni Chousen (Japan).chd
|
|
|
|
Header bytes (hex):
|
|
00000000: 4d43 6f6d 7072 4844 0000 007c 0000 0005 MComprHD...|....
|
|
00000010: 6364 6c7a 6364 7a6c 6364 666c 0000 0000 cdlzcdzlcdfl....
|
|
00000020: 0000 0000 1a97 4e00 0000 0000 1119 b3d0 ......N.........
|
|
00000030: 0000 0000 0000 007c 0000 4c80 0000 0990 .......|..L.....
|
|
00000040: 8389 486c 34df 316d 1fd3 3997 a3ef ce8c ..Hl4.1m..9.....
|
|
00000050: e9c9 6008 0167 fc76 f9e4 312e 6ab4 8fe9 ..`..g.v..1.j...
|
|
00000060: 80d2 ce5b 23f7 75c2 0000 0000 0000 0000 ...[#.u.........
|
|
00000070: 0000 0000 0000 0000 0000 0000 4348 5432 ............CHT2
|
|
|
|
The SHA1 hash (combined raw+meta) at bytes 84-103 is:
|
|
0167 fc76 f9e4 312e 6ab4 8fe9 80d2 ce5b 23f7 75c2
|
|
"""
|
|
chd_file = tmp_path / "Pebble Beach.chd"
|
|
|
|
# Real 128-byte header from the file
|
|
real_header = bytes.fromhex(
|
|
"4d43 6f6d 7072 4844 0000 007c 0000 0005 "
|
|
"6364 6c7a 6364 7a6c 6364 666c 0000 0000 "
|
|
"0000 0000 1a97 4e00 0000 0000 1119 b3d0 "
|
|
"0000 0000 0000 007c 0000 4c80 0000 0990 "
|
|
"8389 486c 34df 316d 1fd3 3997 a3ef ce8c "
|
|
"e9c9 6008 0167 fc76 f9e4 312e 6ab4 8fe9 "
|
|
"80d2 ce5b 23f7 75c2 0000 0000 0000 0000 "
|
|
"0000 0000 0000 0000 0000 0000 4348 5432"
|
|
)
|
|
|
|
chd_file.write_bytes(real_header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
# Expected SHA1 from the header at bytes 84-103 (20 bytes, as per chd.h)
|
|
expected_sha1 = "0167fc76f9e4312e6ab48fe980d2ce5b23f775c2"
|
|
|
|
assert result
|
|
assert result == expected_sha1
|
|
assert len(result) == 40
|
|
# Verify it matches what's in the header
|
|
assert bytes.fromhex(result) == real_header[84:104]
|
|
|
|
def test_extract_chd_hash_with_extra_metadata(self, tmp_path):
|
|
"""Test CHD v5 file with additional metadata beyond header
|
|
|
|
Real CHD files often have map data and metadata after the 124-byte header.
|
|
The hash extraction should work correctly regardless of file size.
|
|
"""
|
|
chd_file = tmp_path / "test_with_metadata.chd"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(5).to_bytes(4, "big")
|
|
test_sha1 = bytes.fromhex("0167fc76f9e4312e6ab48fe980d2ce5b23f775c2")
|
|
header[84:104] = test_sha1
|
|
|
|
# Write header plus extra data (simulating map and metadata)
|
|
extra_data = b"MAP_DATACOMPRESSED_DATA_GOES_HERE" * 100
|
|
|
|
chd_file.write_bytes(header + extra_data)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result
|
|
assert result == "0167fc76f9e4312e6ab48fe980d2ce5b23f775c2"
|
|
assert bytes.fromhex(result) == test_sha1
|
|
|
|
def test_extract_chd_hash_off_by_one_header_sizes(self, tmp_path):
|
|
"""Test boundary conditions around minimum required header size (104 bytes)"""
|
|
test_cases = [
|
|
(103, ""), # 103 bytes - not enough for SHA1 region
|
|
(
|
|
104,
|
|
"0167fc76f9e4312e6ab48fe980d2ce5b23f775c2",
|
|
), # 104 bytes - exactly enough
|
|
(123, "0167fc76f9e4312e6ab48fe980d2ce5b23f775c2"), # 123 bytes
|
|
(124, "0167fc76f9e4312e6ab48fe980d2ce5b23f775c2"), # Full v5 header
|
|
(125, "0167fc76f9e4312e6ab48fe980d2ce5b23f775c2"), # Extra byte
|
|
]
|
|
|
|
for size, expected in test_cases:
|
|
chd_file = tmp_path / f"test_size_{size}.chd"
|
|
|
|
header = bytearray(size)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(5).to_bytes(4, "big")
|
|
if size >= 104:
|
|
header[84:104] = bytes.fromhex(
|
|
"0167fc76f9e4312e6ab48fe980d2ce5b23f775c2"
|
|
)
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert (
|
|
result == expected
|
|
), f"Failed for size {size}: got {result}, expected {expected}"
|
|
|
|
def test_extract_chd_hash_corrupted_header_data(self, tmp_path):
|
|
"""Test handling of corrupted/invalid data in header fields"""
|
|
chd_file = tmp_path / "corrupted_header.chd"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
# Corrupt the version field with invalid bytes
|
|
header[12:16] = b"\xff\xff\xff\xff" # This will be read as 4294967295
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
# Should return empty string because version is not 5
|
|
assert result == ""
|
|
|
|
def test_extract_chd_hash_zero_sha1(self, tmp_path):
|
|
"""Test handling of all-zero SHA1 hash (edge case but valid)"""
|
|
chd_file = tmp_path / "zero_hash.chd"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(5).to_bytes(4, "big")
|
|
# All-zero hash
|
|
header[84:104] = b"\x00" * 20
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result
|
|
assert result == "0" * 40
|
|
assert len(result) == 40
|
|
|
|
def test_extract_chd_hash_max_sha1(self, tmp_path):
|
|
"""Test handling of maximum SHA1 hash (all 0xFF - edge case but valid)"""
|
|
chd_file = tmp_path / "max_hash.chd"
|
|
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = int(5).to_bytes(4, "big")
|
|
# All-FF hash
|
|
header[84:104] = b"\xff" * 20
|
|
|
|
chd_file.write_bytes(header)
|
|
|
|
result = extract_chd_hash(chd_file)
|
|
|
|
assert result
|
|
assert result == "f" * 40
|
|
assert len(result) == 40
|
|
|
|
|
|
KNOWN_REGION_NAMES = frozenset(REGIONS_BY_SHORTCODE.values())
|
|
KNOWN_LANGUAGE_NAMES = frozenset(LANGUAGES_BY_SHORTCODE.values())
|
|
|
|
region_code = st.sampled_from(sorted(REGIONS_BY_SHORTCODE))
|
|
language_code = st.sampled_from(sorted(LANGUAGES_BY_SHORTCODE))
|
|
|
|
|
|
class TestParseTagsProperties:
|
|
"""Property-based tests for FSRomsHandler.parse_tags."""
|
|
|
|
handler = FSRomsHandler()
|
|
|
|
@given(st.text())
|
|
def test_never_raises_on_arbitrary_input(self, fs_name: str):
|
|
self.handler.parse_tags(fs_name)
|
|
|
|
@given(st.text())
|
|
def test_is_deterministic(self, fs_name: str):
|
|
assert self.handler.parse_tags(fs_name) == self.handler.parse_tags(fs_name)
|
|
|
|
@given(st.lists(region_code), st.lists(language_code))
|
|
def test_known_codes_map_to_known_names(self, regions, languages):
|
|
fs_name = "Game"
|
|
for code in regions:
|
|
fs_name += f"({code})"
|
|
for code in languages:
|
|
fs_name += f"({code})"
|
|
fs_name += ".rom"
|
|
|
|
parsed = self.handler.parse_tags(fs_name)
|
|
|
|
assert set(parsed.regions) <= KNOWN_REGION_NAMES
|
|
assert set(parsed.languages) <= KNOWN_LANGUAGE_NAMES
|
|
# Every supplied code resolves to its mapped full name.
|
|
assert {REGIONS_BY_SHORTCODE[c] for c in regions} <= set(parsed.regions)
|
|
assert {LANGUAGES_BY_SHORTCODE[c] for c in languages} <= set(parsed.languages)
|
|
|
|
|
|
def _chd_header_bytes(version: int, sha1: bytes) -> bytes:
|
|
"""Build a 124-byte CHD header with the given version and combined SHA1."""
|
|
header = bytearray(124)
|
|
header[0:8] = b"MComprHD"
|
|
header[12:16] = version.to_bytes(4, "big")
|
|
header[84:104] = sha1
|
|
return bytes(header)
|
|
|
|
|
|
def _run_extract(data: bytes) -> str:
|
|
fd, path = tempfile.mkstemp(suffix=".chd")
|
|
try:
|
|
with os.fdopen(fd, "wb") as f:
|
|
f.write(data)
|
|
return extract_chd_hash(Path(path))
|
|
finally:
|
|
os.unlink(path)
|
|
|
|
|
|
class TestExtractCHDHashProperties:
|
|
"""Property-based tests for extract_chd_hash."""
|
|
|
|
@given(sha1=st.binary(min_size=20, max_size=20))
|
|
def test_valid_v5_returns_embedded_sha1(self, sha1: bytes):
|
|
result = _run_extract(_chd_header_bytes(5, sha1))
|
|
assert result == sha1.hex()
|
|
assert len(result) == 40
|
|
|
|
@given(
|
|
version=st.integers(min_value=0, max_value=2**32 - 1),
|
|
sha1=st.binary(min_size=20, max_size=20),
|
|
)
|
|
def test_non_v5_version_returns_empty(self, version: int, sha1: bytes):
|
|
assume(version != 5)
|
|
assert _run_extract(_chd_header_bytes(version, sha1)) == ""
|
|
|
|
@given(data=st.binary(min_size=16))
|
|
def test_wrong_signature_returns_empty(self, data: bytes):
|
|
assume(data[:8] != b"MComprHD")
|
|
assert _run_extract(data) == ""
|
|
|
|
@given(data=st.binary(max_size=15))
|
|
def test_too_short_for_signature_returns_empty(self, data: bytes):
|
|
assert _run_extract(data) == ""
|
|
|
|
@given(
|
|
sha1=st.binary(min_size=20, max_size=20),
|
|
truncate_len=st.integers(min_value=16, max_value=103),
|
|
)
|
|
def test_truncated_before_sha1_returns_empty(self, sha1: bytes, truncate_len: int):
|
|
truncated = _chd_header_bytes(5, sha1)[:truncate_len]
|
|
assert _run_extract(truncated) == ""
|
|
|
|
@given(data=st.binary())
|
|
def test_never_raises_on_arbitrary_bytes(self, data: bytes):
|
|
_run_extract(data)
|
|
|
|
|
|
class TestIncrementalRomFiles:
|
|
"""`existing_files` lets a rescan keep the hashes of files whose size and
|
|
mtime are unchanged, and only recompute the ROM-level hashes when a
|
|
top-level file changed."""
|
|
|
|
ROM_DIR = "n64/roms/Sonic"
|
|
|
|
@pytest.fixture
|
|
def platform(self):
|
|
return Platform(name="Nintendo 64", slug="n64", fs_slug="n64")
|
|
|
|
@pytest.fixture
|
|
def handler(self, tmp_path: Path):
|
|
handler = FSRomsHandler()
|
|
handler.base_path = tmp_path
|
|
return handler
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def patched(self, mocker):
|
|
config = Config(
|
|
EXCLUDED_PLATFORMS=[],
|
|
EXCLUDED_SINGLE_EXT=[],
|
|
EXCLUDED_SINGLE_FILES=[],
|
|
EXCLUDED_MULTI_FILES=[],
|
|
EXCLUDED_MULTI_PARTS_EXT=["tmp"],
|
|
EXCLUDED_MULTI_PARTS_FILES=[],
|
|
PLATFORMS_BINDING={},
|
|
PLATFORMS_VERSIONS={},
|
|
STRUCTURE_TEMPLATES={
|
|
"default": "{platform}/roms/{game}",
|
|
"firmware": "{platform}/bios",
|
|
},
|
|
)
|
|
mocker.patch(
|
|
"handler.filesystem.roms_handler.cm.get_config", return_value=config
|
|
)
|
|
ra = mocker.patch(
|
|
"adapters.services.rahasher.RAHasherService.calculate_hash",
|
|
AsyncMock(return_value="ra-hash"),
|
|
)
|
|
return SimpleNamespace(ra=ra)
|
|
|
|
def _rom(self, platform: Platform, fs_name: str = "Sonic", ext: str = "") -> Rom:
|
|
return Rom(
|
|
id=7,
|
|
fs_name=fs_name,
|
|
fs_path="n64/roms",
|
|
fs_extension=ext,
|
|
platform=platform,
|
|
crc_hash="stored-crc",
|
|
md5_hash="stored-md5",
|
|
sha1_hash="stored-sha1",
|
|
ra_hash="stored-ra",
|
|
)
|
|
|
|
def _write(self, handler: FSRomsHandler, rel: str, data: bytes) -> os.stat_result:
|
|
path = handler.base_path / rel
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(data)
|
|
return path.stat()
|
|
|
|
def _row(
|
|
self, rom: Rom, rel_dir: str, name: str, st: os.stat_result, hashed: bool = True
|
|
) -> RomFile:
|
|
return RomFile(
|
|
rom_id=rom.id,
|
|
file_name=name,
|
|
file_path=rel_dir,
|
|
file_size_bytes=st.st_size,
|
|
last_modified=st.st_mtime,
|
|
crc_hash="row-crc" if hashed else "",
|
|
md5_hash="row-md5" if hashed else "",
|
|
sha1_hash="row-sha1" if hashed else "",
|
|
)
|
|
|
|
def _folder_rom(self, handler: FSRomsHandler, platform: Platform):
|
|
rom = self._rom(platform)
|
|
game = self._write(handler, f"{self.ROM_DIR}/game.n64", b"game bytes")
|
|
hack = self._write(handler, f"{self.ROM_DIR}/hack/patched.n64", b"hack bytes")
|
|
rows = [
|
|
self._row(rom, self.ROM_DIR, "game.n64", game),
|
|
self._row(rom, f"{self.ROM_DIR}/hack", "patched.n64", hack),
|
|
]
|
|
return rom, rows
|
|
|
|
async def test_unchanged_rows_are_reused_without_hashing(
|
|
self, handler, platform, patched, mocker
|
|
):
|
|
rom, rows = self._folder_rom(handler, platform)
|
|
hasher = mocker.spy(handler, "_calculate_rom_hashes")
|
|
|
|
parsed = await handler.get_rom_files(rom, existing_files=rows)
|
|
|
|
assert {id(f) for f in parsed.rom_files} == {id(r) for r in rows}
|
|
assert parsed.top_level_changed is False
|
|
assert (parsed.crc_hash, parsed.md5_hash, parsed.sha1_hash, parsed.ra_hash) == (
|
|
"stored-crc",
|
|
"stored-md5",
|
|
"stored-sha1",
|
|
"stored-ra",
|
|
)
|
|
hasher.assert_not_called()
|
|
patched.ra.assert_not_called()
|
|
|
|
async def test_new_nested_file_is_hashed_alone(
|
|
self, handler, platform, patched, mocker
|
|
):
|
|
rom, rows = self._folder_rom(handler, platform)
|
|
self._write(handler, f"{self.ROM_DIR}/hack/v2/new.ips", b"new patch")
|
|
hasher = mocker.spy(handler, "_calculate_rom_hashes")
|
|
|
|
parsed = await handler.get_rom_files(rom, existing_files=rows)
|
|
|
|
new = next(f for f in parsed.rom_files if f.file_name == "new.ips")
|
|
assert new.file_path == f"{self.ROM_DIR}/hack/v2"
|
|
assert new.category == RomFileCategory.HACK
|
|
assert (
|
|
new.md5_hash == hashlib.md5(b"new patch", usedforsecurity=False).hexdigest()
|
|
)
|
|
assert all(any(f is row for f in parsed.rom_files) for row in rows)
|
|
assert parsed.top_level_changed is False
|
|
assert parsed.md5_hash == "stored-md5"
|
|
assert hasher.call_count == 1
|
|
patched.ra.assert_not_called()
|
|
|
|
async def test_top_level_addition_recomputes_rom_hashes(
|
|
self, handler, platform, patched, mocker
|
|
):
|
|
rom, rows = self._folder_rom(handler, platform)
|
|
self._write(handler, f"{self.ROM_DIR}/extra.n64", b"extra bytes")
|
|
hasher = mocker.spy(handler, "_calculate_rom_hashes")
|
|
|
|
parsed = await handler.get_rom_files(rom, existing_files=rows)
|
|
|
|
top_level = [f for f in parsed.rom_files if f.file_path == self.ROM_DIR]
|
|
assert {f.file_name for f in top_level} == {"game.n64", "extra.n64"}
|
|
expected_md5 = hashlib.md5(usedforsecurity=False)
|
|
for rom_file in top_level:
|
|
expected_md5.update(
|
|
(
|
|
handler.base_path / rom_file.file_path / rom_file.file_name
|
|
).read_bytes()
|
|
)
|
|
assert parsed.top_level_changed is True
|
|
assert parsed.md5_hash == expected_md5.hexdigest()
|
|
assert parsed.ra_hash == "ra-hash"
|
|
patched.ra.assert_awaited_once()
|
|
# The nested file is untouched, so its row is still reused.
|
|
assert any(f is rows[1] for f in parsed.rom_files)
|
|
assert hasher.call_count == 2
|
|
|
|
async def test_removed_top_level_file_recomputes_rom_hashes(
|
|
self, handler, platform, patched
|
|
):
|
|
rom, rows = self._folder_rom(handler, platform)
|
|
gone = self._write(handler, f"{self.ROM_DIR}/gone.n64", b"gone")
|
|
rows.append(self._row(rom, self.ROM_DIR, "gone.n64", gone))
|
|
(handler.base_path / self.ROM_DIR / "gone.n64").unlink()
|
|
|
|
parsed = await handler.get_rom_files(rom, existing_files=rows)
|
|
|
|
assert parsed.top_level_changed is True
|
|
assert {f.file_name for f in parsed.rom_files} == {"game.n64", "patched.n64"}
|
|
assert (
|
|
parsed.md5_hash
|
|
== hashlib.md5(b"game bytes", usedforsecurity=False).hexdigest()
|
|
)
|
|
|
|
@pytest.mark.parametrize("column", ["file_size_bytes", "last_modified"])
|
|
async def test_changed_file_is_rehashed(self, handler, platform, column):
|
|
rom, rows = self._folder_rom(handler, platform)
|
|
setattr(rows[1], column, getattr(rows[1], column) + 1)
|
|
|
|
parsed = await handler.get_rom_files(rom, existing_files=rows)
|
|
|
|
hack = next(f for f in parsed.rom_files if f.file_name == "patched.n64")
|
|
assert hack is not rows[1]
|
|
assert (
|
|
hack.md5_hash
|
|
== hashlib.md5(b"hack bytes", usedforsecurity=False).hexdigest()
|
|
)
|
|
assert parsed.top_level_changed is False
|
|
|
|
async def test_row_without_hashes_is_rehashed(self, handler, platform):
|
|
rom, rows = self._folder_rom(handler, platform)
|
|
rows[1] = self._row(
|
|
rom,
|
|
f"{self.ROM_DIR}/hack",
|
|
"patched.n64",
|
|
(handler.base_path / self.ROM_DIR / "hack/patched.n64").stat(),
|
|
hashed=False,
|
|
)
|
|
|
|
parsed = await handler.get_rom_files(rom, existing_files=rows)
|
|
|
|
hack = next(f for f in parsed.rom_files if f.file_name == "patched.n64")
|
|
assert hack is not rows[1]
|
|
assert (
|
|
hack.md5_hash
|
|
== hashlib.md5(b"hack bytes", usedforsecurity=False).hexdigest()
|
|
)
|
|
|
|
async def test_row_without_hashes_is_reused_when_hashing_is_disabled(
|
|
self, handler, platform, mocker
|
|
):
|
|
rom, rows = self._folder_rom(handler, platform)
|
|
rows = [
|
|
self._row(
|
|
rom,
|
|
row.file_path,
|
|
row.file_name,
|
|
os.stat(handler.base_path / row.file_path / row.file_name),
|
|
hashed=False,
|
|
)
|
|
for row in rows
|
|
]
|
|
hasher = mocker.spy(handler, "_calculate_rom_hashes")
|
|
|
|
parsed = await handler.get_rom_files(
|
|
rom, calculate_hashes=False, existing_files=rows
|
|
)
|
|
|
|
assert {id(f) for f in parsed.rom_files} == {id(r) for r in rows}
|
|
hasher.assert_not_called()
|
|
|
|
async def test_flat_rom_reuses_its_row(self, handler, platform, patched, mocker):
|
|
rom = self._rom(platform, fs_name="Paper Mario (USA).z64", ext="z64")
|
|
st = self._write(handler, "n64/roms/Paper Mario (USA).z64", b"paper mario")
|
|
row = self._row(rom, "n64/roms", "Paper Mario (USA).z64", st)
|
|
hasher = mocker.spy(handler, "_calculate_rom_hashes")
|
|
|
|
parsed = await handler.get_rom_files(rom, existing_files=[row])
|
|
|
|
assert parsed.rom_files[0] is row
|
|
assert parsed.top_level_changed is False
|
|
assert parsed.md5_hash == "stored-md5"
|
|
hasher.assert_not_called()
|
|
patched.ra.assert_not_called()
|
|
|
|
async def test_replaced_flat_rom_is_rehashed(self, handler, platform):
|
|
rom = self._rom(platform, fs_name="Paper Mario (USA).z64", ext="z64")
|
|
st = self._write(handler, "n64/roms/Paper Mario (USA).z64", b"paper mario")
|
|
row = self._row(rom, "n64/roms", "Paper Mario (USA).z64", st)
|
|
row.file_size_bytes += 1
|
|
|
|
parsed = await handler.get_rom_files(rom, existing_files=[row])
|
|
|
|
assert parsed.rom_files[0] is not row
|
|
assert parsed.top_level_changed is True
|
|
assert (
|
|
parsed.md5_hash
|
|
== hashlib.md5(b"paper mario", usedforsecurity=False).hexdigest()
|
|
)
|
|
|
|
async def test_nested_folders_map_to_categories(self, handler, platform):
|
|
rom = self._rom(platform)
|
|
self._write(handler, f"{self.ROM_DIR}/game.n64", b"game")
|
|
self._write(handler, f"{self.ROM_DIR}/hack/part1 (Hack).bin", b"hack")
|
|
self._write(handler, f"{self.ROM_DIR}/patches/v2/fix.ips", b"patch")
|
|
|
|
parsed = await handler.get_rom_files(rom)
|
|
|
|
by_name = {f.file_name: f for f in parsed.rom_files}
|
|
assert by_name["game.n64"].category is None
|
|
assert by_name["part1 (Hack).bin"].category == RomFileCategory.HACK
|
|
assert by_name["fix.ips"].category == RomFileCategory.PATCH
|
|
|
|
def test_category_matches_plural_forms(self):
|
|
assert category_matches("patch", ["patches"])
|
|
assert category_matches("hack", ["hacks"])
|
|
assert category_matches("cheat", ["n64", "roms", "game", "cheats"])
|
|
assert not category_matches("patch", ["patched"])
|
|
|
|
def test_mtime_matches_accepts_single_precision_rounding(self):
|
|
actual = 1724500000.123456
|
|
rounded = struct.unpack("f", struct.pack("f", actual))[0]
|
|
|
|
assert rounded != actual
|
|
assert mtime_matches(actual, actual)
|
|
assert mtime_matches(rounded, actual)
|
|
assert not mtime_matches(actual + 1, actual)
|
|
assert not mtime_matches(None, actual)
|
|
|
|
def test_upload_temp_files_are_excluded_by_default(self):
|
|
assert "assembling" in DEFAULT_EXCLUDED_EXTENSIONS
|