Files
romm/backend/tests/endpoints/roms/conftest.py
zurdi f9f68dc0f1 Unify manual file uploads into a ROM across the Files and Media tabs
The Files tab streamed chunks through /roms/upload/* and registered the
result with the scanner listing, while each Media route (soundtracks,
screenshots, manual files, walkthrough files) carried its own copy of the
sanitize, promote, mkdir, stream, stat and DB upsert sequence. Those copies
crashed with FileNotFoundError on a file name outside Latin-1: the browser
sends the header as Latin-1 and the multipart field name as UTF-8, so the
parser target registered under the header name never received the part
and os.stat failed after the folder had been created.

handler/rom_upload.py now owns the shared ingestion (resolve and prepare
the destination, stage next to the target, move into place, refresh the
ROM's file rows through the scanner) and both the chunked completion and a
new receive_rom_file sit on it. The media routes only check the extension
and delegate; a body naming the part differently is a 400. /start gains an
optional body filename that wins over the header, so names outside Latin-1
arrive intact, and rom_id becomes optional in that body.

On the v2 frontend a single useRomFileUpload composable wraps the chunked
upload, the single-file conversion prompt, the toasts and the refetch; the
Files tab, Soundtrack panel, Screenshots subtab, Walkthrough subtab and the
manual "add to ROM folder" target all call it with their folder. The
service sends the real name in the body and a percent-encoded one in the
header. The v2-only uploadScreenshots/uploadWalkthroughFiles service
functions and the soundtrack-only toasts are gone. Media subtabs now
refuse an existing file name (409) like the Files tab, instead of
overwriting it silently.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1FgnCzXjM3rzkxggi7g6C
2026-09-16 15:29:22 +02:00

68 lines
2.1 KiB
Python

from pathlib import Path
import pytest
from handler.database import db_rom_handler
from handler.filesystem import fs_rom_handler
from models.platform import Platform
from models.rom import Rom, RomFile, RomFileCategory
from models.user import User
@pytest.fixture
def real_library(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
"""Point fs_rom_handler at a real temp library so file moves and listings
actually happen."""
lib = tmp_path / "library"
lib.mkdir()
monkeypatch.setattr(fs_rom_handler, "base_path", lib.resolve())
return lib
@pytest.fixture
def game_folder_on_disk(real_library: Path, game_folder_rom: Rom) -> Path:
"""The folder ROM's directory in the real library, with every file row
written to disk so a refresh keeps them."""
for rom_file in game_folder_rom.files:
path = real_library / rom_file.full_path
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(b"\0" * rom_file.file_size_bytes)
return real_library / game_folder_rom.full_path
@pytest.fixture
def game_folder_rom(admin_user: User, platform: Platform) -> Rom:
"""A folder-based ROM with two top-level files (so has_simple_single_file is False)."""
rom = Rom(
platform_id=platform.id,
name="multi_rom",
slug="multi_rom_slug",
fs_name="multi_rom",
fs_name_no_tags="multi_rom",
fs_name_no_ext="multi_rom",
fs_extension="",
fs_path=f"{platform.slug}/roms",
)
rom = db_rom_handler.add_rom(rom)
db_rom_handler.add_rom_user(rom_id=rom.id, user_id=admin_user.id)
file_path = f"{platform.slug}/roms/multi_rom"
db_rom_handler.add_rom_file(
RomFile(
rom_id=rom.id,
file_name="game.bin",
file_path=file_path,
file_size_bytes=10,
category=RomFileCategory.GAME,
)
)
db_rom_handler.add_rom_file(
RomFile(
rom_id=rom.id,
file_name="readme.txt",
file_path=file_path,
file_size_bytes=5,
category=RomFileCategory.GAME,
)
)
return db_rom_handler.get_rom(rom.id)