Files
romm/backend/tests/models/test_rom.py
Spinnich 22c0216c14 fix(metadata): identify archives and folder ROMs by the right file
Since 4.9.0 a multi-file archive's RomFile carries a composite digest
streamed across every entry, with per-member digests kept alongside in
archive_members. Hasheous and Playmatch read their lookup digests
straight off that record, but both databases index a multi-file archive
by its largest internal member, so those ROMs stopped matching and fell
back to filename matching.

Add RomFile.lookup_hashes, which decides which digests a ROM database is
queried with: a CHD's embedded disc-data SHA-1, else the largest
archive member, else the file's own. Hasheous and Playmatch both read
it. ScreenScraper deliberately stays on the composite, since its
jeuInfos lookup falls back to romnom.

Playmatch also picked its file with the first one that had bytes,
unfiltered by is_top_level. iter_files is a bare os.walk with no sort
and Rom.files has no order_by, so a multi-disc folder ROM was identified
by an arbitrary file that could differ between machines scanning the
same library. Select the largest top-level file instead, as Hasheous and
ScreenScraper do, with a path tie-break for determinism. This half is
pre-existing rather than a 4.9.0 regression, but leaving it would keep
multi-disc folder ROMs failing on Playmatch.

The manual-match suggestion shares that selection, and no longer falls
back to the ROM-level composite: contributing nothing to a public index
beats contributing a hash of no single file.

Existing libraries need no re-hash; archive_members has been populated
since 4.9.0, so a rescan is enough.

Fixes #3742

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-07 13:09:02 +00:00

152 lines
4.3 KiB
Python

from models.rom import LookupHashes, Rom, RomFile
def test_rom(rom: Rom):
assert rom.fs_path == "test_platform_slug/roms"
assert rom.full_path == "test_platform_slug/roms/test_rom.zip"
def test_rom_with_libretro_match_is_identified(rom: Rom):
rom.libretro_id = "abc123"
assert rom.is_unidentified is False
assert rom.is_identified is True
def _archive(**kwargs) -> RomFile:
return RomFile(
file_name="sf2.zip",
file_path="arcade",
file_size_bytes=100,
crc_hash="compositecrc",
md5_hash="compositemd5",
sha1_hash="compositesha1",
**kwargs,
)
def test_lookup_hashes_uses_the_files_own_digests_by_default():
file = RomFile(
file_name="game.nes",
file_path="nes",
file_size_bytes=100,
crc_hash="crc",
md5_hash="md5",
sha1_hash="sha1",
)
assert file.lookup_hashes == LookupHashes(crc="crc", md5="md5", sha1="sha1")
def test_lookup_hashes_prefers_the_chd_disc_data_sha1():
"""A CHD's own digests cover the container, so only the embedded SHA1 is
worth sending."""
file = RomFile(
file_name="game.chd",
file_path="dc",
file_size_bytes=100,
crc_hash="containercrc",
md5_hash="containermd5",
sha1_hash="containersha1",
chd_sha1_hash="discsha1",
)
assert file.lookup_hashes == LookupHashes(crc=None, md5=None, sha1="discsha1")
def test_lookup_hashes_picks_the_largest_archive_member():
"""ROM databases index a multi-file archive by the ROM inside it, not by
the composite hash RomM stores for the archive as a whole."""
file = _archive(
archive_members=[
{
"name": "readme.txt",
"size": 10,
"crc_hash": "readmecrc",
"md5_hash": "readmemd5",
"sha1_hash": "readmesha1",
},
{
"name": "sf2.rom",
"size": 2048,
"crc_hash": "romcrc",
"md5_hash": "rommd5",
"sha1_hash": "romsha1",
},
],
)
assert file.lookup_hashes == LookupHashes(
crc="romcrc", md5="rommd5", sha1="romsha1"
)
def test_lookup_hashes_of_a_single_member_archive_are_the_files_own():
"""The composite of a one-member archive is that member's digest, so this
case must keep sending exactly what it sent before."""
file = _archive(
archive_members=[
{
"name": "sf2.rom",
"size": 2048,
"crc_hash": "compositecrc",
"md5_hash": "compositemd5",
"sha1_hash": "compositesha1",
},
],
)
assert file.lookup_hashes == LookupHashes(
crc="compositecrc", md5="compositemd5", sha1="compositesha1"
)
def test_lookup_hashes_without_archive_members_uses_the_files_own_digests():
"""Rows scanned before 4.9.0, unreadable archives hashed as raw bytes, and
archives nested inside a folder ROM all leave `archive_members` NULL, and
their own hash is already the largest member's."""
file = _archive(archive_members=None)
assert file.lookup_hashes == LookupHashes(
crc="compositecrc", md5="compositemd5", sha1="compositesha1"
)
def test_lookup_hashes_with_empty_archive_members_uses_the_files_own_digests():
file = _archive(archive_members=[])
assert file.lookup_hashes == LookupHashes(
crc="compositecrc", md5="compositemd5", sha1="compositesha1"
)
def test_lookup_hashes_tolerates_a_member_without_a_size():
file = _archive(
archive_members=[
{
"name": "unsized.bin",
"crc_hash": "unsizedcrc",
"md5_hash": "unsizedmd5",
"sha1_hash": "unsizedsha1",
},
{
"name": "nosize.bin",
"size": None,
"crc_hash": "nosizecrc",
"md5_hash": "nosizemd5",
"sha1_hash": "nosizesha1",
},
{
"name": "sf2.rom",
"size": 2048,
"crc_hash": "romcrc",
"md5_hash": "rommd5",
"sha1_hash": "romsha1",
},
],
)
assert file.lookup_hashes == LookupHashes(
crc="romcrc", md5="rommd5", sha1="romsha1"
)