Files
romm/backend/tests/endpoints/test_sync_snapshot_channels.py
Terri a420c7da31 fix(saves): stop Pokemon scratch RAM writes from flooding save sync (#5758)
* fix(play): show the automatic Save synced toast at most once every 5 minutes

* fix(saves): let sigil decide when a Pokemon save is the same, ignoring scratch RAM

* pr-ready: code-review

* pr-ready: review-polish

* pr-ready: prose

* fix(play): hold SRAM changes in the browser and upload them once the game goes quiet

* fix(play): upload a held save before a state replaces it, and report only new snapshots

* pr-ready: code-review

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

* pr-ready: simplify

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

* pr-ready: review-polish

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

* pr-ready: prose

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

* pr-ready: prose (code-review fixes)

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

* fix(play): leave a save a running session holds out of other tabs' sync passes

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

* build(sigil): pin ef9b51e

---------

Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 07:41:09 -05:00

943 lines
28 KiB
Python

"""Legacy negotiate and slot uploads on a channel a snapshot client keeps."""
import io
import uuid
import zipfile
from datetime import datetime
from typing import Any
import pytest
from fastapi import status
from fastapi.testclient import TestClient
from tests._zipfile_shim import reload_zipfile
from tests.handler.snapshots.pushes import (
md5,
part,
pokemon_red,
push,
red_ram,
save_entry,
stored_bytes,
)
from handler.database import (
db_device_handler,
db_device_save_sync_handler,
db_rom_handler,
db_save_handler,
db_snapshot_handler,
db_state_handler,
)
from handler.snapshots import bridge
from handler.snapshots.manifest import Manifest
from handler.snapshots.write import (
SAVE_PART,
SnapshotWrite,
WriteResult,
state_part,
write_snapshot,
)
from models.assets import Save, SaveFormat, SaveShape
from models.channel import Channel
from models.device import Device
from models.rom import Rom, RomFile
from models.snapshot import SnapshotKind
from models.user import User
from utils.datetime import to_utc
KEPT = b"kept-by-snapshots"
LEGACY = b"legacy-upload"
STATE = b"state-bytes"
pytestmark = pytest.mark.usefixtures("_isolated_assets_dir")
@pytest.fixture
def game_file(rom: Rom) -> RomFile:
return db_rom_handler.add_rom_file(
RomFile(
rom_id=rom.id,
file_name="game.sfc",
file_path=rom.fs_path,
file_size_bytes=1024,
sha1_hash="c" * 40,
)
)
@pytest.fixture
def legacy_device(admin_user: User) -> Device:
return db_device_handler.add_device(
Device(id="legacy", user_id=admin_user.id, sync_enabled=True)
)
def auth(access_token: str) -> dict[str, str]:
return {"Authorization": f"Bearer {access_token}"}
async def snapshot_push(
user: User,
rom: Rom,
rom_file: RomFile,
fmt: SaveFormat = SaveFormat.NATIVE,
is_hardcore: bool = False,
channel_id: uuid.UUID | None = None,
emulator: str = "snes9x",
core: str | None = None,
) -> WriteResult:
states = {} if is_hardcore else {"snes9x": {"auto": md5(STATE)}}
parts = {SAVE_PART: part(KEPT)}
if not is_hardcore:
parts[state_part("snes9x", "auto")] = part(STATE, "game.state")
return await write_snapshot(
push(
user,
rom,
rom_file,
Manifest(
save=save_entry(KEPT, fmt),
states=states,
emulator=emulator,
core=core,
is_hardcore=is_hardcore,
),
expected=None,
channel_id=channel_id,
parts=parts,
device_id=None,
)
)
def upload(
client: TestClient,
access_token: str,
rom: Rom,
device: Device,
data: bytes,
**params,
):
return client.post(
"/api/saves",
params={
"rom_id": rom.id,
"slot": "autosave",
"device_id": device.id,
"emulator": "snes9x",
**params,
},
files={"saveFile": ("game.srm", data)},
headers=auth(access_token),
)
async def test_an_adopted_upload_shows_its_screenshot_in_the_snapshot(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
pushed = await snapshot_push(admin_user, rom, game_file)
synced_to(legacy_device, held_save(pushed))
response = client.post(
"/api/saves",
params={
"rom_id": rom.id,
"slot": "autosave",
"device_id": legacy_device.id,
"emulator": "snes9x",
},
files={
"saveFile": ("game.srm", LEGACY),
"screenshotFile": ("game.png", b"png"),
},
headers=auth(access_token),
)
assert response.status_code == status.HTTP_200_OK
current = channel_of(pushed).current_snapshot_id
assert current is not None and current != pushed.snapshot.id
body = client.get(f"/api/snapshots/{current}", headers=auth(access_token)).json()
assert body["save"]["id"] == response.json()["id"]
assert body["save"]["screenshot"] is not None
assert body["thumbnail"] == body["save"]["screenshot"]
async def test_an_upload_named_like_a_held_save_leaves_its_bytes_alone(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
):
pushed = await snapshot_push(admin_user, rom, game_file)
held = held_save(pushed)
response = client.post(
"/api/saves",
params={"rom_id": rom.id, "emulator": held.emulator},
files={"saveFile": (held.file_name, b"overwritten")},
headers=auth(access_token),
)
assert response.status_code == status.HTTP_409_CONFLICT
assert stored_bytes(held.full_path) == KEPT
async def test_a_state_upload_named_like_a_held_state_leaves_its_bytes_alone(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
):
pushed = await snapshot_push(admin_user, rom, game_file)
[state] = (
db_snapshot_handler.get_stored_content(pushed.snapshot)
.states["snes9x"]
.values()
)
response = client.post(
"/api/states",
params={"rom_id": rom.id, "emulator": state.emulator},
files={"stateFile": (state.file_name, b"overwritten")},
headers=auth(access_token),
)
assert response.status_code == status.HTTP_409_CONFLICT
assert stored_bytes(state.full_path) == STATE
def synced_to(device: Device, save: Save) -> None:
db_device_save_sync_handler.upsert_sync(
device_id=device.id, save_id=save.id, synced_at=save.updated_at
)
def channel_of(result: WriteResult) -> Channel:
assert result.snapshot.channel_id is not None
channel = db_snapshot_handler.get_channel(result.snapshot.channel_id)
assert channel is not None
return channel
def held_save(result: WriteResult) -> Save:
save = db_save_handler.get_save(
user_id=result.snapshot.user_id, id=result.snapshot.save_id or 0
)
assert save is not None
return save
def negotiate(
client: TestClient, access_token: str, device: Device, saves: list[dict[str, Any]]
) -> list[dict[str, Any]]:
response = client.post(
"/api/sync/negotiate",
json={"device_id": device.id, "saves": saves},
headers=auth(access_token),
)
assert response.status_code == status.HTTP_200_OK
operations: list[dict[str, Any]] = response.json()["operations"]
return operations
def client_save(
rom: Rom, data: bytes, updated_at: str = "2026-01-01T00:00:00Z"
) -> dict[str, Any]:
return {
"rom_id": rom.id,
"file_name": "game.srm",
"slot": "autosave",
"content_hash": md5(data),
"updated_at": updated_at,
"file_size_bytes": len(data),
}
async def test_a_legacy_device_downloads_what_a_snapshot_device_pushed(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
first = upload(client, access_token, rom, legacy_device, LEGACY)
assert first.status_code == status.HTTP_200_OK
channel_id = uuid.UUID(first.json()["channel_id"])
pushed = await snapshot_push(admin_user, rom, game_file, channel_id=channel_id)
[op] = negotiate(client, access_token, legacy_device, [client_save(rom, LEGACY)])
assert op["action"] == "download"
assert op["save_id"] == pushed.snapshot.save_id
assert op["slot"] == "autosave"
assert op["server_content_hash"] == md5(KEPT)
async def test_a_fresh_legacy_device_matches_a_channel_only_snapshots_wrote(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
pushed = await snapshot_push(admin_user, rom, game_file)
[op] = negotiate(client, access_token, legacy_device, [client_save(rom, KEPT)])
assert op["action"] == "no_op"
assert op["save_id"] == pushed.snapshot.save_id
LATER = b"later-progress"
async def push_on(
user: User,
rom: Rom,
rom_file: RomFile,
current: WriteResult,
data: bytes,
parent_snapshot_id: int | None = None,
) -> WriteResult:
"""Push `data` as a snapshot client would, onto `current`'s channel."""
fields: dict[str, Any] = (
{} if parent_snapshot_id is None else {"parent_snapshot_id": parent_snapshot_id}
)
return await write_snapshot(
push(
user,
rom,
rom_file,
Manifest(save=save_entry(data, SaveFormat.NATIVE), emulator="snes9x"),
expected=current.snapshot.id,
channel_id=current.snapshot.channel_id,
parts={SAVE_PART: part(data)},
**fields,
)
)
def download(client: TestClient, access_token: str, device: Device, save_id: int):
response = client.get(
f"/api/saves/{save_id}/content",
params={"device_id": device.id},
headers=auth(access_token),
)
assert response.status_code == status.HTTP_200_OK
async def test_a_restore_reaches_a_legacy_device_that_moved_on(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
first = await snapshot_push(admin_user, rom, game_file)
# Written long before the restore, so its age and the restore's can't coincide.
db_save_handler.update_save(
held_save(first).id, {"updated_at": datetime(2026, 1, 1)}
)
[held] = negotiate(client, access_token, legacy_device, [client_save(rom, KEPT)])
assert held["action"] == "no_op"
later = await push_on(admin_user, rom, game_file, first, LATER)
assert later.snapshot.save_id is not None
download(client, access_token, legacy_device, later.snapshot.save_id)
restored = await push_on(
admin_user, rom, game_file, later, KEPT, parent_snapshot_id=first.snapshot.id
)
[op] = negotiate(client, access_token, legacy_device, [client_save(rom, LATER)])
stale = upload(client, access_token, rom, legacy_device, LATER)
assert op["action"] == "download"
assert op["save_id"] == restored.snapshot.save_id
assert op["server_content_hash"] == md5(KEPT)
stored = db_snapshot_handler.get_snapshot(restored.snapshot.id)
assert stored is not None
assert to_utc(datetime.fromisoformat(op["server_updated_at"])) == to_utc(
stored.created_at
)
assert stale.status_code == status.HTTP_409_CONFLICT
assert channel_of(restored).current_snapshot_id == restored.snapshot.id
async def test_a_device_that_changed_a_version_the_channel_moved_past_conflicts(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
first = await snapshot_push(admin_user, rom, game_file)
negotiate(client, access_token, legacy_device, [client_save(rom, KEPT)])
await push_on(admin_user, rom, game_file, first, LATER)
[op] = negotiate(
client, access_token, legacy_device, [client_save(rom, b"played-offline")]
)
assert op["action"] == "conflict"
async def test_a_device_that_downloaded_the_current_can_upload_onto_it(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
first = await snapshot_push(admin_user, rom, game_file)
negotiate(client, access_token, legacy_device, [client_save(rom, KEPT)])
later = await push_on(admin_user, rom, game_file, first, LATER)
assert later.snapshot.save_id is not None
download(client, access_token, legacy_device, later.snapshot.save_id)
response = upload(client, access_token, rom, legacy_device, b"played-on")
assert response.status_code == status.HTTP_200_OK
assert channel_of(later).current_snapshot_id != later.snapshot.id
async def test_a_fresh_legacy_device_finds_a_channel_only_snapshots_wrote(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
pushed = await snapshot_push(admin_user, rom, game_file)
db_save_handler.update_save(
held_save(pushed).id, {"updated_at": datetime(2026, 1, 1)}
)
[op] = negotiate(client, access_token, legacy_device, [])
assert op["action"] == "download"
assert op["save_id"] == pushed.snapshot.save_id
assert op["slot"] == "autosave"
stored = db_snapshot_handler.get_snapshot(pushed.snapshot.id)
assert stored is not None
assert to_utc(datetime.fromisoformat(op["server_updated_at"])) == to_utc(
stored.created_at
)
async def test_a_channel_outside_the_rom_scope_is_not_offered(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
await snapshot_push(admin_user, rom, game_file)
response = client.post(
"/api/sync/negotiate",
json={"device_id": legacy_device.id, "saves": [], "rom_ids": [rom.id + 1]},
headers=auth(access_token),
)
assert response.status_code == status.HTTP_200_OK
assert response.json()["operations"] == []
@pytest.mark.parametrize("listed", [False, True])
async def test_a_channel_on_a_hidden_rom_is_not_offered(
client: TestClient,
editor_access_token: str,
editor_user: User,
rom: Rom,
game_file: RomFile,
listed: bool,
):
from handler.database.base_handler import sync_session
from models.permission import HiddenEntity, PermEntity
device = db_device_handler.add_device(
Device(id="editor-legacy", user_id=editor_user.id, sync_enabled=True)
)
await snapshot_push(editor_user, rom, game_file)
with sync_session.begin() as session:
session.add(
HiddenEntity(
entity=PermEntity.ROMS, entity_id=rom.id, user_id=editor_user.id
)
)
saves = [client_save(rom, LEGACY)] if listed else []
operations = negotiate(client, editor_access_token, device, saves)
assert all(op["save_id"] is None for op in operations)
@pytest.mark.parametrize("restore_unlisted,offered", [(False, False), (True, True)])
async def test_a_current_the_device_held_and_dropped_stays_deleted(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
restore_unlisted: bool,
offered: bool,
):
pushed = await snapshot_push(admin_user, rom, game_file)
negotiate(client, access_token, legacy_device, [client_save(rom, KEPT)])
response = client.post(
"/api/sync/negotiate",
json={
"device_id": legacy_device.id,
"saves": [],
"restore_unlisted": restore_unlisted,
},
headers=auth(access_token),
)
operations = response.json()["operations"]
assert [op["save_id"] for op in operations] == (
[pushed.snapshot.save_id] if offered else []
)
async def test_a_neutral_current_is_never_offered_to_a_legacy_device(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
await snapshot_push(admin_user, rom, game_file, fmt=SaveFormat.NEUTRAL)
[op] = negotiate(client, access_token, legacy_device, [client_save(rom, LEGACY)])
assert op["action"] == "upload"
assert op["save_id"] is None
async def test_a_legacy_upload_becomes_the_current_and_keeps_the_states(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
pushed = await snapshot_push(admin_user, rom, game_file)
synced_to(legacy_device, held_save(pushed))
response = upload(client, access_token, rom, legacy_device, LEGACY)
assert response.status_code == status.HTTP_200_OK
body = response.json()
channel = channel_of(pushed)
assert channel.current_snapshot_id is not None
current = db_snapshot_handler.get_snapshot(channel.current_snapshot_id)
assert current is not None
assert current.save_id == body["id"]
assert current.parent_snapshot_id == pushed.snapshot.id
assert current.origin_device_id == legacy_device.id
assert db_snapshot_handler.get_stored_content(current).resolved().bank == {
"snes9x": {"auto": md5(STATE)}
}
assert body["slot"] == "autosave"
assert body["content_hash"] == md5(LEGACY)
async def test_an_adopted_upload_takes_its_identity_from_sigil(
client: TestClient,
access_token: str,
admin_user: User,
legacy_device: Device,
):
sigil = pytest.importorskip("sigil")
rom, rom_file = pokemon_red(sigil.FEATURE_GB_SCRATCH_GEN1)
db_rom_handler.add_rom_user(rom_id=rom.id, user_id=admin_user.id)
synced_to(legacy_device, held_save(await snapshot_push(admin_user, rom, rom_file)))
response = upload(client, access_token, rom, legacy_device, red_ram())
save = db_save_handler.get_save(user_id=admin_user.id, id=response.json()["id"])
assert save is not None
assert save.identity_hash == sigil.unit_identity(
"gb", sigil.FEATURE_GB_SCRATCH_GEN1, red_ram()
)
async def test_a_deduplicated_legacy_upload_becomes_the_current(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
pushed = await snapshot_push(admin_user, rom, game_file)
synced_to(legacy_device, held_save(pushed))
first = upload(client, access_token, rom, legacy_device, LEGACY).json()
upload(client, access_token, rom, legacy_device, b"moved-on")
response = upload(client, access_token, rom, legacy_device, LEGACY)
assert response.status_code == status.HTTP_200_OK
assert response.json()["id"] == first["id"]
channel = channel_of(pushed)
assert channel.current_snapshot_id is not None
current = db_snapshot_handler.get_snapshot(channel.current_snapshot_id)
assert current is not None
assert current.save_id == first["id"]
async def test_an_adopted_public_upload_follows_its_private_channel(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
pushed = await snapshot_push(admin_user, rom, game_file)
synced_to(legacy_device, held_save(pushed))
first = upload(client, access_token, rom, legacy_device, LEGACY).json()
upload(client, access_token, rom, legacy_device, b"moved-on")
# Shared before sync held it, which the sharing route no longer allows.
db_save_handler.update_save(first["id"], {"is_public": True}, touch=False)
response = upload(client, access_token, rom, legacy_device, LEGACY)
assert response.status_code == status.HTTP_200_OK
adopted = db_save_handler.get_save(user_id=admin_user.id, id=first["id"])
assert adopted is not None
assert adopted.is_public is False
async def test_a_legacy_device_behind_the_snapshots_is_sent_back_to_negotiate(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
pushed = await snapshot_push(admin_user, rom, game_file)
response = upload(client, access_token, rom, legacy_device, LEGACY)
assert response.status_code == status.HTTP_409_CONFLICT
assert channel_of(pushed).current_snapshot_id == pushed.snapshot.id
async def test_a_legacy_upload_leaves_a_hardcore_current_alone(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
pushed = await snapshot_push(admin_user, rom, game_file, is_hardcore=True)
synced_to(legacy_device, held_save(pushed))
response = upload(client, access_token, rom, legacy_device, LEGACY)
assert response.status_code == status.HTTP_200_OK
channel = channel_of(pushed)
assert channel.current_snapshot_id == pushed.snapshot.id
assert response.json()["channel_id"] == str(channel.id)
async def test_slot_cleanup_leaves_a_version_a_snapshot_holds(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
pushed = await snapshot_push(admin_user, rom, game_file)
synced_to(legacy_device, held_save(pushed))
first = upload(client, access_token, rom, legacy_device, LEGACY).json()
adopted = db_save_handler.get_save(user_id=admin_user.id, id=first["id"])
assert adopted is not None
synced_to(legacy_device, adopted)
second = upload(
client,
access_token,
rom,
legacy_device,
b"another-legacy-upload",
autocleanup=True,
autocleanup_limit=1,
)
assert second.status_code == status.HTTP_200_OK
assert db_save_handler.get_save(user_id=admin_user.id, id=first["id"]) is not None
async def test_a_legacy_device_without_the_save_downloads_it_under_its_slot(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
first = upload(client, access_token, rom, legacy_device, LEGACY)
channel_id = uuid.UUID(first.json()["channel_id"])
pushed = await snapshot_push(admin_user, rom, game_file, channel_id=channel_id)
[op] = negotiate(client, access_token, legacy_device, [])
assert op["action"] == "download"
assert op["save_id"] == pushed.snapshot.save_id
assert op["slot"] == "autosave"
async def test_a_legacy_upload_that_loses_a_race_is_kept_as_a_branch(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
monkeypatch: pytest.MonkeyPatch,
):
pushed = await snapshot_push(admin_user, rom, game_file)
synced_to(legacy_device, held_save(pushed))
racer = b"pushed-mid-upload"
raced: list[WriteResult] = []
async def push_first(write: SnapshotWrite) -> WriteResult:
raced.append(
await write_snapshot(
push(
admin_user,
rom,
game_file,
Manifest(save=save_entry(racer, SaveFormat.NATIVE)),
expected=pushed.snapshot.id,
channel_id=pushed.snapshot.channel_id,
parts={SAVE_PART: part(racer)},
)
)
)
return await write_snapshot(write)
monkeypatch.setattr(bridge, "write_snapshot", push_first)
response = upload(client, access_token, rom, legacy_device, LEGACY)
assert response.status_code == status.HTTP_200_OK
[winner] = raced
assert channel_of(pushed).current_snapshot_id == winner.snapshot.id
[branch] = [
s
for s in db_snapshot_handler.get_channel_history(channel_of(pushed).id, 10)
if s.save_id == response.json()["id"]
]
assert branch.kind == SnapshotKind.BRANCH
[op] = negotiate(client, access_token, legacy_device, [client_save(rom, LEGACY)])
assert op["action"] == "conflict"
async def test_a_zipped_legacy_upload_is_held_as_a_multi_file_save(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
pushed = await snapshot_push(admin_user, rom, game_file)
synced_to(legacy_device, held_save(pushed))
reload_zipfile()
buffer = io.BytesIO()
with zipfile.ZipFile(buffer, "w") as zf:
zf.writestr("card.mcd", LEGACY)
zf.writestr("card.idx", b"index")
response = upload(client, access_token, rom, legacy_device, buffer.getvalue())
assert response.status_code == status.HTTP_200_OK
held = db_save_handler.get_save(user_id=admin_user.id, id=response.json()["id"])
assert held is not None
assert held.shape == SaveShape.MULTI
assert channel_of(pushed).current_snapshot_id != pushed.snapshot.id
@pytest.mark.parametrize(
"emulators,offered", [(["mgba"], False), (["SNES9X"], True), (None, True)]
)
async def test_a_current_from_another_named_emulator_is_not_offered(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
emulators: list[str] | None,
offered: bool,
):
pushed = await snapshot_push(admin_user, rom, game_file)
response = client.post(
"/api/sync/negotiate",
json={
"device_id": legacy_device.id,
"saves": [client_save(rom, LEGACY)],
"emulators": emulators,
},
headers=auth(access_token),
)
[op] = response.json()["operations"]
assert (op["save_id"] == pushed.snapshot.save_id) is offered
async def test_a_legacy_upload_from_another_emulator_stays_off_the_snapshot_line(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
pushed = await snapshot_push(admin_user, rom, game_file)
synced_to(legacy_device, held_save(pushed))
response = upload(client, access_token, rom, legacy_device, LEGACY, emulator="mgba")
assert response.status_code == status.HTTP_200_OK
assert channel_of(pushed).current_snapshot_id == pushed.snapshot.id
@pytest.mark.parametrize("cores,offered", [(["snes9x"], True), (["bsnes"], False)])
async def test_a_named_core_decides_over_the_emulator_in_negotiate(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
cores: list[str],
offered: bool,
):
pushed = await snapshot_push(
admin_user, rom, game_file, emulator="argosy", core="snes9x"
)
response = client.post(
"/api/sync/negotiate",
json={
"device_id": legacy_device.id,
"saves": [client_save(rom, LEGACY)],
"emulators": ["retroarch"],
"cores": cores,
},
headers=auth(access_token),
)
[op] = response.json()["operations"]
assert (op["save_id"] == pushed.snapshot.save_id) is offered
async def test_a_legacy_upload_on_the_same_core_joins_another_emulators_line(
client: TestClient,
access_token: str,
admin_user: User,
rom: Rom,
game_file: RomFile,
legacy_device: Device,
):
pushed = await snapshot_push(
admin_user, rom, game_file, emulator="argosy", core="snes9x"
)
synced_to(legacy_device, held_save(pushed))
response = upload(
client,
access_token,
rom,
legacy_device,
LEGACY,
emulator="retroarch",
core="snes9x",
core_version="1.62",
)
current = channel_of(pushed).current_snapshot_id
assert current != pushed.snapshot.id
held = db_save_handler.get_save(user_id=admin_user.id, id=response.json()["id"])
assert held is not None
assert (held.emulator, held.core, held.core_version) == (
"retroarch",
"snes9x",
"1.62",
)
async def test_a_legacy_reupload_keeps_the_versions_it_does_not_resend(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
def backup(data: bytes, **params: str):
return client.post(
"/api/saves",
params={"rom_id": rom.id, "emulator": "retroarch", **params},
files={"saveFile": ("backup.srm", data)},
headers=auth(access_token),
).json()
first = backup(b"first", emulator_version="1.21", core="mgba", core_version="0.10")
second = backup(b"second")
assert second["id"] == first["id"]
saved = db_save_handler.get_save(user_id=admin_user.id, id=first["id"])
assert saved is not None
assert (saved.emulator_version, saved.core, saved.core_version) == (
"1.21",
"mgba",
"0.10",
)
async def test_a_legacy_state_upload_records_its_core(
client: TestClient, access_token: str, admin_user: User, rom: Rom
):
response = client.post(
"/api/states",
params={
"rom_id": rom.id,
"emulator": "retroarch",
"emulator_version": "1.21",
"core": "mgba",
"core_version": "0.10",
},
files={"stateFile": ("game.state1", b"state")},
headers=auth(access_token),
)
assert response.status_code == status.HTTP_200_OK
state = db_state_handler.get_state_by_id(response.json()["id"])
assert state is not None
assert (state.emulator_version, state.core, state.core_version) == (
"1.21",
"mgba",
"0.10",
)
assert state.content_hash == md5(b"state")