Files
romm/backend/tests/endpoints/roms/test_patch.py
Georges-Antoine Assi 4f6532500d fix(patcher): label the archive-member select and cover it with tests
The member picker reused patcher.select-file, so a ROM with several base
files rendered two dropdowns both labelled "Select file". Add
patcher.select-archive-member across all 18 locales and use it.

Treat an empty archive_member_name form field as "not specified" rather
than as a member name that matches nothing.

Fold getZipMembers into the archiveMembers computed and watch that, so
the selection also resets when the member list itself changes.

Add PatcherTab coverage for sending the selected member, typed against
RomArchiveMember so the fixture cannot drift from the schema.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-14 18:41:56 -04:00

98 lines
2.7 KiB
Python

from pathlib import Path
import pytest
from anyio import Path as AnyioPath
from fastapi import status
from fastapi.testclient import TestClient
from endpoints.roms import patch as patch_endpoint
from handler.database import db_rom_handler
from models.rom import Rom, RomFile, RomFileCategory
from utils.rom_patcher import PatcherInputError
def _auth(token: str) -> dict[str, str]:
return {"Authorization": f"Bearer {token}"}
def _patch_file(rom: Rom) -> RomFile:
return db_rom_handler.add_rom_file(
RomFile(
rom_id=rom.id,
file_name="translation.bps",
file_path=rom.fs_path,
file_size_bytes=100,
category=RomFileCategory.PATCH,
)
)
def test_patch_rom_passes_archive_member_and_validation_header(
client: TestClient,
access_token: str,
rom: Rom,
rom_file: RomFile,
monkeypatch: pytest.MonkeyPatch,
):
patch_file = _patch_file(rom)
received_member: str | None = None
async def patch(
_rom_path: Path,
_patch_path: Path,
output_path: Path,
archive_member_name: str | None,
) -> bool:
nonlocal received_member
received_member = archive_member_name
await AnyioPath(output_path).write_bytes(b"patched zip")
return False
monkeypatch.setattr(
patch_endpoint.fs_rom_handler, "validate_path", lambda path: Path(path)
)
monkeypatch.setattr(patch_endpoint, "apply_patch", patch)
response = client.post(
f"/api/roms/{rom_file.id}/patch",
headers=_auth(access_token),
data={
"patch_file_id": patch_file.id,
"archive_member_name": "roms/game.sfc",
},
)
assert response.status_code == status.HTTP_200_OK
assert response.content == b"patched zip"
assert response.headers["x-patch-validated"] == "false"
assert received_member == "roms/game.sfc"
def test_patch_rom_returns_bad_request_for_invalid_archive(
client: TestClient,
access_token: str,
rom: Rom,
rom_file: RomFile,
monkeypatch: pytest.MonkeyPatch,
):
patch_file = _patch_file(rom)
async def reject_patch(*_args, **_kwargs) -> bool:
raise PatcherInputError("Select which file inside the ROM archive to patch")
monkeypatch.setattr(
patch_endpoint.fs_rom_handler, "validate_path", lambda path: Path(path)
)
monkeypatch.setattr(patch_endpoint, "apply_patch", reject_patch)
response = client.post(
f"/api/roms/{rom_file.id}/patch",
headers=_auth(access_token),
data={"patch_file_id": patch_file.id},
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json() == {
"detail": "Select which file inside the ROM archive to patch"
}