Files
romm/backend/tests/endpoints/test_save_conversion.py
MMagTech 0b1e449b0f feat(saves): convert DS, Sega cartridge and Game Boy Advance saves through sigil (#5643)
* feat(saves): convert Nintendo DS saves through sigil

Sigil now converts a DS save between melonDS's raw file and DeSmuME's
.dsv, so a download with core= gets the file that emulator reads instead
of the stored one as is. DS moves from the native list to the platforms
sigil restores; like Saturn and Sega CD it carries no title id.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(saves): accept a single-file neutral unit as sigil names it, for DS and Game Boy

Sigil names a cart's single-file unit after the content, `{stem}.srm`, and
sets the unit format; the neutral check only took `save.raw` for DS and
`save.sram` for Game Boy, so a client pushing sigil's own unit got a 422.
A SINGLE unit's one file may now carry sigil's name on those platforms;
multi-file units and every other platform keep their names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(saves): name DS among the cartridges that take no companions

Sigil refuses companions on every cartridge platform, DS included now
that it converts DS saves, so the refusal says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(saves): convert Mega Drive, 32X, Master System and Game Gear saves through sigil

genesis_plus_gx and picodrive keep these carts' save RAM at the same
offsets but fill and size it differently. With sigil converting between
them, the four platforms move from NATIVE_SAVE_PLATFORM_SLUGS to
SIGIL_RESTORE_PLATFORM_SLUGS (sigil slugs genesis, 32x, sms, gamegear),
so a download with core= gets the file that core keeps. The companion
refusal message names the new cartridges.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(saves): accept the Sega cartridges' single-file neutral unit as sigil names it

Sigil names a Mega Drive, 32X, Master System or Game Gear unit after the
content, `{stem}.srm`. A SINGLE unit's one file may now carry that name,
as DS and Game Boy's may; RomM's `save.sram` still passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(saves): convert Game Boy Advance saves between emulators with sigil

GBA moves from the native-save platforms to the ones sigil reads at scan
time and restores for a core. The stored file doesn't load everywhere:
VBA Next keeps an EEPROM save at 128 KiB into a 136 KiB file, gpSP pads
every save to 128 KiB, and standalone mGBA appends the cart clock. The
scan stores the cart's save chip and clock in sigil_features, which sigil
uses to size a padded save. The tests that used GBA as their native
example now use SNES.

Needs a sigil pin with GBA collect and restore.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(saves): accept a single-file GBA neutral unit as sigil names it

Sigil names a GBA cart's unit as it does a Game Boy's: the save alone as
`{stem}.srm`, or a clock cart's zip of `save.sram` and `clock.rtc`.
GBA now takes Game Boy's form, so the single file may carry sigil's name.
The clock-only form had no other platform left and goes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(saves): pin sigil at the DS merge, and take only a plain file name as its single unit

Both SIGIL_VERSION pins move to baa96b8 (rommapp/argosy-sigil#22), the
first sigil commit with DS. _SIGIL_SINGLE is [^/]+\.srm, so a single unit
named with a folder in it is refused; test_neutral.py checks it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(deps): pin sigil at the GBA merge

rommapp/argosy-sigil#24 is merged as 4980f69. Both SIGIL_VERSION pins
(Dockerfile, docker/Dockerfile) move from baa96b8 to that commit on
sigil's main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(deps): pin sigil at the Mega Drive merge

rommapp/argosy-sigil#23 is merged as be0809d. Both SIGIL_VERSION pins
(Dockerfile, docker/Dockerfile) move from baa96b8 to that commit on
sigil's main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(deps): pin sigil at 650b265

Both SIGIL_VERSION pins (Dockerfile, docker/Dockerfile) move from
be0809d to 650b265, sigil's main. It holds rommapp/argosy-sigil#23
(be0809d) and is the commit rommapp/romm#5758 pins, so the two
pull requests set the same value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(deps): pin sigil at 650b265

Both SIGIL_VERSION pins (Dockerfile, docker/Dockerfile) move from
baa96b8 to 650b265, sigil's main. It holds rommapp/argosy-sigil#22
(baa96b8) and is the commit rommapp/romm#5758 pins, so the two
pull requests set the same value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(deps): pin sigil at 650b265

Both SIGIL_VERSION pins (Dockerfile, docker/Dockerfile) move from
4980f69 to 650b265, sigil's main. It holds rommapp/argosy-sigil#24
(4980f69) and is the commit rommapp/romm#5758 pins, so the two
pull requests set the same value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(saves): name no systems in the companion refusal

The message listed the cartridges that take no companions, so each
platform added to sigil's restores edited it, and the open save PRs
(#5643 to #5647) each edited it differently. It now says what holds for
every platform: companions go only on memory cards and volumes. All five
carry the same line, so they merge in any order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(saves): name no systems in the companion refusal

The message listed the cartridges that take no companions, so each
platform added to sigil's restores edited it, and the open save PRs
(#5643 to #5647) each edited it differently. It now says what holds for
every platform: companions go only on memory cards and volumes. All five
carry the same line, so they merge in any order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(saves): name no systems in the companion refusal

The message listed the cartridges that take no companions, so each
platform added to sigil's restores edited it, and the open save PRs
(#5643 to #5647) each edited it differently. It now says what holds for
every platform: companions go only on memory cards and volumes. All five
carry the same line, so they merge in any order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(saves): restore Sega cart saves through RomM's platform map and binding

From review: the Mega Drive, 32X, Master System and Game Gear tests mock
the binding, so a slug sigil doesn't take would still pass. A ROM on each
RomM platform now goes through stored_game and the real binding, and the
restored file is checked in the form sigil's genesis.md gives the core:
genesis_plus_gx without trailing 0xFF, picodrive's 32 KiB for Master
System and Game Gear. rom_game is the same helper in #5643 to #5647.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(saves): restore a DS save through RomM's platform map and the binding

From review: the DS tests mock the binding, so a slug sigil doesn't take
for DS would still pass. A ROM on RomM's nds platform now goes through
stored_game and the real binding, and the restored file is checked in
the form sigil's nds.md gives each core: DeSmuME's .dsv ends in its
122-byte footer, melonDS's .sav is the save. rom_game is the same helper
in #5643 to #5647.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(saves): restore a GBA save through RomM's platform map and the binding

From review: the GBA tests mock the binding, so a slug sigil doesn't take
for GBA would still pass. A ROM on RomM's gba platform now goes through
stored_game and the real binding, and the restored file is checked at the
size sigil's gba.md gives each core: gpSP 128 KiB padded with 0xFF, VBA
Next 136 KiB, mGBA the chip. rom_game is the same helper in #5643 to
#5647.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(deps): pin sigil at ef9b51e

Both SIGIL_VERSION pins move from 650b265 to ef9b51e, sigil's main
since rommapp/argosy-sigil#30 merged, the commit rommapp/romm#5758
pins now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(deps): pin sigil at ef9b51e

Both SIGIL_VERSION pins move from 650b265 to ef9b51e, sigil's main
since rommapp/argosy-sigil#30 merged, the commit rommapp/romm#5758
pins now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(deps): pin sigil at ef9b51e

Both SIGIL_VERSION pins move from 650b265 to ef9b51e, sigil's main
since rommapp/argosy-sigil#30 merged, the commit rommapp/romm#5758
pins now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: MMagTech <mmagtech@Mac.home>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
2026-11-28 10:39:13 -05:00

854 lines
28 KiB
Python

"""GET and POST /api/saves/{id}/content with a core, with sigil itself mocked."""
import io
import zipfile
from collections.abc import Iterator
from pathlib import Path
from types import SimpleNamespace
from typing import Any
from unittest import mock
from urllib.parse import parse_qsl
import pytest
from fastapi import status
from fastapi.testclient import TestClient
from tests.factories import make_platform, make_rom, make_save
from adapters.services.sigil import SigilGame
from adapters.services.sigil_restore import (
ContainerMismatch,
RefusalCode,
RestoreProfile,
SharedContainerRequired,
SigilRefusal,
)
from endpoints.saves import MAX_COMPANIONS
from handler.database import (
db_device_handler,
db_device_save_sync_handler,
db_rom_handler,
db_save_handler,
db_snapshot_handler,
)
from handler.database.base_handler import sync_session
from handler.filesystem import fs_asset_handler
from handler.snapshots.file_key import FileKey
from models.assets import Save, SaveFormat, SaveShape
from models.device import Device
from models.permission import HiddenEntity, PermEntity
from models.rom import Rom, RomFile
from models.user import User
UNIT = b"stored unit"
GAME = SigilGame(result=SimpleNamespace(platform="psx"), game_ids=("SLUS-01041",))
@pytest.fixture(autouse=True)
def assets(_isolated_assets_dir: Path) -> Path:
return _isolated_assets_dir
def _stored(save: Save, data: bytes = UNIT) -> Save:
path = fs_asset_handler.validate_path(save.full_path)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(data)
return save
def _neutral_snes_unit() -> bytes:
buffer = io.BytesIO()
with zipfile.ZipFile(buffer, "w") as zf:
zf.writestr("save.sram", b"battery")
return buffer.getvalue()
def _rom_on(slug: str, name: str, user: User) -> Rom:
rom = make_rom(make_platform(slug), name, fs_extension="cue")
db_rom_handler.add_rom_user(rom_id=rom.id, user_id=user.id)
return rom
@pytest.fixture
def psx_rom(admin_user: User) -> Rom:
rom = _rom_on("psx", "Chrono Cross (USA)", admin_user)
for file_name in ("Chrono Cross (USA).bin", "Chrono Cross (USA).cue"):
db_rom_handler.add_rom_file(
RomFile(
rom_id=rom.id,
file_name=file_name,
file_path=rom.fs_path,
file_size_bytes=10,
)
)
return rom
@pytest.fixture
def psx_save(psx_rom: Rom, admin_user: User) -> Save:
return _stored(make_save(psx_rom, admin_user, "Chrono Cross (USA).srm"))
@pytest.fixture
def device(admin_user: User) -> Device:
return db_device_handler.add_device(
Device(id="conversion-device", user_id=admin_user.id, name="Handheld")
)
@pytest.fixture
def stored_game() -> Iterator[mock.Mock]:
with mock.patch(
"handler.snapshots.restore.SigilService.stored_game", return_value=GAME
) as patched:
yield patched
@pytest.fixture
def restore() -> Iterator[mock.AsyncMock]:
with mock.patch(
"handler.snapshots.restore.restore_per_game",
new=mock.AsyncMock(return_value={"memcards/Chrono Cross (USA)_1.mcd": b"card"}),
) as patched:
yield patched
@pytest.fixture
def merge() -> Iterator[mock.AsyncMock]:
with mock.patch(
"handler.snapshots.restore.merge_into_container",
new=mock.AsyncMock(return_value={"pcsx-card1.mcd": b"merged"}),
) as patched:
yield patched
def _awaited_args(patched: mock.AsyncMock) -> tuple[Any, ...]:
awaited = patched.await_args
assert awaited is not None
return tuple(awaited.args)
def _get(client: TestClient, save: Save, headers: dict[str, str], query: str = ""):
return client.get(f"/api/saves/{save.id}/content?{query}", headers=headers)
def _post(
client: TestClient,
save: Save,
headers: dict[str, str],
request: dict[str, Any] | None = None,
container: bytes | None = b"card bytes",
query: str = "",
):
if request is None:
request = {"core": "pcsx_rearmed", "container_path": "pcsx-card1.mcd"}
params: list[tuple[str, str | int | float | bool | None]] = [
(key, request[key])
for key in ("core", "container_path", "profile")
if request.get(key) is not None
]
params += [
("option", f"{key}:{value}")
for key, value in request.get("options", {}).items()
]
params += [("companion", str(id)) for id in request.get("companions", [])]
params += parse_qsl(query)
files = {"container": ("card.mcd", container)} if container is not None else None
return client.post(
f"/api/saves/{save.id}/content",
headers=headers,
params=params,
files=files,
)
class TestGetWithCore:
def test_options_split_on_the_first_colon_and_companions_repeat(
self,
client,
headers,
psx_rom: Rom,
psx_save: Save,
admin_user: User,
stored_game,
restore,
):
prequel = _stored(make_save(psx_rom, admin_user, "Prequel.srm"), b"prequel")
sequel = _stored(make_save(psx_rom, admin_user, "Sequel.srm"), b"sequel")
response = _get(
client,
psx_save,
headers,
"core=duckstation&option=Card1Type:PerGame&option=path:a:b"
f"&option=empty:&profile=p1&companion={prequel.id}&companion={sequel.id}",
)
assert response.status_code == status.HTTP_200_OK
unit, game, target, companions = _awaited_args(restore)
assert unit == UNIT
assert game is GAME
assert target.core == "duckstation"
assert target.options == {"Card1Type": "PerGame", "path": "a:b", "empty": ""}
assert target.profile == "p1"
assert [c.unit for c in companions] == [b"prequel", b"sequel"]
assert [c.game_ids for c in companions] == [GAME.game_ids] * 2
@pytest.mark.parametrize("option", ["Card1Type", ":PerGame"])
def test_an_option_without_a_key_and_value_is_refused(
self, client, headers, psx_save: Save, stored_game, restore, option: str
):
response = _get(client, psx_save, headers, f"core=duckstation&option={option}")
assert response.status_code == status.HTTP_400_BAD_REQUEST
restore.assert_not_awaited()
@pytest.mark.parametrize(
"query", ["profile=..", "profile=a/b", "profile=a%5Cb", "option=path:../x"]
)
def test_a_profile_or_option_value_that_is_not_one_segment_is_refused(
self, client, headers, psx_save: Save, stored_game, restore, query: str
):
response = _get(client, psx_save, headers, f"core=duckstation&{query}")
assert response.status_code == status.HTTP_400_BAD_REQUEST
restore.assert_not_awaited()
def test_companions_are_read_once_each_and_capped(
self,
client,
headers,
psx_rom: Rom,
psx_save: Save,
admin_user: User,
stored_game,
restore,
):
prequel = _stored(make_save(psx_rom, admin_user, "Prequel.srm"), b"prequel")
repeated = "&".join([f"companion={prequel.id}"] * (MAX_COMPANIONS + 1))
response = _get(client, psx_save, headers, f"core=duckstation&{repeated}")
assert response.status_code == status.HTTP_200_OK
assert [c.unit for c in _awaited_args(restore)[3]] == [b"prequel"]
def test_more_companions_than_the_cap_are_refused(
self, client, headers, psx_save: Save, stored_game, restore
):
ids = "&".join(f"companion={i}" for i in range(1, MAX_COMPANIONS + 2))
response = _get(client, psx_save, headers, f"core=duckstation&{ids}")
assert response.status_code == status.HTTP_400_BAD_REQUEST
restore.assert_not_awaited()
def test_without_core_the_stored_file_is_served(
self, client, headers, psx_save: Save, restore
):
response = _get(client, psx_save, headers, "option=Card1Type:PerGame")
assert response.status_code == status.HTTP_200_OK
assert response.content == UNIT
assert "x-save-path" not in response.headers
restore.assert_not_awaited()
def test_the_content_path_is_the_roms_loader_file(
self, client, headers, psx_save: Save, stored_game, restore
):
_get(client, psx_save, headers, "core=duckstation")
assert _awaited_args(restore)[2].content_path == "Chrono Cross (USA).cue"
def test_the_content_path_is_the_saves_channel_file(
self,
client,
headers,
psx_rom: Rom,
psx_save: Save,
admin_user: User,
stored_game,
restore,
):
disc = next(
f
for f in db_rom_handler.rom_files_for_rom_id(psx_rom.id)
if f.file_name.endswith(".bin")
)
channel = db_snapshot_handler.add_channel(
FileKey.of_file(disc).new_channel(
admin_user.id, psx_rom.id, psx_rom.platform_id, "default"
)
)
db_save_handler.update_save(psx_save.id, {"channel_id": channel.id})
_get(client, psx_save, headers, "core=duckstation")
assert _awaited_args(restore)[2].content_path == "Chrono Cross (USA).bin"
@pytest.mark.parametrize("slug", ["nes", "snes"])
def test_a_platform_without_a_converter_serves_the_stored_file(
self, client, headers, admin_user: User, restore, slug: str
):
save = _stored(
make_save(_rom_on(slug, "Game", admin_user), admin_user, "g.srm")
)
response = _get(client, save, headers, "core=mesen")
assert response.status_code == status.HTTP_200_OK
assert response.content == UNIT
restore.assert_not_awaited()
def test_a_neutral_single_unit_without_a_converter_serves_the_stored_file(
self, client, headers, admin_user: User, restore
):
save = _stored(
make_save(
_rom_on("snes", "Game", admin_user),
admin_user,
"save.sram",
shape=SaveShape.SINGLE,
format=SaveFormat.NEUTRAL,
)
)
response = _get(client, save, headers, "core=snes9x")
assert response.status_code == status.HTTP_200_OK
assert response.content == UNIT
restore.assert_not_awaited()
@pytest.mark.parametrize("shape", [SaveShape.MULTI, SaveShape.FOLDER])
def test_a_unit_archive_without_a_converter_is_refused(
self, client, headers, admin_user: User, restore, shape: SaveShape
):
save = _stored(
make_save(
_rom_on("snes", "Game", admin_user),
admin_user,
"save.zip",
shape=shape,
format=SaveFormat.NEUTRAL,
),
_neutral_snes_unit(),
)
response = _get(client, save, headers, "core=snes9x")
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert "snes" in response.json()["detail"]
restore.assert_not_awaited()
def test_a_wii_save_converts_for_dolphin(
self, client, headers, admin_user: User, stored_game, restore
):
save = _stored(
make_save(_rom_on("wii", "Game", admin_user), admin_user, "g.zip")
)
response = _get(client, save, headers, "core=dolphin")
assert response.status_code == status.HTTP_200_OK
assert _awaited_args(restore)[2].core == "dolphin"
def test_a_ds_save_converts_for_desmume(
self, client, headers, admin_user: User, stored_game, restore
):
save = _stored(
make_save(_rom_on("nds", "Game", admin_user), admin_user, "Game.sav")
)
response = _get(client, save, headers, "core=desmume")
assert response.status_code == status.HTTP_200_OK
assert _awaited_args(restore)[2].core == "desmume"
@pytest.mark.parametrize(
("slug", "core"),
[
("genesis", "picodrive"),
("sega32", "picodrive"),
("sms", "genesis_plus_gx"),
("gamegear", "genesis_plus_gx"),
],
)
def test_a_sega_cart_save_converts_for_the_core(
self,
client,
headers,
admin_user: User,
stored_game,
restore,
slug: str,
core: str,
):
save = _stored(
make_save(_rom_on(slug, "Game", admin_user), admin_user, "Game.srm")
)
response = _get(client, save, headers, f"core={core}")
assert response.status_code == status.HTTP_200_OK
assert _awaited_args(restore)[2].core == core
def test_a_gba_save_converts_for_gpsp(
self, client, headers, admin_user: User, stored_game, restore
):
save = _stored(
make_save(_rom_on("gba", "Game", admin_user), admin_user, "Game.srm")
)
response = _get(client, save, headers, "core=gpsp")
assert response.status_code == status.HTTP_200_OK
assert _awaited_args(restore)[2].core == "gpsp"
@pytest.mark.parametrize("slug", ["neogeoaes", "turbografx-cd", "unknown"])
def test_a_platform_sigil_does_not_sync_is_refused(
self, client, headers, admin_user: User, restore, slug: str
):
save = _stored(
make_save(_rom_on(slug, "Game", admin_user), admin_user, "g.sav")
)
response = _get(client, save, headers, "core=dolphin")
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert slug in response.json()["detail"]
restore.assert_not_awaited()
def test_without_the_binding_conversion_is_unavailable(
self, client, headers, psx_save: Save, restore
):
with mock.patch(
"handler.snapshots.restore.SigilService.stored_game", return_value=None
):
response = _get(client, psx_save, headers, "core=duckstation")
assert response.status_code == status.HTTP_503_SERVICE_UNAVAILABLE
restore.assert_not_awaited()
def test_a_shared_target_names_the_container_to_post(
self, client, headers, psx_save: Save, stored_game, restore
):
restore.side_effect = SharedContainerRequired("pcsx-card1.mcd")
response = _get(client, psx_save, headers, "core=pcsx_rearmed")
assert response.status_code == status.HTTP_400_BAD_REQUEST
detail = response.json()["detail"]
assert detail["error"] == "SHARED_CONTAINER"
assert detail["container_path"] == "pcsx-card1.mcd"
@pytest.mark.parametrize(
("code", "expected"),
[
(RefusalCode.UNCOLLECTED, status.HTTP_409_CONFLICT),
(RefusalCode.CONFLICT, status.HTTP_409_CONFLICT),
(RefusalCode.EXISTS, status.HTTP_422_UNPROCESSABLE_CONTENT),
(RefusalCode.DAMAGED, status.HTTP_422_UNPROCESSABLE_CONTENT),
(RefusalCode.REGION, status.HTTP_422_UNPROCESSABLE_CONTENT),
(RefusalCode.NO_SPACE, status.HTTP_422_UNPROCESSABLE_CONTENT),
(RefusalCode.NOT_FOUND, status.HTTP_422_UNPROCESSABLE_CONTENT),
(RefusalCode.UNSUPPORTED_FORMAT, status.HTTP_422_UNPROCESSABLE_CONTENT),
(RefusalCode.NO_TARGET, status.HTTP_400_BAD_REQUEST),
(RefusalCode.AMBIGUOUS, status.HTTP_400_BAD_REQUEST),
(RefusalCode.INVALID_ARG, status.HTTP_400_BAD_REQUEST),
(RefusalCode.IO, status.HTTP_500_INTERNAL_SERVER_ERROR),
(RefusalCode.OTHER, status.HTTP_500_INTERNAL_SERVER_ERROR),
],
)
def test_each_refusal_maps_to_its_status(
self,
client,
headers,
psx_save: Save,
device: Device,
stored_game,
restore,
code: RefusalCode,
expected: int,
):
restore.side_effect = SigilRefusal(code, "refused", "the problem")
response = _get(
client, psx_save, headers, f"core=pcsx_rearmed&device_id={device.id}"
)
assert response.status_code == expected
assert response.json()["detail"]["error"] == code.value
assert response.json()["detail"]["problem"] == "the problem"
assert (
db_device_save_sync_handler.get_sync(
device_id=device.id, save_id=psx_save.id
)
is None
)
def test_an_ambiguous_profile_lists_the_profiles(
self, client, headers, psx_save: Save, stored_game, restore
):
restore.side_effect = SigilRefusal(
RefusalCode.AMBIGUOUS,
"ambiguous",
"A Eden\nB citron",
(RestoreProfile("A", "Eden"), RestoreProfile("B", "citron")),
)
response = _get(client, psx_save, headers, "core=eden")
assert response.json()["detail"]["profiles"] == [
{"id": "A", "name": "Eden"},
{"id": "B", "name": "citron"},
]
def test_one_file_comes_back_raw_with_its_save_path(
self, client, headers, psx_save: Save, stored_game, restore
):
response = _get(client, psx_save, headers, "core=duckstation")
assert response.status_code == status.HTTP_200_OK
assert response.content == b"card"
assert (
response.headers["x-save-path"]
== "memcards/Chrono%20Cross%20%28USA%29_1.mcd"
)
assert 'filename="Chrono%20Cross%20%28USA%29_1.mcd"' in (
response.headers["content-disposition"]
)
def test_several_files_come_back_as_a_zip_of_save_paths(
self, client, headers, psx_save: Save, stored_game, restore
):
restore.return_value = {"game.srm": b"ram", "game.rtc": b"clock"}
response = _get(client, psx_save, headers, "core=gambatte")
assert response.status_code == status.HTTP_200_OK
assert "x-save-path" not in response.headers
with zipfile.ZipFile(io.BytesIO(response.content)) as zf:
assert {name: zf.read(name) for name in zf.namelist()} == {
"game.rtc": b"clock",
"game.srm": b"ram",
}
def test_a_converted_download_records_the_device_sync(
self, client, headers, psx_save: Save, device: Device, stored_game, restore
):
response = _get(
client, psx_save, headers, f"core=duckstation&device_id={device.id}"
)
assert response.status_code == status.HTTP_200_OK
sync = db_device_save_sync_handler.get_sync(
device_id=device.id, save_id=psx_save.id
)
assert sync is not None
assert sync.last_sync_server_hash == psx_save.content_hash
LAYOUT = SimpleNamespace(
id="genesis_plus_gx",
platform="segacd",
options=(
SimpleNamespace(
key="genesis_plus_gx_region_detect",
values=("auto", "ntsc-u", "pal", "ntsc-j"),
default="auto",
),
),
region_option="genesis_plus_gx_region_detect",
profiles=False,
needs_existing=False,
)
class TestLayouts:
@pytest.fixture
def layouts(self) -> Iterator[mock.Mock]:
with mock.patch(
"endpoints.saves.restore_layouts", return_value=(LAYOUT,)
) as patched:
yield patched
def test_the_rows_come_back_for_sigils_platform(self, client, headers, layouts):
with mock.patch("endpoints.saves.SigilService.is_enabled", return_value=True):
response = client.get("/api/saves/layouts?platform=segacd", headers=headers)
assert response.status_code == status.HTTP_200_OK
layouts.assert_called_once_with("segacd")
assert response.json() == [
{
"id": "genesis_plus_gx",
"platform": "segacd",
"options": [
{
"key": "genesis_plus_gx_region_detect",
"values": ["auto", "ntsc-u", "pal", "ntsc-j"],
"default": "auto",
}
],
"region_option": "genesis_plus_gx_region_detect",
"profiles": False,
"needs_existing": False,
}
]
def test_a_romm_slug_maps_to_sigils(self, client, headers, layouts):
with mock.patch("endpoints.saves.SigilService.is_enabled", return_value=True):
client.get("/api/saves/layouts?platform=ngc", headers=headers)
layouts.assert_called_once_with("gamecube")
@pytest.mark.parametrize("slug", ["snes", "neogeoaes", "unknown"])
def test_a_platform_sigil_does_not_restore_is_refused(
self, client, headers, layouts, slug: str
):
response = client.get(f"/api/saves/layouts?platform={slug}", headers=headers)
assert response.status_code == status.HTTP_400_BAD_REQUEST
layouts.assert_not_called()
def test_without_the_binding_the_catalog_is_unavailable(
self, client, headers, layouts
):
with mock.patch("endpoints.saves.SigilService.is_enabled", return_value=False):
response = client.get("/api/saves/layouts?platform=psx", headers=headers)
assert response.status_code == status.HTTP_503_SERVICE_UNAVAILABLE
layouts.assert_not_called()
def test_without_the_platform_it_is_unprocessable(self, client, headers, layouts):
response = client.get("/api/saves/layouts", headers=headers)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT
def test_the_binding_reports_wiis_dolphin_rows(self, client, headers):
pytest.importorskip("sigil")
response = client.get("/api/saves/layouts?platform=wii", headers=headers)
assert response.status_code == status.HTTP_200_OK
rows = {row["id"]: row for row in response.json()}
assert response.json()[0]["id"] == "libretro"
assert rows["dolphin"]["platform"] == "wii"
assert rows["dolphin_standalone"]["platform"] == "wii"
class TestCompanionVisibility:
def test_another_users_private_companion_is_not_found(
self,
client,
headers,
psx_rom: Rom,
psx_save: Save,
viewer_user: User,
stored_game,
restore,
):
hidden = _stored(make_save(psx_rom, viewer_user, "Theirs.srm"))
response = _get(
client, psx_save, headers, f"core=pcsx_rearmed&companion={hidden.id}"
)
assert response.status_code == status.HTTP_404_NOT_FOUND
restore.assert_not_awaited()
def test_a_public_companion_on_a_hidden_rom_is_not_found(
self,
client,
viewer_access_token: str,
viewer_user: User,
admin_user: User,
stored_game,
restore,
):
own = _stored(
make_save(_rom_on("psx", "Sequel", viewer_user), viewer_user, "Mine.srm")
)
hidden_rom = _rom_on("psx", "Prequel", admin_user)
companion = _stored(
make_save(hidden_rom, admin_user, "Shared.srm", is_public=True)
)
with sync_session.begin() as session:
session.add(
HiddenEntity(
entity=PermEntity.ROMS,
entity_id=hidden_rom.id,
user_id=viewer_user.id,
)
)
response = _get(
client,
own,
{"Authorization": f"Bearer {viewer_access_token}"},
f"core=pcsx_rearmed&companion={companion.id}",
)
assert response.status_code == status.HTTP_404_NOT_FOUND
restore.assert_not_awaited()
class TestPostContainer:
def test_the_container_merges_and_comes_back_at_its_path(
self, client, headers, psx_save: Save, device: Device, stored_game, merge
):
response = _post(
client,
psx_save,
headers,
{
"core": "pcsx_rearmed",
"options": {"pcsx_rearmed_memcard1": "shared"},
"container_path": "pcsx-card1.mcd",
},
query=f"device_id={device.id}",
)
assert response.status_code == status.HTTP_200_OK
assert response.content == b"merged"
assert response.headers["x-save-path"] == "pcsx-card1.mcd"
unit, game, target, container_path, container, companions = _awaited_args(merge)
assert (unit, game, container_path, container) == (
UNIT,
GAME,
"pcsx-card1.mcd",
b"card bytes",
)
assert target.options == {"pcsx_rearmed_memcard1": "shared"}
assert target.content_path == "Chrono Cross (USA).cue"
assert companions == []
assert db_device_save_sync_handler.get_sync(
device_id=device.id, save_id=psx_save.id
)
@pytest.mark.parametrize(
"container_path",
[
"",
"../Mcd001.ps2",
"memcards/../../Mcd001.ps2",
"/etc/passwd",
"memcards\\Mcd001.ps2",
"C:Mcd001.ps2",
"card\x00.mcd",
],
)
def test_a_container_path_leaving_the_save_root_is_refused(
self, client, headers, psx_save: Save, stored_game, merge, container_path: str
):
response = _post(
client,
psx_save,
headers,
{"core": "pcsx_rearmed", "container_path": container_path},
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
merge.assert_not_awaited()
def test_a_container_over_the_card_limit_is_too_large(
self, client, headers, psx_save: Save, stored_game, merge
):
with mock.patch("endpoints.saves.MEMORY_CARD_MAX_BYTES", 4):
response = _post(client, psx_save, headers, container=b"12345")
assert response.status_code == status.HTTP_413_CONTENT_TOO_LARGE
merge.assert_not_awaited()
def test_a_missing_container_part_is_unprocessable(
self, client, headers, psx_save: Save, stored_game, merge
):
response = _post(client, psx_save, headers, container=None)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT
merge.assert_not_awaited()
@pytest.mark.parametrize(
"named", [{"core": "pcsx_rearmed"}, {"container_path": "pcsx-card1.mcd"}]
)
def test_a_merge_without_its_core_or_container_path_is_unprocessable(
self, client, headers, psx_save: Save, stored_game, merge, named: dict[str, str]
):
response = _post(client, psx_save, headers, named)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT
merge.assert_not_awaited()
def test_a_restore_into_another_container_is_refused(
self, client, headers, psx_save: Save, device: Device, stored_game, merge
):
merge.side_effect = ContainerMismatch(
"scd_E.brm", ("scd_U.brm",), {"genesis_plus_gx_region_detect": None}
)
response = _post(
client,
psx_save,
headers,
{"core": "genesis_plus_gx", "container_path": "scd_E.brm"},
query=f"device_id={device.id}",
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
detail = response.json()["detail"]
assert detail["error"] == "CONTAINER_MISMATCH"
assert detail["container_path"] == "scd_E.brm"
assert detail["written"] == ["scd_U.brm"]
assert detail["options"] == {"genesis_plus_gx_region_detect": None}
assert "genesis_plus_gx_region_detect" in detail["message"]
assert (
db_device_save_sync_handler.get_sync(
device_id=device.id, save_id=psx_save.id
)
is None
)
def test_a_platform_without_a_converter_serves_the_stored_file(
self, client, headers, admin_user: User, merge
):
save = _stored(
make_save(_rom_on("snes", "Game", admin_user), admin_user, "g.srm")
)
response = _post(client, save, headers)
assert response.status_code == status.HTTP_200_OK
assert response.content == UNIT
merge.assert_not_awaited()
def test_a_unit_archive_without_a_converter_is_refused(
self, client, headers, admin_user: User, merge
):
save = _stored(
make_save(
_rom_on("snes", "Game", admin_user),
admin_user,
"save.zip",
shape=SaveShape.MULTI,
format=SaveFormat.NEUTRAL,
),
_neutral_snes_unit(),
)
response = _post(client, save, headers)
assert response.status_code == status.HTTP_400_BAD_REQUEST
merge.assert_not_awaited()
def test_another_users_private_save_is_not_found(
self,
client,
viewer_access_token: str,
psx_save: Save,
stored_game,
merge,
):
response = _post(
client, psx_save, {"Authorization": f"Bearer {viewer_access_token}"}
)
assert response.status_code == status.HTTP_404_NOT_FOUND
merge.assert_not_awaited()