Files
romm/backend/tests/models/test_rom.py
Georges-Antoine Assi fc46c3eb1c Merge branch 'master' into feat/demozoo-pouet-phase0
Renumber the two migrations onto master's head: 0108/0109 collided with
0108_roms_primary_region and 0109_rom_files_category_index, leaving the
chain with two heads. They are now 0114 and 0115 behind 0113.

Both rebuild the roms_facets sync triggers from a hardcoded column list,
so add the publishers and developers columns 0112 introduced. Without
them the rebuilt triggers would have silently stopped mirroring the two.

Adopt master's provider taxonomy. METADATA_PROVIDER_GROUPS now carries
demozoo, pouet, and csdb as catalogs, which replaces the branch's own
GENERAL_PROVIDER_KEYS set and feeds the setup wizard, the scan dialog,
and Settings from one registry. CSDb reaches the scan explainer for the
first time; it was already selectable there.

Regenerate the frontend types: DetailedRomSchema was missing the three
metadata blobs the backend has always declared on RomSchema, and the
blobs are now typed rather than Record<string, unknown>.

Correct the setup strings that still called the three sources enabled by
default, and sort the locale keys.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-30 21:32:11 -04:00

217 lines
6.0 KiB
Python

import pytest
from handler.database import db_rom_handler
from models.platform import Platform
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_defaults_to_non_physical(rom: Rom):
assert rom.is_physical is False
assert rom.upc is None
def test_physical_rom_round_trips(platform: Platform):
rom = db_rom_handler.add_rom(
Rom(
platform_id=platform.id,
name="Sonic the Hedgehog",
fs_name="Sonic the Hedgehog",
fs_path=f"{platform.slug}/roms/.physical",
fs_size_bytes=0,
is_physical=True,
upc="012345678905",
)
)
stored = db_rom_handler.get_rom(rom.id)
assert stored is not None
assert stored.is_physical is True
assert stored.upc == "012345678905"
def test_has_file_on_disk_covers_both_file_less_cases(rom: Rom):
assert rom.has_file_on_disk is True
rom.missing_from_fs = True
assert rom.has_file_on_disk is False
rom.missing_from_fs = False
rom.is_physical = True
assert rom.has_file_on_disk is False
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"
)
@pytest.mark.parametrize(
"stored",
[
"javascript:x",
"../../evil",
"ugPZnsRH٣k٣",
"short",
"waytoolongvideoid",
None,
12345,
],
)
def test_youtube_video_id_drops_anything_that_is_not_an_id(rom: Rom, stored):
"""raw_*_metadata is client-writable, so the embed must not trust the blob."""
rom.igdb_metadata = {"youtube_video_id": stored}
assert rom.youtube_video_id is None
def test_youtube_video_id_falls_through_to_the_next_valid_source(rom: Rom):
rom.igdb_metadata = {"youtube_video_id": "javascript:x"}
rom.demozoo_metadata = {"youtube_video_id": "ugPZnsRHUkc"}
assert rom.youtube_video_id == "ugPZnsRHUkc"